Skip to content

Commit

Permalink
generating part implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
bver committed Sep 15, 2011
1 parent 27e37cb commit 24de953
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
20 changes: 20 additions & 0 deletions lib/mapper_constants.rb
Expand Up @@ -74,6 +74,26 @@ def modify_expansion_base( exp, genome )
end
end

def modify_expansion_generate( exp, genome )
return unless defined? @embedded
exp.each do |token|
next unless token.type == :literal
found = @embedded.fetch( token.data, nil )
next if found.nil?

index = 0
found.codons.times do
index <<= @codon.bit_size
value = @codon.rand_gen
index += value
genome.push value
end

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

end

end # Mapper

48 changes: 44 additions & 4 deletions test/tc_mapper_constants.rb
Expand Up @@ -6,7 +6,9 @@
require 'lib/mapper_constants'

class ConstCodonMock
def initalize seq
attr :seq

def initialize seq
@seq = seq
end

Expand All @@ -25,11 +27,13 @@ def bit_size

class ConstantsTest
include Mapper::ConstantsInGenotype
def initialize

attr :used_length, :codon

def initialize seq=[]
@used_length = 0
@codon = ConstCodonMock.new
@codon = ConstCodonMock.new( seq )
end
attr :used_length
end

class TC_MapperConstants < Test::Unit::TestCase
Expand Down Expand Up @@ -158,6 +162,42 @@ def test_genome_wrapping
assert_equal( 5, mapper.used_length ) #wrapped: 5 % genome.size
end

def test_generating
#_con2 #const1 #_con2
genome_expect = [6,3, 6, 2,5 ]
sequence = genome_expect.clone + [42,4,2]

mapper = ConstantsTest.new sequence
ext = TestExpansion.map { |t| t.clone }
genome = []

# inactive mode
mapper.modify_expansion_generate( ext, genome )

assert_equal( [], genome )
assert_equal( TestExpansion, ext )
assert_equal( genome_expect.size+3, mapper.codon.seq.size )

# set some constants now
mapper.embedded_constants = {
"const1"=>{ "min"=>-2.5, "max"=>1 },
"_con2"=>{"codons"=>2, "min"=>2, "max"=>63002}
}

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( TestExpansion[3], ext[3] )
assert_equal( TestExpansion[4], ext[4] )
assert_equal( TestExpansion[5], ext[5] )
assert_equal( 21002, ext[6].data )

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

end


Expand Down

0 comments on commit 24de953

Please sign in to comment.