Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

HeuristicCallback: setSolution with a multi-dimensional variable

  • 1.  HeuristicCallback: setSolution with a multi-dimensional variable

    Posted Mon September 11, 2017 04:57 PM

    Originally posted by: Maichel_Aguayo


    Hello all,

    I am trying to inject a new incumbent solution using HEURISTICCALLBACK (I am solving a MIP model in CPLEX/C++).  I only have a two-dimensional variable in the model.  How can I use setSolution for this case ?

    I have reproduced below some part of my code for convenience. I got the following error: no matching function for call to "Non_fixedI::setSolution(Arcs&, FloatMatrix&, IloNum&)"

    Thanks,

     

    typedef IloArray<IloIntVarArray> Arcs;

    ...

    ILOHEURISTICCALLBACK1(Non_fixed,Arcs, x) {

     FloatMatrix sol;   // contains the value of the variables

    IloNum objval=new_incumbentObjValue;      // contains the objective value of the incumbent solution

    ...

     setSolution(x, sol, objval);  //error: no matching function for call to 'Non_fixedI::setSolution(Arcs&, FloatMatrix&, IloNum&)'

    }

    int main(int argc, char **argv)

    {

     Arcs x(masterEnv, numNodes);

    IloCplex masterCplex(masterMod);

      masterCplex.use(Non_fixed(masterEnv, x));

    ....

    }


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: HeuristicCallback: setSolution with a multi-dimensional variable

    Posted Mon September 11, 2017 05:02 PM

    You have to copy the variables and values into flat arrays. Something like this:

    IloNumVarArray vars(getEnv());
    IloNumArray vals(getEnv());
    for (IloInt i = 0; i < x.getSize(); ++x)
       for (IloInt j = 0; j < x[i].getSize(); ++j) {
          vars.add(x[i][j]);
          vals.add(sol[i][j]);
       }

    Then you can call setSolution() with the flat arrays (don't forget to end() the arrays after that).


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: HeuristicCallback: setSolution with a multi-dimensional variable

    Posted Tue September 12, 2017 10:49 AM

    Originally posted by: Maichel_Aguayo


    Thanks Daniel for your help. It did work as suggested.

     

    Just in case someone else faces the same issue, you can use setBound and solve in the heuristic callback to check if the solution injected is feasible.  I have reproduced below some part of my code for convenience.  

    ILOHEURISTICCALLBACK1(Non_fixed, Arcs, x) {
      ...
    // New_sol[i][j] contain the solution values


         for (IloInt i = 0; i < x.getSize(); ++i) 
            for (IloInt j = 0; j < x[i].getSize(); ++j) { 
                vars.add(x[i][j]); 
                vals.add(New_sol[i][j]); 
                 setBounds(x[i][j], New_sol[i][j] ,New_sol[i][j] );
       }

         if ( solve() ) {
            setSolution( vars, vals, objval);
         }
         
            vars.end();
            vals.end();
          
       }

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: HeuristicCallback: setSolution with a multi-dimensional variable

    Posted Tue September 12, 2017 10:55 AM

    Calling setBounds() and solve() is actually the recommended way to go! Thanks for mentioning that and posting your code!


    #CPLEXOptimizers
    #DecisionOptimization