2016-09-19 28 views
0

我正在嘗試找出計劃排定問題的CPLEX。 我從IBM閱讀手冊,並編寫了一些代碼。但我不知道爲什麼我看不到每個變量的值。當我寫cplex.exportModel("filename.lp");時,我得到一個空的.lp文件。就像這樣:我的cplex C++代碼exportModel()輸出一個空文件名.lp

\ENCODING=ISO-8859-1 
\Problem name: IloCplex 

Minimize 
obj: 
End 

這裏是我的代碼:

#include <iostream> 
#include <ilcplex/ilocplex.h> 
ILOSTLBEGIN 
//using namespace std; 

/*     Definitions     */ 
IloInt varNumber, lowerBound, upperBound, Wmin, Wmax, Dmin, Dmax; 
IloIntArray objScalars; 

void define_data(IloEnv); 
static void populateSomething(IloModel, IloNumVarArray, IloRangeArray); 


int main(int argc, char **argv) { 
    cout << "running.." << endl << endl; 
    IloEnv env; 

    try{ 

     IloModel model(env); 
     IloCplex cplex(model); 

     IloObjective obj; 
     IloNumVarArray var(env); 
     IloRangeArray rng(env); 

     cplex.exportModel("cplexcpp.lp"); 
     cplex.extract(model); 

     define_data(env); 
     populateSomething(model, var, rng); 


     if(cplex.solve()){ 


      env.out() << "Solution status = " << cplex.getStatus()  << endl; 
      env.out() << "Solution value = " << cplex.getObjValue() << endl; 

      IloNumArray vals(env); 


      cplex.getValues(vals, var); 
      env.out() << "Values  = " << vals << endl; 

      cplex.getSlacks(vals, rng); 
      env.out() << "Slacks  = " << vals << endl; 

      cplex.getDuals(vals, rng); 
      env.out() << "Duals   = " << vals << endl; 

      cplex.getReducedCosts(vals, var); 
      env.out() << "Reduced Costs = " << vals << endl; 
     } 
     obj.end(); 

    }catch (IloException& e){ 
     cerr << "Concert exception caught: " << e << endl; 
    }catch (...){ 
     cerr << "Unknown exception caught: " << endl; 
    } 

    env.end(); 



    return 0; 
} 
/*     Initializations     */ 
void define_data(IloEnv env){ 
    varNumber = 20; 
    lowerBound = 0; 
    upperBound = 1; 
    objScalars = IloIntArray(env, varNumber, 
              10, -1, 8, -1, 
              16, 18, 20, 14, 
              -3, 30, -3, -3, 
              40, -4, -4, 40, 
              -5, 45, 10, -5 
           ); 
    Dmin = 0; 
    Dmax = 10; 
    Wmin = 10; 
    Wmax = 25; 

} 
/* 
static void usage(const char *progname){ 

    cerr << "Usage: " << progname << " -X" << endl; 
    cerr << "Exiting ... " << endl; 
}*/ 

