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

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
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
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New

Other popular topics Top

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
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
PragmaticBookshelf
From finance to artificial intelligence, genetic algorithms are a powerful tool with a wide array of applications. But you don't need an ...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1147 29994 760
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Get the comprehensive, insider information you need for Rails 8 with the new edition of this award-winning classic. Sam Ruby @rubys ...
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
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New
mindriot
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New

Sub Categories: