Mehrnaz
Mehrnaz

Reputation: 31

How to get number of constraints from Cplex

I have a long program that I've written in C++ and I'm using ILOG Cplex12.5 Solver to solve it. How can I get total the number of constraints? Is there a function for it?

Upvotes: 3

Views: 3875

Answers (2)

Nicolas Grebille
Nicolas Grebille

Reputation: 1332

Once you extracted the model (IloModel) in a IloCplex object (you should do that at some point in your program to solve the model), you can call IloCplex::getNrows to get the total number of rows (constraints) of your problem.

Upvotes: 3

David Nehme
David Nehme

Reputation: 21597

There is a class IloModel::Iterator class that lets you visit the IloExtractable objects in an IloModel object. The IloExtractable has a method asConstraint that will return an empty handle if the extractable is not a constraint. The getImpl() method for any ILOG concert handle will return 0. So you can iterate across all extractable objects and count the ones who's asConstraint function don't return an empty handle.

#include <ilconcert/ilomodel.h>

unsigned getNumConstraints(IloModel m)
{
  unsigned count = 0;
  IloModel::Iterator iter(m);
  while (iter.ok()) {
    if ((*iter).asConstraint().getImpl()) {
      ++count;
    }
    ++iter;
  }
  return count;
}

Upvotes: 2

Related Questions