如何在 DOcplex 中扩展和打印非全局约束?
How to expand and print non-global constraints in DOcplex?
我最近从 AMPL/CPLEX-CP 迁移到 Python/DOcplex.CP-MP。在 AMPL 中,可以使用 'expand _constraint_name' 命令显示约束的扩展版本,这在调试模型时很有帮助。
DOcplex中有这样的功能吗?例如:
在 DOcplex 中,可以通过某种方式针对 all_diff 等全局约束执行此操作,如下所示:
import docplex.cp.model as cp
NB_QUEEN = 8
mdl = cp.CpoModel()
x = cp.integer_var_list(NB_QUEEN, 0, NB_QUEEN - 1, "X")
# add the constraints to the model
mdl.add(mdl.all_diff(x[i] + i for i in range(NB_QUEEN)))
# print the constraints
test_constraint = mdl.all_diff(x[i] - i for i in range(NB_QUEEN))
print(test_constraint)
这将打印:
alldiff([X_0 - 0, X_1 - 1, X_2 - 2, X_3 - 3, X_4 - 4, X_5 - 5, X_6 - 6, X_7 - 7])
有没有办法打印以下约束的扩展版本?
mdl.add(mdl.sum( X[i]+j for j in R1) ==1 for i in R2)
仅在上述约束条件下调用 print() 是行不通的。
谢谢。
编辑
感谢 Alex Fleischer,我在下面做了一个小的工作示例,展示了如何从一组约束中打印出一个约束。
x = mdl.integer_var_list(5, name='X')
ct = ( [1 == sum( x[i]+j for i in range(0, 5) ) for j in range(0,5) ])
print(ct[2])
打印
0 + (X_0 + 2) + (X_1 + 2) + (X_2 + 2) + (X_3 + 2) + (X_4 + 2) == 1
如果想打印集合中的所有约束,他们可以这样做
for j in range(0, 5)
print(c[j])
是的,您可能会显示此类约束,但在您的情况下,您有一组约束。让我举一个来自 CPLEX_Studio128\python\examples\cp\basic
中钢厂例子的小例子
from docplex.cp.model import CpoModel
from collections import namedtuple
#-----------------------------------------------------------------------------
# Initialize the problem data
#-----------------------------------------------------------------------------
# List of coils to produce (orders)
Order = namedtuple("Order", ['id', 'weight', 'color'])
ORDERS = (
Order( 1, 22, 5),
Order( 2, 9, 3),
Order( 3, 9, 4),
Order( 4, 8, 5),
Order( 5, 8, 7),
Order( 6, 6, 3),
Order( 7, 5, 6),
Order( 8, 3, 0),
Order( 9, 3, 2),
Order(10, 3, 3),
Order(11, 2, 1),
Order(12, 2, 5)
)
# Max number of different colors of coils produced by a single slab
MAX_COLOR_PER_SLAB = 2
# List of available slab weights.
AVAILABLE_SLAB_WEIGHTS = [11, 13, 16, 17, 19, 20, 23, 24, 25,
26, 27, 28, 29, 30, 33, 34, 40, 43, 45]
#-----------------------------------------------------------------------------
# Prepare the data for modeling
#-----------------------------------------------------------------------------
# Upper bound for the number of slabs to use
MAX_SLABS = len(ORDERS)
# Build a set of all colors
allcolors = set(o.color for o in ORDERS)
# The heaviest slab
max_slab_weight = max(AVAILABLE_SLAB_WEIGHTS)
# Minimum loss incurred for a given slab usage.
# loss[v] = loss when smallest slab is used to produce a total weight of v
loss = [0] + [min([sw - use for sw in AVAILABLE_SLAB_WEIGHTS if sw >= use]) for use in range(1, max_slab_weight + 1)]
#-----------------------------------------------------------------------------
# Build the model
#-----------------------------------------------------------------------------
# Create model
mdl = CpoModel()
total_loss=mdl.integer_var(0,10000000,"total loss")
# Index of the slab used to produce each coil order
production_slab = mdl.integer_var_list(len(ORDERS), 0, MAX_SLABS - 1, "production_slab")
# Usage of each slab
slab_use = mdl.integer_var_list(MAX_SLABS, 0, max_slab_weight, "slab_use")
# The orders are allocated to the slabs with capacity
mdl.add(mdl.pack(slab_use, production_slab, [o.weight for o in ORDERS]))
# Constrain max number of colors produced by each slab
for s in range(MAX_SLABS):
su = 0
for c in allcolors:
lo = False
for i, o in enumerate(ORDERS):
if o.color == c:
lo |= (production_slab[i] == s)
su += lo
mdl.add(su <= MAX_COLOR_PER_SLAB)
# Minimize the total loss
ct=(total_loss == sum([mdl.element(slab_use[s], loss) for s in range(MAX_SLABS)]))
mdl.add(ct)
print("ct=",ct)
mdl.add(mdl.minimize(total_loss))
# Set search strategy
mdl.set_search_phases([mdl.search_phase(production_slab)])
#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------
# Solve model
print("Solving model....")
msol = mdl.solve(FailLimit=100000, TimeLimit=10)
# Print solution
if msol:
print("Solution: ")
for s in set(msol[ps] for ps in production_slab):
# Determine orders using this slab
lordrs = [o for i, o in enumerate(ORDERS) if msol[production_slab[i]] == s]
# Compute display attributes
used_weight = msol[slab_use[s]] # Weight used in the slab
loss_weight = loss[used_weight] # Loss weight
colors = set(o.color for o in lordrs) # List of colors
loids = [o.id for o in lordrs] # List of order irs
print("Slab weight={}, used={}, loss={}, colors={}, orders={}"
.format(used_weight + loss_weight, used_weight, loss_weight, colors, loids))
else:
print("No solution found")
这给出了
ct= "total loss" == 0 + element(slab_use0, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use1, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use2, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use3, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use4, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4,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 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0])
显示的行
# Minimize the total loss
ct=(total_loss == sum([mdl.element(slab_use[s], loss) for s in range(MAX_SLABS)]))
mdl.add(ct)
print("ct=",ct)
我最近从 AMPL/CPLEX-CP 迁移到 Python/DOcplex.CP-MP。在 AMPL 中,可以使用 'expand _constraint_name' 命令显示约束的扩展版本,这在调试模型时很有帮助。
DOcplex中有这样的功能吗?例如:
在 DOcplex 中,可以通过某种方式针对 all_diff 等全局约束执行此操作,如下所示:
import docplex.cp.model as cp
NB_QUEEN = 8
mdl = cp.CpoModel()
x = cp.integer_var_list(NB_QUEEN, 0, NB_QUEEN - 1, "X")
# add the constraints to the model
mdl.add(mdl.all_diff(x[i] + i for i in range(NB_QUEEN)))
# print the constraints
test_constraint = mdl.all_diff(x[i] - i for i in range(NB_QUEEN))
print(test_constraint)
这将打印:
alldiff([X_0 - 0, X_1 - 1, X_2 - 2, X_3 - 3, X_4 - 4, X_5 - 5, X_6 - 6, X_7 - 7])
有没有办法打印以下约束的扩展版本?
mdl.add(mdl.sum( X[i]+j for j in R1) ==1 for i in R2)
仅在上述约束条件下调用 print() 是行不通的。
谢谢。
编辑
感谢 Alex Fleischer,我在下面做了一个小的工作示例,展示了如何从一组约束中打印出一个约束。
x = mdl.integer_var_list(5, name='X')
ct = ( [1 == sum( x[i]+j for i in range(0, 5) ) for j in range(0,5) ])
print(ct[2])
打印
0 + (X_0 + 2) + (X_1 + 2) + (X_2 + 2) + (X_3 + 2) + (X_4 + 2) == 1
如果想打印集合中的所有约束,他们可以这样做
for j in range(0, 5)
print(c[j])
是的,您可能会显示此类约束,但在您的情况下,您有一组约束。让我举一个来自 CPLEX_Studio128\python\examples\cp\basic
中钢厂例子的小例子from docplex.cp.model import CpoModel
from collections import namedtuple
#-----------------------------------------------------------------------------
# Initialize the problem data
#-----------------------------------------------------------------------------
# List of coils to produce (orders)
Order = namedtuple("Order", ['id', 'weight', 'color'])
ORDERS = (
Order( 1, 22, 5),
Order( 2, 9, 3),
Order( 3, 9, 4),
Order( 4, 8, 5),
Order( 5, 8, 7),
Order( 6, 6, 3),
Order( 7, 5, 6),
Order( 8, 3, 0),
Order( 9, 3, 2),
Order(10, 3, 3),
Order(11, 2, 1),
Order(12, 2, 5)
)
# Max number of different colors of coils produced by a single slab
MAX_COLOR_PER_SLAB = 2
# List of available slab weights.
AVAILABLE_SLAB_WEIGHTS = [11, 13, 16, 17, 19, 20, 23, 24, 25,
26, 27, 28, 29, 30, 33, 34, 40, 43, 45]
#-----------------------------------------------------------------------------
# Prepare the data for modeling
#-----------------------------------------------------------------------------
# Upper bound for the number of slabs to use
MAX_SLABS = len(ORDERS)
# Build a set of all colors
allcolors = set(o.color for o in ORDERS)
# The heaviest slab
max_slab_weight = max(AVAILABLE_SLAB_WEIGHTS)
# Minimum loss incurred for a given slab usage.
# loss[v] = loss when smallest slab is used to produce a total weight of v
loss = [0] + [min([sw - use for sw in AVAILABLE_SLAB_WEIGHTS if sw >= use]) for use in range(1, max_slab_weight + 1)]
#-----------------------------------------------------------------------------
# Build the model
#-----------------------------------------------------------------------------
# Create model
mdl = CpoModel()
total_loss=mdl.integer_var(0,10000000,"total loss")
# Index of the slab used to produce each coil order
production_slab = mdl.integer_var_list(len(ORDERS), 0, MAX_SLABS - 1, "production_slab")
# Usage of each slab
slab_use = mdl.integer_var_list(MAX_SLABS, 0, max_slab_weight, "slab_use")
# The orders are allocated to the slabs with capacity
mdl.add(mdl.pack(slab_use, production_slab, [o.weight for o in ORDERS]))
# Constrain max number of colors produced by each slab
for s in range(MAX_SLABS):
su = 0
for c in allcolors:
lo = False
for i, o in enumerate(ORDERS):
if o.color == c:
lo |= (production_slab[i] == s)
su += lo
mdl.add(su <= MAX_COLOR_PER_SLAB)
# Minimize the total loss
ct=(total_loss == sum([mdl.element(slab_use[s], loss) for s in range(MAX_SLABS)]))
mdl.add(ct)
print("ct=",ct)
mdl.add(mdl.minimize(total_loss))
# Set search strategy
mdl.set_search_phases([mdl.search_phase(production_slab)])
#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------
# Solve model
print("Solving model....")
msol = mdl.solve(FailLimit=100000, TimeLimit=10)
# Print solution
if msol:
print("Solution: ")
for s in set(msol[ps] for ps in production_slab):
# Determine orders using this slab
lordrs = [o for i, o in enumerate(ORDERS) if msol[production_slab[i]] == s]
# Compute display attributes
used_weight = msol[slab_use[s]] # Weight used in the slab
loss_weight = loss[used_weight] # Loss weight
colors = set(o.color for o in lordrs) # List of colors
loids = [o.id for o in lordrs] # List of order irs
print("Slab weight={}, used={}, loss={}, colors={}, orders={}"
.format(used_weight + loss_weight, used_weight, loss_weight, colors, loids))
else:
print("No solution found")
这给出了
ct= "total loss" == 0 + element(slab_use0, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use1, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use2, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use3, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use4, [0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,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 3, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 2, 1, 0, 1, 0])
显示的行
# Minimize the total loss
ct=(total_loss == sum([mdl.element(slab_use[s], loss) for s in range(MAX_SLABS)]))
mdl.add(ct)
print("ct=",ct)