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

The custom search limit is used in finalGoal. The solution counter used by the search limit only increments if the sixth car in the sequence is green. When the solution counter reaches the search limit, the search is stopped.

    IloSolver solver(model);
    IloGoal goal = IloGenerate(env, cars, IloChooseMinSizeInt);
    IloInt counter=0;
    IloSearchLimit limit = IloMyLimit(env, counter, 2);
    IloGoal finalGoal = IloLimitSearch(env, goal, limit);
    solver.startNewSearch(finalGoal);
    while (solver.next())
      {
      solver.out() << solver.getStatus() << " Solution" << endl;
      if (solver.getValue(cars[5]) == 0) { // Car 6 is green
        for (i = 0; i < nbCars; i++) {
          solver.out() << "Car " << i+1 <<  " color:     "
                       << Names[(IloInt)solver.getValue(cars[i])]
                       << endl;
        }
        counter++;
      }
    }
      solver.endSearch();