Skip to content Skip to sidebar Skip to footer

How To Use Continuous Variables For IF-THEN Constraints On DOCPLEX (Python)?

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-TH

Solution 1:

Copying my answer from here:

You cannot use continuous variables for if-then constraints.

The reason is this: the 'if' clause can take value either true or false. Depending on this, the 'then' clause is activated or not. If nbBus40 is continuous then CPLEX does have to distinguish the cases nbBus40 >= 3 and nbBus40 < 3. Note that the latter is strict inequality! Strict inequality is not supported by the theory of linear programming.

If nbBus40 is instead integer then the cases to distinguish can be written as nbBus40 >= 3 and nbBus40 <= 2. None of these is a strict inequality.

A typical way around this is to use an epsilon and define the two cases nbBus40 >= 3 and nbBus40 <= 3 - eps. That will then be supported as well. However, the eps should depend on the actual expression, so there is no good way to choose a generic eps. That is why docplex leaves that to the user.

You can write your constraints like so:

 with Model() as m:
     nbBus40 = m.continuous_var()
     nbBus30 = m.continuous_var()
     helper = m.binary_var()

     eps = 1e-3
     m.add(m.if_then(helper == 0, nbBus40 <= 3 - eps))
     m.add(m.if_then(helper == 1, nbBus40 >= 3))
     m.add(m.if_then(helper == 1, nbBus30 >= 7))
     m.solve()

Notice however that having these eps is frequently asking for numerical trouble. So if_then on continuous expressions is best avoided. Maybe you can elaborate why you want to consider a fractional number of buses. It could well be that there are other ways to achieve what you want.


Post a Comment for "How To Use Continuous Variables For IF-THEN Constraints On DOCPLEX (Python)?"