How can I write an if condition for my variable in GLPK?
Asked Answered
O

1

6

Here is my full problem:

enter image description here

Information:

*Max. total investment: $125

*Pay-off is the sum of the units bought x pay-off/unit

*Cost per investment: Buy-in cost + cost/unit x number of units if you buy at least one unit

*The cost is sum of the costs per investment

Constraints:

*You may not invest in both 2 and 5.

*You may invest in 1 only if you invest at least one of 2 and 3.

*You must invest at least two of 3,4,5.

*You may not invest more than max number of units.

Problem: Maximize profit : pay-off - cost

 xi: # of units i ∈ {1,2,3,4,5}
 yi=1 if xi>0 else yi=0
 cost = sum{i in I} buyInCost_i * yi + cost-unit_i*xi
 pay-off = sum{i in I} (pay-off/unit)_i*xi
 profit = pay-off - cost

 Maximize profit

 Subject to

 y2+y5 <= 1
 y1<= y2+y3
 y3+y4+y5 >= 2
 x1<=5, x2<=4, x3<=5, x4<=7, x5<=3
 cost<=125

Here is my question:

For example I have this binary variable y

 yi=1 if xi>0 else yi=0  and i ∈ {1,2,3,4,5}

I declared i as a data set

 set I;

 data;

 set I := 1 2 3 4 5;

I don't know how to add if else condition to y variable in glpk. Can you please help me out?

My modelling :

 set I;

 /*if x[i]>0 y[i]=1 else y[i]=0 ?????*/
 var y{i in I}, binary;

 param a{i in I};
 /* buy-in cost of investment i */

 param b{i in I};
 /* cost per unit of investment i */

 param c{i in I};
 /* pay-off per unit of investment i */

 param d{i in I};
 /* max number of units of investment i */

 var x{i in I} >=0;
 /* Number of units that is bought of investment i */

 var po := sum{i in I} c[i]*x[i];

 var cost := sum{i in I} a[i]*y[i] + b[i]*x[i];

 maximize profit: po-cost;

 s.t. c1: y[2]+y[5]<=1;
 s.t. c2: y[1]<y[2]+y[3];
 s.t. c3: y[3]+y[4]+y[5]>=2;
 s.t. c4: x[1]<=5 
     x[2]<=4
     x[3]<=5
     x[4]<=7
     x[5]<=3;

 s.t. c5: cost <=125;
 s.t. c6{i in I}: M * y[i] > x[i];   // if condition of y[i] 

 set I := 1 2 3 4 5;
 param a :=
1 25
2 35
3 28
4 20
5 40;

 param b :=
1 5
2 7
3 6
4 4
5 8;

 param c :=
1 15
2 25
3 17
4 13
5 18;

 param d :=
1 5
2 4
3 5
4 7
5 3;

 param M := 10000;

I am getting this syntax error:

      problem.mod:21: syntax error in variable statement 
      Context: ...I } ; param d { i in I } ; var x { i in I } >= 0 ; var po :=
      MathProg model processing error
Ostiole answered 17/3, 2013 at 18:18 Comment(3)
What is x_i? Is it a continuous, integer, or binary variable, or is it input data?Elfin
xi is the number of i product that is bought. Those values should be found by the program to optimize.Coopersmith
I think your answer may be here, but I've never used GLPK: en.wikibooks.org/wiki/GLPK/GMPL_WorkaroundsElfin
L
11

You can't directly do that (there is no way to write 'directly' an if constraint in a LP).

However, there are workarounds for this. For example, you can write:

M * yi > xi

where M is a large constant (greater than any value of xi).

This way:

  • if xi > 0, then the constraint is equivalent to yi > 0, that is yi == 1 since yi is binary (if M is large enough).
  • if xi == 0, then the constraint is always verified, and yi will be equal to 0 since your objective is increasing with yi and you are minimizing.

in both case, the constraint is equivalent to the if test.

Liz answered 18/3, 2013 at 12:43 Comment(4)
Thank you, Nicholas. I ran my code but It gave me this syntax error and I can't figure it out. Can you please help me out? I edited my question with my modelling and syntax error.Coopersmith
I'm sorry I don't know your modelling language; however, it seems that your declaration var {x in I} >=0; is incorrect. There may be a better way to do it, but I suggest you declare the variable as the other ones var {x in I}; and then add x[i] >= 0 as you did with the other constraints (for each i). If this doesn't work, I suggest you post another question specifically about the synthax error in your model, you will more likely get help (there is few chance someone look at this question now, since you accepted an answer).Liz
Also, if you add another question, you should add a tag for the specific modelling language you use (it looks like ampl for me, but I'm not sure and, as I sayd, I don't use those). glpk is actually the 'core' lp solver: it can be used with a stand-alone program, such as glpsol, etc; but it is also a C library, so tagging glpk is a little bit misleading (your problems seems to be more with the modelling language than with the solver itself).Liz
Well, I changed it as you said but same syntax problem occurred. I am running it with: glpsol -m problem.mod -o problem.sol // So I am gonna ask it in a new question as you suggest now. Thank you. =)Coopersmith

© 2022 - 2024 — McMap. All rights reserved.