Class: Gene
- Inherits:
-
Object
- Object
- Gene
- Defined in:
- lib/copycats/gene.rb
Overview
todo/check:
find a better name for Gene (incl.4 genes)
e.g. GeneFour, Gene4, GeneGroup, GeneSlice,TraitGenes,... -why? why not?
Instance Attribute Summary collapse
-
#d ⇒ Object
readonly
Returns the value of attribute d.
-
#r1 ⇒ Object
readonly
Returns the value of attribute r1.
-
#r2 ⇒ Object
readonly
Returns the value of attribute r2.
-
#r3 ⇒ Object
readonly
Returns the value of attribute r3.
Instance Method Summary collapse
-
#initialize(arg) ⇒ Gene
constructor
d (dominant gene) – todo/check: rename to just d instead of d0 - why? why not? r1 (1st order recessive gene) r2 (2nd order recessive gene) r3 (3rd order recessive gene).
- #mix(other) ⇒ Object
- #mix_inner(other) ⇒ Object
- #mutate(other) ⇒ Object
- #swap ⇒ Object
-
#to_kai ⇒ Object
return a string in kai/base32 notation.
Constructor Details
#initialize(arg) ⇒ Gene
d (dominant gene) – todo/check: rename to just d instead of d0 - why? why not? r1 (1st order recessive gene) r2 (2nd order recessive gene) r3 (3rd order recessive gene)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/copycats/gene.rb', line 16 def initialize( arg ) ## (always) assume String in base32/kai for now kai = arg ## puts "Gene.initialize #{kai}" kai = kai.reverse @d = kai[0] @r1 = kai[1] @r2 = kai[2] @r3 = kai[3] end |
Instance Attribute Details
#d ⇒ Object (readonly)
Returns the value of attribute d.
10 11 12 |
# File 'lib/copycats/gene.rb', line 10 def d @d end |
#r1 ⇒ Object (readonly)
Returns the value of attribute r1.
10 11 12 |
# File 'lib/copycats/gene.rb', line 10 def r1 @r1 end |
#r2 ⇒ Object (readonly)
Returns the value of attribute r2.
10 11 12 |
# File 'lib/copycats/gene.rb', line 10 def r2 @r2 end |
#r3 ⇒ Object (readonly)
Returns the value of attribute r3.
10 11 12 |
# File 'lib/copycats/gene.rb', line 10 def r3 @r3 end |
Instance Method Details
#mix(other) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/copycats/gene.rb', line 82 def mix( other ) puts "Gene#mix" self_swapped = swap other_swapped = other.swap gene = self_swapped.mix_inner( other_swapped ) pp gene gene end |
#mix_inner(other) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/copycats/gene.rb', line 64 def mix_inner( other ) puts "Gene#mix_inner" new_d = mutate( other ) if new_d.nil? ## no mutation of gene.d - use "regular" formula new_d = Lottery.rand(100) < 50 ? d : other.d end new_r1 = Lottery.rand(100) < 50 ? r1 : other.r1 new_r2 = Lottery.rand(100) < 50 ? r2 : other.r2 new_r3 = Lottery.rand(100) < 50 ? r3 : other.r3 gene = Gene.new( new_r3 + new_r2 + new_r1 + new_d ) pp gene gene end |
#mutate(other) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/copycats/gene.rb', line 43 def mutate( other ) puts "Gene#mutate" gene1 = Kai::NUMBER[d] ## dominant gene1 gene2 = Kai::NUMBER[other.d] ## dominant gene2 if gene1 > gene2 gene1, gene2 = gene2, gene1 ## make sure gene2 is always bigger end if (gene2 - gene1) == 1 && gene1.even? probability = 25 probability /= 2 if gene1 > 23 if Lottery.rand(100) < probability genex = (gene1 / 2) + 16 ## 16=2^4 puts " bingo! mutation #{gene2}+#{gene1} => #{genex}" puts " #{Kai::ALPHABET[gene2]}+#{Kai::ALPHABET[gene1]} => #{Kai::ALPHABET[genex]}" return Kai::ALPHABET[genex] end end nil # no mutation end |
#swap ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/copycats/gene.rb', line 29 def swap puts "Gene#swap" kai = to_kai.reverse # note: use reverse kai string (kai[0] is first char/digit/letter) 3.downto(1) do |i| if Lottery.rand(100) < 25 puts " bingo! swap #{i}<>#{i-1}" kai[i-1], kai[i] = kai[i], kai[i-1] end end Gene.new( kai.reverse ) ## note: do NOT forget to pass in kai (unreversed) end |
#to_kai ⇒ Object
return a string in kai/base32 notation
27 |
# File 'lib/copycats/gene.rb', line 27 def to_kai() @r3 + @r2 + @r1 + @d; end |