简体   繁体   中英

My cplex c++ codes exportModel() output an empty filename.lp

I'm working on CPLEX trying to figure out scheduling problems. I read the manuals from IBM, and wrote some codes. But I don't know why I could not see the values of each variable. When I wrote cplex.exportModel("filename.lp"); I got an empty .lp file. Like this:

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

Minimize
 obj:
End

Here is my codes:

#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));
    }


}

After I run the model, the result is like:

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

My goal is to get every single decision variable's value which is either 1 or 0. Does anyone know what my problem is? And how do I fix this? Thank you so much!!

First observation, you are exporting a model before you have added anything to the model. Try moving your export to just before you solve it.

So I've been having the same problem as you ie printing an "empty" file. Recently though, I came accros the IloCplex::extract method and apparently you have to first "extract" your model and then "export". In my case, the following did the trick:

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

where model is my object and "recourse.lp" is my const char * filename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM