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

New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
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
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
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
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
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
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’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

Sub Categories: