Skip to content

Commit

Permalink
integrated into DepthLocusEmbConsts mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
bver committed Sep 15, 2011
1 parent 24de953 commit 0b7aacc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
17 changes: 17 additions & 0 deletions lib/mapper.rb
Expand Up @@ -18,6 +18,7 @@ module Mapper
class DepthFirst < Generator
include LocusFirst
include ExtendAll #behavior same as ExtendDepth, but simpler
include ConstantsNoSupport
end

# Mapper class employing the breath-first node expansion strategy:
Expand All @@ -31,6 +32,7 @@ class DepthFirst < Generator
class BreadthFirst < Generator
include LocusFirst
include ExtendBreadth
include ConstantsNoSupport
end

###
Expand All @@ -50,8 +52,21 @@ class BreadthFirst < Generator
class DepthLocus < Generator
include LocusGenetic
include ExtendDepth
include ConstantsNoSupport
end

# Mapper class employing the depth-locus node expansion strategy with embedded constants support
# The expansion strategy is same as in Mapper::DepthLocus.
# For embedded constants technique see:
# http://dl.acm.org/citation.cfm?id=2001966
#
class DepthLocusEmbConsts < Generator
include LocusGenetic
include ExtendAll #behavior same as ExtendDepth, but simpler
include ConstantsInGenotype
end


# Mapper class employing the breadth-locus node expansion strategy:
# 1. Create the list L of the all unresolved nodes (nonterminal symbols ready for the expansion).
# 2. Select only the nodes with the minimal depth from the list L and name it M.
Expand All @@ -67,6 +82,7 @@ class DepthLocus < Generator
class BreadthLocus < Generator
include LocusGenetic
include ExtendBreadth
include ConstantsNoSupport
end

# Mapper class employing the all-locus node expansion strategy:
Expand All @@ -80,6 +96,7 @@ class BreadthLocus < Generator
class AllLocus < Generator
include LocusGenetic
include ExtendAll
include ConstantsNoSupport
end

end # Mapper
Expand Down
13 changes: 11 additions & 2 deletions lib/mapper_base.rb
@@ -1,6 +1,7 @@

require 'lib/grammar'
require 'lib/codon_mod'
require 'lib/mapper_constants'

module Mapper

Expand Down Expand Up @@ -173,8 +174,10 @@ def pick_rule( symbol_token, genome )
faded_index = read_genome( genome, rule.size )

alt_index = @codon.interpret( rule.size, faded_index, symbol_token.data )
alt = rule.at alt_index
return use_expansion( symbol_token, alt.deep_copy )

expansion = rule.at(alt_index).deep_copy
modify_expansion_base( expansion, genome )
return use_expansion( symbol_token, expansion )
end

def find_nonterminals_by_depth( tokens, depth )
Expand Down Expand Up @@ -229,4 +232,10 @@ def find_nonterminals tokens
end
end

module ConstantsNoSupport
def modify_expansion_base( exp, genome )
exp
end
end

end # Mapper
5 changes: 2 additions & 3 deletions lib/mapper_constants.rb
Expand Up @@ -70,7 +70,7 @@ def modify_expansion_base( exp, genome )
index += @codon.raw_read genome.at(position)
end

token.data = found.mapping(index)
token.data = found.mapping(index).to_s
end
end

Expand All @@ -89,11 +89,10 @@ def modify_expansion_generate( exp, genome )
genome.push value
end

token.data = found.mapping(index)
token.data = found.mapping(index).to_s
end
end

end

end # Mapper

10 changes: 9 additions & 1 deletion lib/mapper_generator.rb
Expand Up @@ -101,7 +101,9 @@ def generate_rule( recurs, symbol_token, genome, allowed_depth )
else
alt = rule.first
end
return use_expansion( symbol_token, alt.deep_copy )
expansion = alt.deep_copy
modify_expansion_generate( expansion, genome )
return use_expansion( symbol_token, expansion )
end

def filter_expansions_by_depth( rule, allowed_depth )
Expand Down Expand Up @@ -132,5 +134,11 @@ def generate_locus( selected_indices, genome )
selected_indices.at index
end
end

module ConstantsNoSupport
def modify_expansion_generate( exp, genome )
exp
end
end

