tvanderpol

tvanderpol

Genetic Algorithms in Elixir: Ch4, tracking temperature in evolve() (~p178)

The evolve signature in this code sample suddenly loses the population argument which I think isn’t intended, the code goes on to use it a little later without an alternate source for it.

Edit: In this same code block, the variable best gets assigned an integer whereas previous it’d been a Chromosome. Other code (like our printing the results) expects best to contain a Chromosome, as it gets returned from evolve().

My amended evolve function looks like this instead:

  def evolve(population, problem, generation, last_max_fitness, temperature, opts \\ []) do
    population   = evaluate(population, &problem.fitness_function/1, opts)
    best         = Enum.max_by(population, &problem.fitness_function/1)
    best_fitness = best.fitness
    temperature  = 0.8 * (temperature + (best_fitness - last_max_fitness))

    IO.write("\rCurrent Best: #{List.to_string(best.genes)} (#{best.fitness})")
    if problem.terminate?(population, generation, temperature) do
      best
    else
      generation = generation + 1
      population
      |> select(opts)
      |> crossover(opts)
      |> mutation(opts)
      |> evolve(problem, generation, best_fitness, temperature, opts)
    end
  end

(Disregard the IO.write, I like to see what’s going on :))

Marked As Solved

seanmor5

seanmor5

Author of Genetic Algorithms in Elixir

This was a transcription error between my code and what’s presented in the book.

best should be a Chromosome as evolve should return the entire solution found. Thanks for pointing this out!

Where Next?

Popular Pragmatic Bookshelf topics Top

telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
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
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

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
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
PragmaticBookshelf
Use WebRTC to build web applications that stream media and data in real time directly from one user to another, all in the browser. ...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
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
RobertRichards
Hair Salon Games for Girls Fun Girls Hair Saloon game is mainly developed for kids. This game allows users to select virtual avatars to ...
New

Sub Categories: