Skip to content

changed queue to set in AC3 #1051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def conflicted_vars(self, current):
def AC3(csp, queue=None, removals=None):
"""[Figure 6.3]"""
if queue is None:
queue = [(Xi, Xk) for Xi in csp.variables for Xk in csp.neighbors[Xi]]
queue = {(Xi, Xk) for Xi in csp.variables for Xk in csp.neighbors[Xi]}
csp.support_pruning()
while queue:
(Xi, Xj) = queue.pop()
Expand All @@ -169,7 +169,7 @@ def AC3(csp, queue=None, removals=None):
return False
for Xk in csp.neighbors[Xi]:
if Xk != Xj:
queue.append((Xk, Xi))
queue.add((Xk, Xi))
return True


Expand Down Expand Up @@ -243,7 +243,7 @@ def forward_checking(csp, var, value, assignment, removals):

def mac(csp, var, value, assignment, removals):
"""Maintain arc consistency."""
return AC3(csp, [(X, var) for X in csp.neighbors[var]], removals)
return AC3(csp, {(X, var) for X in csp.neighbors[var]}, removals)

# The search, proper

Expand Down Expand Up @@ -374,7 +374,7 @@ def make_arc_consistent(Xj, Xk, csp):
# Found a consistent assignment for val1, keep it
keep = True
break

if not keep:
# Remove val1
csp.prune(Xj, val1, None)
Expand Down
30 changes: 15 additions & 15 deletions tests/test_csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from csp import *
import random


random.seed("aima-python")


Expand Down Expand Up @@ -174,7 +173,7 @@ def test_csp_conflicted_vars():
def test_revise():
neighbors = parse_neighbors('A: B; B: ')
domains = {'A': [0], 'B': [4]}
constraints = lambda X, x, Y, y: x % 2 == 0 and (x+y) == 4
constraints = lambda X, x, Y, y: x % 2 == 0 and (x + y) == 4

csp = CSP(variables=None, domains=domains, neighbors=neighbors, constraints=constraints)
csp.support_pruning()
Expand All @@ -196,24 +195,24 @@ def test_revise():
def test_AC3():
neighbors = parse_neighbors('A: B; B: ')
domains = {'A': [0, 1, 2, 3, 4], 'B': [0, 1, 2, 3, 4]}
constraints = lambda X, x, Y, y: x % 2 == 0 and (x+y) == 4 and y % 2 != 0
constraints = lambda X, x, Y, y: x % 2 == 0 and (x + y) == 4 and y % 2 != 0
removals = []

csp = CSP(variables=None, domains=domains, neighbors=neighbors, constraints=constraints)

assert AC3(csp, removals=removals) is False

constraints = lambda X, x, Y, y: (x % 2) == 0 and (x+y) == 4
constraints = lambda X, x, Y, y: (x % 2) == 0 and (x + y) == 4
removals = []
csp = CSP(variables=None, domains=domains, neighbors=neighbors, constraints=constraints)

assert AC3(csp, removals=removals) is True
assert (removals == [('A', 1), ('A', 3), ('B', 1), ('B', 3)] or
removals == [('B', 1), ('B', 3), ('A', 1), ('A', 3)])
domains = {'A': [ 2, 4], 'B': [ 3, 5]}
constraints = lambda X, x, Y, y: int(x) > int (y)
removals=[]

domains = {'A': [2, 4], 'B': [3, 5]}
constraints = lambda X, x, Y, y: int(x) > int(y)
removals = []
csp = CSP(variables=None, domains=domains, neighbors=neighbors, constraints=constraints)

assert AC3(csp, removals=removals)
Expand Down Expand Up @@ -247,7 +246,7 @@ def test_num_legal_values():
def test_mrv():
neighbors = parse_neighbors('A: B; B: C; C: ')
domains = {'A': [0, 1, 2, 3, 4], 'B': [4], 'C': [0, 1, 2, 3, 4]}
constraints = lambda X, x, Y, y: x % 2 == 0 and (x+y) == 4
constraints = lambda X, x, Y, y: x % 2 == 0 and (x + y) == 4
csp = CSP(variables=None, domains=domains, neighbors=neighbors, constraints=constraints)
assignment = {'A': 0}

Expand All @@ -269,13 +268,13 @@ def test_mrv():
def test_unordered_domain_values():
map_coloring_test = MapColoringCSP(list('123'), 'A: B C; B: C; C: ')
assignment = None
assert unordered_domain_values('A', assignment, map_coloring_test) == ['1', '2', '3']
assert unordered_domain_values('A', assignment, map_coloring_test) == ['1', '2', '3']


def test_lcv():
neighbors = parse_neighbors('A: B; B: C; C: ')
domains = {'A': [0, 1, 2, 3, 4], 'B': [0, 1, 2, 3, 4, 5], 'C': [0, 1, 2, 3, 4]}
constraints = lambda X, x, Y, y: x % 2 == 0 and (x+y) == 4
constraints = lambda X, x, Y, y: x % 2 == 0 and (x + y) == 4
csp = CSP(variables=None, domains=domains, neighbors=neighbors, constraints=constraints)
assignment = {'A': 0}

Expand Down Expand Up @@ -347,7 +346,7 @@ def test_min_conflicts():
assert min_conflicts(france)

tests = [(usa, None)] * 3
assert failure_test(min_conflicts, tests) >= 1/3
assert failure_test(min_conflicts, tests) >= 1 / 3

australia_impossible = MapColoringCSP(list('RG'), 'SA: WA NT Q NSW V; NT: WA Q; NSW: Q V; T: ')
assert min_conflicts(australia_impossible, 1000) is None
Expand Down Expand Up @@ -419,9 +418,9 @@ def test_parse_neighbours():

def test_topological_sort():
root = 'NT'
Sort, Parents = topological_sort(australia,root)
Sort, Parents = topological_sort(australia, root)

assert Sort == ['NT','SA','Q','NSW','V','WA']
assert Sort == ['NT', 'SA', 'Q', 'NSW', 'V', 'WA']
assert Parents['NT'] == None
assert Parents['SA'] == 'NT'
assert Parents['Q'] == 'SA'
Expand All @@ -432,10 +431,11 @@ def test_topological_sort():

def test_tree_csp_solver():
australia_small = MapColoringCSP(list('RB'),
'NT: WA Q; NSW: Q V')
'NT: WA Q; NSW: Q V')
tcs = tree_csp_solver(australia_small)
assert (tcs['NT'] == 'R' and tcs['WA'] == 'B' and tcs['Q'] == 'B' and tcs['NSW'] == 'R' and tcs['V'] == 'B') or \
(tcs['NT'] == 'B' and tcs['WA'] == 'R' and tcs['Q'] == 'R' and tcs['NSW'] == 'B' and tcs['V'] == 'R')


if __name__ == "__main__":
pytest.main()
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy