Skip to content

Commit

Permalink
Merge pull request #47 from IBMDecisionOptimization/release_2.21.207
Browse files Browse the repository at this point in the history
sync with release docplex 2.21
  • Loading branch information
vlkong authored Jun 7, 2021
2 parents f540bcc + 2efff7f commit 78fbc1c
Show file tree
Hide file tree
Showing 26 changed files with 1,377 additions and 1,135 deletions.
43 changes: 20 additions & 23 deletions examples/cp/visu/flow_shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
Please refer to documentation for appropriate setup of solving configuration.
"""

from docplex.cp.model import CpoModel
import docplex.cp.utils_visu as visu
from docplex.cp.model import *
import os


#-----------------------------------------------------------------------------
# Initialize the problem data
#-----------------------------------------------------------------------------
Expand All @@ -30,11 +28,10 @@
# The rest of the file consists of one line per job that contains the list of
# operations given as durations for each machines.

filename = os.path.dirname(os.path.abspath(__file__)) + "/data/flowshop_default.data"
with open(filename, "r") as file:
filename = os.path.dirname(os.path.abspath(__file__)) + '/data/flowshop_default.data'
with open(filename, 'r') as file:
NB_JOBS, NB_MACHINES = [int(v) for v in file.readline().split()]
JOB_DURATIONS = [[int(v) for v in file.readline().split()] for i in range(NB_JOBS)]

OP_DURATIONS = [[int(v) for v in file.readline().split()] for i in range(NB_JOBS)]

#-----------------------------------------------------------------------------
# Build the model
Expand All @@ -44,40 +41,40 @@
mdl = CpoModel()

# Create one interval variable per job operation
job_operations = [[mdl.interval_var(size=JOB_DURATIONS[j][m], name="J{}-M{}".format(j, m)) for m in range(NB_MACHINES)] for j in range(NB_JOBS)]
operations = [[interval_var(size=OP_DURATIONS[j][m], name='J{}-M{}'.format(j, m)) for m in range(NB_MACHINES)] for j in range(NB_JOBS)]

# Force each operation to start after the end of the previous
for j in range(NB_JOBS):
for m in range(1, NB_MACHINES):
mdl.add(mdl.end_before_start(job_operations[j][m - 1], job_operations[j][m]))
for m in range(1,NB_MACHINES):
mdl.add(end_before_start(operations[j][m-1], operations[j][m]))

# Force no overlap for operations executed on a same machine
for m in range(NB_MACHINES):
mdl.add(mdl.no_overlap([job_operations[j][m] for j in range(NB_JOBS)]))
mdl.add(no_overlap(operations[j][m] for j in range(NB_JOBS)))

# Minimize termination date
mdl.add(mdl.minimize(mdl.max([mdl.end_of(job_operations[i][NB_MACHINES - 1]) for i in range(NB_JOBS)])))

mdl.add(minimize(max(end_of(operations[i][NB_MACHINES-1]) for i in range(NB_JOBS))))

#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------

# Solve model
print("Solving model....")
msol = mdl.solve(FailLimit=10000, TimeLimit=10)
print("Solution: ")
msol.print_solution()
print('Solving model...')
res = mdl.solve(TimeLimit=10,LogPeriod=1000000)
print('Solution:')
res.print_solution()

# Display solution
if msol and visu.is_visu_enabled():
visu.timeline("Solution for flow-shop " + filename)
visu.panel("Jobs")
import docplex.cp.utils_visu as visu
if res and visu.is_visu_enabled():
visu.timeline('Solution for flow-shop ' + filename)
visu.panel('Jobs')
for i in range(NB_JOBS):
visu.sequence(name='J' + str(i),
intervals=[(msol.get_var_solution(job_operations[i][j]), j, 'M' + str(j)) for j in range(NB_MACHINES)])
visu.panel("Machines")
intervals=[(res.get_var_solution(operations[i][j]), j, 'M' + str(j)) for j in range(NB_MACHINES)])
visu.panel('Machines')
for j in range(NB_MACHINES):
visu.sequence(name='M' + str(j),
intervals=[(msol.get_var_solution(job_operations[i][j]), j, 'J' + str(i)) for i in range(NB_JOBS)])
intervals=[(res.get_var_solution(operations[i][j]), j, 'J' + str(i)) for i in range(NB_JOBS)])
visu.show()
40 changes: 19 additions & 21 deletions examples/cp/visu/flow_shop_permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
Please refer to documentation for appropriate setup of solving configuration.
"""

