Search code examples
amplglpk

Display commend in ampl


I have a 2 dimension variable in ampl and I want to display it. I want to change the order of the indices but I do not know how to do that! I put my code , data and out put I described what kind of out put I want to have. Here is my code:

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

The output of this code using glpksol is like 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

but I want the result to be like this:

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

any idea?


Solution

  • You can use for loops and printf commands in your .run file:

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

    or even:

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

    I don't get the same numerical results as you, but anyway the output works:

    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