eekenpiet

eekenpiet

Genetic Algorithms in Elixir: Chapter5 pag82 ebook-1.0.2 Hanging of "tournament_no-duplicates"

The function tournament_no_duplicates keeps hanging, because it will never reach n winners.
In a selection of n chromosomes you can never have n winners.
Maybe you will ever reach n-(tournsize-1) winners.
So I propose a different function.

def tournament_no_duplicates(problem, population, n, tournsize) do
  set =
    tournament(problem, population, n, tournsize)
    |> MapSet.new()
    |> MapSet.to_list()
# you get an error in crossover if this selection is not even.
# therefore I delete the weakest of the list
  if rem(length(set), 2) == 0 do
    set
  else
    Enum.sort_by(set, &problem.fitness_function(&1), :desc)
    List.delete_at(set, -1)
  end
end

Your selection could go to [ ],
therefore the population must be much bigger then the number of generations you run.
Yet you can reach good results.

I added this to the first part of evolve() in Genetic:

if population == [] do
  IO.write("\n\nYou should make your population bigger\n\n")
  raise "Watch it! Population is empty"
end

best = hd(population)
IO.inspect(best, label: "\n\nCurrent best:")
IO.write("Generation: #{generation}")

Where Next?

Popular Pragmatic Bookshelf topics Top

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
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
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
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
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
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
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
Write Elixir tests that you can be proud of. Dive into Elixir’s test philosophy and gain mastery over the terminology and concepts that u...
New
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
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
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
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: