I am relatively new to (mixed) integer programming and got stuck with the formulation of a constraint.
In my simplified model I have one Parameter and two Variables that are positive Reals having the value 321 as upper bound. The logic I want to express is here:
if Parameter > Variable1:
Variable2 = Variable1
else:
Variable2 = Parameter
**edit** (while Variable1 is always >= Variable2)
Is it actually possible to describe this using linear in(equalities)?
If it helps: For the implementation I am using Python, Pyomo and the newest gurobi solver.
Thanks for your help!
Edit: Setting
Variable2
equal to min or max ofVariable1
andParameter
.min(Parameter,Variable1)
:If you are sure that
Variable2
"wants" to be small in the objective function, then you just need to requireVariable2
to be less than or equal to bothParameter
andVariable1
:max(Parameter,Variable1)
:If you are sure that
Variable2
"wants" to be large in the objective function, then you just need to requireVariable2
to be greater than or equal to bothParameter
andVariable1
:In either case:
If there's a chance that it will be optimal to set
Variable2
to something strictly less thanmin(Parameter,Variable1)
/ strictly greater thanmax(Parameter,Variable1)
, then you will also (in addition to the constraints above) need to introduce a new binary variable that equals 1 ifParameter > Variable1
:where
M
is a large number. So, ifParameter > Variable1
thenNewVar
must equal 1, while ifParameter < Variable1
thenNewVar
must equal 0.min(Parameter,Variable1)
:Introduce constraints that ensure
Variable2 >= min(Parameter,Variable1)
:So, if
Parameter > Variable1
thenNewVar = 1
, the first constraint has no effect, and the second saysVariable2 >= Variable1
. IfParameter < Variable1
thenNewVar = 0
, the first constraint saysVariable2 >= Parameter
, and the second constraint has no effect.max(Parameter,Variable1)
:Introduce constraints that ensure
Variable2 <= max(Parameter,Variable1)
:So, if
Parameter > Variable1
thenNewVar = 1
, the first constraint saysVariable2 <= Parameter
, and the second constraint has no effect. IfParameter < Variable1
thenNewVar = 0
, the first constraint has no effect, and the second saysVariable2 <= Variable1
.In either case:
Note that
M
should be as small as possible while still ensuring that triggering theM
in the constraint makes the constraint non-binding. I think it's sufficient to set it equal to the largest value that|Parameter - Variable1|
can possibly get. In general these "big-Ms" weaken the formulation and result in longer solve times, so you always want them as small as possible.