Genetic Algorithms in Elixir: Ch4. errata in evolve function (page 59)

At the beginning of page 59, in chapter 4

alter the evolve function to track a generation parameter, like this:

def evolve(problem, generation, opts \\ []) do
  # ...
  if terminate?(population, generation) do
    # ...
  else
    generation = generation + 1
    ...
    |> evolve(population, generation opts)
  end
end

The evolve definition is missing the first param population;
a comma is also missing between the last two params of the evolve call, at the end of the function’s body,

Edit: I guess problem should be in the params of the recursive call to evolve in place of population , and as prefix in the terminate? call.

We have also to include an initial value for generation in run/2’s evolve call, to avoid an>

(ArithmeticError) bad argument in arithmetic expression

due to an uninitialized generation in the + 1 operation. (I’m starting from generation 0)

Corrige:


def run(problem, opts \\ []) do
  population = initialize(&problem.genotype/0, opts)
  first_generation = 0

  population
  |> evolve(problem, first_generation, opts)
end

def evolve(population, problem, generation, opts \\ []) do
  # ...
  if problem.terminate?(population, generation) do
    # ...
  else
    generation = generation + 1
    ...
    |> evolve(problem, generation, opts)
  end
end

(Hope I’m not bothering anyone with this small corrections, I’m really enjoying the book)

1 Like