from docplex.cp.model import CpoModel
import docplex.cp.utils_visu as visu
from docplex.cp.model import *
import os


Expand All @@ -30,10 +29,10 @@
# First line contains the number of jobs, and the number of machines.
# The rest of the file consists of one line per job that contains the list of
# operations given as durations for each machines.
filename = os.path.dirname(os.path.abspath(__file__)) + "/data/flowshop_default.data"
with open(filename, "r") as file:
filename = os.path.dirname(os.path.abspath(__file__)) + '/data/flowshop_default.data'
with open(filename, 'r') as file:
NB_JOBS, NB_MACHINES = [int(v) for v in file.readline().split()]
JOB_DURATIONS = [[int(v) for v in file.readline().split()] for i in range(NB_JOBS)]
OP_DURATIONS = [[int(v) for v in file.readline().split()] for i in range(NB_JOBS)]


#-----------------------------------------------------------------------------
Expand All @@ -44,47 +43,46 @@
mdl = CpoModel()

# Create one interval variable per job operation
job_operations = [[mdl.interval_var(size=JOB_DURATIONS[j][m], name="J{}-M{}".format(j, m)) for m in range(NB_MACHINES)] for j in range(NB_JOBS)]
operations = [[interval_var(size=OP_DURATIONS[j][m], name='J{}-M{}'.format(j, m)) for m in range(NB_MACHINES)] for j in range(NB_JOBS)]

# Create sequence of operation for each machine
op_sequences = [mdl.sequence_var([job_operations[i][j] for i in range(NB_JOBS)], name="M{}".format(j)) for j in range(NB_MACHINES)]
op_sequences = [sequence_var([operations[i][j] for i in range(NB_JOBS)], name='M{}'.format(j)) for j in range(NB_MACHINES)]

# Force each operation to start after the end of the previous
for j in range(NB_JOBS):
for m in range(1, NB_MACHINES):
mdl.add(mdl.end_before_start(job_operations[j][m - 1], job_operations[j][m]))
mdl.add(end_before_start(operations[j][m-1], operations[j][m]))

# Force no overlap for operations executed on a same machine
for m in range(NB_MACHINES):
mdl.add(mdl.no_overlap(op_sequences[m]))
mdl.add(no_overlap(op_sequences[m]))

# Force sequences to be all identical on all machines
for m in range(1, NB_MACHINES):
mdl.add(mdl.same_sequence(op_sequences[0], op_sequences[m]))
mdl.add(same_sequence(op_sequences[0], op_sequences[m]))

# Minimize termination date
mdl.add(mdl.minimize(mdl.max([mdl.end_of(job_operations[i][NB_MACHINES - 1]) for i in range(NB_JOBS)])))
mdl.add(minimize(max([end_of(operations[i][NB_MACHINES-1]) for i in range(NB_JOBS)])))


#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------

# Solve model
print("Solving model....")
msol = mdl.solve(FailLimit=10000, TimeLimit=10)
print("Solution: ")
msol.print_solution()
print('Solving model...')
res = mdl.solve(FailLimit=10000, TimeLimit=10)

