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

Thank you for finding all these things :slight_smile:. I’m glad you’re enjoying the book! I’ll go back and fix this one as well.

2 Likes

It is a pleasure to be helpful, I’m just reading the book, studying the concepts and putting the code at work, so the small glitches in the redaction wake up the little debugger-daemon in me… :bug::smiling_imp: They actually help me to focus and understand the overall logic and goals! Also I know how hard is to spot them when you know every and each word of your book by memory. I’m glad to be of some help, while learning so much!

1 Like