Class: Mapper::Generator

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

Overview

“Sensible Initialization” of genotypes for Grammatical Evolution.

See: Grammatical Evolution: Evolutionary Automatic Programming in an Arbitrary Language, section 8.8: www.springer.com/computer/artificial/book/978-1-4020-7444-8

or: www-dept.cs.ucl.ac.uk/staff/W.Langdon/ftp/papers/azad_thesis.ps.gz Chapter 7 Sensible Initialisation

or: www.essex.ac.uk/dces/research/publications/technicalreports/2007/ces475.pdf

Mapper::Generator uses Mapper::Grammar and the source of randomness to create “syntactically correct” genotypes with a given depth of a phenotype tree.

Direct Known Subclasses

AllLocus, BreadthFirst, BreadthLocus, DepthFirst, DepthLocus, DepthLocusEmbConsts

Instance Attribute Summary (collapse)

Attributes inherited from Base

#codon, #complexity, #consume_trivial_codons, #grammar, #mapped_count, #track_support, #track_support_on, #used_length, #wraps_to_fading, #wraps_to_fail

Instance Method Summary (collapse)

Methods inherited from Base

#phenotype

Constructor Details

- (Generator) initialize(*args)

Initialize the generator with the arguments necessary for Mapper::Base#initialize



25
26
27
28
29
# File '../lib/mapper_generator.rb', line 25

def initialize *args
  super
  @random = Kernel
  @generated_count = 0
end

Instance Attribute Details

- (Object) generated_count (readonly)

Total number of genotypes generated, from the initialisation (for diagnostic purposes).



41
42
43
# File '../lib/mapper_generator.rb', line 41

def generated_count
  @generated_count
end

- (Object) random

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



38
39
40
# File '../lib/mapper_generator.rb', line 38

def random
  @random
end

Instance Method Details

- (Object) generate(recursivity, required_depth)

Generate the genotype using the recursivity information. The recursivity argument is the array of allowed node recursivity types (before the required_depth is reached). Mapper::Generator#generate_full uses [:cyclic], Mapper::Generator#generate_grow uses [:cyclic, :terminating].



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File '../lib/mapper_generator.rb', line 66

def generate( recursivity, required_depth )
  @generated_count += 1
  @fading = nil

  genome = []
  tokens = [ Token.new( :symbol, @grammar.start_symbol, 0 ) ]

  until ( selected_indices = find_nonterminals( tokens ) ).empty?
    selected_index = generate_locus( selected_indices, genome )
    selected_token = tokens[selected_index]

    selected_symbol = selected_token.data
    return genome if @grammar[selected_symbol].recursivity == :infinite # emergency fallback     

    rec = (selected_token.depth < required_depth) ? recursivity : [:terminating]
    expansion = generate_rule( rec, selected_token, genome, required_depth-selected_token.depth )
    expansion.each { |t| t.depth = selected_token.depth+1 }

    tokens = apply_expansion( tokens, expansion, selected_index )
   
  end
 
  genome
end

- (Object) generate_full(required_depth)

Generate the genotype using the “full” method: if the depth of the current node is smaller, then select only :cyclic nodes for a deeper level, otherwise select only :terminating nodes.

See also Mapper::Validator.analyze_recursivity for discussion of node recursivity types.



49
50
51
# File '../lib/mapper_generator.rb', line 49

def generate_full required_depth 
  generate( [:cyclic], required_depth )
end

- (Object) generate_grow(required_depth)

Generate the genotype using the “grow” method: if the depth of the current node is smaller, then select :cyclic and/or :terminating nodes for a deeper level, otherwise select only :terminating nodes.

See also Mapper::Validator.analyze_recursivity for discussion of node recursivity types.



59
60
61
# File '../lib/mapper_generator.rb', line 59

def generate_grow required_depth 
  generate( [:cyclic, :terminating], required_depth )
end