Class: Evolvable::CountGene

Inherits:
Object
  • Object
show all
Includes:
Gene
Defined in:
lib/evolvable/count_gene.rb

Constant Summary collapse

LAMBDAS =
[->(a, b) { [a, b].sample.count + rand(-1..1) },
->(a, b) { a.count + b.count / 2 }].freeze

Instance Attribute Summary collapse

Attributes included from Gene

#evolvable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gene

included, #key

Constructor Details

#initialize(range:, count: nil) ⇒ CountGene

Returns a new instance of CountGene.


23
24
25
26
# File 'lib/evolvable/count_gene.rb', line 23

def initialize(range:, count: nil)
  @range = range
  @count = count
end

Instance Attribute Details

#rangeObject (readonly)

Returns the value of attribute range.


28
29
30
# File 'lib/evolvable/count_gene.rb', line 28

def range
  @range
end

Class Method Details

.combinationObject


18
19
20
# File 'lib/evolvable/count_gene.rb', line 18

def combination
  LAMBDAS.sample
end

.combine(gene_a, gene_b) ⇒ Object


8
9
10
11
12
13
# File 'lib/evolvable/count_gene.rb', line 8

def combine(gene_a, gene_b)
  min = gene_a.min_count
  max = gene_a.max_count
  count = combination.call(gene_a, gene_b).clamp(min, max)
  new(range: gene_a.range, count: count)
end

Instance Method Details

#countObject


30
31
32
# File 'lib/evolvable/count_gene.rb', line 30

def count
  @count ||= rand(@range)
end

#max_countObject


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

def max_count
  @range.max
end

#min_countObject


34
35
36
# File 'lib/evolvable/count_gene.rb', line 34

def min_count
  @range.min
end