Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Question about getValues()

    Posted 09/16/15 11:03 PM

    Originally posted by: brownflaming1


    I was trying to extract solution values in a callback function, but it seems that getValues() doesn't work... Does anyone know what the problem is?

    Below is the modified code of cplex example: ilomipex1.cpp.

    Thanks!

    ===============================================

    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

     

    static void

       populatebyrow(IloModel model, IloNumVarArray var, IloRangeArray con);

     

    ILOHEURISTICCALLBACK1(node0info, IloCplex, cplex)

    {

      if ( getNnodes() == 0 )

        {

          cout << " NODE0 Obj: " << getObjValue();

          cout << " BestObj: " << getBestObjValue();

          cout << " time " << cplex.getTime();

          IloEnv env = getEnv();

          IloNumArray rtSol(env);

          IloNumVarArray var(env);

          getValues(rtSol, var);

          cout << " Solutions: " << rtSol << endl;   ?????? This line output nothing....

          cout << endl;

        }

    }

     

    int

    main (void) {

       IloEnv env;

       try {

          IloModel model(env);

     

          IloNumVarArray var(env);

          IloRangeArray con(env);

          populatebyrow (model, var, con);

     

          IloCplex cplex(model);

          //cplex.setParam(IloCplex::NodeLim, 1);                                                                                          

          cplex.setParam(IloCplex::PreInd, false);

          cplex.setParam(IloCplex::HeurFreq, -1); //turn off heuristic                                                                     

          //cplex.setParam(IloCplex::CutsFactor, 1.0);                                                                                     

          cplex.use(node0info(env, cplex));

          cplex.solve();

     

          env.out() << "Solution status = " << cplex.getStatus() << endl;

          env.out() << "Solution value  = " << cplex.getObjValue() << endl;

     

          IloNumArray vals(env);

          cplex.getValues(vals, var);

          env.out() << "Values        = " << vals << endl;

          cplex.getSlacks(vals, con);

          env.out() << "Slacks        = " << vals << endl;

     

          cplex.exportModel("mipex1.lp");

       }

       catch (IloException& e) {

          cerr << "Concert exception caught: " << e << endl;

       }

       catch (...) {

          cerr << "Unknown exception caught" << endl;

       }

     

       env.end();

       return 0;

    }

     

     

    static void

    populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)

    {

       IloEnv env = model.getEnv();

     

       x.add(IloNumVar(env, 0.0, 40.0));

       x.add(IloNumVar(env));

       x.add(IloNumVar(env));

       x.add(IloNumVar(env, 2.0, 3.0, ILOINT));

       model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2] + x[3]));

     

       c.add( - x[0] +     x[1] + x[2] + 10 * x[3] <= 20);

       c.add(   x[0] - 3 * x[1] + x[2]             <= 30);

       c.add(              x[1]        - 3.5* x[3] == 0);

       model.add(c);

     

    // END populatebyrow      


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Question about getValues()

    Posted 09/16/15 11:44 PM

    Originally posted by: brownflaming1


    I think I have to pass "var" as an argument in the callback function, is this the right solution?

    If yes, suppose I also want to access the optimal dual multipliers at the root relaxation, why does cplex.getDuals produce an exception?

     

    I changed the callback function as follows:

     

    ILOHEURISTICCALLBACK3(node0info, IloCplex, cplex, IloNumVarArray, var, IloRangeArray, con)

    {

      if ( getNnodes() == 0 )

        {

          cout << " NODE0 Obj: " << getObjValue();

          cout << " BestObj: " << getBestObjValue();

          cout << " time " << cplex.getTime();

          IloEnv env = getEnv();

          IloNumArray rtSol(env);

          getValues(rtSol, var);

          cout << " Solutions: " << rtSol << endl;   // Now this is correct.

          IloNumArray dual(env);

          cplex.getDuals(dual, con);

          cout << " Optimal dual multipliers: " << dual << endl;  // This produce an exception which I don't understand why?

          cout << endl;

        }

    }


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Question about getValues()

    Posted 10/16/15 03:12 AM

    In your first post you passed an empty array of variables to getValues(). That is why the function returned an empty array of values. Maybe the reference documentation can help you to understand how that function works. As soon as you pass in a non-empty array (as in your second post) you will actually get values.

    The heuristic callback does not provide a way to get duals, that is why your code does not work. Moreover, you must not use any cplex.foo() function from a callback. Only use callback member functions from a callback.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Question about getValues()

    Posted 04/22/16 12:33 AM

    Originally posted by: Job_Jonah


    Hi, 

    I am also having a similar problem. I have used the following code:

    IloNumVarArray Vars(cplex.getEnv());
    IloNumArray Vals(cplex.getEnv());
    cplex.getValues(Vals, Vars);
    
            for(s=0; s<nbscns; s++)
    
            x_1[nCut][s] = cplex.getValue (x1[s]);
                    
    Vals.end();
    Vars.end();    
    

    where x_1[nCut][s] is the extreme point solution for each cut for  x1[s]. 

    This gives me an error. It says "Error: IloExtractable 52544 IloNumVarI has not been extracted by IloAlgorithm". What might be the problem?

    Thank you in advance.

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Question about getValues()

    Posted 04/22/16 01:43 AM

    You should have searched the forum for this error message. This problem has been discussed many times.

    The exception occurs if you attempt to query the value for a variable that does not appear in the objective function or any constraint of the extracted model. Such a variable is unknown to the solver and thus the solver has no value for it. Make sure that x1[s] appears in at least one constraint or in the objective function or use IloModel::add(x1[s]) to explicitly add this variable to the model.


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Question about getValues()

    Posted 04/22/16 02:23 PM

    Originally posted by: Job_Jonah


    Hi Daniel.

    Thank you so much for your prompt answer. Actually I have that variable in many constraints and also in the objective function. I thought it might be a problem the way I have coded it. 

    I will check the forum for the similar queries.

    Thanks

     


    #CPLEXOptimizers
    #DecisionOptimization