diff --git a/docs/2.25.236/mp/creating_model.html b/docs/2.25.236/mp/creating_model.html index 8ce797d..b10f1d7 100644 --- a/docs/2.25.236/mp/creating_model.html +++ b/docs/2.25.236/mp/creating_model.html @@ -195,44 +195,60 @@
from docplex.mp.model import Model
-myInput =[[8, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 3, 6, 0, 0, 0, 0, 0],
- [0, 7, 0, 0, 9, 0, 2, 0, 0],
- [0, 5, 0, 0, 0, 7, 0, 0, 0],
- [0, 0, 0, 0, 4, 5, 7, 0, 0],
- [0, 0, 0, 1, 0, 0, 0, 3, 0],
- [0, 0, 1, 0, 0, 0, 0, 6, 8],
- [0, 0, 8, 5, 0, 0, 0, 1, 0],
- [0, 9, 0, 0, 0, 0, 4, 0, 0]]
+myInput = \
+[[8, 0, 0, 0, 0, 0, 0, 0, 0],
+ [0, 0, 3, 6, 0, 0, 0, 0, 0],
+ [0, 7, 0, 0, 9, 0, 2, 0, 0],
+ [0, 5, 0, 0, 0, 7, 0, 0, 0],
+ [0, 0, 0, 0, 4, 5, 7, 0, 0],
+ [0, 0, 0, 1, 0, 0, 0, 3, 0],
+ [0, 0, 1, 0, 0, 0, 0, 6, 8],
+ [0, 0, 8, 5, 0, 0, 0, 1, 0],
+ [0, 9, 0, 0, 0, 0, 4, 0, 0]]
model = Model("sudoku")
R = range(1, 10)
idx = [(i, j, k) for i in R for j in R for k in R]
-x = model.binary_var_dict(idx, "X")
+x = model.binary_var_dict(idx, name="X")
+# fix numbers in prepopulated cells
for i in R:
for j in R:
if myInput[i - 1][j - 1] != 0:
model.add_constraint(x[i, j, myInput[i - 1][j - 1]] == 1)
+# a cell must contain a single number
for i in R:
for j in R:
model.add_constraint(model.sum(x[i, j, k] for k in R) == 1)
+
+# every number must appear exactly once in each row
for j in R:
for k in R:
model.add_constraint(model.sum(x[i, j, k] for i in R) == 1)
+
+# every number must appear exactly once in each column
for i in R:
for k in R:
model.add_constraint(model.sum(x[i, j, k] for j in R) == 1)
+# every number must appear exactly once in every main 3x3 section
+R_small = range(1, 4)
+steps = range(0, 9, 3)
+for section_i in steps:
+ for section_j in steps:
+ for k in R:
+ model.add_constraint(model.sum(x[section_i+step_i, section_j+step_j, k]
+ for step_i in R_small for step_j in R_small) == 1)
+
solution = model.solve()
-solution.print_information()
+model.solve_details.print_information()
The solve() method returns an object of class SolveSolution that contains the result of solving, or None if the model has no solution. This object is described in the section “Retrieve results”.
-The method print_information() prints a default view of the status of the solve and the values of all variables. +
The method print_information() prints a default view of the status of the solve and its details. The object SolveSolution contains all the necessary accessors to create a customized solution output.
from docplex.mp.model import Model
-myInput =[[8, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 3, 6, 0, 0, 0, 0, 0],
- [0, 7, 0, 0, 9, 0, 2, 0, 0],
- [0, 5, 0, 0, 0, 7, 0, 0, 0],
- [0, 0, 0, 0, 4, 5, 7, 0, 0],
- [0, 0, 0, 1, 0, 0, 0, 3, 0],
- [0, 0, 1, 0, 0, 0, 0, 6, 8],
- [0, 0, 8, 5, 0, 0, 0, 1, 0],
- [0, 9, 0, 0, 0, 0, 4, 0, 0]]
+myInput = \
+[[8, 0, 0, 0, 0, 0, 0, 0, 0],
+ [0, 0, 3, 6, 0, 0, 0, 0, 0],
+ [0, 7, 0, 0, 9, 0, 2, 0, 0],
+ [0, 5, 0, 0, 0, 7, 0, 0, 0],
+ [0, 0, 0, 0, 4, 5, 7, 0, 0],
+ [0, 0, 0, 1, 0, 0, 0, 3, 0],
+ [0, 0, 1, 0, 0, 0, 0, 6, 8],
+ [0, 0, 8, 5, 0, 0, 0, 1, 0],
+ [0, 9, 0, 0, 0, 0, 4, 0, 0]]
model = Model("sudoku")
R = range(1, 10)
idx = [(i, j, k) for i in R for j in R for k in R]
-x = model.binary_var_dict(idx, "X")
+x = model.binary_var_dict(idx, name="X")
+# fix numbers in prepopulated cells
for i in R:
for j in R:
if myInput[i - 1][j - 1] != 0:
model.add_constraint(x[i, j, myInput[i - 1][j - 1]] == 1)
+# a cell must contain a single number
for i in R:
for j in R:
model.add_constraint(model.sum(x[i, j, k] for k in R) == 1)
+
+# every number must appear exactly once in each row
for j in R:
for k in R:
model.add_constraint(model.sum(x[i, j, k] for i in R) == 1)
+
+# every number must appear exactly once in each column
for i in R:
for k in R:
model.add_constraint(model.sum(x[i, j, k] for j in R) == 1)
+# every number must appear exactly once in every main 3x3 section
+R_small = range(1, 4)
+steps = range(0, 9, 3)
+for section_i in steps:
+ for section_j in steps:
+ for k in R:
+ model.add_constraint(model.sum(x[section_i+step_i, section_j+step_j, k]
+ for step_i in R_small for step_j in R_small) == 1)
+
solution = model.solve()
-solution.print_information()
+model.solve_details.print_information()
The solve() method returns an object of class SolveSolution that contains the result of solving, or None if the model has no solution. This object is described in the section “Retrieve results”.
-The method print_information() prints a default view of the status of the solve and the values of all variables. +
The method print_information() prints a default view of the status of the solve and its details. The object SolveSolution contains all the necessary accessors to create a customized solution output.