You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using DOCPLEX to build up a Mixed Integer Linear Programming (MILP) problem which is then solved via CPLEX on Python. However, upon trying to solve the MILP problem using IF-THEN constraints, I receive the following error:
DOcplexException: Model.if_then(), nbBus40 >= 3.0 is not discrete
This is happening because I have declared nbbus40 variable to be continuous, as shown in the code below:
from docplex.mp.model import Model mdl = Model(name='buses') nbbus40 = mdl.continuous_var(name='nbBus40') nbbus30 = mdl.integer_var(name='nbBus30') mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids') mdl.add(mdl.if_then((nbbus40>=3),(nbbus30>=7))) mdl.minimize(nbbus40*500 + nbbus30*400) mdl.solve() for v in mdl.iter_integer_vars(): print(v," = ",v.solution_value)
However, if I keep the nbbus40 variable as an integer, then I get the solution to the MILP problem, as shown below:
from docplex.mp.model import Model 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.add(mdl.if_then((nbbus40>=3),(nbbus30>=7))) mdl.minimize(nbbus40*500 + nbbus30*400) mdl.solve() for v in mdl.iter_integer_vars(): print(v," = ",v.solution_value)
RESULT:
nbBus40 = 0 nbBus30 = 10.0
How can I use the IF-THEN constraint in DOCPLEX for continuous variables?
The text was updated successfully, but these errors were encountered:
I'm using DOCPLEX to build up a Mixed Integer Linear Programming (MILP) problem which is then solved via CPLEX on Python. However, upon trying to solve the MILP problem using IF-THEN constraints, I receive the following error:
DOcplexException: Model.if_then(), nbBus40 >= 3.0 is not discrete
This is happening because I have declared nbbus40 variable to be continuous, as shown in the code below:
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.continuous_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.add(mdl.if_then((nbbus40>=3),(nbbus30>=7)))
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
However, if I keep the nbbus40 variable as an integer, then I get the solution to the MILP problem, as shown below:
from docplex.mp.model import Model
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.add(mdl.if_then((nbbus40>=3),(nbbus30>=7)))
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
RESULT:
nbBus40 = 0
nbBus30 = 10.0
How can I use the IF-THEN constraint in DOCPLEX for continuous variables?
The text was updated successfully, but these errors were encountered: