Class: Evolvable::CountGene
- Inherits:
-
Object
- Object
- Evolvable::CountGene
- 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
-
#range ⇒ Object
readonly
Returns the value of attribute range.
Attributes included from Gene
Class Method Summary collapse
Instance Method Summary collapse
- #count ⇒ Object
-
#initialize(range:, count: nil) ⇒ CountGene
constructor
A new instance of CountGene.
- #max_count ⇒ Object
- #min_count ⇒ Object
Methods included from Gene
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
#range ⇒ Object (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
.combination ⇒ Object
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
#count ⇒ Object
30 31 32 |
# File 'lib/evolvable/count_gene.rb', line 30 def count @count ||= rand(@range) end |
#max_count ⇒ Object
38 39 40 |
# File 'lib/evolvable/count_gene.rb', line 38 def max_count @range.max end |
#min_count ⇒ Object
34 35 36 |
# File 'lib/evolvable/count_gene.rb', line 34 def min_count @range.min end |