Cplex c++ multidimensional decision variable

后端 未结 2 994
感动是毒
感动是毒 2021-01-22 12:49

I\'m new using cplex and I try to find some information on internet but didn\'t find clear stuff to help me in my problem.

I have P[k] k will be equal to 1 to 4

相关标签:
2条回答
  • 2021-01-22 13:20

    Usertfwr already gave a good answer, but I would like to give another version of solution which might help you to code CPLEX applications in a more generic way. First, I would suggest you to use a text file to hold all the data (objective function coefficients) which will be fed into the program. In your case, you only have to copy literally the following matrix like data to notepad and name it as “coef.dat”:

    [1000, 2000, 500, 1500]

    Now comes the full code, let me know if have difficulties understanding any statement:

      #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 讨论(0)
  • 2021-01-22 13:42

    You probably want to use an 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]; );
    }
    

    You also need to declare x as a 2-dimensional array.

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

    Also, in c++, array indices are from 0 to size-1, not 1 to size. Your objective should be written

    model.add(IloMaximize(env, 1000 * p[0] + 2000 * p[1] + 500 * p[2] + 1500 * p[3]));
    
    0 讨论(0)
提交回复
热议问题