|
22 | 22 | Please refer to documentation for appropriate setup of solving configuration. |
23 | 23 | """ |
24 | 24 |
|
25 | | -from docplex.cp.model import * |
26 | | -from sys import stdout |
| 25 | +from docplex.cp.model import CpoModel, CpoNotSupportedException |
27 | 26 |
|
28 | | -from docplex.cp.config import context |
29 | 27 |
|
30 | | -# Set model parameters |
31 | | -ORDER = 10 # Number of marks |
32 | | -MAX_LENGTH = (ORDER - 1) ** 2 # Max rule length |
| 28 | +#----------------------------------------------------------------------------- |
| 29 | +# Initialize the problem data |
| 30 | +#----------------------------------------------------------------------------- |
| 31 | + |
| 32 | +# Number of marks on the ruler |
| 33 | +ORDER = 8 |
| 34 | + |
| 35 | + |
| 36 | +#----------------------------------------------------------------------------- |
| 37 | +# Prepare the data for modeling |
| 38 | +#----------------------------------------------------------------------------- |
| 39 | + |
| 40 | +# Estimate an upper bound to the ruler length |
| 41 | +MAX_LENGTH = (ORDER - 1) ** 2 |
| 42 | + |
| 43 | + |
| 44 | +#----------------------------------------------------------------------------- |
| 45 | +# Build the model |
| 46 | +#----------------------------------------------------------------------------- |
33 | 47 |
|
34 | 48 | # Create model |
35 | 49 | mdl = CpoModel() |
36 | 50 |
|
37 | 51 | # Create array of variables corresponding to position rule marks |
38 | | -marks = integer_var_list(ORDER, 0, MAX_LENGTH, "M") |
| 52 | +marks = mdl.integer_var_list(ORDER, 0, MAX_LENGTH, "M") |
39 | 53 |
|
40 | 54 | # Create marks distances that should be all different |
41 | 55 | dist = [marks[i] - marks[j] for i in range(1, ORDER) for j in range(0, i)] |
42 | | -mdl.add(all_diff(dist)) |
| 56 | +mdl.add(mdl.all_diff(dist)) |
43 | 57 |
|
44 | 58 | # Avoid symmetric solutions by ordering marks |
45 | 59 | mdl.add(marks[0] == 0) |
|
50 | 64 | mdl.add((marks[1] - marks[0]) < (marks[ORDER - 1] - marks[ORDER - 2])) |
51 | 65 |
|
52 | 66 | # Minimize ruler size (position of the last mark) |
53 | | -minexpr = minimize(marks[ORDER - 1]) |
54 | | -mdl.add(minexpr) |
| 67 | +mdl.add(mdl.minimize(marks[ORDER - 1])) |
| 68 | + |
| 69 | + |
| 70 | +#----------------------------------------------------------------------------- |
| 71 | +# Solve the model and display the result |
| 72 | +#----------------------------------------------------------------------------- |
55 | 73 |
|
56 | 74 | # Call propagation |
57 | | -solver = CpoSolver(mdl) |
58 | 75 | try: |
59 | | - msol = solver.propagate() |
60 | | - msol.print_solution() |
| 76 | + msol = mdl.propagate() |
| 77 | + msol.print_solution() |
61 | 78 | except CpoNotSupportedException: |
62 | | - print("The configured solver agent does not support propagation.") |
| 79 | + print("Method 'propagate' not supported by this solver agent") |
0 commit comments