IBM ILOG Solver User's Manual > Extending the Library > Writing a Search Limit: Car Sequencing > Writing a search limit: Car sequencing > Writing the custom search limit

You use the class IlcSearchLimitI to write a custom search limit:

class IlcMyLimitI : public IlcSearchLimitI {
private:
  IlcInt& _counter;
  IlcInt _limit;
public:
  IlcMyLimitI(IloSolver, IlcBool, IlcInt& counter, IlcInt limit);
  void init(const IlcSearchNode);
  IlcBool check(const IlcSearchNode) const;
  IlcSearchLimitI* duplicateLimit(IloSolver);
};

The constructor initializes the _limit member:

IlcMyLimitI::IlcMyLimitI (IloSolver s, IlcBool dup, IlcInt& counter,
                              IlcInt limit) :
  IlcSearchLimitI(s, dup), _counter(counter), _limit(limit) {}

The member function init stores a reference state for the node evaluator. This will be used when Solver checks the limit.

void IlcMyLimitI::init(const IlcSearchNode) {}

The member function check checks to see if the solution counter has passed the search limit:

IlcBool IlcMyLimitI::check(const IlcSearchNode node) const {
  if (_counter >= _limit) {
    node.getSolver().out()
      << "Limit crossed: the counter has attained the limit "
      << _limit << endl;
    return IlcTrue;
  }
  return IlcFalse;
}

The member function duplicateLimit clones the current object and sets the value of the duplicate parameter in the constructor to IlcTrue. It allocates the duplicated object on the C++ heap:

IlcSearchLimitI* IlcMyLimitI::duplicateLimit(IloSolver s) {
  return new IlcMyLimitI(s, IlcTrue, _counter, _limit);
}

The function IlcMyLimit returns a handle of type IlcSearchLimit built from an instance of the class IlcMyLimitI. This instance is built with the duplicate parameter set to IlcFalse, and it must be allocated on the reversible Solver heap:

IlcSearchLimit IlcMyLimit(IloSolver s, IlcInt& c, IlcInt l) {
  IlcSearchLimitI* limit = new (s.getHeap()) IlcMyLimitI(s, IlcFalse , c, l);
  return IlcSearchLimit(limit);
}