展示推荐

时间:2019-06-08 13:20:04

标签: ampl glpk

我在ampl中有一个2维变量,我想显示它。我想更改索引的顺序,但是我不知道该怎么做!我放置了代码,数据和输出,描述了我想要的输出。 这是我的代码:

param n;
param t;
param w;
param p;
set Var, default{1..n};
set Ind, default{1..t};
set mode, default{1..w};
var E{mode, Ind};
var B{mode,Var};
var C{mode,Ind};
param X{mode,Var,Ind};
var H{Ind};

minimize obj: sum{m in mode,i in Ind}E[m,i];
s.t. a1{m in mode,  i in Ind}: sum{j in Var} X[m,j,i]*B[m,j] -C[m,i]       <=E[m,i];
 solve;
  display C;
data;
param w:=4;
param n:=9;
param t:=2;
param X:=
[*,*,1]: 1    2   3  4 5 6 7 8 9   :=
1   69  59  100 70  35  1   1   0   0
2   34  31  372 71  35  1   0   1   0
3   35  25  417 70  35  1   0   0   1
4   0   10  180 30  35  1   0   0   0
[*,*,2]: 1    2   3  4 5 6 7 8 9   :=
1   64  58  68  68  30  2   1   0   0
2   44  31  354 84  30  2   0   1   0
3   53  25  399 85  30  2   0   0   1
4   0   11  255 50  30  2   0   0   0

使用glpksol的这段代码的输出就像tis:

C[1,1].val = -1.11111111111111
C[1,2].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[2,2].val = -1.11111111111111
C[3,1].val = -0.915032679738562
C[3,2].val = -1.11111111111111
C[4,1].val = 0.141414141414141
C[4,2].val = 0.2003367003367

但是我希望结果是这样的:

C[1,1].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[3,1].val = -0.915032679738562
C[4,1].val = 0.141414141414141
C[1,2].val = -1.11111111111111
C[2,2].val = -1.11111111111111
C[3,2].val = -1.11111111111111
C[4,2].val = 0.2003367003367

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

您可以在for文件中使用printf循环和.run命令:

for {i in Ind}
    for {m in mode}
        printf "C[%d,%d] = %.4f\n", m, i, C[m,i];

甚至:

printf {i in Ind, m in mode} "C[%d,%d] = %.4f\n", m, i, C[m,i];

我没有得到与您相同的数值结果,但是无论如何输出都可以:

C[1,1] = 0.0000
C[2,1] = 0.0000
C[3,1] = 0.0000
C[4,1] = 0.0000
C[1,2] = 0.0000
C[2,2] = 0.0000
C[3,2] = 0.0000
C[4,2] = 0.0000