eekenpiet
Genetic Algorithms in Elixir: Errors in ch11 page 182 ebook version 1.0.2 concerning Agents
There are quite a lot of errors in the text.
My version of elixir (1.17.3) told me it is not allowed to place the first three functions in chromosome.ex, since the declaration of %{Chromo..} happens there. So I placed them in genetic.
Here is my version:
To Genetic!
def start_link(genotype) do
Agent.start_link(fn -> genotype.() end)
end
def get_fitness(pid) do
c = Agent.get(pid, & &1)
c.fitness
end
def eval(pid, fitness_func) do
c = Agent.get(pid, & &1)
Agent.update(pid, fn c ->
%Chromosome{c | fitness: fitness_func.(c)} end)
end
In module genetic:
def initialize(genotype, opts \\ []) do
population_size = Keyword.get(opts, :population_size, 4)
agents_population = for _ <- 1..population_size do
start_link(genotype)
end
end
def evaluate(agents_population, fitness_function, _opts \\ []) do
pids = Enum.map(agents_population, fn {:ok, pid} -> pid end)
do_tasks =
Enum.map(pids, &Task.async(fn -> eval(&1, fitness_function) end))
|> Enum.map(&Task.await(&1))
agents_population = Enum.sort_by(pids, fn pid -> get_fitness(pid) end)
end
You have to watch the timeout of your agents (maybe set it to :infinity)
To test it you have to comment out all the lines of evolve but the first.
The reason is that you also have to rewrite crossover, mutation and so on for this Agents method!
If you came to this page via a book’s portal and it has pre-filled part of the title and tags for you please only add what you need to after the pre-filled text in the title, this is usually a description of what you are posting and the page number so it follows this format:
Title: Name of Book: description (page number)
Example: Programming Flutter: ‘pub get’ command not working (page 15)
(If you did not come via a book portal please follow the above format and please also add the book’s tag if it is not already present (initiating thread creation via the book’s portal is advised as it pre-fills all of the important information - which helps authors spot your thread)).
Please also use Markdown to format your post (three backticks ``` on a separate line above and below for code blocks)
Thanks! ![]()
Popular Pragmatic Bookshelf topics
Other popular topics
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /zig
- /centos
- /deepseek
- /php
- /scala
- /react-native
- /lisp
- /textmate
- /sublime-text
- /nixos
- /debian
- /agda
- /django
- /deno
- /kubuntu
- /arch-linux
- /nodejs
- /ubuntu
- /revery
- /manjaro
- /spring
- /julia
- /lua
- /diversity
- /markdown
- /c









