2013-03-19 47 views
1

我是新來的使用cplex,我試圖找到一些在互聯網上的信息,但沒有找到明確的東西來幫助我解決我的問題。Cplex C++多維決策變量

我有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 ]。

但我很遠離這個結果。

有沒有人有想法?

感謝

回答

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到1,而不是1到1。你的目標應該寫成

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

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(); 
} 
+0

你是什麼意思與'ILOSTBEGIN'? – 2015-02-13 18:41:44

+0

該宏在ilconcert/ilosys.h中定義爲「using namespace std」。所以它可以讓你用C++的STL運行你的應用程序。 – Deanna1125 2015-02-15 18:10:11