I have a constraint set for each vehicle "p" and each arc (k,m) that belongs to this vehicle route. At each iteration, I change the route of a vehicle. Then, at each iteration, I need to change the constraints related to this vehicle. For a specific "p", I need to delete the constraints related to it and add new ones. How can I do this?
I don't know how to use the remove function in this case. How can I identify the subset constraints related to "p" to be deleted? In the way I'm doing I'm deleting the always the first constraints of the set.
typedef struct{
IloCplex cplex;
IloEnv env;
IloModel mod;
IloNumVarArray f;
IloNumArray _f;
IloNumVarArray q;
IloNumArray _q;
IloRangeArray constraints_r3;
IloObjective fo;
IloNum _fo;
} CPX_RHLPflow;
struct DATA {
int n;
vector<vector<vector<int> > > _x;
int p;
vector<double> tau;
};
int MAPf (int router, int nodei, int nodej, int nodeu, int nodev, int N) {
return router*N*N*N*N+nodei*N*N*N+nodej*N*N+nodeu*N+nodev;
}
void create_model_RHLPflow(CPX_RHLPflow &mono, DATA data){
IloEnv& env = mono.env;
mono.mod = IloModel(env);
mono.cplex = IloCplex(mono.mod);
int sizef = data.p*data.n*data.n*data.n*data.n;
mono.f = IloNumVarArray(env, sizef, 0.0, +IloInfinity, ILOFLOAT);
mono._f = IloNumArray(env,sizef);
mono.q = IloNumVarArray(env, data.p, 0.0, +IloInfinity, ILOFLOAT);
mono._q = IloNumArray(env,data.p);
mono.constraints_r3 = IloRangeArray(env);
char name[50];
for (int p = 0; p < data.p; p++) {
for (int k = 0; k < data.n; k++) {
for (int m = 0; m < data.n; m++) {
if (k != m) {
if (data._x[p][k][m] == 1) {
IloExpr r_3(env);
for (int i = 0; i < data.n; i++) {
for (int j = 0; j < data.n; j++) {
if (i != j) {
if (k != j and m != i) {
r_3 += mono.f[MAPf(p,i,j,k,m,data.n)] ;
sprintf(name, "f(%d,%d,%d,%d,%d)",p,i,j,k,m);
mono.f[MAPf(p,i,j,k,m,data.n)].setName(name);
}
}
}
}
r_3 -= mono.q[p] * data.tau[p];
sprintf(name, "q(%d)",p);
mono.q[p].setName(name);
IloRange ctr;
ctr = (r_3 <= 0);
sprintf(name, "r3_%d_%d_%d",p,k,m);
ctr.setName(name);
mono.constraints_r3.add(ctr);
r_3.end();
}
}
}
}
}
mono.mod.add(mono.constraints_r3);
}
void DeleteConstraints (CPX_RHLPflow &mono, DATA data, int vehicle) {
char name[50];
int cont = 0;
for (int p = 0; p < data.p; p++) {
if (vehicle == p) {
for (int k = 0; k < data.n; k++) {
for (int m = 0; m < data.n; m++) {
if (k != m) {
if (data._x[p][k][m] == 1) {
mono.mod.remove(mono.constraints_r3[cont]);
cont += 1;
}
}
}
}
break;
}
}
sprintf(name, "ModelAfterModification.lp");
mono.cplex.exportModel(name);
}
int main(int argc, char* argv[]) {
CPX_RHLPflow mono;
DATA data;
data.n = 5;
data.p = 3;
data._x = vector<vector<vector<int> > > (data.p, vector<vector<int> > (data.n, vector<int> (data.n,0)));
data.tau = vector<double> (data.p, 1.00);
data._x[0][0][1] = 1;
data._x[0][1][3] = 1;
data._x[0][3][4] = 1;
data._x[0][4][0] = 1;
data._x[1][2][4] = 1;
data._x[1][4][3] = 1;
data._x[1][3][2] = 1;
data._x[2][1][2] = 1;
data._x[2][2][3] = 1;
data._x[2][3][4] = 1;
data._x[2][4][1] = 1;
cout << "oi" << endl;
create_model_RHLPflow (mono, data);
char name[50];
sprintf(name, "ModelBeforeModification.lp");
mono.cplex.exportModel(name);
int vehicle = 1;
DeleteConstraints(mono,data,vehicle);
data._x[1][2][4] = 0;
data._x[1][4][3] = 0;
data._x[1][3][2] = 0;
data._x[1][2][3] = 1;
data._x[1][3][1] = 1;
data._x[1][1][4] = 1;
data._x[1][4][2] = 1;
return 0;
}
If I right understood your question, you can use the solution below.
// Insert the constraint
// bc <- my branch-and-cut class
// bc.env_ <- IloEnv
// expr <- IloExpr
// x <- integer
IloRange neigboor(bc.env_, -IloInfinity, expr, x, "constraintName");
// model_ <- IloModel
bc.model_->add(neigboor);
bc.solve();
bc.model_->remove(neigboor);
neigboor.end();
You can repeat it indefinitely, adding or removing as many constraints as you want.
I used the above code to perform local searches using CPLEX as neighborhood solver.
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.