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
As per the title, thanks.
New
Hi @Margaret ,
On page VII the book tells us the example and snippets will be all using Elixir version 1.11
But on page 3 almost the en...
New
The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
New
@noelrappin
Running the webpack dev server, I receive the following warning:
ERROR in tsconfig.json
TS18003: No inputs were found in c...
New
Hi,
I completed chapter 6 but am getting the following error when running:
thread 'main' panicked at 'Failed to load texture: IoError(O...
New
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:
d...
New
Title: Agile Web Development with Rails 7: (page 70)
I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
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
Write Elixir tests that you can be proud of. Dive into Elixir’s test philosophy and gain mastery over the terminology and concepts that u...
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
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
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
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New
Author Spotlight
Rebecca Skinner
@RebeccaSkinner
Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
Open-source implementation of the classic GTA engine now running directly in your browser. Experience the reVC technology demo on DOS.Zon...
New
Woke up to this today: Claude Code’s complete source code exposed via npm source map. Not a snippet. All 512,000 lines. 1,900 TypeScript ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /typescript
- /svelte
- /onivim
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /deepseek
- /opensuse
- /zig
- /centos
- /php
- /scala
- /react-native
- /lisp
- /textmate
- /sublime-text
- /nixos
- /debian
- /agda
- /deno
- /django
- /kubuntu
- /arch-linux
- /nodejs
- /ubuntu
- /spring
- /revery
- /manjaro
- /lua
- /diversity
- /julia
- /laravel
- /markdown









