Class: Evolvable::Mutation

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Examples:

Basic initialization patterns

# Default: 3% of instances get one mutated gene
Evolvable::Mutation.new

# 50% of instances get one mutated gene
Evolvable::Mutation.new(probability: 0.5)

# All instances are considered, with 3% of genes mutating in each
Evolvable::Mutation.new(rate: 0.03)

# 30% of instances have 3% of their genes mutated
Evolvable::Mutation.new(probability: 0.3, rate: 0.03)

Parameters:

  • probability (Float, nil) (defaults to: nil)

    Chance of an instance being mutated (0.0-1.0)

  • rate (Float, nil) (defaults to: nil)

    Chance of each gene mutating when an instance is selected (0.0-1.0)



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

#probabilityFloat

The probability that an evolvable instance will undergo mutation. Value between 0.0 (never) and 1.0 (always).

Returns:

  • (Float)

    The mutation probability



71
72
73
# File 'lib/evolvable/mutation.rb', line 71

def probability
  @probability
end

#rateFloat?

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.

Returns:

  • (Float, nil)

    The mutation rate



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.

Parameters:

Returns:



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.

Parameters:

  • evolvables (Array<Evolvable>)

    The collection of evolvable instances to potentially mutate

Returns:

  • (Array<Evolvable>)

    The potentially mutated evolvables



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