Class: Mapper::Base

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

Overview

The core Grammatical Evolution genotype->phenotype mapper. It generates a program (phenotype) according syntactic rules (Mapper::Grammar). The selection of rules is driven by the vector of numbers (genotype),

See: www.grammatical-evolution.org/ eprints.kfupm.edu.sa/43213/1/43213.pdf

Direct Known Subclasses

Generator

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Base) initialize(grammar, wraps_to_fail = 1, wraps_to_fading = nil, consume_trivial_codons = true)

The required parameter is the grammar (Mapper::Grammar).

Optional wraps_to_fail argument specifies the maximal number of genotype wrappings. If the genotype vector is used repetitively wraps_to_fail times, the maping fails. Default is 1.


 

Optional wraps_to_fading argument specifies the number of genotype wrappings during which all possible (:terminating and/or :cyclic) rule alternations (Mapper::RuleAlt) can be selected from. After wraps_to_fading wrappings only the :terminating rule alternations are available for selection. If wraps_to_fading is set to nil (default), the fading strategy is turned off. See also Validator.analyze_recursivity.

If the optional consume_trivial_codons argument is set to true (default), the allele codons are used even if the number of rule alternations for selecting from is 1. Set it to false if the codons should not be “wasted” during such trivial decision cases. Note that setting consume_trivial_codons=false may affect the functionality of genetic operators, due to the number of used codons which is not always an even number.



44
45
46
47
48
49
50
51
52
# File '../lib/mapper_base.rb', line 44

def initialize( grammar, wraps_to_fail=1, wraps_to_fading=nil, consume_trivial_codons=true )
  @grammar = grammar
  @wraps_to_fail = wraps_to_fail
  @wraps_to_fading = wraps_to_fading
  @consume_trivial_codons = consume_trivial_codons 
  @track_support_on = false
  @codon = CodonMod.new # standard 8-bit codons
  @mapped_count = 0
end

Instance Attribute Details

- (Object) codon

Codon encoding scheme. By default the instance of the CodonMod class is used (ie. standard GE 8-bit codons) See CodonMod for details.



82
83
84
# File '../lib/mapper_base.rb', line 82

def codon
  @codon
end

- (Object) complexity (readonly)

The complexity of the expression is the complexity of its root node. The complexity of the node i is recursively defined as:


   complexity(i) = node_count(i) + sum_over_all_subnodes_of_i( complexity(subnode) )
   node_count(i) = 1 + sum_over_all_subnodes_of_i( node_count(subnode) )

See: scholar.google.cz/scholar?cluster=18084534358495618318&hl=en&as_sdt=0,5



78
79
80
# File '../lib/mapper_base.rb', line 78

def complexity
  @complexity
end

- (Object) consume_trivial_codons

See Mapper::Base#initialize



62
63
64
# File '../lib/mapper_base.rb', line 62

def consume_trivial_codons
  @consume_trivial_codons
end

- (Object) grammar (readonly)

The grammar used.



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

def grammar
  @grammar
end

- (Object) mapped_count (readonly)

Total number of phenotypes mapped, from the initialisation (for diagnostic purposes).



85
86
87
# File '../lib/mapper_base.rb', line 85

def mapped_count
  @mapped_count
end

- (Object) track_support (readonly)

The output array of the TrackNodes. (See Operator::CrossoverLHS and Mapper::TrackNode for explanation.)



68
69
70
# File '../lib/mapper_base.rb', line 68

def track_support
  @track_support
end

- (Object) track_support_on

true means the track_support for LHS Crossover is turned on



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

def track_support_on
  @track_support_on
end

- (Object) used_length (readonly)

The number of codons used by the last mapping process. This value is set by the previous Mapper::Base#phenotype call. Note the used_length may be greather than the genotype.size because of the wrapping effect.



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

def used_length
  @used_length
end

- (Object) wraps_to_fading

See Mapper::Base#initialize



62
63
64
# File '../lib/mapper_base.rb', line 62

def wraps_to_fading
  @wraps_to_fading
end

- (Object) wraps_to_fail

See Mapper::Base#initialize



62
63
64
# File '../lib/mapper_base.rb', line 62

def wraps_to_fail
  @wraps_to_fail
end

Instance Method Details

- (Object) phenotype(genome)

Take the genome (the vector of Fixnums) and use it for the genotype->phenotype mapping. Returns the phenotype string (or nil if the mapping process fails).



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File '../lib/mapper_base.rb', line 89

def phenotype genome
  @mapped_count += 1
  @fading = @wraps_to_fading 

  return nil if genome.empty?

  tokens = [ Token.new( :symbol, @grammar.start_symbol, 0 ) ]
  @used_length = 0
  @track_support = nil 
  @complexity = 1 
  length_limit = @wraps_to_fail*genome.size

  until ( selected_indices = find_nonterminals( tokens ) ).empty?
  
    tsi1 = @used_length
    return nil if @used_length > length_limit
    selected_index, loc_idx = pick_locus( selected_indices, genome )
    selected_token = tokens[selected_index]

    return nil if @used_length > length_limit
    expansion, alt_idx = pick_rule( selected_token, genome )
    expansion.each { |t| t.depth = selected_token.depth+1 }

    @complexity += selected_token.depth * expansion.arity + 1
    track_expansion( selected_token, expansion, tsi1, alt_idx, loc_idx ) if @track_support_on

    tokens = apply_expansion( tokens, expansion, selected_index )

  end

  return ( tokens.collect {|t| t.data} ).join
end