Class: GeneGenie::SimpleGeneMutator

Inherits:
Object
  • Object
show all
Defined in:
lib/gene_genie/mutator/simple_gene_mutator.rb

Overview

A SimpleGeneMutator loops through each member of a hash, and has a 1% chance of swapping the value for another valid value (based on the template)

Since:

  • 0.0.1

Instance Method Summary collapse

Constructor Details

#initialize(template, mutation_rate = 0.01) ⇒ SimpleGeneMutator

Returns a new instance of SimpleGeneMutator.

Since:

  • 0.0.1



7
8
9
10
# File 'lib/gene_genie/mutator/simple_gene_mutator.rb', line 7

def initialize(template, mutation_rate = 0.01)
  @template = template
  @mutation_rate = mutation_rate
end

Instance Method Details

#call(genes) ⇒ Object

Since:

  • 0.0.1



12
13
14
15
16
17
18
19
20
21
# File 'lib/gene_genie/mutator/simple_gene_mutator.rb', line 12

def call(genes)
  genes.each_with_index do |hash, index|
    hash.each do |k, v|
      if rand < @mutation_rate
        hash[k] = rand(@template[index][k])
      end
    end
  end
  genes
end