Class: Evolvable::CountGene
- Inherits:
-
Object
- Object
- Evolvable::CountGene
- Includes:
- Gene
- Defined in:
- lib/evolvable/count_gene.rb
Overview
The CountGene class handles the dynamic count of genes in evolvable instances.
When a gene is defined with a range for count:
(e.g., count: 2..8
), a CountGene
is created to manage this count, allowing the number of genes to change over
successive generations.
Constant Summary collapse
- LAMBDAS =
Available strategies for combining count genes. These lambdas determine how two count genes are merged during evolution.
- Random selection with slight mutation (-1, 0, or +1)
- Average of the two counts
[->(a, b) { [a, b].sample.count + rand(-1..1) }, ->(a, b) { a.count + b.count / 2 }].freeze
Instance Attribute Summary collapse
-
#range ⇒ Range
readonly
The valid range for the count value.
Attributes included from Gene
Class Method Summary collapse
-
.combination ⇒ Proc
Selects a random combination strategy from LAMBDAS.
-
.combine(gene_a, gene_b) ⇒ CountGene
Combines two count genes to produce a new count gene.
Instance Method Summary collapse
-
#count ⇒ Integer
Returns the current count value.
-
#initialize(range:, count: nil) ⇒ CountGene
constructor
Initializes a new CountGene instance.
-
#max_count ⇒ Integer
The maximum allowed count value.
-
#min_count ⇒ Integer
The minimum allowed count value.
Methods included from Gene
Constructor Details
#initialize(range:, count: nil) ⇒ CountGene
Initializes a new CountGene instance.
67 68 69 70 |
# File 'lib/evolvable/count_gene.rb', line 67 def initialize(range:, count: nil) @range = range @count = count end |
Instance Attribute Details
#range ⇒ Range (readonly)
The valid range for the count value.
77 78 79 |
# File 'lib/evolvable/count_gene.rb', line 77 def range @range end |
Class Method Details
.combination ⇒ Proc
Selects a random combination strategy from LAMBDAS.
56 57 58 |
# File 'lib/evolvable/count_gene.rb', line 56 def combination LAMBDAS.sample end |
.combine(gene_a, gene_b) ⇒ CountGene
Combines two count genes to produce a new count gene. The combination strategy is randomly selected from LAMBDAS.
34 35 36 37 38 39 |
# File 'lib/evolvable/count_gene.rb', line 34 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 ⇒ Integer
Returns the current count value. If not already set, randomly selects a value from the range.
85 86 87 |
# File 'lib/evolvable/count_gene.rb', line 85 def count @count ||= rand(@range) end |
#max_count ⇒ Integer
The maximum allowed count value.
103 104 105 |
# File 'lib/evolvable/count_gene.rb', line 103 def max_count @range.max end |
#min_count ⇒ Integer
The minimum allowed count value.
94 95 96 |
# File 'lib/evolvable/count_gene.rb', line 94 def min_count @range.min end |