IBM ILOG Solver User's Manual > Extending the Library > Writing a Search Limit: Car Sequencing > Writing your own search limits

To demonstrate how to write a search limit, you will write a time limit on the search. You will use a floating-point value to build the time limit, and this choice gives you the following API:

class IlcTimeLimitI : public IlcSearchLimitI {
private:
  IlcFloat _startTime;
  IlcFloat _limit;
public:
  IlcTimeLimitI(IloSolver solver, IlcBool duplicate, IlcFloat l);
  void init();
  IlcBool check() const;
  IlcSearchLimitI* duplicateLimit(IloSolver);
};


Its constructor simply initializes the _limit member.

IlcTimeLimitI::IlcTimeLimitI (IloSolver solver, IlcBool duplicate, IlcFloat l)
  : IlcSearchLimitI(solver, duplicate), _startTime(0.0), _limit(l) {}

You write a member function init to set the initial time to be used later when Solver checks the limit.

void IlcTimeLimitI::init() {
  _startTime = getSolver()->getTime();
}

Now you write a member function check to compare the current time to the initial time plus the time limit.

IlcBool IlcTimeLimitI::check() const {
  return (getSolver()->getTime() >= _startTime + _limit);
}

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* IlcTimeLimitI::duplicateLimit(IloSolver solver) {
  return new IlcTimeLimitI(solver, IlcTrue, _limit);
}

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

IlcSearchLimit IlcTimeLimit(IloSolver solver, IlcFloat l) {
  IlcSearchLimitI* limit = new (solver.getHeap()) IlcTimeLimitI(solver, 
IlcFalse, l);
  return IlcSearchLimit(limit);
}