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

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
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
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
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
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
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
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
PragmaticBookshelf
Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
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: