Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

How to access to a multidimensional decision variable array in Ilog Cplex using Java

  • 1.  How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Thu November 29, 2018 12:00 PM

    Originally posted by: ZakariaChekoubi


    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 you  help to use the simulation results of my model.

     

    Best regards,

     

    Zakaria

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Thu November 29, 2018 12:48 PM

    Hi,

    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);

    regards

     

    https://www.linkedin.com/pulse/low-barrier-entry-optimization-through-cplex-alex-fleischer/


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Thu November 29, 2018 04:17 PM

    Originally posted by: ZakariaChekoubi


    @AlexFleischer 

    if I understand correctly, I have a problem with the declaration of the decision variable?


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Thu November 29, 2018 01:53 PM

    cplex.getValue(y[i][j][k][l])

    will give you the value for the respective variable.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Thu November 29, 2018 04:31 PM

    Originally posted by: ZakariaChekoubi


    @DanielJunglas Thank you for your answer. I tried your proposal to use the cplex.getValue(y [i] [j] [k] [l]) method, but the problem still persists:

    for (int i = 0; i < this.Ncd; i++) {
    
                    for (int j = 0; j < this.Ncd; j++) {
                       for (int k = 1; k < this.Nbv; k++) {
                               for(int t=1; t<this.T; t++)
                          System.out.printf("valeur = "+this.cplex.getValue(this.y[i][j][k][t]));
                          }
                       }
                    }
    

    This is the error code : 

    at ilog.cplex.IloCplex.getValue(IloCplex.java:5943)
    at MyCplexModel.solve(MyCplexModel.java:194)
    at MyCplexModel.main(MyCplexModel.java:240)

    MyCplexModel.java:194 -> corresponds to line code : 

    System.out.printf("valeur = "+this.cplex.getValue(this.y[i][j][k][t]));
    

    Please how can I fix this problem ?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Thu November 29, 2018 04:54 PM

    Hard to tell what is wrong because you omitted the actual error message. Can you please show the full exception message?


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Thu November 29, 2018 04:59 PM

    Originally posted by: ZakariaChekoubi


    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)

     

    What do you think about it ?


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Fri November 30, 2018 03:33 AM

    Hi,

    IloCplex mycplex = new IloCplex();

    int Ncd=2;
    int Nbv=6;
    int T=4;

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

                            y[i][j][k]= mycplex.boolVarArray(T+1);
    for(int t=1; t<T; t++)
    {
                            IloRange myConstraint2 = mycplex.addEq(y[i][j][k][t],1);
    mycplex.add(myConstraint2);
    }


                }
            }
        }

    mycplex.solve();


    for (int i = 0; i < Ncd; i++) {

                    for (int j = 0; j < Ncd; j++) {
                       for (int k = 1; k < Nbv; k++) {
                               for(int t=1; t<T; t++)
                          System.out.printf("valeur = "+mycplex.getValue(y[i][j][k][t]));
                          }
                       }
                    }

     

    works fine

     

    regards

    https://www.linkedin.com/pulse/loptimisation-faire-plus-avec-moins-un-zoo-des-bus-et-alex-fleischer-1/

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Fri November 30, 2018 05:06 AM

    Originally posted by: ZakariaChekoubi


    @AlexFleischer a1d4492b-6cf8-402d-ae3c-dd656c427dc7 Goode morning. Thank you for your interest in my question. I am confused about the declaration of the decision variable. You declare it as follows:

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

    I do not have to consider the line code 9. Right? 


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  回复:Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Fri November 30, 2018 05:13 AM

    indeed you may remove that line.

    I think this was from your initial post

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  回复:Re: How to access to a multidimensional decision variable array in Ilog Cplex using Java

    Posted Fri November 30, 2018 05:29 AM

    Originally posted by: ZakariaChekoubi


    Thank you. I think I have a problem with my constraints because i guess variables declarations are now ok. Now I think I have to check my constraints. Please, can I show you my constraints to give me your remarks?  


    #CPLEXOptimizers
    #DecisionOptimization