From 20b0f664a907fb555c8dd5a6bce77eeef4765361 Mon Sep 17 00:00:00 2001 From: Guglielmo-Sanchini1 Date: Thu, 25 May 2023 16:49:45 +0200 Subject: [PATCH] fix broken sudoku example --- docs/2.25.236/mp/creating_model.html | 40 +++++++++++++++++++--------- docs/mp/creating_model.html | 40 +++++++++++++++++++--------- 2 files changed, 56 insertions(+), 24 deletions(-) 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 @@

Import necessary modulesdocplex/mp/examples.

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.

diff --git a/docs/mp/creating_model.html b/docs/mp/creating_model.html index 8ce797d..b10f1d7 100644 --- a/docs/mp/creating_model.html +++ b/docs/mp/creating_model.html @@ -195,44 +195,60 @@

Import necessary modulesdocplex/mp/examples.

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.