Reputation: 12559
Here is my full problem:
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
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
Can you please help me out?
Upvotes: 1
Views: 2601
Reputation: 21
There are many errors in your MathProg code. This should work:
set I;
param M;
/*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 cost, >= 0;
maximize profit: sum{i in I} c[i] * x[i] - sum{i in I} (a[i] * y[i] + b[i] * x[i]);
s.t. c01: y[2] + y[5]<=1;
s.t. c02: y[1] <= y[2]+y[3];
s.t. c03: y[3]+y[4]+y[5]>=2;
s.t. c04: x[1]<=5;
s.t. c05: sum{i in I} (a[i]*y[i] + b[i]*x[i]) <= 125;
s.t. c6{i in I}: M * y[i] >= x[i]; /* if condition of y[i] */
s.t. c10: x[2]<=4;
s.t. c11: x[3]<=5;
s.t. c12: x[4]<=7;
s.t. c13: x[5]<=3;
data;
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;
Upvotes: 2
Reputation: 12559
This answer came from Andrew Makhorin from [email protected]
In MathProg you cannot assign a value to a variable.
If you need to fix a variable at some value, you need to use appropriate equality constraint, e.g.
var po;
s.t. foo: po = sum{i in I} c[i]*x[i];
var cost;
s.t. bar: cost = sum{i in I} a[i]*y[i] + b[i]*x[i];
Upvotes: 1