class GeneGenie::Gene

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. @since 0.0.1

Public Class Methods

new(information, fitness_evaluator) click to toggle source
# File lib/gene_genie/gene.rb, line 7
def initialize(information, fitness_evaluator)
  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
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/gene_genie/gene.rb, line 41
def <=>(other)
  fitness <=> other.fitness
end
combine(other_gene) click to toggle source
# File lib/gene_genie/gene.rb, line 29
def combine(other_gene)
  other_gene_hash = other_gene.to_hashes
  new_information = @information.map.with_index do |part, index|
    new_hash = {}
    part.each do |k, v|
      new_hash[k] = (rand > 0.5) ? v : other_gene_hash[index][k]
    end
    new_hash
  end
  Gene.new(new_information, @fitness_evaluator)
end
fitness() click to toggle source
# File lib/gene_genie/gene.rb, line 19
def fitness
  @fitness ||= @fitness_evaluator.fitness(@information)
end
mutate(mutator) click to toggle source
# File lib/gene_genie/gene.rb, line 23
def mutate(mutator)
  @information = mutator.call @information
  @fitness = nil
  self
end
to_hashes() click to toggle source
# File lib/gene_genie/gene.rb, line 15
def to_hashes
  @information
end