Genetic Algorithms in Elixir: Using reinsertions (source code)

Provided source code has some problems when dealing with the reinsertions.

  1. scripts/schedule.exs example has a default reinsertion strategy set to “elitist”. The problem is, it actually doesn’t and is always running the default “pure” strategy because the naming of the optional parameter has a typo. Changing “reinserton_strategy” to “reinsertion_strategy” does the trick.
  2. If we run the script again, correct strategy is used, but now we get an “ArgumentError” in “Toolbox.Reinsertion.elitist” function when trying to sort data based on fitness.
    The problem actually lies in the “Genetic.evolve” function. In it, “select” function is called which returns “parents” ( composed of pairs of chromosomes, ie: [{p1, p2}, {p3, p4}, …] ) and “leftovers” ( represented as a normal list of chromosomes, ie: [l1, l2, l3, …] ). These two get recombined in the
    “Toolbox.Reinsertion.elitist” function producing a list like [{p1, p2}, {p3, p4}, l5, l6] which is of wrong format.
    A solution then is to de-chunk the “parents” list before sending it to reinsertion function. Something like the following should do the trick:
      parents = parents
        |> Enum.reduce([],
          fn {p1, p2}, acc ->
            [p1, p2 | acc]
          end)
  1. While testing this I also found that the “population_size” parameter is not working. This is because “Genetic.run” does not pass optional parameters to function “Genetic.initialize”. Changing the call to:
population = initialize(&problem.genotype/0, opts)

solves it.

3 Likes