Class: Biopsy::SPEA2
- Inherits:
-
Object
- Object
- Biopsy::SPEA2
- Defined in:
- lib/biopsy/optimisers/spea2.rb
Instance Attribute Summary collapse
-
#archive_size ⇒ Object
Returns the value of attribute archive_size.
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#population_size ⇒ Object
Returns the value of attribute population_size.
-
#probability_of_cross ⇒ Object
Returns the value of attribute probability_of_cross.
Instance Method Summary collapse
- #best ⇒ Object
-
#finished? ⇒ Boolean
check if termination conditions are met.
- #get_children_to_score(selected_to_mate) ⇒ Object
-
#initialize(parameter_ranges, threads = 8) ⇒ SPEA2
constructor
A new instance of SPEA2.
-
#knows_starting_point? ⇒ Boolean
return true if algorithm knows its own starting point.
-
#random_parameter_set ⇒ Object
generates a random parameter set.
-
#run_one_iteration(parameters, score) ⇒ Object
accepts a parameter set and a score (which are added to population) returns another parameter set to be scored which is then accepted next iteration.
- #run_one_mating_iteration(parameters, score) ⇒ Object
-
#select_starting_point ⇒ Object
returns a parameter set to be used as the starting point.
- #setup(start_point) ⇒ Object
- #update_best? ⇒ Boolean
Constructor Details
#initialize(parameter_ranges, threads = 8) ⇒ SPEA2
Returns a new instance of SPEA2.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/biopsy/optimisers/spea2.rb', line 9 def initialize(parameter_ranges, threads=8) @parameter_ranges = parameter_ranges @currently_mating = false @probability_of_cross = 0.94 @population_size = 50 @archive_size = 15 @mutation_rate = 0.20 @generation_handler = Generation.new(@population_size, @archive_size) @mating_handler = MatingHandler.new(@population_size, @probability_of_cross, @parameter_ranges, @mutation_rate) end |
Instance Attribute Details
#archive_size ⇒ Object
Returns the value of attribute archive_size.
8 9 10 |
# File 'lib/biopsy/optimisers/spea2.rb', line 8 def archive_size @archive_size end |
#current ⇒ Object (readonly)
Returns the value of attribute current.
6 7 8 |
# File 'lib/biopsy/optimisers/spea2.rb', line 6 def current @current end |
#population_size ⇒ Object
Returns the value of attribute population_size.
8 9 10 |
# File 'lib/biopsy/optimisers/spea2.rb', line 8 def population_size @population_size end |
#probability_of_cross ⇒ Object
Returns the value of attribute probability_of_cross.
6 7 8 |
# File 'lib/biopsy/optimisers/spea2.rb', line 6 def probability_of_cross @probability_of_cross end |
Instance Method Details
#best ⇒ Object
78 79 80 |
# File 'lib/biopsy/optimisers/spea2.rb', line 78 def best @best end |
#finished? ⇒ Boolean
check if termination conditions are met
75 76 77 |
# File 'lib/biopsy/optimisers/spea2.rb', line 75 def finished? true if rand < 0.00025 end |
#get_children_to_score(selected_to_mate) ⇒ Object
44 45 46 |
# File 'lib/biopsy/optimisers/spea2.rb', line 44 def get_children_to_score selected_to_mate @children_to_score = @mating_handler.run(selected_to_mate) end |
#knows_starting_point? ⇒ Boolean
return true if algorithm knows its own starting point
55 56 57 |
# File 'lib/biopsy/optimisers/spea2.rb', line 55 def knows_starting_point? return true end |
#random_parameter_set ⇒ Object
generates a random parameter set
71 72 73 |
# File 'lib/biopsy/optimisers/spea2.rb', line 71 def random_parameter_set Hash[@parameter_ranges.map { |k, v| [k, v.sample] }] end |
#run_one_iteration(parameters, score) ⇒ Object
accepts a parameter set and a score (which are added to population) returns another parameter set to be scored which is then accepted next iteration
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/biopsy/optimisers/spea2.rb', line 25 def run_one_iteration(parameters, score) self.run_one_mating_iteration(parameters, score) if @currently_mating == true @current = {:parameters => parameters, :score => score} # update best score? self.update_best? # push new parameter set into generation @currently_mating = @generation_handler.add_new_individual(@current) if @currently_mating == true # begin mating process # empty current population @generation_handler.population_array = [] selected_to_mate = @mating_handler.select_mating_population(@generation_handler.archive_array) self.get_children_to_score(selected_to_mate) @children_to_score.pop[:parameters] else # get next parameter set to score self.random_parameter_set end end |
#run_one_mating_iteration(parameters, score) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/biopsy/optimisers/spea2.rb', line 47 def run_one_mating_iteration(parameters, score) @generation_handler.add_new_individual({:parameters => parameters, :score => score}) if @children_to_score.length == 1 @currently_mating = false end @children_to_score.pop end |
#select_starting_point ⇒ Object
returns a parameter set to be used as the starting point
67 68 69 |
# File 'lib/biopsy/optimisers/spea2.rb', line 67 def select_starting_point self.random_parameter_set end |
#setup(start_point) ⇒ Object
19 20 21 22 |
# File 'lib/biopsy/optimisers/spea2.rb', line 19 def setup start_point @current = {:parameters => start_point, :score => nil} @best = @current end |
#update_best? ⇒ Boolean
58 59 60 61 62 63 64 65 |
# File 'lib/biopsy/optimisers/spea2.rb', line 58 def update_best? if @best[:score].nil? || @current[:score] > @best[:score] @best = @current.clone @iterations_since_best = 0 else @iterations_since_best += 1 end end |