Mathprog文本文件与3个参数相关

时间:2016-12-03 10:02:18

标签: r mathprog

我一直在尝试使用mathprog方法来解决问题,但是当我使用R来读取txt文件时,我遇到了麻烦。

我想我知道我遇到了什么问题,但我不知道如何解决它。

这是整个代码,但问题是在我使用param a和param tc 3变量时最终:

set I;
/* plants*/

set J;
/* customers*/

set T;
/* types*/

set S;
/* modes*/

param pc{i in I, s in S};
/* variable production cost */

param tc{i in I, j in J, t in T};
/* transportation cost */

param a{i in I, t in T, s in S};
/* quantity of product produced */

param b{j in J, t in T};
/* transportation cost */

var z{i in I, s in S} >= 0;
/* intensity of the plant */

var u{i in I, j in J, t in T} >= 0;
/* amount of product shipped from plant to costumer*/

minimize cost: sum{i in I, s in S} pc[i,s] * z[i,s] + sum{t in T, j in J, i in I} tc[i,j,t] * u[i,j,t];

/* production transportation problem */

s.t. first{i in I, j in J, t in T}: sum{i in I} u[i,j,t] = b[j,t];
/* First Constraint */

s.t. second{i in I, j in J, t in T, s in S}: sum{j in J} u[i,j,t] <= sum{s in S} a[i,t,s] * z[i,s];
/* Second Constraint */

s.t. third{i in I, s in S}: sum{s in S} z[i,s] <= 1;
/* Third Constraint */

s.t. fourth{i in I, s in S}: z[i,s] >= 0;
/* Fourth Constraint */

s.t. fifth{i in I, j in J, t in T}: u[i,j,t] >= 0;
/* Fifth Constraint */

data;

set I := P1 P2;

set J := C1 C2 C3 C4 C5;

set T := T1 T2 T3 T4;

set S := S1;

param pc : S1 :=
P1     5000
P2     7000;

param tc : P1T1 P1T2 P1T3 P1T4 P2T1 P2T2 P2T3 P2T4:= 
C1    10 15 10 20 10 10 12 12
C2    20 10 15 10 12 12 10 15
C3    20 15 10 10 15 10 12 18
C4    10 10 10 10 10 10 10 10
C5    5  10 15 20 20 23 21 15;

param a : S1P1 S1P2 := 
T1    50 100
T2    150 50
T3    200 80
T4    150 200;

param b : T1 T2 T3 T4 := 
C1    80 100 80 120
C2    50 100 105 100
C3    80 105 100 100
C4    80 50 15 100
C5    50  100 150 200;

end;

我使用这些函数在R:

中读取它
model.lp <- Rglpk_read_file ("mathprog_file.txt", type = "MathProg", verbose = F)

1 个答案:

答案 0 :(得分:0)

可能你的Rglpk工作正常。 我看到的是一些错误的mathprog语法。

在你的约束中,你要声明两次相同的索引:

s.t. first{i in I, j in J, t in T}: sum{i in I} u[i,j,t] = b[j,t];

虽然左约束语句中已经定义了i,但您还在sum语句中再次使用i。您可以使用{ii in I}之类的其他表示法来解决此问题,然后在引用i时使用正确的iiu

三维参数的定义中也存在错误。看看这里的示例:explanation of the datablock以获取示例和解释如何执行此操作。