linear-programming - 如何在GLPK中为变量编写if条件?

标签 linear-programming glpk mathprog

这是我的全部问题:

信息:

*最大限度。总投资:125美元

*支付是购买的单位x支付/单位的总和

*每笔投资成本:买入成本+成本/单位x单位数量(如果您购买至少一个单位)

*费用为每笔投资费用之和

限制条件:

*您可能无法同时投资2和5。

*仅当您至少投资2和3之一时,您才可以投资1。

*您必须投资3、4、5中的至少两项。

*您的投资额不得超过最大单位数。

问题:最大化利润: yield -成本

 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

这是我的问题:

例如我有这个二进制变量y
 yi=1 if xi>0 else yi=0  and i ∈ {1,2,3,4,5}

我宣布我为数据集
 set I;

 data;

 set I := 1 2 3 4 5;

我不知道如何在glpk中将其他条件添加到y变量中。你能帮我吗?

我的造型:
 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;

我收到此语法错误:
      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

最佳答案

您无法直接做到这一点(无法在LP中“直接”写入if约束)。

但是,有解决方法。
例如,您可以编写:

M * yi > xi

其中M是一个大常数(大于xi的任何值)。

这条路:
  • 如果为xi > 0,则约束等效于yi > 0,即yi == 1,因为yi是二进制的(如果M足够大)。
  • 如果为xi == 0,则始终验证约束,并且yi将等于0,因为您的目标随着yi的增加而增加,并且正在最小化。

  • 在这两种情况下,约束都等于if测试。

    关于linear-programming - 如何在GLPK中为变量编写if条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15464392/

    相关文章:

    python - Scipy - 找到矩阵列空间的基础

    variable-assignment - 用于线性瓶颈分配 (LBAP) 的代码或伪代码?

    linear-programming - 用 Mathprog 编写的线性程序中变量的已知值

    grammar - 无法弄清楚如何解决减少/减少冲突

    python - pandas, melt, unmelt 保存索引

    linear-programming - 混合整数线性规划中的整数除法

    python - GLPK线性规划

    python - 为安装编译 pyglpk 时出错(Ubuntu 14.04)

    julia - Julia 的广义旅行商问题