/*     Implementation     */ 
static void populateSomething(IloModel model, IloNumVarArray Yijk, IloRangeArray rng){ 
    IloEnv env = model.getEnv(); 

    Yijk = IloNumVarArray(env, varNumber, lowerBound, upperBound, ILOINT); 
    //IloNumVarArray Xik = IloNumVarArray(env, varNumber, lowerBound, upperBound, ILOINT); 

    //Objective Function: 
    model.add(IloMaximize(env, IloScalProd(objScalars, Yijk))); 


    //Constraint 1-1: 

    IloExpr expr1(env); 
    for(IloInt i = 0; i < varNumber; i += 4){ 
     expr1.setLinearCoef(Yijk[i], 1); 
    } 
    IloRange rng1 = IloRange(env, 2, expr1, 2); 
    rng.IloExtractableArray::add(rng1); 
    expr1.end(); 

    //Constraint 1-2: 

    IloExpr expr2(env); 
    for (IloInt i = 1; i < varNumber; i += 4) { 
     expr2.setLinearCoef(Yijk[i], 1); 
    } 
    IloRange rng2 = IloRange(env, 1, expr2, 1); 
    rng.IloExtractableArray::add(rng2); 
    expr2.end(); 

    //Constraint 1-3: 

    IloExpr expr3(env); 
    for(IloInt i = 2; i < varNumber; i += 4){ 
     expr3.setLinearCoef(Yijk[i], 1); 
    } 
    IloRange rng3 = IloRange(env, 1, expr3, 1); 
    rng.IloExtractableArray::add(rng3); 
    expr3.end(); 

    //Constraint 1-4: 

    IloExpr expr4(env); 
    for(IloInt i = 3; i < varNumber; i += 4){ 
     expr4.setLinearCoef(Yijk[i], 1); 
    } 
    IloRange rng4 = IloRange(env, 2, expr4, 2); 
    rng.IloExtractableArray::add(rng4); 
    expr4.end(); 


    //Constraint 2: 

    for(IloInt i = 0; i < varNumber; i += 2){ 
     //IloRange(env, 0, 7 * Yijk[i] + 8 * Yijk[i+1], 10); 
     rng.add(IloRange(env, Dmin, 7 * Yijk[i] + 8 * Yijk[i+1], Dmax)); 
    } 

    //Constraint 3: 

    for(IloInt i = 0; i < varNumber; i += 4){ 
     rng.add(IloRange(env, Wmin, 7 * Yijk[i] + 8 * Yijk[i+1] + 7 * Yijk[i+2] + 8 * Yijk[i+3] ,Wmax)); 
    } 


} 

我運行模型後,結果是這樣的:

Default variable names x1, x2 ... being created. 
Default row names c1, c2 ... being created. 
THE MODEL : 
IloModel model0 = { 
obj42 = (10 * IloIntVar(1)[0..1] + -1 * IloIntVar(2)[0..1] + 8 * IloIntVar(3)[0..1] + -1 * IloIntVar(4)[0..1] + 16 * IloIntVar(5)[0..1] + 18 * IloIntVar(6)[0..1] + 20 * IloIntVar(7)[0..1] + 14 * IloIntVar(8)[0..1] + -3 * IloIntVar(9)[0..1] + 30 * IloIntVar(10)[0..1] + -3 * IloIntVar(11)[0..1] + -3 * IloIntVar(12)[0..1] + 40 * IloIntVar(13)[0..1] + -4 * IloIntVar(14)[0..1] + -4 * IloIntVar(15)[0..1] + 40 * IloIntVar(16)[0..1] + -5 * IloIntVar(17)[0..1] + 45 * IloIntVar(18)[0..1] + 10 * IloIntVar(19)[0..1] + -5 * IloIntVar(20)[0..1] , IloObjective, Maximize); 

} 