end # Mapper
23 changes: 23 additions & 0 deletions test/tc_generators.rb
Expand Up @@ -383,5 +383,28 @@ def test_codon_interface
assert_equal( 7, m.codon.bit_size )
end

def test_embedded_constants
m = Mapper::DepthLocusEmbConsts.new @grammar

r = MockRand.new [{1=>0},0,{3=>2},0, {3=>2},0,{3=>1},0, {2=>1},0,{2=>1},0, {1=>0},0,{3=>0},0]
m.random = r
gen = [0,2, 2,1, 1,1, 0,0]

# inactive mode first
assert_equal( gen, m.generate_grow( 5 ) )
assert_equal( '(x*y)', m.phenotype(gen) )

# set replacing of a 'y' literal:
m.embedded_constants = {"y"=>{"codons"=>2, "min"=>0, "max"=>42}}
assert_equal( 8, m.codon.bit_size )
r = MockRand.new [{1=>0},0,{3=>2},0, {3=>2},0,{3=>1},0, {256=>255}, {256=>255}, {2=>1},0,{2=>1},0, {1=>0},0,{3=>0},0]
m.random = r
gen = [0,2, 2,1, 255, 255, 1,1, 0,0]

assert_equal( gen, m.generate_grow( 5 ) )
assert_equal( '(x*42)', m.phenotype(gen) )
end


end

20 changes: 10 additions & 10 deletions test/tc_mapper_constants.rb
Expand Up @@ -75,7 +75,7 @@ def test_base
assert_equal( 1, mapper.used_length )
assert_equal( TestExpansion[0], ext[0] )
assert_equal( TestExpansion[1], ext[1] )
assert_equal( -1.5, ext[2].data )
assert_equal( '-1.5', ext[2].data )
assert_equal( :literal, ext[2].type )
assert_equal( TestExpansion[3], ext[3] )
assert_equal( TestExpansion[4], ext[4] )
Expand Down Expand Up @@ -116,12 +116,12 @@ def test_two_consts

mapper.modify_expansion_base( ext, genome )
assert_equal( TestExpansion[0], ext[0] )
assert_equal( 51002, ext[1].data )
assert_equal( 0.5, ext[2].data )
assert_equal( '51002', ext[1].data )
assert_equal( '0.5', ext[2].data )
assert_equal( TestExpansion[3], ext[3] )
assert_equal( TestExpansion[4], ext[4] )
assert_equal( TestExpansion[5], ext[5] )
assert_equal( 21002, ext[6].data )
assert_equal( '21002', ext[6].data )

assert_equal( 5, mapper.used_length )
end
Expand Down Expand Up @@ -152,12 +152,12 @@ def test_genome_wrapping

mapper.modify_expansion_base( ext, genome )
assert_equal( TestExpansion[0], ext[0] )
assert_equal( 51002, ext[1].data )
assert_equal( 0.5, ext[2].data )
assert_equal( '51002', ext[1].data )
assert_equal( '0.5', ext[2].data )
assert_equal( TestExpansion[3], ext[3] )
assert_equal( TestExpansion[4], ext[4] )
assert_equal( TestExpansion[5], ext[5] )
assert_equal( 30002, ext[6].data )
assert_equal( '30002', ext[6].data )

assert_equal( 5, mapper.used_length ) #wrapped: 5 % genome.size
end
Expand Down Expand Up @@ -187,12 +187,12 @@ def test_generating
mapper.modify_expansion_generate( ext, genome )

assert_equal( TestExpansion[0], ext[0] )
assert_equal( 51002, ext[1].data )
assert_equal( 0.5, ext[2].data )
assert_equal( '51002', ext[1].data )
assert_equal( '0.5', ext[2].data )
assert_equal( TestExpansion[3], ext[3] )
assert_equal( TestExpansion[4], ext[4] )
assert_equal( TestExpansion[5], ext[5] )
assert_equal( 21002, ext[6].data )
assert_equal( '21002', ext[6].data )

assert_equal( genome_expect, genome )
assert_equal( [42,4,2], mapper.codon.seq )
Expand Down

0 comments on commit 0b7aacc

Please sign in to comment.