from docplex.cp.model import CpoModel
mdl = CpoModel()
在[0,10]:
之间创建一个整数变量variable = mdl.integer_var(min=0, max=10, name="variable")
如何创建只能从列表中取整数值的变量?例如,变量只能是1,3,5或6?
答案 0 :(得分:0)
您可以使用set_domain
set_domain([1, 3, 4, 5, 9])
问候
NB:
完整示例:
from docplex.cp.model import CpoModel
mdl = CpoModel()
dvar = mdl.integer_var(0, 3, "dvar")
dvar.set_domain([1, 3])
mdl.add(mdl.minimize(dvar))
msol = mdl.solve( )
if msol:
print("Solution status: " + msol.get_solve_status())
print("dvar = " , msol[dvar])
else:
print("No solution found")
NB:
你也可以写
from docplex.cp.model import CpoModel
mdl = CpoModel()
dvar = mdl.integer_var(name="dvar",domain=[1, 3])
mdl.add(mdl.minimize(dvar))
msol = mdl.solve( )
if msol:
print("Solution status: " + msol.get_solve_status())
print("dvar = " , msol[dvar])
else:
print("No solution found")