-2

I need to access the values of the four-dimensional decion variable after solving my model. Here is the declaration of my decion variable in Java:

this.y = new IloIntVar[this.Ncd][][][]; 
        for(int i=0; i<this.y.length;i++) { 
            this.y[i]= new IloIntVar[this.Ncd][][]; 
            for(int j=0; j<this.y[i].length;j++) {
                this.y[i][j]= new IloIntVar[this.Nbv+1][];
                for(int k=1; k<this.y[i][j].length; k++) {
                    this.y[i][j][k]= new IloIntVar[this.T+1];

                        this.y[i][j][k]= this.cplex.boolVarArray(this.T+1);

            }
        }
    }

I really need your help to use the simulation results of my model.

I am looking forward to hearing from you.

Zakaria

  • As an aside, in your code snippet, you are setting `this.y[i][j][k]` twice. Once with `new IloIntVar[this.T+1]` and again with `this.cplex.boolVarArray(this.T+1)`. You should probably get rid of the former as it's not doing anything productive. – rkersh Nov 29 '18 at 18:24
  • It also seems a bit strange to be starting with `k=1` in the inner most for loop. Is that on purpose? – rkersh Nov 29 '18 at 18:28
  • @rkersh Thank you for your answer .. I purport that I start from k = 1 because it concerns all the vehicles. Regarding your comment on the reporting structure I wonder if I can use the following statement: `this.y = new IloIntVar [this.Ncd] [this.Ncd] [this.Nbv + 1] [this.T + 1]; for (int i = 0; i – ZAKARIA CHEKOUBI Nov 29 '18 at 20:58
  • It's fine to use `k=1` if that is what you want; there will be some empty slots in your multidimensional array and you just need to be aware of that. There's no need to stop using `boolVarArray`, otherwise. – rkersh Nov 29 '18 at 21:40
  • @rkersh If I understand correctly this last statement is correct? – ZAKARIA CHEKOUBI Nov 29 '18 at 21:44
  • What I meant is that your original snippet is fine. The alternative statement you put in the comments is also fine if that's what you want (note that it is not equivalent because you are also using `t = 1` there.). In general, you may get better performance using `boolVarArray()` (i.e., creating variables in batches rather than one at a time). – rkersh Nov 29 '18 at 21:50
  • Yes, for both indexes k and t, they must have started at 1 that's what I want. You are right about creating variables in batches rather than one at a time. – ZAKARIA CHEKOUBI Nov 29 '18 at 21:55
  • @rkersh Can I please send you my model under java to give me your comments and proposal because I am really lost especially that I did everything right I think. – ZAKARIA CHEKOUBI Nov 29 '18 at 21:59

2 Answers2

1

It sounds like you've successfully built and solved your model. Correct? Assuming that is true, you should be able to access the solution values with something like the following:

 for (int i = 0; i < this.y.length; i++) {
    for (int j = 0; j < this.y[i].length; j++) {
       for (int k = 1; k < this.y[i][j].length; k++) {
          double[] x = cplex.getValues(y[i][j][k]);
          for (int l = 0; l < this.y[i][j][k].length; ++l) {
             System.out.printf("Variable [%d][%d][%d][%d] = %f%n",
                               i, j, k, l, x[l]);
          }
       }
    }
 }
rkersh
  • 4,447
  • 2
  • 22
  • 31
  • I tried your snippet of code but the compiler shows me this error: **Exception in thread "main" ilog.cplex.IloCplex$UnknownObjectException: CPLEX Error: object is unknown to IloCplex at ilog.cplex.IloCplex.getValues(IloCplex.java:6055) at ilog.cplex.IloCplex.getValues(IloCplex.java:5996) at MyCplexModel.solve(MyCplexModel.java:190) at MyCplexModel.main(MyCplexModel.java:239)** please, how can I solve this problem? can it be caused by the erroneous declaration of one of the constraints? – ZAKARIA CHEKOUBI Nov 29 '18 at 21:06
  • This error usually occurs when you attempt to use `getValues` on an object that is not part of the model. You should check that each variable you want to get a solution value for is included in a constraint or the objective and has been added to the model. – rkersh Nov 29 '18 at 21:35
  • Can I send you my model under java to give me your comments and proposal because I am really lost especially that I did everything right I think. – ZAKARIA CHEKOUBI Nov 29 '18 at 21:46
  • I suggested using `getValues` rather than `getValue` because it is usually faster to query variables in batches rather than one at a time. However, for the purposes of debugging, you could just replace `x[l]` with `cplex.getValue(y[i][j][k][l])`. I would add a print statement above that as well so that you can easily see what the indices are before you get a failure (or use a try/catch statement). At any rate, that should make it easy to track down the variable that is causing problems. – rkersh Nov 29 '18 at 21:59
  • Itried your proposal: (...) System.out.printf("Variable [%d][%d][%d][%d] = %f%n", i, j, k, l, this.cplex.getValue(this.y[i][j][k][l])); (...) But the same error : Exception in thread "main" java.lang.NullPointerException at ilog.cplex.IloCplex.getValues(IloCplex.java:6054) at ilog.cplex.IloCplex.getValues(IloCplex.java:5996) at MyCplexModel.solve(MyCplexModel.java:190) at MyCplexModel.main(MyCplexModel.java:239) MyCplexModel.java:190 corresponds to line: double[] x = cplex.getValues(y[i][j][k]); – ZAKARIA CHEKOUBI Nov 29 '18 at 22:15
  • You should comment out the line with `getValues`. Note that you are **not** getting the same error as above, though. Now you are getting a `NullPointerException` rather than an `UnknownObjectException`. Did you change your code elsewhere as well? – rkersh Nov 29 '18 at 22:24
  • Good morning. I still have the same problem even after commenting out the line double [] x = this.cplex.getValues (y [i] [j] [k]) : Exception in thread "main" java.lang.NullPointerException at ilog.cplex.IloCplex.getValue(IloCplex.java:5942) at MyCplexModel.solve(MyCplexModel.java:193) at MyCplexModel.main(MyCplexModel.java:239) – ZAKARIA CHEKOUBI Nov 30 '18 at 09:56
-1

similar question at https://www.ibm.com/developerworks/community/forums/html/topic?id=1865f18c-0176-48b8-8dbe-daa7f88773c4&ps=25#repliesPg=0

I would recommend the example facility.java in CPLEX_Studio128\cplex\examples\src\java

And there you will see

// Create variables. We have variables
         // open[j]        if location j is open.
         // supply[i][j]]  how much client i is supplied from location j
         IloNumVar[] open = cplex.boolVarArray(nbLocations);       
         IloNumVar[][] supply = new IloNumVar[nbClients][];
         for (int i = 0; i < nbClients; i++)
            supply[i] = cplex.numVarArray(nbLocations, 0.0, 1.0);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15