-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku.py
39 lines (31 loc) · 1.09 KB
/
sudoku.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
import pandas as pd
import pandas as pd
from docplex.cp.model import *
GRNG = range(9)
problem_data = pd.read_csv("sudoku.csv", sep=";")
problem = []
for t in problem_data.itertuples(index=False):
problem.append([i for i in t])
mdl = CpoModel(name="Sudoku")
grid = [[integer_var(min=1, max=9, name="C" + str(l) + str(c)) for l in GRNG] for c in GRNG]
for l in GRNG:
mdl.add(all_diff([grid[l][c] for c in GRNG]))
for c in GRNG:
mdl.add(all_diff([grid[l][c] for l in GRNG]))
ssrng = range(0, 9, 3)
for sl in ssrng:
for sc in ssrng:
mdl.add(all_diff([grid[l][c] for l in range(sl, sl + 3) for c in range(sc, sc + 3)]))
for l in GRNG:
for c in GRNG:
v = problem[l][c]
if v > 0:
grid[l][c].set_domain((v, v))
print("\nSolving model....")
msol = mdl.solve(TimeLimit=10)
sol = [[msol[grid[l][c]] for c in GRNG] for l in GRNG]
pd.DataFrame(sol).to_csv("sudoku_res.csv", sep=";", index=False)
from docplex.worker.clientapi import set_output_attachments
outputs = dict()
outputs['sudoku_res.csv'] = './sudoku_res.csv'
set_output_attachments(outputs)