[英]My cplex c++ codes exportModel() output an empty filename.lp
I'm working on CPLEX trying to figure out scheduling problems.我正在研究 CPLEX,试图找出调度问题。 I read the manuals from IBM, and wrote some codes.
我阅读了 IBM 的手册,并编写了一些代码。 But I don't know why I could not see the values of each variable.
但我不知道为什么我看不到每个变量的值。 When I wrote
cplex.exportModel("filename.lp");
当我写
cplex.exportModel("filename.lp");
I got an empty .lp file.我有一个空的 .lp 文件。 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?我的目标是让每个决策变量的值都是 1 或 0。有谁知道我的问题是什么? 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".不过,最近我遇到了
IloCplex::extract
方法,显然您必须先“提取”模型,然后“导出”。 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
其中
model
是我的对象, "recourse.lp"
是我的const char * filename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.