Class: Evolvable::Mutation
- Inherits:
-
Object
- Object
- Evolvable::Mutation
- Extended by:
- Forwardable
- Defined in:
- lib/evolvable/mutation.rb
Overview
Mutation introduces genetic variation by randomly replacing genes with new ones. This helps the population explore new areas of the solution space and prevents premature convergence on suboptimal solutions.
Mutation is controlled by two key parameters:
- probability: Likelihood that an individual will undergo mutation (range: 0.0–1.0)
- rate: Fraction of genes to mutate within those individuals (range: 0.0–1.0)
A typical strategy is to start with higher mutation to encourage exploration:
population = MyEvolvable.new_population(
mutation: { probability: 0.4, rate: 0.2 }
)
Then later reduce the mutation rate to focus on refinement and convergence:
population.mutation_probability = 0.1
population.mutation_rate = 0.05
Constant Summary collapse
- DEFAULT_PROBABILITY =
The default probability of mutation (3%). This is used when no probability is specified.
0.03
Instance Attribute Summary collapse
-
#probability ⇒ Float
The probability that an evolvable instance will undergo mutation.
-
#rate ⇒ Float?
The rate at which genes mutate within an instance.
Instance Method Summary collapse
-
#call(population) ⇒ Evolvable::Population
Applies mutations to the population's evolvables based on the configured probability and rate.
-
#initialize(probability: nil, rate: nil) ⇒ Mutation
constructor
Initializes a new mutation object.
-
#mutate_evolvables(evolvables) ⇒ Array<Evolvable>
Mutates a collection of evolvable instances based on the mutation probability and rate.
Constructor Details
#initialize(probability: nil, rate: nil) ⇒ Mutation
Initializes a new mutation object.
When rate is specified but probability isn't, probability defaults to 1.0. When rate is 0 or not specified, only one gene is mutated per affected instance.
60 61 62 63 |
# File 'lib/evolvable/mutation.rb', line 60 def initialize(probability: nil, rate: nil) @probability = probability || (rate ? 1 : DEFAULT_PROBABILITY) @rate = rate end |
Instance Attribute Details
#probability ⇒ Float
The probability that an evolvable instance will undergo mutation. Value between 0.0 (never) and 1.0 (always).
71 72 73 |
# File 'lib/evolvable/mutation.rb', line 71 def probability @probability end |
#rate ⇒ Float?
The rate at which genes mutate within an instance. Value between 0.0 (no genes mutate) and 1.0 (all genes likely to mutate). When nil, exactly one random gene is mutated per instance.
80 81 82 |
# File 'lib/evolvable/mutation.rb', line 80 def rate @rate end |
Instance Method Details
#call(population) ⇒ Evolvable::Population
Applies mutations to the population's evolvables based on the configured probability and rate.
89 90 91 92 |
# File 'lib/evolvable/mutation.rb', line 89 def call(population) mutate_evolvables(population.evolvables) unless probability.zero? population end |
#mutate_evolvables(evolvables) ⇒ Array<Evolvable>
Mutates a collection of evolvable instances based on the mutation probability and rate.
101 102 103 104 105 106 107 |
# File 'lib/evolvable/mutation.rb', line 101 def mutate_evolvables(evolvables) evolvables.each do |evolvable| next unless rand <= probability evolvable.genome.each { |_key, config| mutate_genes(config[:genes]) } end end |