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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
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
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
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
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Sub Categories: