Class: GeneticAlgorithm

Inherits:
Object
  • Object
show all
Defined in:
lib/gga4r.rb

Constant Summary

VERSION =
'0.9.3'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_pop, prop = {}) ⇒ GeneticAlgorithm

Must be initialized with a Array of chromosomes To be a chomosome the object must implement the next methods:

- fitness
- recombine
- mutate
- distance (only for multi-modal optimization)
Accepts the next properties:
 - max_population: maximum number of individuals that are allowed to form a generation.
 - logger: logger to write messages if given.
 - multi_recombination: set to true if the result of a chromosome's #recombination method
   returns an array. Default to false
 - multi_modal: set to true to use a multi-modal algorithm using deterministic crowding and a distance-derated fitness.
 - share_radius: in multi-modal optimization, determines the niche radius for derated fitness calculation.


19
20
21
22
23
24
25
26
27
# File 'lib/gga4r.rb', line 19

def initialize(in_pop, prop = {})
  @max_population =  prop[:max_population]
  @logger = prop[:logger] || Logger.new('/dev/null')
  @population = in_pop
  @multi_recombination = prop[:multi_recombination] || false
  @generations = []
  @multi_modal = prop[:multi_modal] || false
  @share_radius = prop[:share_radius] or 3
end

Class Method Details

.populate_from_file(filename, prop = {}) ⇒ Object

Returns a GeneticAlgorithm object with the generations loaded from given files and with properties prop. Files must contain the chromosomes in YAML format.



48
49
50
# File 'lib/gga4r.rb', line 48

def self.populate_from_file(filename, prop = {})
  GeneticAlgorithm.new(YAML.load(File.open(filename, 'r')), prop)
end

Instance Method Details

#best_fitObject

Returns an array with the best fitted individuals for last generation



30
31
32
# File 'lib/gga4r.rb', line 30

def best_fit
  @population.max_by(&:fitness)
end

#best_fitted(n) ⇒ Object

Returns an array with the best fitted n individuals from the population (might include local optima)



35
36
37
# File 'lib/gga4r.rb', line 35

def best_fitted(n)
  @population.uniq.sort_by{|c| -c.fitness}.first(n)
end

#best_fitted_derated(n) ⇒ Object

Returns an array with the best fitted n individuals from the population (might include local optima) Uses a distance-derated fitness metric



41
42
43
# File 'lib/gga4r.rb', line 41

def best_fitted_derated(n)
  @population.uniq.sort_by{|c| -(derated_fitness(c,@population))}.first(n)
end

#evolve(num_steps = 1) ⇒ Object

Evolves the actual generation num_steps steps (1 by default).



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gga4r.rb', line 63

def evolve(num_steps = 1)
  num_steps.times do |t|
    @population = selection(@population)
    new_gen = @population.map { |chromosome| chromosome.dup }
    if !@multi_modal
      @population += recombination(new_gen) + mutation(new_gen)
    else
      @population = deterministic_crowding(@population)
      @population = mutation(@population)
    end
  end
end

#save_population(filename) ⇒ Object

Saves into filename and in yaml format the generation that matchs with given generation number ( by default from last generation ).



54
55
56
57
58
# File 'lib/gga4r.rb', line 54

def save_population(filename)
  f = File.new(filename, "w")
  f.write(@population.to_yaml)
  f.close
end