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

标签 c++ optimization scheduling cplex

我正在研究 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。 有谁知道我的问题是什么?我该如何解决这个问题? 非常感谢!!

最佳答案

首先观察,您在向模型添加任何内容之前导出模型。尝试将导出移动到解决问题之前。

关于c++ - 我的 cplex c++ 代码 exportModel() 输出一个空文件名.lp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39567612/

相关文章:

Android NDK C++ openGL ES 2 上下文显示效果不佳

c++ - 无法检测浏览器窗口中的点击以触发功能

performance - 在 VB6 中计时功能/测量性能的最佳方法是什么?

Java 线程亲和性

IOS - 使后台获取每天只发生一次(不需要特定时间)

c++ - 硬件功能的包装器

c++ - __VA_ARGS__ 有问题

c++ - 更快的编辑距离算法

optimization - JIT 编译器能做什么 AOT 编译器不能做的事?

java - 如何安排每小时开始的任务