THE RNG : 
[IloIntVar(1)[0..1] + IloIntVar(5)[0..1] + IloIntVar(9)[0..1] + IloIntVar(13)[0..1] + IloIntVar(17)[0..1] == 2 , IloIntVar(2)[0..1] + IloIntVar(6)[0..1] + IloIntVar(10)[0..1] + IloIntVar(14)[0..1] + IloIntVar(18)[0..1] == 1 , IloIntVar(3)[0..1] + IloIntVar(7)[0..1] + IloIntVar(11)[0..1] + IloIntVar(15)[0..1] + IloIntVar(19)[0..1] == 1 , IloIntVar(4)[0..1] + IloIntVar(8)[0..1] + IloIntVar(12)[0..1] + IloIntVar(16)[0..1] + IloIntVar(20)[0..1] == 2 , 0 <= 7 * IloIntVar(1)[0..1] + 8 * IloIntVar(2)[0..1] <= 10 , 0 <= 7 * IloIntVar(3)[0..1] + 8 * IloIntVar(4)[0..1] <= 10 , 0 <= 7 * IloIntVar(5)[0..1] + 8 * IloIntVar(6)[0..1] <= 10 , 0 <= 7 * IloIntVar(7)[0..1] + 8 * IloIntVar(8)[0..1] <= 10 , 0 <= 7 * IloIntVar(9)[0..1] + 8 * IloIntVar(10)[0..1] <= 10 , 0 <= 7 * IloIntVar(11)[0..1] + 8 * IloIntVar(12)[0..1] <= 10 , 
0 <= 7 * IloIntVar(13)[0..1] + 8 * IloIntVar(14)[0..1] <= 10 , 0 <= 7 * IloIntVar(15)[0..1] + 8 * IloIntVar(16)[0..1] <= 10 , 0 <= 7 * IloIntVar(17)[0..1] + 8 * IloIntVar(18)[0..1] <= 10 , 0 <= 7 * IloIntVar(19)[0..1] + 8 * IloIntVar(20)[0..1] <= 10 , 10 <= 7 * IloIntVar(1)[0..1] + 8 * IloIntVar(2)[0..1] + 7 * IloIntVar(3)[0..1] + 8 * IloIntVar(4)[0..1] <= 25 , 10 <= 7 * IloIntVar(5)[0..1] + 8 * IloIntVar(6)[0..1] + 7 * IloIntVar(7)[0..1] + 8 * IloIntVar(8)[0..1] <= 25 , 10 <= 7 * IloIntVar(9)[0..1] + 8 * IloIntVar(10)[0..1] + 7 * IloIntVar(11)[0..1] + 8 * IloIntVar(12)[0..1] <= 25 , 10 <= 7 * IloIntVar(13)[0..1] + 8 * IloIntVar(14)[0..1] + 7 * IloIntVar(15)[0..1] + 8 * IloIntVar(16)[0..1] <= 25 , 10 <= 7 * IloIntVar(17)[0..1] + 8 * IloIntVar(18)[0..1] + 7 * IloIntVar(19)[0..1] + 8 * IloIntVar(20)[0..1] <= 25 , IloIntVar(1)[0..1] <= IloIntVar(21)[0..1] , 
IloIntVar(2)[0..1] <= IloIntVar(22)[0..1] , IloIntVar(3)[0..1] <= IloIntVar(23)[0..1] , IloIntVar(4)[0..1] <= IloIntVar(24)[0..1] , IloIntVar(5)[0..1] <= IloIntVar(25)[0..1] , IloIntVar(6)[0..1] <= IloIntVar(26)[0..1] , IloIntVar(7)[0..1] <= IloIntVar(27)[0..1] , IloIntVar(8)[0..1] <= IloIntVar(28)[0..1] , IloIntVar(9)[0..1] <= IloIntVar(29)[0..1] , IloIntVar(10)[0..1] <= IloIntVar(30)[0..1] , IloIntVar(11)[0..1] <= IloIntVar(31)[0..1] , 
IloIntVar(12)[0..1] <= IloIntVar(32)[0..1] , IloIntVar(13)[0..1] <= IloIntVar(33)[0..1] , IloIntVar(14)[0..1] <= IloIntVar(34)[0..1] , IloIntVar(15)[0..1] <= IloIntVar(35)[0..1] , IloIntVar(16)[0..1] <= IloIntVar(36)[0..1] , IloIntVar(17)[0..1] <= IloIntVar(37)[0..1] , IloIntVar(18)[0..1] <= IloIntVar(38)[0..1] , IloIntVar(19)[0..1] <= IloIntVar(39)[0..1] , IloIntVar(20)[0..1] <= IloIntVar(40)[0..1] ] 
Found incumbent of value 0.000000 after 0.00 sec. (0.00 ticks) 
Found incumbent of value 222.000000 after 0.00 sec. (0.00 ticks) 
Tried aggregator 1 time. 
MIP Presolve eliminated 0 rows and 20 columns. 
All rows and columns eliminated. 
Presolve time = 0.00 sec. (0.00 ticks) 

Root node processing (before b&c): 
    Real time    = 0.00 sec. (0.01 ticks) 
Parallel b&c, 4 threads: 
    Real time    = 0.00 sec. (0.00 ticks) 
    Sync time (average) = 0.00 sec. 
    Wait time (average) = 0.00 sec. 
          ------------ 
Total (root+branch&cut) = 0.00 sec. (0.01 ticks) 
Solution status = Optimal 
Solution value = 251 
Values  = [] 
Concert exception caught: IloExtractable 44 IloRangeI has not been extracted by IloAlgorithm 0x101813000 
Program ended with exit code: 0 

我的目標是讓每一個決策變量的值這是1或0. 有誰知道我的問題是什麼?我該如何解決這個問題? 非常感謝你!

回答

1

首先觀察,在向模型添加任何內容之前,您正在導出模型。在解決問題之前嘗試移動您的導出。