0

I am relatively new to integer programming and (again) got stuck with the formulation of a constraint.

In my simplified model I have a (continous) variable with a lower bound LB below zero and an upper bound UB above zero. Now I want to assign the variable value to other variables depending on the value that the variable has taken.

The logic I want to express is the following:

LB > 0
UB > 0
-LB <= Variable1 <= UB

if Variable1 => 0:
    Variable2 = Variable1
    Variable3 = 0
else:
    Variable2 = 0
    Variable3 = abs(Variable1)

How can I describe this using linear (in)equalities?

I guess am a bit slow on the uptake..

Thanks in advance!

** Edit: For the modeling I am using Python, Pyomo and the newest Gurobi solver.

*** Edit: I have now formulated it the following way by the use of a binary variable. (I know it is quadratic but this can be linearized later):

LB > 0
UB > 0

-LB <= Variable1 <= UB
0 <= Variable2 <= UB
0 <= Variable3 <= LB
Variable4 = Variable2 * BinaryVariable - Variable3 * (1-BinaryVariable)

But now I still have to make sure that Variable3 is 0 if Variable2 is > 0 and vice versa.

Any ideas?

Cord Kaldemeyer
  • 6,405
  • 8
  • 51
  • 81
  • "mixed integer programming" .... "within ... reals" ... So is this integer programming, or floating point (pseudo-real)? Also what language/framework/platform/program are you talking about here? Expressing logic of any sort generally requires a particular language to be utilized. And what's wrong with the logic you've already expressed? What have you tried, and what isn't working about it? – twalberg Feb 12 '15 at 17:46
  • Oh, sorry, you are right! It is integer programming and the variables are continous (see my edit). I am searching for a way to describe this using (in)equalities. For the modeling I am using Python, Pyomo and the newest gurobi solver! – Cord Kaldemeyer Feb 12 '15 at 22:49

1 Answers1

1

First create a binary variable that equals 1 if Variable1 > 0 and 0 if Variable1 < 0:

Variable1 <= UB * BinaryVar
LB * (1 - BinaryVar) <= Variable1

(If Variable1 > 0, then BinaryVar must equal 1. If Variable1 < 0, then BinaryVar must equal 0. Note that if Variable1 = 0, then BinaryVar could equal 0 or 1, but that doesn't matter for your problem because if Variable1 = 0 then Variable2 = Variable3 = 0 and the constraints below work out OK.)

Now add constraints enforcing the values for Variable2 and Variable3:

Variable2 = Variable1 * BinaryVar
Variable3 = -Variable1 * (1 - BinaryVar)

These are quadratic constraints, which you can then linearize.

Linearization:

Variable2 <= UB * BinaryVar
Variable2 >= -LB * BinaryVar
Variable2 <= Variable1 + LB * (1 - BinaryVar)
Variable2 >= Variable1 - UB * (1 - BinaryVar)
Variable3 = Variable2 - Variable1
Cord Kaldemeyer
  • 6,405
  • 8
  • 51
  • 81
LarrySnyder610
  • 2,277
  • 12
  • 24
  • Thanks for your answer, grendelsdad! Your formulation seems logical to me. But if I put the equations in my model, the BinaryVar gets always set to 1 and Variable1, Variable2 and Variable3 are set to zero (or SomeValue*e-13). I have already checked if Variable1 stays within the upper and lower bounds which is the case. So far, I could not find the reason why this does't work... – Cord Kaldemeyer Feb 14 '15 at 16:31
  • The problem occurs with the second unequation (LB * (1 - BinaryVar) <= Variable1) if the value of Variable1 is negative. Then, the unequation cannot be fulfilled since LB * (1 - BinaryVar) can only be above or equal zero. I am already looking for a solution.. – Cord Kaldemeyer Feb 14 '15 at 17:16
  • Formulating the second unequation to "-LB * (1 - BinaryVar) <= Variable1" should fix the problem but if I formulate it like this, the solver also sets the values for Variable1 either to zero, SomeValue x e-13 or -SomeValue x e-13 and in the case that Variable1 equals zero the values for the BinaryVariable get set randomly to 0/1. I guess the problem occurs in the case that Variable1 is exactly zero.. – Cord Kaldemeyer Feb 14 '15 at 17:36
  • By the way, I have changed the question formulation to LB > 0, UB > 0 and -LB <= Variable1 <= UB since it is a bit more intuitive.. – Cord Kaldemeyer Feb 14 '15 at 17:49
  • Interesting. In the formulation "LB > 0 / UB > 0 / -LB <= Variable1 <= UB / Variable1 <= UB * BinaryVar / -LB * (1 - BinaryVar) <= Variable1" the BinaryVariable gets set rightly in most cases. But somehow then the upper and lower boundaries UB/LB for Variable1 are not satisfied and values like SomeValue x e-15 or -SomeValue x e-15 occur. For these values, the value of BinaryVariable ist set wrongly.. – Cord Kaldemeyer Feb 14 '15 at 18:21
  • What you're describing sounds perfectly "legal" given the constraints I suggested and the problem that you posed in the OP. If `Variable1 = 0` then `BinaryVar` can equal either 0 or 1 (it doesn't matter) and we should have `Variable2 = Variable3 = 0`. I think probably your objective function *wants* to have `Variable1 = 0`, in which case all the other variables follow. – LarrySnyder610 Feb 15 '15 at 19:11
  • The second inequality should be fine even if `Variable1 < 0` provided `LB` is negative (or positive but you replace it with `-LB`) -- if `BinaryVar = 1` then `Variable1` must be `>= 0` and if `BinaryVar = 0` then `Variable1` is unrestricted, which is what we want. – LarrySnyder610 Feb 15 '15 at 19:12
  • As for the e-15 issue, I think you'll need to give the specific values of all the variables, not just a verbal description of some of them. And, providing the objective function would help as well. – LarrySnyder610 Feb 15 '15 at 19:12
  • Thanks for helping me! Somehow the BinaryVar now gets set correctly to 1 if Variable1>0 and to 0 if Variable1<0.. But if I activate the "assigment constraints" the values for Variable2 and Variable3 are set to zero no matter if the BinaryVar has a value of 1 or 0. Variable1 is set implicitly to the sum of 3 other Variables and not contained in the objective function. Neither are Variable2 and Variable3. My model is quite complex and I guess posting it here would probably be too confusing. – Cord Kaldemeyer Feb 16 '15 at 09:06
  • But it seems I now only have to solve my assignment problem ;-) – Cord Kaldemeyer Feb 16 '15 at 09:07
  • OK, well hopefully this is enough to get you started. – LarrySnyder610 Feb 16 '15 at 12:31
  • After linearizing the last two unequations like described [here](http://orinanobworld.blogspot.de/2010/10/binary-variables-and-quadratic-terms.html) it works perfectly! So it somehow was the quadratic formulation that did not work. Thank you!!! You saved my week again ;-) – Cord Kaldemeyer Feb 17 '15 at 10:06
  • I have added the linearization to your answer! – Cord Kaldemeyer Feb 17 '15 at 10:18