Class: MHL::BitstringGenotypeSpace

Inherits:
Object
  • Object
show all
Defined in:
lib/mhl/bitstring_genotype_space.rb

Overview

This class implements a genotype with bitstring representation

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ BitstringGenotypeSpace

Returns a new instance of BitstringGenotypeSpace.



7
8
9
10
11
12
13
14
# File 'lib/mhl/bitstring_genotype_space.rb', line 7

def initialize(opts)
  @bitstring_length = opts[:bitstring_length].to_i
  unless @bitstring_length and @bitstring_length > 0
    raise ArgumentError, 'Must have positive integer bitstring_length'
  end

  @random_func = opts[:random_func] || default_random_func(opts[:random_one_to_zero_ratio] || 1.0)
end

Instance Method Details

#get_randomObject



16
17
18
# File 'lib/mhl/bitstring_genotype_space.rb', line 16

def get_random
  @random_func.call
end

#reproduce_from(p1, p2, mutation_rv, recombination_rv) ⇒ Object

reproduction with bitflip mutation and one-point crossover



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mhl/bitstring_genotype_space.rb', line 21

def reproduce_from(p1, p2, mutation_rv, recombination_rv)
  # make copies of p1 and p2
  # (we're only interested in the :genotype key)
  c1 = { :genotype => p1[:genotype].dup }
  c2 = { :genotype => p2[:genotype].dup }

  # mutation comes first
  bitflip_mutation(c1[:genotype], mutation_rv)
  bitflip_mutation(c2[:genotype], mutation_rv)

  # and then recombination
  c1[:genotype], c2[:genotype] =
    onepoint_crossover(c1[:genotype], c2[:genotype], recombination_rv)

  return c1, c2
end