Genetic Algorithms in Elixir: Chapter 5

Now using B2.

The tournament_no_duplicates code sample has a nested function that is invalid.

This is a corrected version:

  def tournament_no_duplicates(population, n, tournsize) do
    selected = MapSet.new()
    tournament_helper(population, n, tournsize, selected)
  end

  defp tournament_helper(population, n, tournsize, selected) do
    if MapSet.size(selected) == n do
      MapSet.to_list(selected)
    else
      chosen = population
        |> Enum.take_random(tournsize)
        |> Enum.max_by(&(&1.fitness))
      tournament_helper(population, n, tournsize, MapSet.put(selected, chosen))
    end
   end
1 Like

Thanks for finding this! I’ll update for the next beta!