Sena
Sena

Reputation: 31

How to use 'IloOplOutputFile' in Cplex to write variables with more than one index?

I am trying to write the result data to an external file because of an error I got after running the code for 16 hours.

I found the code above, it works for the variables with one index but my variables are not with one index. There are even variables with 4 indexes. How can I adapt this code to my situation?

execute{
  var ofile = new IloOplOutputFile("modelRun.txt");
  ofile.writeln("Data:");
  for(var i in thisOplModel.r){
     ofile.writeln("d["+i+"+"]="+thisOplModel.d[i]]);
  }
  ofile.writeln("Optimal objective value="+cplex.getObjValue());
  ofile.writeln("Optimal variable values:");
  for(i in thisOplModel.r){
     ofile.writeln("x["+i+"]="+thisOplModel.x[i]);
  }
  ofile.close();
}

Thank you for any help!

Upvotes: 0

Views: 536

Answers (2)

Alex Fleischer
Alex Fleischer

Reputation: 10062

If x is a 4D array instead of:

for(i in thisOplModel.r){
     ofile.writeln("x["+i+"]="+thisOplModel.x[i]);
  }

you could directly write:

ofile.writeln("x="+thisOplModel.x);

Upvotes: 0

Alex Fleischer
Alex Fleischer

Reputation: 10062

About displaying 4D arrays:

range r=1..2;
int x[i in r][j in r][k in r][l in r]=i+j+k+l;

execute
{
writeln("x=",x);
}

which gives

x= [[[[4 5]
                 [5 6]]
             [[5 6]
                 [6 7]]]
         [[[5 6]
                 [6 7]]
             [[6 7]
                 [7 8]]]]

Upvotes: 0

Related Questions