kdiogenes

kdiogenes

Genetic Algorithms in Elixir: Chapter 1 sample implementation in Ruby finds a solution much faster

I translated the implementation in Chapter 1 (without mutation) for Ruby and get surprised that it’s find a solution much faster than the Elixir implementation.

The Ruby implementation finds the solution in about 10 seconds, while the Elixir one takes about 1 minute. I get a bit surprised by this result and couldn’t think of an explanation for this difference.

I’m sharing my Ruby implementation for anyone curious to explain this difference:

#!/usr/bin/env ruby
population =
  Array.new(100) do
    Array.new(1000) { [0, 1].sample }
  end

def evaluate(population)
  population.sort_by(&:sum).reverse
end

def selection(population)
  population.each_slice(2)
end

def crossover(population)
  population.reduce([]) do |offspring, parents|
    cx_point = cut = rand(1...parents[0].length)
    child1 = parents[0][0...cx_point] + parents[1][cx_point..-1]
    child2 = parents[1][0...cx_point] + parents[0][cx_point..-1]
    offspring << child1 << child2
  end
end

def algorithm(population)
  loop do
    best = population.max_by(&:sum)
    print "Current Best: #{best.sum}\r"
    break best if best.sum == 1000

    population = evaluate(population)
    population = selection(population)
    population = crossover(population)
  end
end

solution = algorithm(population)
puts "\nAnswer is\n"
puts solution.inspect

First Post!

kdiogenes

kdiogenes

After going further in the book (the algorithms have more output) I verified that the ruby implementation takes more generations to find the solution, but it runs each generation faster.

Where Next?

Popular Pragmatic Bookshelf topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
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
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=&gt; (create-migration "guestbook") Execution error (Ill...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
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
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
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
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
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
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
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
New
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New

Sub Categories: