Skip to content

Commit 0dda1f2

Browse files
committed
2.3.44
1 parent 2dbbc1f commit 0dda1f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+723
-3160
lines changed

examples/cp/basic/color.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
Please refer to documentation for appropriate setup of solving configuration.
1515
"""
1616

17-
from docplex.cp.model import *
17+
from docplex.cp.model import CpoModel
1818

1919
# Create CPO model
2020
mdl = CpoModel()
2121

2222
# Create model variables containing colors of the countries
23-
Belgium = integer_var(0, 3, "Belgium")
24-
Denmark = integer_var(0, 3, "Denmark")
25-
France = integer_var(0, 3, "France")
26-
Germany = integer_var(0, 3, "Germany")
27-
Luxembourg = integer_var(0, 3, "Luxembourg")
28-
Netherlands = integer_var(0, 3, "Netherlands")
23+
Belgium = mdl.integer_var(0, 3, "Belgium")
24+
Denmark = mdl.integer_var(0, 3, "Denmark")
25+
France = mdl.integer_var(0, 3, "France")
26+
Germany = mdl.integer_var(0, 3, "Germany")
27+
Luxembourg = mdl.integer_var(0, 3, "Luxembourg")
28+
Netherlands = mdl.integer_var(0, 3, "Netherlands")
2929
ALL_COUNTRIES = (Belgium, Denmark, France, Germany, Luxembourg, Netherlands)
3030

3131
# Create constraints

examples/cp/basic/facility.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
from docplex.cp.model import CpoModel
3232
from collections import namedtuple
3333

34-
##############################################################################
35-
## Problem data
36-
##############################################################################
34+
#-----------------------------------------------------------------------------
35+
# Initialize the problem data
36+
#-----------------------------------------------------------------------------
3737

3838
Warehouse = namedtuple('Wharehouse', ('city', # Name of the city
3939
'capacity', # Capacity of the warehouse
@@ -62,9 +62,9 @@
6262
(54, 64, 65, 89, 89))
6363

6464

65-
##############################################################################
66-
## Modeling
67-
##############################################################################
65+
#-----------------------------------------------------------------------------
66+
# Build the model
67+
#-----------------------------------------------------------------------------
6868

6969
# Create CPO model
7070
mdl = CpoModel()
@@ -93,9 +93,9 @@
9393
mdl.add(mdl.minimize(total_cost))
9494

9595

96-
##############################################################################
97-
## Solving
98-
##############################################################################
96+
#-----------------------------------------------------------------------------
97+
# Solve the model and display the result
98+
#-----------------------------------------------------------------------------
9999

100100
# Solve model
101101
print("\nSolving model....")

examples/cp/basic/golomb_ruler.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,39 @@
1717
Please refer to documentation for appropriate setup of solving configuration.
1818
"""
1919

20-
from docplex.cp.model import *
20+
from docplex.cp.model import CpoModel
2121
from sys import stdout
2222

23-
# Set model parameters
24-
ORDER = 8 # Number of marks
25-
MAX_LENGTH = (ORDER - 1) ** 2 # Max rule length
23+
24+
#-----------------------------------------------------------------------------
25+
# Initialize the problem data
26+
#-----------------------------------------------------------------------------
27+
28+
# Number of marks on the ruler
29+
ORDER = 8
30+
31+
32+
#-----------------------------------------------------------------------------
33+
# Prepare the data for modeling
34+
#-----------------------------------------------------------------------------
35+
36+
# Estimate an upper bound to the ruler length
37+
MAX_LENGTH = (ORDER - 1) ** 2
38+
39+
40+
#-----------------------------------------------------------------------------
41+
# Build the model
42+
#-----------------------------------------------------------------------------
2643

2744
# Create model
2845
mdl = CpoModel()
2946

3047
# Create array of variables corresponding to position ruler marks
31-
marks = integer_var_list(ORDER, 0, MAX_LENGTH, "M")
48+
marks = mdl.integer_var_list(ORDER, 0, MAX_LENGTH, "M")
3249

3350
# Create marks distances that should be all different
3451
dist = [marks[i] - marks[j] for i in range(1, ORDER) for j in range(0, i)]
35-
mdl.add(all_diff(dist))
52+
mdl.add(mdl.all_diff(dist))
3653

3754
# Avoid symmetric solutions by ordering marks
3855
mdl.add(marks[0] == 0)
@@ -43,9 +60,13 @@
4360
mdl.add((marks[1] - marks[0]) < (marks[ORDER - 1] - marks[ORDER - 2]))
4461

4562
# Minimize ruler size (position of the last mark)
46-
mdl.add(minimize(marks[ORDER - 1]))
63+
mdl.minimize(marks[ORDER - 1])
64+
65+
66+
#-----------------------------------------------------------------------------
67+
# Solve the model and display the result
68+
#-----------------------------------------------------------------------------
4769

48-
# Solve model
4970
print("\nSolving model....")
5071
msol = mdl.solve(TimeLimit=100)
5172

