Class: EvoSynth::Mutations::ExchangeMutation
- Inherits:
-
Object
- Object
- EvoSynth::Mutations::ExchangeMutation
- Defined in:
- lib/evosynth/operators/mutations/exchange_mutation.rb
Constant Summary collapse
- DEFAULT_SWAP_COUNT =
2
Instance Attribute Summary collapse
-
#swap_count ⇒ Object
The number of genes that will be swapped during this mutation (default is 2).
Instance Method Summary collapse
-
#initialize(swap_count = DEFAULT_SWAP_COUNT) ⇒ ExchangeMutation
constructor
:call-seq: ExchangeMutation.new ExchangeMutation(Fixnum) -> ExchangeMutation (overrides default swap count).
-
#mutate(individual) ⇒ Object
:call-seq: mutate(Individual) -> Individual.
-
#to_s ⇒ Object
:call-seq: to_s -> string.
Constructor Details
#initialize(swap_count = DEFAULT_SWAP_COUNT) ⇒ ExchangeMutation
:call-seq: ExchangeMutation.new ExchangeMutation(Fixnum) -> ExchangeMutation (overrides default swap count)
Returns a new ExchangeMutation. In the first form, the default swap count is used. In the second it creates a ExchangeMutation with the given swap count.
ExchangeMutation.new
ExchangeMutation.new(3)
52 53 54 |
# File 'lib/evosynth/operators/mutations/exchange_mutation.rb', line 52 def initialize(swap_count = DEFAULT_SWAP_COUNT) @swap_count = swap_count end |
Instance Attribute Details
#swap_count ⇒ Object
The number of genes that will be swapped during this mutation (default is 2).
38 39 40 |
# File 'lib/evosynth/operators/mutations/exchange_mutation.rb', line 38 def swap_count @swap_count end |
Instance Method Details
#mutate(individual) ⇒ Object
:call-seq: mutate(Individual) -> Individual
Returns the mutation of a given individual.
m = ExchangeMutation.new
m.mutate(a_individual) #=> a_new_individual
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/evosynth/operators/mutations/exchange_mutation.rb', line 64 def mutate(individual) mutated = individual.deep_clone genome = mutated.genome indexes = rand_indexes(genome.size) (indexes.size - 1).times do |index| index_one = indexes[index] index_two = indexes[index + 1] index_two = indexes[0] if index_two.nil? genome[index_one], genome[index_two] = genome[index_two], genome[index_one] end mutated end |
#to_s ⇒ Object
:call-seq: to_s -> string
Returns description of this mutation
m = ExchangeMutation.new
m.to_s #=> "exchange mutation"
89 90 91 |
# File 'lib/evosynth/operators/mutations/exchange_mutation.rb', line 89 def to_s "exchange mutation <swap count: #{@swap_count}>" end |