我的 C++ Concert Cplex 有问题。 我正在尝试重新创建最短路径问题。输出到文本文件是:
Minimize
obj: 2 x_12 + x_13 + 2 x_21 + x_24 + x_31 + 3 x_34 + x_42 + 3 x_43 + x9
Subject To
c1: x_12 + x_13 - x_21 - x_31 + x_14 - x_41 = 1
c2: - x_12 + x_21 + x_24 - x_42 + x_23 - x_32 = 0
c3: - x_13 + x_31 + x_34 - x_43 - x_23 + x_32 = 0
c4: - x_24 - x_34 + x_42 + x_43 - x_14 + x_41 = -1
Bounds
x9 = 0
End
然后我使用下面的代码来获得解决方案:
IloCplex spp(model);
spp.setParam(IloCplex::RootAlg, IloCplex::AutoAlg);
spp.solve();
IloArray<IloNumArray> vals(env);
env.out() << "Solution status = " << spp.getStatus() << endl;
env.out() << "Solution value = " << spp.getObjValue() << endl;
env.out() << "Values x = " << vals << endl;
但是我不断得到的输出是:
Solution status = Optimal
Solution value = 0
Values x = []
有人知道我的程序有什么问题吗? 谢谢
编辑:
我的模型是在程序本身中构建的,这是第一部分:
IloEnv env;
IloModel model(env);
IloArray<IloNumVarArray> x(env);
IloRangeArray c(env);
IloInt nnodes = G.size();
IloInt i, j;
IloEnv env = model.getEnv();
//SHORTEST PATH PROBLEM
for (i = 0; i < nnodes; i++){ //x decision variables
x.add(IloNumVarArray(env, nnodes, 0, IloInfinity));
}
for (i = 0; i < nnodes; i++){
for (j = 0; j < nnodes; j++){
stringstream sts;
sts << "x_" << i + 1 << j + 1;
x[i][j].setName(sts.str().c_str()); //SET NAMES
}
}
//set objective min sum_(all ij)[c_ij][x_ij]
IloExpr obj(env);
for (i = 0; i < nnodes; i++){
for (j = 0; j < nnodes; j++){
obj += G[i][j] * x[i][j];
}
}
model.add(IloMinimize(env, obj));
obj.end();
//constraints sum_j[x_ij]-sum_j[x_ji] = 1 for s, -1 for t, or 0
for (i = 0; i < nnodes; i++){
int ss = 0;
if (i == s) ss = 1;
if (i == t) ss = -1;
IloExpr sum1(env);
IloExpr sum2(env);
for (j = 0; j < nnodes; j++){
sum1 += x[i][j];
sum2 += x[j][i];
}
c.add(sum1 - sum2 == ss);
sum1.end();
sum2.end();
}
model.add(c);
//solving---------------------------------------------------------
IloCplex spp(model);
//write to file
spp.exportModel("model1.lp");
spp.solve();
最佳答案
显然您没有从文件中读取模型。这是一个 example .所以在你的情况下:
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
int main (int argc, char **argv)
{
IloEnv env;
try {
IloModel model(env);
IloCplex cplex(model);
IloObjective obj;
IloNumVarArray var(env);
IloRangeArray con(env);
cplex.importModel(model, "tmp.lp", obj, var, con);
cplex.extract(model);
// Optimize the problem and obtain solution.
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
IloNumArray vals(env);
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
cplex.getValues(vals, var);
env.out() << "Values = " << vals << endl;
cplex.getSlacks(vals, con);
env.out() << "Slacks = " << vals << endl;
cplex.getDuals(vals, con);
env.out() << "Duals = " << vals << endl;
cplex.getReducedCosts(vals, var);
env.out() << "Reduced Costs = " << vals << endl;
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
} // END main
tmp.lp 是您的 LP 模型文件。运行我得到的这段代码
Tried aggregator 1 time.
LP Presolve eliminated 3 rows and 12 columns.
Aggregator did 1 substitutions.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.01 ticks)
Solution status = Optimal
Solution value = 0
Values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
Slacks = [0, 0, 0, 0]
Duals = [1, 0, 0, 1]
Reduced Costs = [1, 0, 3, 2, 2, 4, 0, 2, 1, 0, 0, 0, 0]
关于c++ - 复杂模型 : no solution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38278871/