I am using the C++ API for CPLEX. After solving my model with CPLEX I would like to add further constraints to the model. I have a vector of requests R, with a corresponding variable p_i for each i in R and a constraint p_i = 1 (there are more variables and constraints which I have omitted here). After the first solve a new request appears for which I would like to create a new variable p_{new_request} and the corresponding constraint p_{new_request} = 1. I've tried this in to different ways, one way works using
model.add(p[rmap[new_request]] == 1);
the other one doesn't, i.e. the constraint is not added to the model.
accept.add(IloRange());
accept[rmap[new_request]] = IloRange(env,1,p[rmap[new_request]],1,name.str().c_str());
I need the second way, because I also need to remove certain constraints from the model once the request has been served, i.e.
accept[rmap[i]].end();
How can I do that properly?
Help is very much appreciated!!!
Here's the complete code:
#include <vector>
#include <unordered_map>
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
int main(){
bool solved;
// vector R contains all requests
std::vector <int> R = {1,2,3,4,5,6,7,8,9,10,25,26};
// use this unordered map to index variables p with requests
std::unordered_map<int,int> rmap;
int count = 0;
for (const auto& i: R)
{
rmap[i] = count;
count++;
}
int new_request;
// use this stringstream to create variable and constraint names
std::stringstream name;
IloEnv env;
IloModel model(env);
IloNumVarArray p(env,R.size());
IloRangeArray accept(env,R.size());
for (const auto& i: R)
{
name << "p_" << i;
p[rmap[i]] = IloNumVar(env, 0, 1, ILOBOOL, name.str().c_str());
name.str("");
}
for (const auto& i : R)
{
name << "accept_" << i;
accept[rmap[i]] = IloRange(env,1,p[rmap[i]],1,name.str().c_str());
name.str(""); // Clean name
}
model.add(accept);
IloExpr expr(env);
// Create objective function
for (const auto& i : R)
{
expr += p[rmap[i]];
}
IloObjective obj(env, expr, IloObjective::Minimize);
model.add(obj);
expr.clear();
// Create the solver object
IloCplex cplex(model);
env.out() << "Before" << endl;
env.out() << model << endl;
cplex.exportModel("Before.1.lp");
solved = cplex.solve();
if (solved)
{
// If CPLEX successfully solved the model, print the results
std::cout << "\n\nCplex success!\n";
std::cout << "\tStatus: " << cplex.getStatus() << "\n";
std::cout << "\tObjective value: " << cplex.getObjValue() << "\n";
// add new request to R, create variable p and corresponding constraint
new_request = 23;
R.push_back(new_request);
rmap[new_request] = R.size() - 1;
name << "p_" << new_request;
p.add(IloNumVar(env,0,1,ILOBOOL,name.str().c_str()));
name.str("");
name << "accept_" << new_request;
// These to lines don't work
accept.add(IloRange());
accept[rmap[new_request]] = IloRange(env,1,p[rmap[new_request]],1,name.str().c_str());
// this line works
//model.add(p[rmap[new_request]] == 1);
name.str(""); // Clean name
env.out() << "After" << endl;
env.out() << model << endl;
cplex.exportModel("After.1.lp");
solved = cplex.solve();
}
expr.end();
env.end();
}
#DecisionOptimization#Support#SupportMigration