Class: RandomWheelPickStrategy
- Inherits:
-
PickStrategy
- Object
- PickStrategy
- RandomWheelPickStrategy
- Defined in:
- lib/gimuby/genetic/population/pick_strategy/random_wheel_pick_strategy.rb
Instance Attribute Summary collapse
-
#random_wheel_probability_reason ⇒ Object
Returns the value of attribute random_wheel_probability_reason.
Attributes inherited from PickStrategy
Instance Method Summary collapse
-
#initialize ⇒ RandomWheelPickStrategy
constructor
A new instance of RandomWheelPickStrategy.
- #pick(population) ⇒ Object
Constructor Details
#initialize ⇒ RandomWheelPickStrategy
Returns a new instance of RandomWheelPickStrategy.
5 6 7 8 |
# File 'lib/gimuby/genetic/population/pick_strategy/random_wheel_pick_strategy.rb', line 5 def initialize super @random_wheel_probability_reason = 0.7 end |
Instance Attribute Details
#random_wheel_probability_reason ⇒ Object
Returns the value of attribute random_wheel_probability_reason.
10 11 12 |
# File 'lib/gimuby/genetic/population/pick_strategy/random_wheel_pick_strategy.rb', line 10 def random_wheel_probability_reason @random_wheel_probability_reason end |
Instance Method Details
#pick(population) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gimuby/genetic/population/pick_strategy/random_wheel_pick_strategy.rb', line 12 def pick(population) number = get_number_to_pick(population) reason = @random_wheel_probability_reason solutions = population.solutions candidates = solutions.clone candidates.sort! do |x, y| (population.get_fitness x) <=> (population.get_fitness y) end picked = [] begin # we use a geometric sequence max_of_the_rand = (1 - reason ** candidates.length) / (1 - reason) r = rand() * max_of_the_rand element = 1 candidates.each do |solution| max_accepted_value = (1 - reason ** element) / (1 - reason) element += 1 if max_accepted_value > r picked.push solution ind = candidates.index solution candidates.slice! ind break end end end while picked.length < number picked end |