Class: Mapper::CodonMod

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

Overview

The standard codon representation in GE.

Direct Known Subclasses

CodonBucket, CodonGray

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CodonMod) initialize(bit_size = 8)

Initialise a codon representation. bit_size is the number of bits per one codon.



10
11
12
13
# File '../lib/codon_mod.rb', line 10

def initialize( bit_size=8 )
  self.bit_size= bit_size
  @random = Kernel
end

Instance Attribute Details

- (Object) bit_size

The number of bits per codon.



19
20
21
# File '../lib/codon_mod.rb', line 19

def bit_size
  @bit_size
end

- (Object) random

The source of randomness, used for calling “random.rand( limit )”, defaulting to ‘Kernel’ class.



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

def random
  @random
end

Instance Method Details

- (Object) generate(numof_choices, index, dummy = nil)

Create the codon from the index of the choice and a number of choices. The index has to be the number from the interval 0 .. numof_choices-1 Resultant codon is a stochastically produced number which, when subjected to the modulo operation, produces an index, ie:


  codon = index unmod numof_choices 

See: www-dept.cs.ucl.ac.uk/staff/W.Langdon/ftp/papers/azad_thesis.ps.gz Chapter 7 Sensible Initialisation, page 132, ‘unmod’ operation.



47
48
49
50
51
52
# File '../lib/codon_mod.rb', line 47

def generate( numof_choices, index, dummy=nil )
  raise "CodonMod: cannot accomodate #{numof_choices} choices in #{@bit_size}-bit codon" if numof_choices > @card
  raise "CodonMod: index (#{index}) must be lower than numof choices (#{numof_choices})" if index >= numof_choices
  return index if numof_choices == 0
  index + numof_choices * @random.rand( @card/numof_choices )
end

- (Object) interpret(numof_choices, codon, dummy = nil)

Interpret the codon given a number of choices, returning the index:


  index = codon mod numof_choices


31
32
33
34
# File '../lib/codon_mod.rb', line 31

def interpret( numof_choices, codon, dummy=nil )
  raise "CodonMod: codon #{codon} out of range 0..#{@card-1}" unless valid_codon? codon
  codon.divmod(numof_choices).last
end

- (Object) mutate_bit(codon)

Mutate a single bit in the source codon, return the mutated codon.



65
66
67
# File '../lib/codon_mod.rb', line 65

def mutate_bit codon
  codon ^ (2 ** @random.rand( @bit_size ))
end

- (Object) rand_gen

Generate a valid random codon



55
56
57
# File '../lib/codon_mod.rb', line 55

def rand_gen
  @random.rand @card
end

- (Object) raw_read(codon)

Read a raw codon value



60
61
62
# File '../lib/codon_mod.rb', line 60

def raw_read codon
  codon
end

- (Boolean) valid_codon?(codon)

Return true if the codon is valid

Returns:

  • (Boolean)


70
71
72
# File '../lib/codon_mod.rb', line 70

def valid_codon? codon
  codon >= 0 and codon < @card
end