update sudoku example
@@ -23,27 +23,27 @@ given = np.array([
# Variables
-puzzle = IntVar(1,9, shape=given.shape, name="puzzle")
+puzzle = intvar(1,9, shape=given.shape, name="puzzle")
-constraints = []
+
+model = Model(
+ # Constraints on values (cells that are not empty)
+ puzzle[given!=e] == given[given!=e], # numpy's indexing, vectorized equality
# Constraints on rows and columns
-constraints += [ alldifferent(row) for row in puzzle ]
-constraints += [ alldifferent(col) for col in puzzle.T ] # numpy's Transpose
+ [AllDifferent(row) for row in puzzle],
+ [AllDifferent(col) for col in puzzle.T], # numpy's Transpose
+)
# Constraints on blocks
for i in range(0,9, 3):
for j in range(0,9, 3):
- constraints += [ alldifferent(puzzle[i:i+3, j:j+3]) ] # python's indexing
-
-# Constraints on values (cells that are not empty)
-constraints += [ puzzle[given!=e] == given[given!=e] ] # numpy's indexing
+ model += AllDifferent(puzzle[i:i+3, j:j+3]) # python's indexing
# Solve and print
-model = Model(constraints)
if model.solve():
#print(puzzle.value())
- # pretty print, highlight givens
+ # pretty print, mark givens with *
out = ""
for r in range(0,9):
for c in range(0,9):
