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)
Popular Pragprog topics










Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
Other popular topics










Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm