IBM ILOG Solver User's Manual > Local Search > Basic Local Search > Example: Using metaheuristics > Searching for a solution

The search can now progress. First, we create a solution to keep the best solution found by simulated annealing over the run. As cost degrading moves can be taken, the final solution may not be the same as the best one found.

  IloSolution best = soln.makeClone(env);

Now we enter an optimization loop, where we perform up to 100 iterations:

  IloInt iter = 100;
  do {
    while (--iter > 0 && solver.solve(move)) {
      if (soln.getObjectiveValue() < best.getObjectiveValue())
        best.copy(soln);
    }
  } while (iter > 0 && !simAnn.complete());

The inner while loop can exit under two conditions: the first is that the iteration limit has been exhausted. In this case, the outer loop also terminates immediately. The inner loop can also terminate when solver.solve(move) returns IloFalse. This is the case when the move goal fails, that is, when all moves from the neighborhood are rejected and none can be chosen. When this happens for a metaheuristic, Solver provides an API for offering the metaheuristic the chance to change its filtering so that moves can be re-taken in the future. The complete() method of the metaheuristics performs this action. If IloFalse is returned, it means that the metaheuristic wishes to continue working, whereas if it returns IloTrue, it indicates that there is no way for it to relax its filtering, and so it would like to terminate the search.

For the simulated annealing metaheuristic, the temperature is divided by the ratio specified when the metaheuristic was constructed (0.99 here), slightly increasing the temperature. This is done in the hope that an increased temperature can allow search to progress more easily. The return value of complete() is IloFalse for the simulated annealing metaheuristic, indicating that it never wants to stop search: it is up to the user to impose a limit.

After the optimization loop has terminated, we can restore the best solution found:

  solver.solve(IloRestoreSolution(env, best));