简体   繁体   中英

Javascript - How to convert an array indexed by tuples to a multidimensional array?

I have an array - X[(i,j,l)] that is indexed by tuples of 3 indexes, the indexes of i and j run from 1 to n, and the index of l runs from 1 to "layers". My array is binary - the value of each element is 0 or 1.

I got this array as a result of running an optimization problem in opl - in CPLEX.

I would like to read the values of X as a multidimensional array X[i][j][l] using the execution code in javascript in the model window.

This is what I tried:

var ofile_varx = new IloOplOutputFile("initial_varx.csv");
ofile_varx.writeln(x);

var x_arr=new Array (n);
for (var i=0; i<n; i++) {
    x_arr[i]=new Array (n);
    for (var j=0; j<n; j++) {
        x_arr[i][j]=new Array (layers);
    }
}           
for (var tup in ijl) {
    x_arr[tup.i][tup.j][tup.l]=x[tup];
}

I got an error from the last line that says that it cannot assign a property "null" to the array.

Any idea of how to get to my requested array x_arr?

Thank you!

tuple t
{
 int i;
 int j;
 int l;
}

int n=2;
int layers=3;
{t} ijl={<1,2,3>,<1,2,1>};

dvar int x[ijl];
subject to
{
forall(i in ijl) x[i]==i.i;
}


execute
{
var x_arr=new Array (n);
for (var i=0; i<=n; i++) {
    x_arr[i]=new Array (n);
    for (var j=0; j<=n; j++) {
        x_arr[i][j]=new Array (layers);
    }
}           
for (var tup in ijl) {
    x_arr[tup.i][tup.j][tup.l]=x[tup].solutionValue;
}
}

works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM