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
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.
Popular Pragmatic Bookshelf topics
As per the title, thanks.
New
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’.
In particula...
New
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
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
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159
When I execute the command:
user=> (create-...
New
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
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
I am using Android Studio Chipmunk | 2021.2.1 Patch 2
Build #AI-212.5712.43.2112.8815526, built on July 10, 2022
Runtime version: 11.0....
New
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
Other popular topics
Brace yourself for a fun challenge: build a photorealistic 3D renderer from scratch! In just a couple of weeks, build a ray tracer that r...
New
Ruby, Io, Prolog, Scala, Erlang, Clojure, Haskell. With Seven Languages in Seven Weeks, by Bruce A. Tate, you’ll go beyond the syntax—and...
New
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
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
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig.
General-purpose programming language and toolchain for maintaini...
New
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc.
However, I don’t...
New
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
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /crystal
- /c-plus-plus
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /zig
- /deepseek
- /scala
- /sublime-text
- /lisp
- /textmate
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /deno
- /django
- /revery
- /ubuntu
- /nodejs
- /spring
- /manjaro
- /diversity
- /lua
- /julia
- /c
- /slackware







