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

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
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
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
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
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
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
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
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
Or looking forward to? :nerd_face:
483 10427 254
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
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
PragmaticBookshelf
Create efficient, elegant software tests in pytest, Python's most powerful testing framework. Brian Okken @brianokken Edited by Kat...
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
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New

Sub Categories: