Module: Evolvable::Gene
- Included in:
- CountGene, RigidCountGene
- Defined in:
- lib/evolvable/gene.rb
Overview
Genes are the building blocks of evolvable objects, encapsulating individual characteristics that can be combined and mutated during evolution. Each gene represents a trait or behavior that can influence an evolvable's performance.
To define a gene class:
- Include the
Evolvable::Gene
module - Define how the gene's value is determined
class BehaviorGene
include Evolvable::Gene
def value
@value ||= %w[explore gather attack defend build].sample
end
end
Then use it in an evolvable class:
class Robot
include Evolvable
gene :behaviors, type: BehaviorGene, count: 3..5
gene :speed, type: SpeedGene, count: 1
def fitness
run_simulation(behaviors: behaviors.map(&:value), speed: speed.value)
end
end
Gene Count
You can control how many copies of a gene are created using the count:
parameter:
count: 1
(default) creates a single instance.- A numeric value (e.g.
count: 5
) creates a fixed number of genes usingRigidCountGene
. - A range (e.g.
count: 2..8
) creates a variable number of genes usingCountGene
, allowing the count to evolve over time.
Evolves melody length:
gene :notes, type: NoteGene, count: 4..12
Custom Combination
By default, the combine
method randomly picks one of the two parent genes.
A gene class can implement custom behavior by overriding .combine
.
class SpeedGene
include Evolvable::Gene
def self.combine(gene_a, gene_b)
new_gene = new
new_gene.value = (gene_a.value + gene_b.value) / 2
new_gene
end
attr_writer :value
def value
@value ||= rand(1..100)
end
end
Design Patterns
Effective gene design typically follows these principles:
- Immutability: Cache values after initial sampling (e.g.,
@value ||= ...
) - Self-Contained: Genes should encapsulate their logic and state
- Composable: You can build complex structures using multiple genes or clusters
- Domain-Specific: Genes should map directly to your problem’s traits or features
Genes come in various types, each representing different aspects of a solution. Common examples include numeric genes for quantities, selection genes for choices from sets, boolean genes for binary decisions, structural genes for architecture, and parameter genes for configuration settings.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#evolvable ⇒ Evolvable
The evolvable instance that this gene belongs to.
Class Method Summary collapse
-
.included(base) ⇒ Object
When included in a class, extends the class with ClassMethods.
Instance Method Summary collapse
-
#key ⇒ Symbol
Returns the unique key for this gene instance.
Instance Attribute Details
#evolvable ⇒ Evolvable
The evolvable instance that this gene belongs to. Used for accessing other genes or evolvable properties.
152 153 154 |
# File 'lib/evolvable/gene.rb', line 152 def evolvable @evolvable end |
Class Method Details
.included(base) ⇒ Object
When included in a class, extends the class with ClassMethods. Gene classes should include this module to participate in the evolutionary process.
103 104 105 |
# File 'lib/evolvable/gene.rb', line 103 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#key ⇒ Symbol
Returns the unique key for this gene instance. Delegates to the class-level key.
160 161 162 |
# File 'lib/evolvable/gene.rb', line 160 def key self.class.key end |