Create an integer variable that can only takes values from a list with the cplex python api

you could use set_domain



set_domain([1, 3, 4, 5, 9])


regards



NB:



complete example:



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:



You could also write



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")


More: