Decision Optimization

Decision Optimization

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

 View Only
  • 1.  CPLEX Error: object is unknown to IloCplex

    Posted Tue November 07, 2017 07:18 AM

    Originally posted by: ivonner


    Good Morning,

    I've been working on my thesis modelling this in java to use CPLEX.

    min sum_(d∈D)(Xd*bd)

    constraint 1: sum_(p∈P(d))sum_(c∈C(d)) y_pc+x_d = ∀ d ∈ D

    constraint 2: sum_(d∈D)sum_(p∈P(d))sum_(c∈C(d))gamma_cs*delta_pe*y_pc <=1  ∀ e ∈ E, s ∈ S

    Until now, I've been working on the first constrint but I always get CPLEX Error: object is unknown to IloCplex. My code is

     

     

    try{
                IloCplex cplex = new IloCplex();

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

     

    //At this point d=2;

                            int c_total_usados = {10,9};

                            int p_total_usados = {1,1};
                            int c_total = 19;

                            int p_total = 2;


                    c_total = c_total + c_total_usados[i];
                    p_total = p_total + p_total_usados[i];
                    System.out.println("c_total_usados en "+i+" es "+c_total_usados[i]);
                    System.out.println("p_total_usados en "+i+" es "+p_total_usados[i]);
                }
                System.out.println("c total: "+c_total);
                System.out.println("p total: "+p_total);

                IloNumVar[][] ypc = new IloNumVar[p_total][];
                
                for(int p=0; p<p_total; p++){
                    ypc[p] = cplex.boolVarArray(c_total);
                }
                
                System.out.println("ypc.length "+ypc.length);
                System.out.println("ypc[0].length "+ypc[0].length);
                
      
                
                IloNumVar[] x = new IloNumVar[d];
                for(int i=0; i<d;i++){
                    x[i] = cplex.boolVar(); // Lleno el vector en posición i con un binario
                }
                
                
                /////////// PRIMERA RESTRICCIÓN ////////////
                IloLinearNumExpr[] PrimeraCondicion = new IloLinearNumExpr[d];
                int p1=0;
                int p2=0;
                int s1=0;
                int s2=0;
                for(int i=0;i<d;i++){
                    PrimeraCondicion[i] = cplex.linearNumExpr();
                    p1=p2;
                    p2=p1+p_total_usados[i];
                    s1=s2;
                    s2=s1+c_total_usados[i];
                    for(int p=p1; p<p2; p++){
                        for (int c=s1; c<s2; c++){
                            System.out.println("p "+p);
                            System.out.println("c "+c);
                            PrimeraCondicion[i].addTerm(1.0, x[i]);
                            PrimeraCondicion[i].addTerm(1.0, ypc[p][c]);
                        }
                    }
                }
                
                ///////////////FUNCIÓN OBJETIVO//////////////////
                // Declaro como expresión
                IloLinearNumExpr obj = cplex.linearNumExpr();
                for (int i=0; i<d; i++){
                    obj.addTerm(x[i], bd[i]);
                }
                // Objetivo CPLEX
                cplex.addMinimize(obj);        
                
                ///////// ESTABLECER CONDICIONES ////////
                for(int i = 0; i<d; i++){
                    cplex.addEq(PrimeraCondicion[i], 1.0);    
                }
                
                ////// SOLUCIÓN DEL MODELO ////////
                 cplex.solve();
                 if(cplex.solve()){
                     System.out.println("Solved");
                     System.out.println("FO: "+cplex.getObjValue());
                     for (int i=0; i<d; i++){
                         System.out.println("X"+(i+1)+"is: "+cplex.getValue(x[i]));
                     }                
                     for(int p=0; p<ypc.length; p++){
                         for(int c=0; c<ypc[0].length; c++){
                             System.out.println("ypc- in i-j "+p+"-"+c+"is: "+cplex.getValue(ypc[p][c]));/////////////// ERROR!!!
                     }
                     }
                 }
                 else{
                     System.out.println("Not solved");
                 }
                 cplex.end();
            }
            
            catch(IloException exc)
            {exc.printStackTrace();}
        }
        
        
        public static void main(String[] args) {
            Modelo_V.solveMe();
        }
    }

    Thanks in advance for your time, I really hope you can find my mistake.


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: CPLEX Error: object is unknown to IloCplex

    Posted Tue November 07, 2017 08:56 AM

    This exception is thrown if you call getValue() with a variable that does not appear in any constraint or the objective.

    Your ypc array has dimensions p_total times c_total but I am not sure you are using all of these variables when setting up PrimeraCondicion. You probably want to double check what you do there.

    A very simple workaround for the exception is to add this code

    for (IloNumVar[] v : ypc) cplex.add(v);

    That is, explicitly add all the variables to the model. Then you can query all variables and the exception should not longer occur.
     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: CPLEX Error: object is unknown to IloCplex

    Posted Tue November 07, 2017 09:29 AM

    Originally posted by: ivonner


    Thank you so much!! I've been dealing with this for some days and finally it is working because of your help!


    #CPLEXOptimizers
    #DecisionOptimization