# Draw solution
if msol and visu.is_visu_enabled():
visu.timeline("Solution for permutation flow-shop " + filename)
visu.panel("Jobs")
import docplex.cp.utils_visu as visu
if res and visu.is_visu_enabled():
visu.timeline('Solution for permutation flow-shop ' + filename)
visu.panel('Jobs')
for i in range(NB_JOBS):
visu.sequence(name='J' + str(i),
intervals=[(msol.get_var_solution(job_operations[i][j]), j, 'M' + str(j)) for j in range(NB_MACHINES)])
visu.panel("Machines")
intervals=[(res.get_var_solution(operations[i][j]), j, 'M' + str(j)) for j in range(NB_MACHINES)])
visu.panel('Machines')
for j in range(NB_MACHINES):
visu.sequence(name='M' + str(j),
intervals=[(msol.get_var_solution(job_operations[i][j]), j, 'J' + str(i)) for i in range(NB_JOBS)])
intervals=[(res.get_var_solution(operations[i][j]), j, 'J' + str(i)) for i in range(NB_JOBS)])
visu.show()
64 changes: 32 additions & 32 deletions examples/cp/visu/house_building_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
Please refer to documentation for appropriate setup of solving configuration.
"""

from docplex.cp.model import CpoModel
import docplex.cp.utils_visu as visu
from docplex.cp.model import *


#-----------------------------------------------------------------------------
Expand All @@ -24,44 +23,45 @@
# Create model
mdl = CpoModel()

masonry = mdl.interval_var(name='masonry', size=35)
carpentry = mdl.interval_var(name='carpentry', size=15)
plumbing = mdl.interval_var(name='plumbing', size=40)
ceiling = mdl.interval_var(name='ceiling', size=15)
roofing = mdl.interval_var(name='roofing', size=5)
painting = mdl.interval_var(name='painting', size=10)
windows = mdl.interval_var(name='windows', size=5)
facade = mdl.interval_var(name='facade', size=10)
garden = mdl.interval_var(name='garden', size=5)
moving = mdl.interval_var(name='moving', size=5)
masonry = interval_var(name='masonry', size=35)
carpentry = interval_var(name='carpentry', size=15)
plumbing = interval_var(name='plumbing', size=40)
ceiling = interval_var(name='ceiling', size=15)
roofing = interval_var(name='roofing', size=5)
painting = interval_var(name='painting', size=10)
windows = interval_var(name='windows', size=5)
facade = interval_var(name='facade', size=10)
garden = interval_var(name='garden', size=5)
moving = interval_var(name='moving', size=5)

# Add precedence constraints
mdl.add(mdl.end_before_start(masonry, carpentry))
mdl.add(mdl.end_before_start(masonry, plumbing))
mdl.add(mdl.end_before_start(masonry, ceiling))
mdl.add(mdl.end_before_start(carpentry, roofing))
mdl.add(mdl.end_before_start(ceiling, painting))
mdl.add(mdl.end_before_start(roofing, windows))
mdl.add(mdl.end_before_start(roofing, facade))
mdl.add(mdl.end_before_start(plumbing, facade))
mdl.add(mdl.end_before_start(roofing, garden))
mdl.add(mdl.end_before_start(plumbing, garden))
mdl.add(mdl.end_before_start(windows, moving))
mdl.add(mdl.end_before_start(facade, moving))
mdl.add(mdl.end_before_start(garden, moving))
mdl.add(mdl.end_before_start(painting, moving))
mdl.add(end_before_start(masonry, carpentry))
mdl.add(end_before_start(masonry, plumbing))
mdl.add(end_before_start(masonry, ceiling))
mdl.add(end_before_start(carpentry, roofing))
mdl.add(end_before_start(ceiling, painting))
mdl.add(end_before_start(roofing, windows))
mdl.add(end_before_start(roofing, facade))
mdl.add(end_before_start(plumbing, facade))
mdl.add(end_before_start(roofing, garden))
mdl.add(end_before_start(plumbing, garden))
mdl.add(end_before_start(windows, moving))
mdl.add(end_before_start(facade, moving))
mdl.add(end_before_start(garden, moving))
mdl.add(end_before_start(painting, moving))


#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------

# Solve model
print("Solving model....")
msol = mdl.solve(TimeLimit=10)
print("Solution: ")
msol.print_solution()
print('Solving model...')
res = mdl.solve(TimeLimit=10)
print('Solution:')
res.print_solution()

# Draw solution
if msol and visu.is_visu_enabled():
visu.show(msol)
import docplex.cp.utils_visu as visu
if res and visu.is_visu_enabled():
visu.show(res)
Loading

0 comments on commit 78fbc1c

Please sign in to comment.