Class: Biopsy::GeneticAlgorithm
- Inherits:
-
Object
- Object
- Biopsy::GeneticAlgorithm
- Defined in:
- lib/biopsy/optimisers/genetic_algorithm.rb
Instance Attribute Summary collapse
-
#best ⇒ Object
readonly
Returns the value of attribute best.
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#generation_no ⇒ Object
readonly
Returns the value of attribute generation_no.
-
#get_homog ⇒ Object
readonly
Returns the value of attribute get_homog.
Instance Method Summary collapse
- #finished? ⇒ Boolean
- #generate_chromosome ⇒ Object
- #get_population ⇒ Object
-
#initialize(population_size, parameter_ranges) ⇒ GeneticAlgorithm
constructor
A new instance of GeneticAlgorithm.
- #next_candidate(chromosome) ⇒ Object
- #run ⇒ Object
- #run_one_iteration(parameters, score) ⇒ Object
- #update_best?(current) ⇒ Boolean
- #update_ga ⇒ Object
Constructor Details
#initialize(population_size, parameter_ranges) ⇒ GeneticAlgorithm
Returns a new instance of GeneticAlgorithm.
179 180 181 182 183 184 185 186 187 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 179 def initialize (population_size, parameter_ranges) @ranges = parameter_ranges @population_size = population_size @current_generation = Generation.new(@population_size, @ranges) @best = { :parameters => nil, :score => 0.0 } end |
Instance Attribute Details
#best ⇒ Object (readonly)
Returns the value of attribute best.
177 178 179 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 177 def best @best end |
#current ⇒ Object (readonly)
Returns the value of attribute current.
177 178 179 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 177 def current @current end |
#generation_no ⇒ Object (readonly)
Returns the value of attribute generation_no.
177 178 179 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 177 def generation_no @generation_no end |
#get_homog ⇒ Object (readonly)
Returns the value of attribute get_homog.
177 178 179 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 177 def get_homog @get_homog end |
Instance Method Details
#finished? ⇒ Boolean
229 230 231 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 229 def finished? false end |
#generate_chromosome ⇒ Object
234 235 236 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 234 def generate_chromosome return Hash[@ranges.map { |param, range| [param, range.sample] }] end |
#get_population ⇒ Object
238 239 240 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 238 def get_population return @current_generation.get_population end |
#next_candidate(chromosome) ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 210 def next_candidate (chromosome) # .. will run update ga if @current_generation.last? is true @current_generation.next_chromosome(chromosome) if @current_generation.last? return self.update_ga end return @current end |
#run ⇒ Object
189 190 191 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 189 def run nil end |
#run_one_iteration(parameters, score) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 193 def run_one_iteration (parameters, score) @current = {:parameters => parameters, :score => score} # update best score? self.update_best? @current # push next chromosome to GA, generation will compute if population size is full return self.next_candidate @current # update tabu list #self.update_tabu #@current end |
#update_best?(current) ⇒ Boolean
204 205 206 207 208 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 204 def update_best? (current) # ... runs an identical method in GenerationHandler @current_generation.update_best? current @best = current if current[:score] > @best[:score] end |
#update_ga ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 220 def update_ga # ... will run to next generation store = @current_generation.run_generation @current_generation.homogeneous_test @get_homog = @current_generation.population_homogenosity @current_generation = Generation.new(@population_size, @ranges) return store end |