@@ -290,9 +290,9 @@ def forward_checking(csp, var, value, assignment, removals):
290
290
return True
291
291
292
292
293
- def mac (csp , var , value , assignment , removals , constraint_propagation = AC3 ):
293
+ def mac (csp , var , value , assignment , removals ):
294
294
"""Maintain arc consistency."""
295
- return constraint_propagation (csp , {(X , var ) for X in csp .neighbors [var ]}, removals )
295
+ return AC3 (csp , {(X , var ) for X in csp .neighbors [var ]}, removals )
296
296
297
297
298
298
# The search, proper
@@ -326,11 +326,11 @@ def backtrack(assignment):
326
326
327
327
328
328
# ______________________________________________________________________________
329
- # Min-conflicts Hill Climbing search for CSPs
329
+ # Min-conflicts hillclimbing search for CSPs
330
330
331
331
332
332
def min_conflicts (csp , max_steps = 100000 ):
333
- """Solve a CSP by stochastic Hill Climbing on the number of conflicts."""
333
+ """Solve a CSP by stochastic hillclimbing on the number of conflicts."""
334
334
# Generate a complete assignment for all variables (probably with conflicts)
335
335
csp .current = current = {}
336
336
for var in csp .variables :
@@ -532,7 +532,7 @@ def queen_constraint(A, a, B, b):
532
532
return A == B or (a != b and A + a != B + b and A - a != B - b )
533
533
534
534
535
- class NQueens (CSP ):
535
+ class NQueensCSP (CSP ):
536
536
"""Make a CSP for the nQueens problem for search with min_conflicts.
537
537
Suitable for large n, it uses only data structures of size O(n).
538
538
Think of placing queens one per column, from left to right.
@@ -548,7 +548,7 @@ class NQueens(CSP):
548
548
a variable, and a best value for the variable, are each O(n).
549
549
If you want, you can keep track of conflicted variables, then variable
550
550
selection will also be O(1).
551
- >>> len(backtracking_search(NQueens (8)))
551
+ >>> len(backtracking_search(NQueensCSP (8)))
552
552
8
553
553
"""
554
554
@@ -673,37 +673,7 @@ class Sudoku(CSP):
673
673
>>> h = Sudoku(harder1)
674
674
>>> backtracking_search(h, select_unassigned_variable=mrv, inference=forward_checking) is not None
675
675
True
676
-
677
- >>> e = Sudoku(easy1)
678
- >>> e.display(e.infer_assignment())
679
- . . 3 | . 2 . | 6 . .
680
- 9 . . | 3 . 5 | . . 1
681
- . . 1 | 8 . 6 | 4 . .
682
- ------+-------+------
683
- . . 8 | 1 . 2 | 9 . .
684
- 7 . . | . . . | . . 8
685
- . . 6 | 7 . 8 | 2 . .
686
- ------+-------+------
687
- . . 2 | 6 . 9 | 5 . .
688
- 8 . . | 2 . 3 | . . 9
689
- . . 5 | . 1 . | 3 . .
690
- >>> AC4(e); e.display(e.infer_assignment())
691
- True
692
- 4 8 3 | 9 2 1 | 6 5 7
693
- 9 6 7 | 3 4 5 | 8 2 1
694
- 2 5 1 | 8 7 6 | 4 9 3
695
- ------+-------+------
696
- 5 4 8 | 1 3 2 | 9 7 6
697
- 7 2 9 | 5 6 4 | 1 3 8
698
- 1 3 6 | 7 9 8 | 2 4 5
699
- ------+-------+------
700
- 3 7 2 | 6 8 9 | 5 1 4
701
- 8 1 4 | 2 5 3 | 7 6 9
702
- 6 9 5 | 4 1 7 | 3 8 2
703
- >>> h = Sudoku(harder1)
704
- >>> backtracking_search(h, select_unassigned_variable=mrv, inference=forward_checking) is not None
705
- True
706
- """
676
+ """ # noqa
707
677
708
678
R3 = _R3
709
679
Cell = _CELL
0 commit comments