Class: Mapper::CodonBucket

Inherits:
CodonMod show all
Defined in:
../lib/codon_bucket.rb

Overview

“Bucket rule” codon representation:


  index_of_choice = (codon / bucket) mod num_of_choices 

where


  bucket(symbolX) = num_of_choices[symbol0] * num_of_choices[symbol1] ... * num_of_choices[symbolX-1] 

This grammar-dependent representation modifies the standard GE representation implemented by Mapper::CodonMod. With this rule, every codon encodes a unique set of producion rules, each from one nonterminal. See:


 http://books.google.com/books?id=eCbu4GwRLusC&lpg=PA123&ots=hUc3zvqYIh&lr&pg=PA123#v=onepage&q&f=false

Instance Attribute Summary (collapse)

Attributes inherited from CodonMod

#bit_size, #random

Instance Method Summary (collapse)

Methods inherited from CodonMod

#raw_read

Constructor Details

- (CodonBucket) initialize

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



23
24
25
26
27
# File '../lib/codon_bucket.rb', line 23

def initialize
  super
  @bucket = nil    
  @max_closure = nil
end

Instance Attribute Details

- (Object) bucket (readonly)

Closures hash. The element c.bucket[’s’] represents a modifier for all codons encoding a symbol ’s’.



30
31
32
# File '../lib/codon_bucket.rb', line 30

def bucket
  @bucket
end

- (Object) max_closure (readonly)

Maximal value of the codon modifier.



33
34
35
# File '../lib/codon_bucket.rb', line 33

def max_closure
  @max_closure
end

Instance Method Details

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

Create the codon from the index of the choice, number of choices and a symbol. See CodonMod#generate for details.



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

def generate( numof_choices, index, symbol=nil )
  codon = super( numof_choices, index )
  ( @bucket.nil? or symbol.nil? ) ? codon : ( codon * @bucket[symbol] )
end

- (Object) grammar=(gram)

Set the current grammar for codon representation.



36
37
38
39
40
41
42
43
44
# File '../lib/codon_bucket.rb', line 36

def grammar= gram
  @bucket = {}
  @max_closure = 1
  gram.symbols.each do |sym|
    alts = gram[sym] 
    @bucket[sym] = @max_closure
    @max_closure *= alts.size
  end
end

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

Interpret the codon given a number of choices and a symbol ( (c/s) mod n )



47
48
49
50
# File '../lib/codon_bucket.rb', line 47

def interpret( numof_choices, codon, symbol=nil )
  codon = codon.divmod( @bucket[symbol] ).first unless @bucket.nil? or symbol.nil?
  super( numof_choices, codon )
end

- (Object) mutate_bit(codon)

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



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

def mutate_bit codon
  return super if @bucket.nil?
  codon ^ (2 ** @random.rand( @bit_size + @max_closure.to_s(2).size-1 ))
end

- (Object) rand_gen

Generate a valid random codon



66
67
68
69
# File '../lib/codon_bucket.rb', line 66

def rand_gen
  return super if @bucket.nil?     
  @random.rand( @card * @max_closure )
end

- (Boolean) valid_codon?(codon)

Return true if the codon is valid

Returns:

  • (Boolean)


72
73
74
75
# File '../lib/codon_bucket.rb', line 72

def valid_codon? codon
  return super if @bucket.nil?
  codon >= 0 and codon < @card*@max_closure
end