Xavier Nayrac

Accro au TDD, rubyiste mais pas que, maker, heureux utilisateur de Vim, accordéoniste.
Si vous vous sentez particulièrement généreux, suivez moi sur Twitter.

Un algorithme génétique en Julia - partie 12

| Comments

Niveau : intermédiaire

Avec ce que j’ai compris au dernier épisode à propos des types, je peux améliorer la fonction mutate:

1
2
3
4
5
6
7
8
function mutate(ch)
  mutator(g) = if rand(1:10) == 1
    g == 1 ? 0 : 1
  else
    g
  end
  Chromosome([ mutator(x)::Int for x in ch.genes ])
end

Maintenant les gènes des chromosomes restent toujours des tableaux de Int:

julia> include("main.jl")
mutate (generic function with 1 method)

julia> pop = create_population(8, 20)
8-element Array{Chromosome,1}:
 Chromosome([0,0,0,0,1,0,0,0,1,0,0,1,1,1,0,1,0,1,1,1])
 Chromosome([1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1])
 Chromosome([1,1,1,0,1,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0])
 Chromosome([0,0,1,0,0,1,1,0,1,0,1,0,1,0,0,0,0,0,1,1])
 Chromosome([0,1,0,1,0,1,1,0,1,0,1,0,1,1,0,0,0,0,1,1])
 Chromosome([0,0,0,1,0,1,1,1,0,0,1,0,1,0,0,0,0,1,0,0])
 Chromosome([0,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,1,1])
 Chromosome([0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1])

julia> scores = score(pop)
8-element Array{Int32,1}:
  9
  9
 10
  8
 10
  7
  8
  6

julia> selection = tournament(scores)
16-element Array{Int32,1}:
 7
 2
 7
 5
 2
 7
 3
 7
 2
 2
 1
 3
 6
 5
 2
 3

julia> gen2 = reproduction([], pop, selection)
8-element Array{Chromosome,1}:
 Chromosome([1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1])
 Chromosome([0,1,1,0,0,0,1,1,0,0,0,1,0,0,1,0,0,1,1,1])
 Chromosome([1,1,1,1,1,0,0,0,0,1,0,1,0,0,1,0,0,1,1,1])
 Chromosome([0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1])
 Chromosome([1,1,1,0,1,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0])
 Chromosome([0,0,1,0,1,1,0,0,1,1,1,0,1,0,0,0,0,1,0,0])
 Chromosome([0,0,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,0,1,1])
 Chromosome([1,0,1,0,1,1,0,0,0,1,1,0,1,0,1,0,1,1,1,0])

À demain.

Articles connexes

Commentaires