6 from pyscipopt
import Model, quicksum
9 init = [5, 3, 0, 0, 7, 0, 0, 0, 0,
10 6, 0, 0, 1, 9, 5, 0, 0, 0,
11 0, 9, 8, 0, 0, 0, 0, 6, 0,
12 8, 0, 0, 0, 6, 0, 0, 0, 3,
13 4, 0, 0, 8, 0, 3, 0, 0, 1,
14 7, 0, 0, 0, 2, 0, 0, 0, 6,
15 0, 6, 0, 0, 0, 0, 2, 8, 0,
16 0, 0, 0, 4, 1, 9, 0, 0, 5,
17 0, 0, 0, 0, 8, 0, 0, 7, 9]
26 name = str(i) +
',' + str(j) +
',' + str(k)
27 x[i, j, k] = m.addVar(name, vtype=
'B')
32 if init[j + 9 * i] != 0:
33 m.addCons(x[i, j, init[j + 9 * i] - 1] == 1)
38 m.addCons(
quicksum(x[i, j, k]
for k
in range(9)) == 1)
43 m.addCons(
quicksum(x[ind, j, k]
for j
in range(9)) == 1)
44 m.addCons(
quicksum(x[i, ind, k]
for i
in range(9)) == 1)
50 m.addCons(
quicksum(x[i + 3 * row, j + 3 * col, k]
for i
in range(3)
for j
in range(3)) == 1)
55 if m.getStatus() !=
'optimal':
56 print(
'Sudoku is not feasible!')
58 print(
'\nSudoku solution:\n')
64 if m.getVal(x[i, j, k]) == 1:
66 out += str(sol[i, j]) +
' '