我的复杂c++代码exportModel()输出一个空文件名.lp



我正在CPLEX上工作,试图找出调度问题。我阅读了IBM的使用手册,并编写了一些代码。但是我不知道为什么我看不到每个变量的值。当我写cplex.exportModel("filename.lp");时,我得到一个空的。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。有人知道我的问题是什么吗?我该如何解决这个问题?非常感谢!!

第一个观察,在您向模型添加任何内容之前,您正在导出一个模型。在解决问题之前,请尝试将导出移到。

所以我一直有和你一样的问题,即打印"空"文件。但最近,我遇到了IloCplex::extract方法,显然你必须首先"提取"。你的模型然后"导出"。在我的例子中,下面的代码完成了这个任务:

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

其中model是我的对象,"recourse.lp"是我的const char * filename

相关内容

  • 没有找到相关文章

最新更新