Class: Evolvable::Mutation
- Inherits:
-
Object
- Object
- Evolvable::Mutation
- Extended by:
- Forwardable
- Defined in:
- lib/evolvable/mutation.rb
Overview
Mutation serves the role of increasing genetic variation. When an evolvable undergoes a mutation, one or more of its genes are replaced by newly initialized ones. In effect, a gene mutation invokes a new random outcome from the genetic search space.
Mutation frequency is configurable using the probability
and rate
parameters.
Constant Summary collapse
- DEFAULT_PROBABILITY =
0.03
Instance Attribute Summary collapse
-
#probability ⇒ Object
Returns the value of attribute probability.
-
#rate ⇒ Object
Returns the value of attribute rate.
Instance Method Summary collapse
- #call(population) ⇒ Object
-
#initialize(probability: nil, rate: nil) ⇒ Mutation
constructor
Initializes a new mutation object.
- #mutate_evolvables(evolvables) ⇒ Object
Constructor Details
#initialize(probability: nil, rate: nil) ⇒ Mutation
Initializes a new mutation object.
Keyword arguments:
probability
The probability that a particular instance undergoes a mutation.
By default, the probability is 0.03 which translates to 3%.
If initialized with a rate
, the probability will be 1 which
means all genes can undergo mutation, but actual gene mutations
will be subject to the given mutation rate.
rate
the rate at which individual genes mutate. The default rate is 0 which,
when combined with a non-zero probability
(the default), means that
one gene for each instance that undergoes mutation will change.
If a rate is given, but no probability
is given, then the probability
will bet set to 1 which always defers to the mutation rate.
To summarize, the probability
represents the chance of mutation on
the instance level and the rate
represents the chance on the gene level.
The probability
and rate
can be any number from 0 to 1. When the
probability
is 0, no mutation will ever happen. When the probability
is not 0 but the rate is 0, then any instance that undergoes mutation
will only receive one mutant gene. If the rate is not 0, then if an
instance has been chosen to undergo mutation, each of its genes will
mutate with a probability as defined by the rate
.
Custom mutation objects must implement the #call
method which accepts the population as the first object.
57 58 59 60 |
# File 'lib/evolvable/mutation.rb', line 57 def initialize(probability: nil, rate: nil) @probability = probability || (rate ? 1 : DEFAULT_PROBABILITY) @rate = rate end |
Instance Attribute Details
#probability ⇒ Object
Returns the value of attribute probability.
62 63 64 |
# File 'lib/evolvable/mutation.rb', line 62 def probability @probability end |
#rate ⇒ Object
Returns the value of attribute rate.
62 63 64 |
# File 'lib/evolvable/mutation.rb', line 62 def rate @rate end |
Instance Method Details
#call(population) ⇒ Object
65 66 67 68 |
# File 'lib/evolvable/mutation.rb', line 65 def call(population) mutate_evolvables(population.evolvables) unless probability.zero? population end |
#mutate_evolvables(evolvables) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/evolvable/mutation.rb', line 70 def mutate_evolvables(evolvables) evolvables.each do |evolvable| next unless rand <= probability evolvable.genome.each { |_key, config| mutate_genes(config[:genes]) } end end |