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

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |> handle_event() |> render() but the correc...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
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
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
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

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
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
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
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
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 Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
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
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

Sub Categories: