如何使用C ++获取Concert技术中的约束系数

时间:2019-09-26 09:15:36

标签: cplex

我导入了MPS文件,我想获得所有约束下的变量系数和右手尺寸。 我如何获得这些参数?

1 个答案:

答案 0 :(得分:1)

importModel方法有几个重载。使用此功能,您可以轻松访问有关约束的信息。下面的代码段是main示例中ilomipex2.cpp函数的修改版本。它演示了如何打印出有关约束的信息:

int
main (int argc, char **argv)
{
   IloEnv env;
   try {
      IloModel model(env);
      IloCplex cplex(env);

      if ( argc != 2 ) {
         usage (argv[0]);
         throw(-1);
      }

      IloObjective   obj;
      IloNumVarArray var(env);
      IloRangeArray  rng(env);
      IloSOS1Array   sos1(env);
      IloSOS2Array   sos2(env);
      IloRangeArray  lazy(env);
      IloRangeArray  cuts(env);

      cplex.importModel(model, argv[1], obj, var, rng, sos1, sos2, lazy, cuts);

      cplex.extract(model);

      if ( lazy.getSize() > 0 )  cplex.addLazyConstraints (lazy);
      if ( cuts.getSize() > 0 )  cplex.addUserCuts (cuts);

      // Print out information about the constraints:
      for (int i = 0; i < rng.getSize(); ++i) {
         IloExpr rngexpr = rng[i].getExpr();
         // Constraint name, lower bound, and upper bound (i.e., RHS):
         env.out() << "Constraint: " << rng[i].getName() << ", LB: " <<
            rng[i].getLB() << ", UB: " << rng[i].getUB() << endl;
         // The variables and coefficients in the constraint:
         for (IloExpr::LinearIterator rngit(rngexpr.getLinearIterator());
              rngit.ok(); ++rngit) {
            env.out() << "Var: " << rngit.getVar() << ", Coef: " <<
               rngit.getCoef() << endl;
         }
      }
      ...
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;
}  // END main