Class: AverageNewGenerationStrategy

Inherits:
NewGenerationStrategy show all
Defined in:
lib/gimuby/genetic/solution/new_generation_strategy/average_new_generation_strategy.rb

Instance Method Summary collapse

Constructor Details

#initialize(best_weight = 1.0) ⇒ AverageNewGenerationStrategy

Returns a new instance of AverageNewGenerationStrategy.

Parameters:

  • best_weight (Float) (defaults to: 1.0)

    The weight given to the best solution



6
7
8
# File 'lib/gimuby/genetic/solution/new_generation_strategy/average_new_generation_strategy.rb', line 6

def initialize(best_weight = 1.0)
  @best_weight = best_weight
end

Instance Method Details

#reproduce(solution1, solution2) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gimuby/genetic/solution/new_generation_strategy/average_new_generation_strategy.rb', line 10

def reproduce(solution1, solution2)
  weight1 = 1.0
  weight2 = 1.0
  if solution1.get_fitness < solution2.get_fitness
    weight1 = @best_weight
  else
    weight2 = @best_weight
  end
  x_values1 = solution1.get_solution_representation
  x_values2 = solution2.get_solution_representation
  reproduce_from_representation(x_values1, x_values2, weight1, weight2)
end