我最近从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])
答案 0 :(得分:1)
是的,您可能会显示这样的约束,但是在您的情况下,您有一组约束。 让我在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, 3, 2, 1, 0, 2, 1, 0, 1, 0]) + element(slab_use5, [0, 10, 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的行
# 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)
致谢