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