I have a very large LP problem to solve and have to solve it many times.
Each time, I only need to change some coefficient and run again.
So my strategy is to formulate model for the basic problem, and save it.
Each time, I get a copy of the basic model, and try to change the coefficient.
The problem is how can I change the coefficient for the new copy.
I know how to change the coefficient when creating the model. But I dont want to repeat the creating process , since it costs a lot of time.
Is there any direct method to change the coefficient without creating the model again?
I've modified the ilolpex1.cpp example that is shipped with CPLEX below:
#include <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;
// Modify one of the coefficients and solve again.
con[0].setLinearCoef(var[2], 2);
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;
}
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
I've used the setLinearCoef method to change one of the coefficients. You can compare the two LP files ("lpex1.1.lp" and "lpex1.2.lp") to see/verify the changes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.