-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathac3.py
49 lines (34 loc) · 1.31 KB
/
ac3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from utils import is_different
"""
Constraint Propagation with AC-3
pseudo code found @ https://en.wikipedia.org/wiki/AC-3_algorithm
python implementation inspired by http://aima.cs.berkeley.edu/python/csp.html
"""
def AC3(csp, queue=None):
if queue == None:
queue = list(csp.binary_constraints)
while queue:
(xi, xj) = queue.pop(0)
if remove_inconsistent_values(csp, xi, xj):
# if a cell has 0 possibilities, sudoku has no solution
if len(csp.possibilities[xi]) == 0:
return False
for Xk in csp.related_cells[xi]:
if Xk != xi:
queue.append((Xk, xi))
return True
"""
remove_inconsistent_values
returns true if a value is removed
"""
def remove_inconsistent_values(csp, cell_i, cell_j):
removed = False
# for each possible value remaining for the cell_i cell
for value in csp.possibilities[cell_i]:
# if cell_i=value is in conflict with cell_j=poss for each possibility
if not any([is_different(value, poss) for poss in csp.possibilities[cell_j]]):
# then remove cell_i=value
csp.possibilities[cell_i].remove(value)
removed = True
# returns true if a value has been removed
return removed