gpt4 book ai didi

c++ - 如何使用 c++/cplex 删除约束集的子集并向该集添加新约束?

转载 作者:行者123 更新时间:2023-11-28 04:28:29 29 4
gpt4 key购买 nike

我为每辆车“p”和属于该车辆路线的每条弧线 (k,m) 设置了约束。在每次迭代中,我都会更改车辆的路线。然后,在每次迭代中,我需要更改与该车辆相关的约束。对于特定的“p”,我需要删除与其相关的约束并添加新的约束。我该怎么做?

我不知道在这种情况下如何使用删除功能。如何识别与要删除的“p”相关的子集约束?在我这样做的方式中,我删除了集合中始终是第一个约束。

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;

}

最佳答案

如果我正确理解你的问题,你可以使用下面的解决方案。

// 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();

您可以无限期地重复它,添加或删除任意数量的约束。

我使用上面的代码执行本地搜索,使用 CPLEX 作为邻域求解器。

关于c++ - 如何使用 c++/cplex 删除约束集的子集并向该集添加新约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53672194/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com