class Population

Attributes

pick_strategy[RW]
replace_strategy[RW]
selection_rate[RW]
solutions[R]

Public Class Methods

new(solutions = nil) click to toggle source
# File lib/gimuby/genetic/population/population.rb, line 7
def initialize(solutions = nil)
  if solutions.nil?
    @solutions = []
  else
    @solutions = solutions
  end
  @pick_strategy ||= RandomWheelPickStrategy.new
  @replace_strategy ||= ReplaceWorstReplaceStrategy.new

  trigger(:on_population_init)
end

Public Instance Methods

add_solution(solution) click to toggle source

Add a solution @param solution [Solution]

# File lib/gimuby/genetic/population/population.rb, line 53
def add_solution(solution)
  @solutions.push solution
end
generation_step() click to toggle source

Run a step of genetic algorithm: reproduction + mutation

# File lib/gimuby/genetic/population/population.rb, line 25
def generation_step
  reproduce
  @solutions.each do |solution|
    solution.mutate
  end
  trigger(:on_population_generation_step)
end
get_average_fitness() click to toggle source
# File lib/gimuby/genetic/population/population.rb, line 66
def get_average_fitness
  sum = 0
  @solutions.each do |solution|
    sum += solution.get_fitness
  end
  # Beware of that division by 0
  sum / @solutions.length
end
get_best_fitness() click to toggle source
# File lib/gimuby/genetic/population/population.rb, line 75
def get_best_fitness
  best_solution = get_best_solution
  best_solution.get_fitness
end
get_best_solution() click to toggle source
# File lib/gimuby/genetic/population/population.rb, line 80
def get_best_solution
  best_solution = @solutions.min_by do |solution|
    solution.get_fitness
  end
  best_solution
end
get_fitness(solution) click to toggle source

Can be overridden

# File lib/gimuby/genetic/population/population.rb, line 62
def get_fitness(solution)
  solution.get_fitness
end
get_population_size() click to toggle source
# File lib/gimuby/genetic/population/population.rb, line 57
def get_population_size
  @solutions.size
end
pick() click to toggle source

Pick some solutions @internal

# File lib/gimuby/genetic/population/population.rb, line 47
def pick
  @pick_strategy.pick(self)
end
replace(solutions) click to toggle source

Replace part of the population (used by {Archipelago}) @internal

# File lib/gimuby/genetic/population/population.rb, line 35
def replace(solutions)
  @solutions = @replace_strategy.replace(self, solutions)
end
reproduce() click to toggle source

Simply pick some solutions and make them reproduce @internal

# File lib/gimuby/genetic/population/population.rb, line 41
def reproduce
  @solutions = @replace_strategy.replace(self)
end

Protected Instance Methods

get_event_manager() click to toggle source
# File lib/gimuby/genetic/population/population.rb, line 94
def get_event_manager
  $dependencies.event_manager
end
trigger(event_type) click to toggle source
# File lib/gimuby/genetic/population/population.rb, line 89
def trigger(event_type)
  event_data = {:population => self}
  get_event_manager.trigger_event(event_type, event_data)
end