Class: Biopsy::GeneticAlgorithm

Inherits:
Object
  • Object
show all
Defined in:
lib/biopsy/optimisers/genetic_algorithm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bestObject (readonly)

Returns the value of attribute best.



177
178
179
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 177

def best
  @best
end

#currentObject (readonly)

Returns the value of attribute current.



177
178
179
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 177

def current
  @current
end

#generation_noObject (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_homogObject (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

Returns:

  • (Boolean)


229
230
231
# File 'lib/biopsy/optimisers/genetic_algorithm.rb', line 229

def finished?
  false
end

#generate_chromosomeObject



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_populationObject



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

#runObject



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

Returns:

  • (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_gaObject



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