class SolutionSpaceMutationStrategy

Public Class Methods

new(mutation_rate = 0.01) click to toggle source
Calls superclass method MutationStrategy::new
# File lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb, line 5
def initialize(mutation_rate = 0.01)
  super(mutation_rate)
  @default_min = nil
  @default_max = nil
  @mins = {}
  @maxs = {}
end

Public Instance Methods

perform_mutation(solution) click to toggle source
# File lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb, line 13
def perform_mutation(solution)
  x_values = solution.get_solution_representation
  x_values = perform_mutation_from_representation(x_values)
  solution.set_solution_representation(x_values)
end
set_max(max, index = nil) click to toggle source
# File lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb, line 27
def set_max(max, index = nil)
  if index.nil?
    @default_max = max
  else
    @maxs[index] = max
  end
end
set_min(min, index = nil) click to toggle source
# File lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb, line 19
def set_min(min, index = nil)
  if index.nil?
    @default_min = min
  else
    @mins[index] = min
  end
end

Protected Instance Methods

get_max(index) click to toggle source
# File lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb, line 61
def get_max(index)
  max = @default_max
  if @maxs.has_key?(index)
    max = @maxs[index]
  end
  max
end
get_min(index) click to toggle source
# File lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb, line 53
def get_min(index)
  min = @default_min
  if @mins.has_key?(index)
    min = @mins[index]
  end
  min
end
perform_mutation_from_representation(x_values) click to toggle source
# File lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb, line 37
def perform_mutation_from_representation(x_values)
  index = rand(x_values.length)

  x_value = x_values[index]
  if x_value.class == Array
    x_value = perform_mutation_from_representation(x_value)
  else
    min = get_min(index)
    max = get_max(index)
    range = max - min
    x_value = rand() * range + min
  end
  x_values[index] = x_value
  x_values
end