eekenpiet

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! :slight_smile:

Where Next?

Popular Pragmatic Bookshelf topics Top

brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

PragmaticBookshelf
Machine learning can be intimidating, with its reliance on math and algorithms that most programmers don't encounter in their regular wor...
New
PragmaticBookshelf
Ruby, Io, Prolog, Scala, Erlang, Clojure, Haskell. With Seven Languages in Seven Weeks, by Bruce A. Tate, you’ll go beyond the syntax—and...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
PragmaticBookshelf
Explore the power of Ash Framework by modeling and building the domain for a real-world web application. Rebecca Le @sevenseacat and ...
New
xiji2646-netizen
Woke up to this today: Claude Code’s complete source code exposed via npm source map. Not a snippet. All 512,000 lines. 1,900 TypeScript ...
New

Sub Categories: