Experimenting with Reinsertion
Genetic Algorithms in Elixir — by Sean Moriarity (66 / 101)
👈 Understanding Reinsertion | TOC | Growing and Shrinking Populations 👉
To see the impact of each of these reinsertion strategies in action, you can apply them to the scheduling problem you implemented earlier in this chapter to see how your outcomes differ.
Pure reinsertion is the default reinsertion strategy, so you should try that one first because you don’t have to change anything in your original problem:
$ mix run scripts/schedule.exs
Current best: 15.9000 Generation: 1000
%Types.Chromosome{
age: 1,
fitness: 15.899999999999999,
genes: [1, 1, 0, 1, 1, 1, 0, 0, 0, 1],
size: 10
}Next, try elitist reinsertion with a survival rate of 10%. To use elitist reinsertion, you have to use a partial application of the elitist/4 function, like this:
soln = Genetic.run(Schedule,
reinserton_strategy:
&Toolbox.Reinsertion.elitist(&1, &2, &3, 0.1),
selection_rate: 0.8,
mutation_rate: 0.1)
IO.write("\n")
IO.inspect(soln)You use & to create a partial application of elitist/4 and then specify a survival rate of 0.1. Additionally, you ensure that selection_rate, mutation_rate, and the specified survival rate all add up to 1.0. You’ll see why that’s important in the next section.
Run your algorithm with elitist reinsertion:
$ mix run scripts/schedule.exs
Current best: 15.9000 Generation: 1000
%Types.Chromosome{
age: 1,
fitness: 15.899999999999999,
genes: [1, 1, 0, 1, 1, 1, 0, 0, 0, 1],
size: 10
}Finally, test your problem with uniform reinsertion. To do this, take the same approach you took with elitist reinsertion:
soln = Genetic.run(Schedule,
reinserton_strategy:
&Toolbox.Reinsertion.elitist(&1, &2, &3, 0.1),
selection_rate: 0.8,
mutation_rate: 0.1)
IO.write("\n")
IO.inspect(soln)Now run your algorithm with uniform reinsertion:
$ mix run scripts/schedule.exs
Current best: 15.3000 Generation: 1000
%Types.Chromosome{
age: 1,
fitness: 15.299999999999999,
genes: [1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
size: 10
}You can see that each reinsertion strategy performed similarly. They all converged on similar results, albeit likely with different levels of efficiency. Uniform reinsertion performed slightly worse than both elitist and pure reinsertion — which is to be expected, considering it doesn’t account for fitness at all. It’s also likely that pure reinsertion and elitist reinsertion converged before uniform reinsertion. The results of this experiment are sensitive to the random nature of genetic algorithms.
What you should take away from this is that in practice, if you need your algorithms to converge quickly and efficiently, use elitist or pure reinsertion. Ultimately, elitist reinsertion is usually the better choice, although it’s difficult to determine without concrete statistics. In Chapter 9, Tracking Genetic Algorithms, you’ll learn more about comparing different genetic algorithms.
👈 Understanding Reinsertion | TOC | Growing and Shrinking Populations 👉
Genetic Algorithms in Elixir by Sean Moriarity can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

