Class: GeneGenie::Gene
- Inherits:
-
Object
- Object
- GeneGenie::Gene
- Defined in:
- lib/gene_genie/gene.rb
Overview
A Gene is the basic unit of the genetic algorithm. Genes hold the information used to evaluate their fitness. They are combined into new Genes during the optimisation process.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #combine(other_gene) ⇒ Object
- #fitness ⇒ Object
- #fitness_evaluator ⇒ Object
-
#initialize(information:, fitness_evaluator:, gene_combiner: GeneGenie::Combiner::OnePointCombiner.new) ⇒ Gene
constructor
A new instance of Gene.
- #mutate(mutator) ⇒ Object
- #normalised_fitness(minimum) ⇒ Object
- #to_hashes ⇒ Object
Constructor Details
#initialize(information:, fitness_evaluator:, gene_combiner: GeneGenie::Combiner::OnePointCombiner.new) ⇒ Gene
Returns a new instance of Gene.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/gene_genie/gene.rb', line 9 def initialize(information:, fitness_evaluator:, gene_combiner: GeneGenie::Combiner::OnePointCombiner.new) fail ArgumentError, 'information must be Array' unless information.kind_of? Array fail ArgumentError, 'information must be Array of Hashes' unless information[0].kind_of? Hash @information = information @fitness_evaluator = fitness_evaluator @combiner = gene_combiner end |
Instance Method Details
#<=>(other) ⇒ Object
47 48 49 |
# File 'lib/gene_genie/gene.rb', line 47 def <=>(other) fitness <=> other.fitness end |
#combine(other_gene) ⇒ Object
43 44 45 |
# File 'lib/gene_genie/gene.rb', line 43 def combine(other_gene) @combiner.call(self, other_gene) end |
#fitness ⇒ Object
24 25 26 |
# File 'lib/gene_genie/gene.rb', line 24 def fitness @fitness ||= @fitness_evaluator.fitness(@information) end |
#fitness_evaluator ⇒ Object
28 29 30 |
# File 'lib/gene_genie/gene.rb', line 28 def fitness_evaluator @fitness_evaluator end |
#mutate(mutator) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/gene_genie/gene.rb', line 36 def mutate(mutator) @information = mutator.call @information @fitness = nil @normalised_fitness = nil self end |
#normalised_fitness(minimum) ⇒ Object
32 33 34 |
# File 'lib/gene_genie/gene.rb', line 32 def normalised_fitness(minimum) @normalised_fitness ||= fitness - minimum end |
#to_hashes ⇒ Object
20 21 22 |
# File 'lib/gene_genie/gene.rb', line 20 def to_hashes @information end |