是否可以为 CPLEX 中的变量指定一组特定的值?

Is it possible to specify a specific set of values for a variable in CPLEX?

我使用 Python API 在 DOCPLEX 中使用整型变量。我知道如何使用上限和下限为这些变量提供一系列可能的值,但如何确保变量仅从一组整数中获取值?

例如。我想要一个变量 'var' 只从集合 {-2, -1, 5, 10} 中取值。我该怎么做?

https://www.linkedin.com/pulse/making-optimization-simple-python-alex-fleischer/

具有 cplex 和 cpo 的域中的决策变量

from docplex.mp.model import Model

# Now suppose we can rent only 0,1,3,4 or 5 buses of 40 seats buses

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)

allowedQuantities=[0,1,3,4,5];
mdl.add_constraint(1==sum((a==nbbus40) for a in allowedQuantities))

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)


"""
which gives
nbBus40  =  3.0
nbBus30  =  6.0
"""

或使用 cpoptimizer

#Now suppose we can rent only 0,1,3,4 or 5 buses of 40 seats bu

from docplex.cp.model import CpoModel

mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
mdl.add(nbbus40*40 + nbbus30*30 >= 300)
mdl.minimize(nbbus40*500 + nbbus30*400)

allowedQuantities=[0,1,3,4,5];
nbbus40.set_domain(allowedQuantities)

msol=mdl.solve()

print(msol[nbbus40]," buses 40 seats")
print(msol[nbbus30]," buses 30 seats") 


"""
which gives
3  buses 40 seats
6  buses 30 seats
"""