Simple_Linear_Programming_with_CPLEX icon indicating copy to clipboard operation
Simple_Linear_Programming_with_CPLEX copied to clipboard

How to use Continuous Variables for IF-THEN constraints on DOCPLEX (Python)?

Open Alizak97 opened this issue 6 years ago • 0 comments

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?

Alizak97 avatar Nov 14 '19 09:11 Alizak97