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

时间:2018-12-07 15:10:36

标签: c++ constraints cplex

我为每个车辆“ 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;

}

1 个答案:

答案 0 :(得分:1)

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

// 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作为邻域求解器来执行本地搜索。