如何在cplex / c ++中更改约束的左侧?

时间:2018-12-06 18:38:31

标签: c++ constraints cplex

我该怎么做才能仅更改约束子集的左侧?

例如,我具有以下约束:

typedef struct{
  IloCplex cplex;
  IloEnv env;
  IloModel mod;
  IloNumVarArray f;
  IloNumArray _f;
  IloRangeArray constraints_r1c;
  IloObjective fo;
  IloNum _fo;
} CPX_RHLPflow;

struct DATA {

  int n;
  vector<vector<vector<int> > > _x;
  int p;

};

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.constraints_r1c = IloRangeArray(env);

  char name[50];
  for (int i = 0; i < data.n; i++) {
    for (int j = 0; j < data.n; j++) {
        if (i != j) {
          IloExpr r_1c(env);
          for (int k = 0; k < data.n; k++) {
              if (k != j) {
                  for (int p = 0; p < data.p; p++) {
                      if (data._x[p][k][j] == 1) {
                          r_1c += mono.f[MAPf(p,i,j,k,j,data.n)];
              sprintf(name, "f(%d,%d,%d,%d,%d)",p,i,j,k,j);
                          mono.f[MAPf(p,i,j,k,j,data.n)].setName(name);
                      }
                  }
               }
          }
          IloRange ctr;
          ctr = (1 <= r_1c <= 1);
      sprintf(name, "r1c_%d_%d",i,j);
          ctr.setName(name);
          mono.constraints_r1c.add(ctr);
          r_1c.end();
      }
    }
  }   

  mono.mod.add(mono.constraints_r1c);

}

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._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);

  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;

}

在每次迭代中,对于特定的p值,参数data._x[p][k][j]的值都会改变。

因此,要添加到f子集(具有相应contraints_r1c的子集)左侧的p变量也必须更改。 我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:0)

我发现了如何做我想做的事情,我认为在这里分享是很棒的事情。

Microsoft.AspNet.Identity.CoreCompat