sharp_c-tudent
sharp_c-tudent

Reputation: 463

How to say a variable is one of three values in linear programming

I'm using LPSolve to solve a linear problem, however I'm having problems expressing myself on the constraints. I want to write on my constraints that a variable t3 is one of three values. so I did something like this:

min t3;

t3 >= 2+t1;
t3 >= 2+t4;
t3 >= 2+t8;

(This is just an example not the actual thing of course otherwise t4, t1 and t8 where not doing anything). However the final value of t3 is the highest and not the lowest... What could I be doing wrong, I think this makes sense. Of course it doesn't work if I try to maximize t3 as there isn't an upper bound but that's not really the issue here I think.

Upvotes: 3

Views: 1110

Answers (1)

josliber
josliber

Reputation: 44330

It sounds like you're trying to model some variable z to be equal to exactly one of the values {t1, t2, t3}. I would approach this by adding three binary variables, b1, b2, and b3, which indicate the selected element. Since we only select one of the three elements, we would add the constraint:

b1 + b2 + b3 = 1

Now we need to enforce the relationship between z and the b variables. To do so, I'll define three new variables z1, z2, and z3. Variable zi will take value 0 if bi=0 and will otherwise take value ti. To do this, we include the following constraints, where M is a large positive constant:

z1 >= 0 - M*b1
z1 <= M*b1
z1 >= t1 - M*(1-b1)
z1 <= t1 + M*(1-b1)

If b1=0, then the first two constraints fix z1=0, and the next two constraints do nothing. If b1=1, then the first two constraints do nothing and the next two constraints fix z1=t1. You would add the same four constraints to set z2 based on b2 and t2 and to set z3 based on b3 and t3.

Finally, you need to set z based on the zi values:

z = z1 + z2 + z3

Upvotes: 4

Related Questions