Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

How to modify model and query solution multiple times using C++ API?

  • 1.  How to modify model and query solution multiple times using C++ API?

    Posted 04/30/21 12:21 PM

    I would like to modify a Cplex model created with the C++ API after solving it and query the solution each time after solving as well. For that purpose I used cplex.getValues(). In the following I modified the ilolpex1.cpp example. I get an error "Segmentation fault: 11".

    <ilcplex/ilocplex.h> ILOSTLBEGIN static void populatebyrow(IloModel model, IloNumVarArray var, IloRangeArray con); int main (int argc, char **argv) { IloEnv env; try { IloModel model(env); IloNumVarArray var(env); IloRangeArray con(env); populatebyrow (model, var, con); IloCplex cplex(model); cplex.exportModel("lpex1.1.lp"); // Optimize the problem and obtain solution. if ( !cplex.solve() ) { env.error() << "Failed to optimize LP" << endl; throw(-1); } env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; IloNumArray x_sol(env); cplex.getValues(x_sol,var); // Delete one of the variables and solve again var[0].end(); cplex.exportModel("lpex1.2.lp"); // Optimize the problem and obtain solution. if ( !cplex.solve() ) { env.error() << "Failed to optimize LP" << endl; throw(-1); } env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; cplex.getValues(x_sol,var); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; } // END main // To populate by row, we first create the variables, and then use them to // create the range constraints and objective. 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)); model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2])); c.add( - x[0] + x[1] + x[2] <= 20); c.add( x[0] - 3 * x[1] + x[2] <= 30); x[0].setName("x1"); x[1].setName("x2"); x[2].setName("x3"); c[0].setName("c1"); c[1].setName("c2"); model.add(c); } // END populatebyrow




    #DecisionOptimization
    #Support
    #SupportMigration


  • 2.  RE: How to modify model and query solution multiple times using C++ API?

    Posted 04/30/21 05:51 PM

    I hadn't run your code. But once you end var[0], you shouldn't try to get its value again from the second solve. You need to construct a different IloNumVarArray that doesn't include var[0] for your second call to cplex.getValues.

    If you need further discussion, please open a case if you are entitled. Otherwise, please discuss your question in Data Science Community https://community.ibm.com/community/user/datascience/communities/community-home/digestviewer?communitykey=ab7de0fd-6f43-47a9-8261-33578a231bb7&tab=digestviewer






    #DecisionOptimization
    #Support
    #SupportMigration