Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aade5ad

Browse files
committed
Made CSP work as a Problem: state must be hashable.
1 parent 1783bc6 commit aade5ad

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

csp.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ class CSP(search.Problem):
3333
curr_domains[var] Slot: remaining consistent values for var
3434
Used by constraint propagation routines.
3535
The following methods are used only by graph_search and tree_search:
36-
succ(a) Return a list of (action, state) pairs
36+
successor(a) Return a list of (action, state) pairs
3737
goal_test(a) Return true if all constraints satisfied
3838
The following are just for debugging purposes:
3939
nassigns Slot: tracks the number of assignments made
4040
display(a) Print a human-readable representation
4141
42-
>>> search.depth_first_tree_search(australia)
43-
<Node {'Q': 'B', 'T': 'B', 'WA': 'B', 'V': 'B', 'SA': 'G', 'NT': 'R', 'NSW': 'R'}>
42+
>>> search.depth_first_graph_search(australia)
43+
<Node (('WA', 'B'), ('Q', 'B'), ('T', 'B'), ('V', 'B'), ('SA', 'G'), ('NT', 'R'), ('NSW', 'R'))>
4444
"""
4545

4646
def __init__(self, vars, domains, neighbors, constraints):
4747
"Construct a CSP problem. If vars is empty, it becomes domains.keys()."
4848
vars = vars or domains.keys()
4949
update(self, vars=vars, domains=domains,
5050
neighbors=neighbors, constraints=constraints,
51-
initial={}, curr_domains=None, pruned=None, nassigns=0)
51+
initial=(), curr_domains=None, pruned=None, nassigns=0)
5252

5353
def assign(self, var, val, assignment):
5454
"""Add {var: val} to assignment; Discard the old value if any.
@@ -101,21 +101,22 @@ def display(self, assignment):
101101

102102
## These methods are for the tree and graph search interface:
103103

104-
def successor(self, assignment):
104+
def successor(self, state):
105105
"Return a list of (action, state) pairs."
106-
if len(assignment) == len(self.vars):
106+
if len(state) == len(self.vars):
107107
return []
108108
else:
109+
assignment = dict(state)
109110
var = find_if(lambda v: v not in assignment, self.vars)
110111
result = []
111112
for val in self.domains[var]:
112113
if self.nconflicts(var, val, assignment) == 0:
113-
a = assignment.copy(); a[var] = val
114-
result.append(((var, val), a))
114+
result.append(((var, val), state + ((var, val),)))
115115
return result
116116

117-
def goal_test(self, assignment):
117+
def goal_test(self, state):
118118
"The goal is to assign all vars, with all constraints satisfied."
119+
assignment = dict(state)
119120
return (len(assignment) == len(self.vars) and
120121
every(lambda var: self.nconflicts(var, assignment[var],
121122
assignment) == 0,

0 commit comments

Comments
 (0)