Writing a 3D array from C-plex to Excel with a different matrix configuration

Viewed 21

I have been trying to write a 3D decision variable from C-plex to Excel. My problem is that when I convert the decisional variable array into a tuple to write the values into an excel file, the values are always written in one singular column (In my example below the actual values are written into 180 rows(15x2x6=180) and four columns meaning a 180 x 4 matrix.

I would like to restructure the format in which the values are written into the excel file. Meaning, I would like to restructure in a way where I can get a 15 X 12 matrix (k = 15 rows and LxT=2x6=12 columns).
Is this possible to achieve in C-plex ?

My code is as following:

    .
.mod:
int K = 15;
int L = 2;
int T = 6;

range k = 1..K;
range l = 1..L;
range t = 1..T;

dvar X[k][l][t];

subject to {
...
}
tuple sample{
int K;
int L;
int T;
int value;
};
{sample} example = {<K,L,T,X[K,L,T]> | K in k, L in l, T in t};

.dat:
SheetConnection Test ("Thesis.xlsx");
example to SheetWrite(Test,"Output!C2:F181"); 

Thank You!

1 Answers

Indeed you can change your 3D matrix into a 2D matrix and then use SheetWrite for that 2D matrix.

For instance:

int K = 15;
int L = 2;
int T = 6;

range k = 1..K;
range l = 1..L;
range t = 1..T;

dvar int X[k][l][t];

subject to {
forall(a in k,b in l,c in t)  X[a][b][c]==a*b*c;
}

tuple lt
{
  int l;
  int t;
}

{lt} lts={<a,b> | a in l,b in t};

execute
{
  writeln("card of lts = ",lts.size);
}

int matrix2D[a in k][b in lts]=X[a][b.l][b.t];

execute
{
  writeln(matrix2D);
}

gives

card of lts = 12
 [[1 2 3 4 5 6 2 4 6 8 10 12]
         [2 4 6 8 10 12 4 8 12 16 20 24]
         [3 6 9 12 15 18 6 12 18 24 30 36]
         [4 8 12 16 20 24 8 16 24 32 40 48]
         [5 10 15 20 25 30 10 20 30 40 50 60]
         [6 12 18 24 30 36 12 24 36 48 60 72]
         [7 14 21 28 35 42 14 28 42 56 70 84]
         [8 16 24 32 40 48 16 32 48 64 80 96]
         [9 18 27 36 45 54 18 36 54 72 90 108]
         [10 20 30 40 50 60 20 40 60 80 100 120]
         [11 22 33 44 55 66 22 44 66 88 110 132]
         [12 24 36 48 60 72 24 48 72 96 120 144]
         [13 26 39 52 65 78 26 52 78 104 130 156]
         [14 28 42 56 70 84 28 56 84 112 140 168]
         [15 30 45 60 75 90 30 60 90 120 150 180]]
Related