eekenpiet
Genetic Algorithms in Elixir: Chapter 9/10 ebook-1.0.2 Genealogy not working well
Chapter 9/10 ebook-1.02 Genealogy not working well.
As already mentioned in one of the errata the ID of every chromosome is the same.
So plotting the tree with libgraph gives many problems.
For instance parents are not unique, they are all inserted.
I did change a few things. Nodes are now unique.
Labels are much smaller, because I changed them to fitness.
You now get a nice plot with small nodes.
This is the relevant part of genealogy.ex:
def handle_cast({:add_chromosome, parent, child}, genealogy) do
new_genealogy =
genealogy
|> Graph.add_vertex(child.id, child.fitness)
|> Graph.add_edge(parent.id, child.id)
{:noreply, new_genealogy}
end
def handle_cast({:add_chromosome, parent_a, parent_b, child}, genealogy) do
new_genealogy =
genealogy
|> Graph.add_vertex(child.id, child.fitness)
|> Graph.add_edge(parent_a.id, child.id)
|> Graph.add_edge(parent_b.id, child.id)
{:noreply, new_genealogy}
end
The last part of crossover().
ID’s are now unique. Fitness are assigned
def crossover(population, problem, opts \\ []) do
................
................
c1 = %Chromosome{
c1
| id: Base.encode16(:crypto.strong_rand_bytes(64)),
fitness: problem.fitness_function(c1)
}
c2 = %Chromosome{
c2
| id: Base.encode16(:crypto.strong_rand_bytes(64)),
fitness: problem.fitness_function(c2)
}
Utilities.Genealogy.add_chromosome(p1, p2, c1)
Utilities.Genealogy.add_chromosome(p1, p2, c2)
[c1 | [c2 | acc]]
and of mutation():
def mutation(population, problem, opts \\ []) do
.......
.......
mutant = %Chromosome{
mutant
| id: Base.encode16(:crypto.strong_rand_bytes(64)),
fitness: problem.fitness_function(mutant)
}
Utilities.Genealogy.add_chromosome(c, mutant)
mutant
I did use the following tiger_simulation, and watch the change in genotype:
defmodule TigerSimulation do
@behaviour Problem
alias Types.Chromosome
@impl true
def genotype do
genes = for _ <- 1..8, do: Enum.random(0..1)
chromo = %Chromosome{genes: genes, size: 8, id: Base.encode16(:crypto.strong_rand_bytes(64))}
fitness = fitness_function(chromo)
%Chromosome{chromo | fitness: fitness}
end
@impl true
def fitness_function(chromosome) do
tropic_scores = [0.0, 3.0, 2.0, 1.0, 0.5, 1.0, -1.0, 0.0]
# tundra_scores = [1.0, 3.0, -2.0, -1.0, 0.5, 2.0, 1.0, 0.0]
traits = chromosome.genes
traits
|> Enum.zip(tropic_scores)
|> Enum.map(fn {t, s} -> t * s end)
|> Enum.sum()
end
@impl true
def terminate?(_population, generation), do: generation == 10
end
_tiger =
Genetic.run(TigerSimulation,
population_size: 8,
selection_rate: 0.8,
mutation_rate: 0.1
)
genealogy = Utilities.Genealogy.get_tree()
{:ok, dot} = Graph.Serializers.DOT.serialize(genealogy)
{:ok, dotfile} = File.open("tiger_simulation.dot", [:write]) #staat in hoofd-directory
:ok = IO.binwrite(dotfile, dot)
:ok = File.close(dotfile)
and in evolve()
........
.......
children = crossover(parents, problem, opts)
......
mutants = mutation(population, problem, opts)
Tip: In Intellij Idea you have a nice plugin for .dot files: Dot plus!
If selection <1 you wil see also loose nodes. Nice!
And some other suggestions:
def run(problem, opts \\ []) do
population = initialize(&problem.genotype/0, opts)
start_bestfitness = %Chromosome{hd(population) | fitness: 0.0}
population
|> evolve(problem, 0, start_bestfitness, bestgeneration = 0, opts)
end
def evolve(population, problem, generation, bestfitness, bestgeneration, opts \\ []) do
population = evaluate(population, &problem.fitness_function/1, opts)
best = hd(population)
best1 = best.fitness
best2 = bestfitness.fitness
bestfitness =
if best1 > best2 do
best
else
bestfitness
end
bestgeneration =
if best1 > best2 do
generation
else
bestgeneration
end
IO.inspect(best, label: "\n\ncurrent best:")
IO.write("Generation: #{generation}")
statistics(population, generation, opts)
if problem.terminate?(population, generation) do
............................
.............................
Popular Pragmatic Bookshelf topics
This test is broken right out of the box…
— FAIL: TestAgent (7.82s)
agent_test.go:77:
Error Trace: agent_test.go:77
agent_test.go:...
New
Hi Jamis,
I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019.
This test doesn’t pass for me:
...
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
Hi Travis! Thank you for the cool book! :slight_smile:
I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
Page 28: It implements io.ReaderAt on the store type.
Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt?
...
New
Hello! Thanks for the great book.
I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
When trying to run tox in parallel as explained on page 151, I got the following error:
tox: error: argument -p/–parallel: expected one...
New
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
Is there any plan for volume 2? :slight_smile:
New
Other popular topics
If it’s a mechanical keyboard, which switches do you have?
Would you recommend it? Why?
What will your next keyboard be?
Pics always w...
New
Algorithms and data structures are much more than abstract concepts. Mastering them enables you to write code that runs faster and more e...
New
Andy and Dave wrote this influential, classic book to help their clients create better software and rediscover the joy of coding. Almost ...
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
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
Hi folks,
I don’t know if I saw this here but, here’s a new programming language, called Roc
Reminds me a bit of Elm and thus Haskell. ...
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
If you want a quick and easy way to block any website on your Mac using Little Snitch simply…
File > New Rule:
And select Deny, O...
New
Leverage Elixir and the Nx ecosystem to build intelligent applications that solve real-world problems in computer vision, natural languag...
New
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
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /java
- /clojure
- /emacs
- /haskell
- /typescript
- /svelte
- /onivim
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /vscode
- /flutter
- /ash
- /html
- /deepseek
- /opensuse
- /zig
- /centos
- /php
- /scala
- /react-native
- /lisp
- /sublime-text
- /textmate
- /nixos
- /debian
- /agda
- /deno
- /django
- /kubuntu
- /arch-linux
- /nodejs
- /spring
- /ubuntu
- /revery
- /manjaro
- /julia
- /diversity
- /lua
- /quarkus
- /laravel









