【发布时间】:2017-05-17 23:48:47
【问题描述】:
我试图得到约束的对偶
这是代码,用 C++ 实现:
IloEnv env;
IloModel MasterProblem(env);
IloNumVarArray XX(env,Create_routes.size(),0,IloInfinity,ILOFLOAT);
IloNumVarArray t(env,m,0,IloInfinity,ILOFLOAT);
IloExpr expr(env);
////defining ojective of problem
IloObjective masterObj(env,expr,IloObjective::Maximize);
expr.end();
MasterProblem.add(masterObj);
IloRangeArray const1(env); //hala yeki yeki mahdudiyatha ro misazim
for (int i=0; i<n; i++){
IloExpr expr(env);
for (int j=0; j<Create_routes.size(); j++){
if (Create_routes[j]->internalnodes[i+m]==1)
expr+=XX[j];
}
const1.add(1==expr);
MasterProblem.add(const1[i]);
expr.end();
}
IloRangeArray const2(env);
IloRangeArray const4(env);//mahdudiate depohaye open shode
for (i=0; i<m; i++){
IloExpr expr(env);
for (int j=0; j<Create_routes.size(); j++){
if (Create_routes[j]->depot==i){
expr+=XX[j]*Create_routes[j]->demand_collected;
}
}
expr-=t[i]*g[i]->QF;
const2.add(0>=expr);
MasterProblem.add(const2[i]);
expr.end();
}
IloRangeArray2 const3(env,m);
for (i=0; i<m; i++){
const3[i]=IloRangeArray(env);
}
for (int f=0; f<m; f++){
for (i=0; i<n; i++){
IloExpr expr(env);
for (int j=0; j<Create_routes.size(); j++){
if ((Create_routes[j]->depot==f)&&(Create_routes[j]->internalnodes[i+m]==1)){
expr+=XX[j];
}
}
expr-=t[f];
const3[f].add(0>=expr);
MasterProblem.add(const3[f][i]);
expr.end();
}
}
IloCplex cplexM(MasterProblem);
cplexM.setParam(IloCplex::RootAlg, IloCplex::Barrier);
cplexM.setParam(IloCplex::Threads, 4);
if ( !cplexM.solve() ){
env.error() << "Failed to optimize LP." << endl;
nodee->uperbound=0;
env.end();
return;
}
else{
if (!cplexM.isPrimalFeasible()){//agar infeasible bud bia birun
nodee->uperbound=0;
return;
}
cout<<"MasterProblem Solved"<<endl;
cout<<"objective="<<cplexM.getObjValue()<<endl;
javab=cplexM.getObjValue();
}
IloNumArray duall(env,n);
IloNumArray duall1(env,m);
cplexM.getDuals(duall,const1);
cplexM.getDuals(duall1,const2);
IloNumArray2 duall2(env,m);
for (i=0; i<m; i++){
duall2[i]=IloNumArray(env,n);
for (j=0;j<n;j++){
duall2[i][j]=cplexM.getDual(const3[i][j]);
}
}
当通过不同的 cplex 方法(如 Barrier、Primal、Dual、Network)解决这个 LP 问题时,我最终得到了完全不同的 Dual 值和不同的解决方案。为什么会这样? 是因为我的问题中有平等约束吗? 如何确定真实值通过 cplex?
非常感谢任何帮助。
【问题讨论】:
-
这看起来像是来自某种车辆路线应用程序的列生成主控。这些大师是高度堕落的,所以大卫在下面的回答是正确的。我建议您使用 Simplex,因为当您添加新列时,您不需要从头开始重新优化(这对于屏障来说是正确的)。对于这样的模型,我过去尝试使用 sifting 优化器得到了一些不错的结果。
-
@Ioannis :感谢您的有用评论,使用单工是什么意思?!
-
Simplex 是 CPLEX 使用的线性规划的默认算法。有对偶单纯形、原始单纯形和网络单纯形等变体,它们基于相同的原理,但分别利用原始、对偶或网络结构。 CPLEX uses by default the dual Simplex method 但可以更改。
标签: c++ mathematical-optimization linear-programming cplex ilog