【问题标题】:Cplex c++ multidimensional decision variableCplex C++ 多维决策变量
【发布时间】:2013-03-19 09:35:56
【问题描述】:

我是使用 cplex 的新手,我尝试在互联网上查找一些信息,但没有找到可以帮助我解决问题的明确内容。

我有 P[k] k 将等于 1 到 4

我有一个决策变量 x[i][k] 必须等于 0 或 1(也是 p[k])

i 在 1 到 5 之间

现在我喜欢这个

  IloEnv env;
  IloModel model(env);
  IloNumVarArray p(env);
  p.add(IloNumVar(env, 0, 1));
  p.add(IloNumVar(env, 0, 1));
  p.add(IloNumVar(env, 0, 1));
  IloIntVar x(env, 0, 1);

  model.add(IloMaximize(env, 1000 * p[1] + 2000 * p[2] + 500 * p[3] + 1500 * p[4]));

   for(int k = 1; k <= 4; k++){
    for(int i = 1; i <= 5; i++){
      model.add(x[i][k] + x[i][k] + x[i][k] + x[i][k] + x[i][k] => 2 * p[k]; );
    }}

循环应该做这样的事情:

x[1][1] + x[2][1] + x[3][1] + x[4][1] + x[5][1] => 2 * p[1] ;

x[1][2] + x[2][2] + x[3][2] + x[4][2] + x[5][2] => 2 * p[2] ;

x[1][3] + x[2][3] + x[3][3] + x[4][3] + x[5][3] => 2 * p[3] ;

x[1][4] + x[2][4] + x[3][4] + x[4][4] + x[5][4] => 3 * p[4] ;

但我离这个结果还很远。

有人有想法吗?

谢谢

【问题讨论】:

    标签: c++ variables cplex


    【解决方案1】:

    您可能想要使用 IloNumExpr

    for(int k = 0; k < 4; k++){
       IloNumExpr sum_over_i(env);
       for(int i = 0; i < 5; i++){
            sum_over_i += x[i][k];
       }
       model.add(sum_over_i >= 2 * p[k]; );
    }
    

    您还需要将 x 声明为二维数组。

    IloArray x(env, 4);
    for (int k = 0; k < 4; ++k)
          x[k] = IloIntVarArray(env, 5, 0, 1);
    

    此外,在 c++ 中,数组索引是从 0 到 size-1,而不是从 1 到 size。你的目标应该写出来

    model.add(IloMaximize(env, 1000 * p[0] + 2000 * p[1] + 500 * p[2] + 1500 * p[3]));
    

    【讨论】:

      【解决方案2】:

      Usertfwr 已经给出了一个很好的答案,但我想提供另一个版本的解决方案,它可以帮助您以更通用的方式编写 CPLEX 应用程序。首先,我建议您使用文本文件来保存将输入程序的所有数据(目标函数系数)。在您的情况下,您只需将以下矩阵(如数据)复制到记事本并将其命名为“coef.dat”:

      [1000, 2000, 500, 1500]

      现在是完整的代码,如果理解任何语句有困难,请告诉我:

        #include <ilcplex/ilocplex.h>
        #include <fstream>
        #include <iostream>
        ILOSTLBEGIN
      
        int main(int argc, char **argv) {
          IloEnv env; 
          try {
              const char* inputData = "coef.dat";
      
              ifstream inFile(inputData);   // put your data in the same directory as your executable
              if(!inFile) {
                  cerr << "Cannot open the file " << inputData << " successfully! " <<endl;
                  throw(-1);
              }
      
              // Define parameters (coef of objective function)
              IloNumArray a(env); 
      
              // Read in data
              inFile >> a; 
      
              // Define variables 
              IloBoolVarArray p(env, a.getSize());  // note that a.getSize() = 4 
              IloArray<IloBoolVarArray> X(env, 5);  // note that you need a 5x4 X variables, not 4x5
              for(int i = 0; i < 5; i++) {
                  X[i] = IloBoolVarArray(env,4);
              }
      
              // Build model
              IloModel model(env);
      
              // Add objective function 
              IloExpr  objFun (env); 
              for(int i = 0; i < a.getSize(); i++){
                  objFun += a[i]*p[i];
              }
      
              model.add(IloMaximize(env, objFun));  
      
              objFun.end();  
      
              // Add constraints -- similar to usertfwr’s answer
              for(int i = 0; i < 4; k++){
                  IloExpr sumConst (env);
                  for(int j = 0; j < 5; i++){
                          sumConst += x[j][i];
                  }
                  // before clearing sumConst expr, add it to model
                  model.add(sumConst >= 2*p[i]);
                  sumConst.end(); // very important to end after having been added to the model
              }
      
              // Extract the model to CPLEX 
              IloCplex cplex(mod);
      
              // Export the LP model to a txt file to check correctness
              //cplex.exportModel("model.lp");
      
              // Solve model 
              cplex.solve();
      
          }
          catch (IloException& e) {
              cerr << "Concert exception caught: " << e << endl;
          }
          catch (...) {
                 cerr << "Unknown exception caught" << endl;
          }
              env.end();
      }
      

      【讨论】:

      • ILOSTBEGIN 是什么意思?
      • 此宏在 ilconcert/ilosys.h 中定义为“使用命名空间 std”。因此,它将使您能够使用 C++ 的 STL 运行您的应用程序。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多