Class: Population

Inherits:
Object
  • Object
show all
Defined in:
lib/gimuby/genetic/population/population.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(solutions = nil) ⇒ Population

Returns a new instance of Population.



7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#pick_strategyObject

Returns the value of attribute pick_strategy.



20
21
22
# File 'lib/gimuby/genetic/population/population.rb', line 20

def pick_strategy
  @pick_strategy
end

#replace_strategyObject

Returns the value of attribute replace_strategy.



22
23
24
# File 'lib/gimuby/genetic/population/population.rb', line 22

def replace_strategy
  @replace_strategy
end

#selection_rateObject

Returns the value of attribute selection_rate.



21
22
23
# File 'lib/gimuby/genetic/population/population.rb', line 21

def selection_rate
  @selection_rate
end

#solutionsObject (readonly)

Returns the value of attribute solutions.



19
20
21
# File 'lib/gimuby/genetic/population/population.rb', line 19

def solutions
  @solutions
end

Instance Method Details

#add_solution(solution) ⇒ Object

Add a solution

Parameters:



53
54
55
# File 'lib/gimuby/genetic/population/population.rb', line 53

def add_solution(solution)
  @solutions.push solution
end

#generation_stepObject

Run a step of genetic algorithm: reproduction + mutation



25
26
27
28
29
30
31
# 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_fitnessObject



66
67
68
69
70
71
72
73
# 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_fitnessObject



75
76
77
78
# 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_solutionObject



80
81
82
83
84
85
# 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) ⇒ Object

Can be overridden



62
63
64
# File 'lib/gimuby/genetic/population/population.rb', line 62

def get_fitness(solution)
  solution.get_fitness
end

#get_population_sizeObject



57
58
59
# File 'lib/gimuby/genetic/population/population.rb', line 57

def get_population_size
  @solutions.size
end

#pickObject

Pick some solutions



47
48
49
# File 'lib/gimuby/genetic/population/population.rb', line 47

def pick
  @pick_strategy.pick(self)
end

#replace(solutions) ⇒ Object

Replace part of the population (used by Archipelago)



35
36
37
# File 'lib/gimuby/genetic/population/population.rb', line 35

def replace(solutions)
  @solutions = @replace_strategy.replace(self, solutions)
end

#reproduceObject

Simply pick some solutions and make them reproduce



41
42
43
# File 'lib/gimuby/genetic/population/population.rb', line 41

def reproduce
  @solutions = @replace_strategy.replace(self)
end