IBM ILOG Solver User's Manual > Evolutionary Algorithms > Car Sequencing Using EA > Problem solving: Solution replacement

The final remaining point to address is how the replacement of solutions operates in the genetic algorithm. Each generation produces a single offspring, and so a single member of the population will be replaced by this offspring. Here, a particular technique is used to maintain diversity of the population. This technique replaces one of the offspring's parents by the offspring. As children are often similar to parents (in computer genetics, as well as is in real genetics), this tends to keep a good diversity of solutions in the population. In order to favor better solutions, where there is more than one parent, the worst parent is chosen for selection.

There is one exception to the above rule. A rule often used in genetic algorithms is the so-called elitist rule where the best member of the population is never replaced if there is no equally good solution in the population. This rule is implemented here, and comes into effect when using the mutation operator (only one parent), and the child is of poorer quality than the parent. If, in this case the parent was the best of the population, it is not replaced, but the child is discarded instead.

Step 16   -  

Add the replacement selector

Insert the following code after the comment // The replacement selector

ILOSELECTOR2(ReplaceWorstParent, IloSolution,
             IloSolutionPool, in,
             IloSolutionPool, pop,
             IloSolutionPool, parents) {
  IloSolution candidate = parents.getWorstSolution();
  IloSolution child = in.getSolution(0);
  pop.sort();
  if (pop.getSize() <= 1 || !candidate.isBetterThan(pop.getSolution(1))) {
    pop.remove(candidate);
    pop.add(child);
  }
  else
    candidate = child;
  select(candidate);
}

The parameters to the ILOSELECTOR2 macro are the name of the resulting selector function, the type of object to select, the input pool to the selector from the pool processor to the left, and then the two parameters passed at construction time (population and parent pools).

First, the potential solution to replace, which is the worst of the parents, is taken from the parent pool; the child is also retrieved from the input pool. After sorting the population, the elitist rule is applied. The worst parent is replaced if the population only has one element, or if the second best member of the population is at least as good as the worst parent. This last test avoids replacing the parent if the parent is the best of the population, and better in quality than the second best of the population. If the worst parent is to be replaced, it is removed from the population and the child added in its place. Otherwise, the candidate for destruction is the child. Finally, the consider method of the selector selects the candidate for destruction, which will be passed to the destroy processor in the code body.