examples/cp/basic/golomb_ruler_all_solutions.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,39 @@
2929
"""
3030

3131

32-
from docplex.cp.model import *
32+
from docplex.cp.model import CpoModel
33+
from docplex.cp.utils import CpoNotSupportedException
3334
from sys import stdout
3435

35-
# Set model parameters
36-
ORDER = 7 # Number of marks
37-
MAX_LENGTH = (ORDER - 1) ** 2 # Max rule length
36+
#-----------------------------------------------------------------------------
37+
# Initialize the problem data
38+
#-----------------------------------------------------------------------------
39+
40+
# Number of marks on the ruler
41+
ORDER = 7
42+
43+
44+
#-----------------------------------------------------------------------------
45+
# Prepare the data for modeling
46+
#-----------------------------------------------------------------------------
47+
48+
# Estimate an upper bound to the ruler length
49+
MAX_LENGTH = (ORDER - 1) ** 2
50+
51+
52+
#-----------------------------------------------------------------------------
53+
# Build the model
54+
#-----------------------------------------------------------------------------
3855

3956
# Create model
4057
mdl = CpoModel()
4158

4259
# Create array of variables corresponding to position rule marks
43-
marks = integer_var_list(ORDER, 0, MAX_LENGTH, "M")
60+
marks = mdl.integer_var_list(ORDER, 0, MAX_LENGTH, "M")
4461

4562
# Create marks distances that should be all different
4663
dist = [marks[i] - marks[j] for i in range(1, ORDER) for j in range(0, i)]
47-
mdl.add(all_diff(dist))
64+
mdl.add(mdl.all_diff(dist))
4865

4966
# Avoid symmetric solutions by ordering marks
5067
mdl.add(marks[0] == 0)
@@ -55,9 +72,14 @@
5572
mdl.add((marks[1] - marks[0]) < (marks[ORDER - 1] - marks[ORDER - 2]))
5673

5774
# Minimize ruler size (position of the last mark)
58-
minexpr = minimize(marks[ORDER - 1])
75+
minexpr = mdl.minimize(marks[ORDER - 1])
5976
mdl.add(minexpr)
6077

78+
79+
#-----------------------------------------------------------------------------
80+
# Solve the model and display the result
81+
#-----------------------------------------------------------------------------
82+
6183
# First solve the model to find the smallest ruler length
6284
msol = mdl.solve(TimeLimit=100)
6385
if not msol:

examples/cp/basic/golomb_ruler_with_propagate.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,38 @@
2222
Please refer to documentation for appropriate setup of solving configuration.
2323
"""
2424

25-
from docplex.cp.model import *
26-
from sys import stdout
25+
from docplex.cp.model import CpoModel, CpoNotSupportedException
2726

28-
from docplex.cp.config import context
2927

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+
#-----------------------------------------------------------------------------
3347

3448
# Create model
3549
mdl = CpoModel()
3650

3751
# 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")
3953

4054
# Create marks distances that should be all different
4155
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))
4357

4458
# Avoid symmetric solutions by ordering marks
4559
mdl.add(marks[0] == 0)
@@ -50,13 +64,16 @@
5064
mdl.add((marks[1] - marks[0]) < (marks[ORDER - 1] - marks[ORDER - 2]))
5165

5266
# 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+
#-----------------------------------------------------------------------------
5573

5674
# Call propagation
57-
solver = CpoSolver(mdl)
5875
try:
59-
msol = solver.propagate()
60-
msol.print_solution()
76+
msol = mdl.propagate()
77+
msol.print_solution()
6178
except CpoNotSupportedException:
62-
print("The configured solver agent does not support propagation.")
79+
print("Method 'propagate' not supported by this solver agent")

examples/cp/basic/golomb_ruler_with_refine_conflicts.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,37 @@
2323
"""
2424

2525

26-
from docplex.cp.model import *
27-
from sys import stdout
26+
from docplex.cp.model import CpoModel
2827

29-
# Set model parameters
30-
ORDER = 5 # Number of marks
31-
MAX_LENGTH = 10 # Max rule length, voluntarily TOO SHORT
28+
#-----------------------------------------------------------------------------
29+
# Initialize the problem data
30+
#-----------------------------------------------------------------------------
31+
32+
# Number of marks on the ruler
33+
ORDER = 5
34+
35+
36+
#-----------------------------------------------------------------------------
37+
# Prepare the data for modeling (Voluntarily wrong)
38+
#-----------------------------------------------------------------------------
39+
40+
# Estimate an upper bound to the ruler length
41+
MAX_LENGTH = 10 # TOO SHORT
42+
43+
44+
#-----------------------------------------------------------------------------
45+
# Build the model
46+
#-----------------------------------------------------------------------------
3247

3348
# Create model
3449
mdl = CpoModel()
3550

3651
# Create array of variables corresponding to position rule marks
37-
marks = integer_var_list(ORDER, 0, MAX_LENGTH, "M")
52+
marks = mdl.integer_var_list(ORDER, 0, MAX_LENGTH, "M")
3853

3954
# Create marks distances that should be all different
4055
dist = [marks[i] - marks[j] for i in range(1, ORDER) for j in range(0, i)]
41-
mdl.add(all_diff(dist))
56+
mdl.add(mdl.all_diff(dist))
4257

4358
# Avoid symmetric solutions by ordering marks
4459
mdl.add(marks[0] == 0)
@@ -49,17 +64,17 @@
4964
mdl.add((marks[1] - marks[0]) < (marks[ORDER - 1] - marks[ORDER - 2]))
5065

5166
# Minimize ruler size (position of the last mark)
52-
minexpr = minimize(marks[ORDER - 1])
53-
mdl.add(minexpr)
67+
mdl.add(mdl.minimize(marks[ORDER - 1]))
68+
69+
70+
#-----------------------------------------------------------------------------
71+
# Solve the model and display the result
72+
#-----------------------------------------------------------------------------
5473

5574
# First solve the model
56-
solver = CpoSolver(mdl)
57-
msol = solver.solve()
75+
msol = mdl.solve()
5876
if msol:
5977
msol.print_solution()
6078
else:
61-
try:
62-
rsol = solver.refine_conflict()
63-
rsol.print_conflict()
64-
except CpoNotSupportedException:
65-
print("The configured solver agent does not support conflict refiner.")
79+
rsol = mdl.refine_conflict()
80+
rsol.print_conflict()

0 commit comments

Comments
 (0)