
ryanzidago
Genetic Algorithms in Elixir: Wrong function to calculate the average genes (page 148)
The average_tiger
function is written like so in the book:
def average_tiger(population) do
genes = Enum.map(population, & &1.genes)
fitnesses = Enum.map(population, & &1.fitness)
ages = Enum.map(population, & &1.age)
num_tigers = length(population)
avg_fitness = Enum.sum(fitnesses) / num_tigers
avg_age = Enum.sum(ages) / num_tigers
avg_genes =
genes
|> Enum.zip()
|> Enum.map(&(Enum.sum(&1) / num_tigers))
%Chromosome{genes: avg_genes, age: avg_age, fitness: avg_fitness}
end
Genes is a list of list:
[
[1, 1, 1, 0, 1, 1, 0, 1],
[0, 1, 1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 1, 0, 1],
[0, 1, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 0],
[0, 0, 1, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 1],
[0, 1, 0, 1, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 0, 0]
]
With Enum.zip/1
this list of lists is transformed to a list of tuples (with columns zipped together):
[
{1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0},
{1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0},
{1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0},
{1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0}
]
The problem is that Tuple
does not implement the Enumerable
protocol so it is not possible to map the element of tuple like this:
genes
|> Enum.zip()
|> Enum.map(&(Enum.sum(&1) / num_tigers))
I think some code got los in the process
To calculate the average genes, the code should look like this:
avg_genes =
genes
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
|> Enum.map(&Enum.sum/1)
|> Enum.map(&(&1 / num_tigers))
|> Enum.map(&Float.round(&1, 2))
Popular Prag Prog topics

your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New

Hi,
build fails on:
bracket-lib = “~0.8.1”
when running on Mac Mini M1 Rust version 1.5.0:
Compiling winit v0.22.2
error[E0308]: mi...
New

I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New

When installing Cards as an editable package, I get the following error:
ERROR: File “setup.py” not found. Directory cannot be installe...
New

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

The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error.
lib/pento_web/live/product_l...
New

Hi all,
currently I wonder how the Tailwind colours work (or don’t work).
For example, in app/views/layouts/application.html.erb I have...
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

root_layout: {PentoWeb.LayoutView, :root},
This results in the following following error:
no “root” html template defined for PentoWeb...
New

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

New

You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
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

This looks like a stunning keycap set :orange_heart:
A LEGENDARY KEYBOARD LIVES ON
When you bought an Apple Macintosh computer in the e...
New

In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first:
asdf plugin-upd...
New

If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New

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

Author Spotlight
Jamis Buck
@jamis
This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New

The overengineered Solution to my Pigeon Problem.
TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New

I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /react-native
- /v
- /wasm
- /django
- /security
- /nodejs
- /centos
- /rails
- /haskell
- /fable
- /gleam
- /js
- /swift
- /deno
- /tailwind
- /assemblyscript
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /preact
- /flutter
- /actix
- /java
- /angular
- /ocaml
- /kubuntu
- /zig
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /emacs
- /vuejs
- /nim
- /elm
- /nerves