-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric Matte
committed
Nov 17, 2019
1 parent
5a6271f
commit 34dcccc
Showing
9 changed files
with
253 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
# A member of a given population | ||
class Genetic::Algorithm::Chromosome | ||
attr_accessor :genes | ||
attr_accessor :fitness | ||
module Genetic | ||
module Algorithm | ||
# A member of a given population | ||
class Chromosome | ||
attr_reader :genes | ||
attr_reader :fitness | ||
|
||
def initialize(genes:, fitness:) | ||
@genes = genes | ||
@fitness = fitness | ||
end | ||
def initialize(genes, fitness_calculator) | ||
@genes = genes | ||
@fitness = fitness_calculator.call(genes) | ||
end | ||
|
||
# Randomly change one of its gene. | ||
# Help maintain diversity within the population and prevent premature convergence. | ||
# @param [(Object | Float)[][]] ranges - The available ranges to mutate from for all genes. ranges.lenght must equals genes.length | ||
# @param [Boolean] sampleFromRanges | ||
# If true: take one of the available values for a given range | ||
# if false: take a random floating value between ranges[i][0] and ranges[i][1] | ||
def mutate(ranges, sample_from_ranges = false) | ||
i = rand(0..(genes.length - 1)) | ||
@genes[i] = sample_from_ranges ? ranges[i].sample : rand(ranges[i][0]..ranges[i][1]) | ||
# Randomly change one of its gene. | ||
# Help maintain diversity within the population and prevent premature convergence. | ||
def mutate(genes_generator) | ||
i = rand(0..(genes.length - 1)) | ||
@genes[i] = genes_generator.call(i) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module Genetic | ||
module Algorithm | ||
class Optimiser | ||
attr_accessor :crossover_probability # Int | ||
attr_accessor :mutation_probability # Int | ||
attr_accessor :population_size # Int | ||
attr_accessor :number_of_generations # Int | ||
attr_accessor :genes_generator # (index: Int?) -> Array<Gene> | Gene if index.nil? | ||
attr_accessor :fitness_calculator # (genes: Array<Gene>) -> Int | ||
|
||
def execute | ||
population = initialize_population | ||
|
||
fitness_recorder = FitnessRecorder.new | ||
@number_of_generations.times.each do |generation| | ||
if generation != @number_of_generations | ||
population = do_natural_selection(population) | ||
end | ||
fitness_recorder.record_generation(generation, population) | ||
end | ||
|
||
fitness_recorder.display_best_individual | ||
end | ||
|
||
private | ||
|
||
def initialize_population | ||
population = [] | ||
@population_size.times.each do | ||
population << Chromosome.new(@genes_generator.call, @fitness_calculator) | ||
end | ||
population | ||
end | ||
|
||
def do_natural_selection(population) | ||
total_fitness = population.map(&:fitness).inject(:+) | ||
new_population = [] | ||
(@population_size / 2).times.each do | ||
parent1 = weighted_random_sampling(population, total_fitness) | ||
parent2 = weighted_random_sampling(population - [parent1], total_fitness) | ||
|
||
child1, child2 = mate(parent1, parent2) | ||
|
||
new_population << child1 | ||
new_population << child2 | ||
end | ||
|
||
new_population | ||
end | ||
|
||
def weighted_random_sampling(population, total_fitness) | ||
population.max_by { |chromosome| rand**(total_fitness / chromosome.fitness) } | ||
end | ||
|
||
def mate(parent1, parent2) | ||
# crossover | ||
if rand < @crossover_probability | ||
i = rand(1..parent1.genes.length - 1) | ||
child1 = Chromosome.new(parent1.genes[0..i - 1] + parent2.genes[i..-1], @fitness_calculator) | ||
child2 = Chromosome.new(parent2.genes[0..i - 1] + parent1.genes[i..-1], @fitness_calculator) | ||
else | ||
child1 = Chromosome.new(parent1.genes.dup, @fitness_calculator) | ||
child2 = Chromosome.new(parent2.genes.dup, @fitness_calculator) | ||
end | ||
|
||
# mutation | ||
if rand < @mutation_probability | ||
child1.mutate(@genes_generator) | ||
child2.mutate(@genes_generator) | ||
end | ||
|
||
[child1, child2] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.