Genetic Algorithms in Elixir: Wrong function to calculate the average genes (page 148)

The average_tiger function is written like so in the book:

  def average_tiger(population) do
    genes = Enum.map(population, & &1.genes)
    fitnesses = Enum.map(population, & &1.fitness)
    ages = Enum.map(population, & &1.age)
    num_tigers = length(population)

    avg_fitness = Enum.sum(fitnesses) / num_tigers
    avg_age = Enum.sum(ages) / num_tigers

    avg_genes =
      genes
      |> Enum.zip()
      |> Enum.map(&(Enum.sum(&1) / num_tigers))

    %Chromosome{genes: avg_genes, age: avg_age, fitness: avg_fitness}
  end

Genes is a list of list:

[
  [1, 1, 1, 0, 1, 1, 0, 1],
  [0, 1, 1, 1, 1, 0, 1, 0],
  [1, 1, 1, 0, 0, 0, 0, 0],
  [0, 0, 1, 1, 1, 1, 0, 0],
  [0, 1, 0, 1, 1, 0, 0, 0],
  [1, 1, 0, 0, 1, 1, 0, 1],
  [0, 1, 0, 1, 0, 0, 0, 1],
  [1, 0, 1, 1, 0, 1, 0, 0],
  [0, 0, 1, 1, 0, 1, 0, 0],
  [0, 1, 0, 0, 1, 0, 0, 1],
  [0, 1, 0, 1, 1, 0, 1, 1],
  [0, 0, 1, 1, 0, 1, 1, 1],
  [0, 0, 1, 1, 0, 0, 1, 1],
  [1, 1, 0, 0, 0, 0, 1, 1],
  [0, 0, 0, 1, 0, 1, 0, 0],
  [0, 0, 0, 1, 1, 0, 0, 0],
  [1, 0, 0, 1, 1, 1, 1, 1],
  [1, 0, 0, 0, 0, 1, 0, 1],
  [1, 0, 0, 1, 1, 0, 1, 1],
  [0, 0, 0, 0, 1, 0, 0, 0]
]

With Enum.zip/1 this list of lists is transformed to a list of tuples (with columns zipped together):

[
  {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0},
  {1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0},
  {1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0},
  {1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1},
  {1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0},
  {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0},
  {1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0}
]

The problem is that Tuple does not implement the Enumerable protocol so it is not possible to map the element of tuple like this:

      genes
      |> Enum.zip()
      |> Enum.map(&(Enum.sum(&1) / num_tigers))

I think some code got los in the process :man_shrugging:
To calculate the average genes, the code should look like this:

avg_genes =
      genes
      |> Enum.zip()
      |> Enum.map(&Tuple.to_list/1)
      |> Enum.map(&Enum.sum/1)
      |> Enum.map(&(&1 / num_tigers))
      |> Enum.map(&Float.round(&1, 2))