
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 Prag Prog topics

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

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

I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New

Title: Web Development with Clojure, Third Edition, vB17.0 (p9)
The create table guestbook syntax suggested doesn’t seem to be accepted ...
New

Is the book’s epub format available to read on Google Play Books?
New

On page 78 the following code appears:
<%= link_to ‘Destroy’, product,
class: ‘hover:underline’,
method: :delete,
data: { confirm...
New

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

Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New

Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels.
Before ...
New

Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
Other popular topics

Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it.
Does your monitor have eye p...
New

No chair. I have a standing desk.
This post was split into a dedicated thread from our thread about chairs :slight_smile:
New

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
https://on...
New

I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New

Author Spotlight
Dmitry Zinoviev
@aqsaqal
Today we’re putting our spotlight on Dmitry Zinoviev, author of Data Science Essentials in ...
New

Author Spotlight
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New

Author Spotlight:
Tammy Coron
@Paradox927
Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig.
General-purpose programming language and toolchain for maintaini...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /zig
- /html
- /scala
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /textmate
- /sublime-text
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /django
- /spring
- /diversity
- /nodejs
- /lua
- /julia
- /c
- /slackware
- /neovim