Module: Evolvable::ClassMethods

Defined in:
lib/evolvable.rb

Instance Method Summary collapse

Instance Method Details

#after_evolution(population) ⇒ Object

Runs after evolution.



193
# File 'lib/evolvable.rb', line 193

def after_evolution(population); end

#before_evaluation(population) ⇒ Object

Runs before evaluation.



169
# File 'lib/evolvable.rb', line 169

def before_evaluation(population); end

#before_evolution(population) ⇒ Object

Runs after evaluation and before evolution.

Examples:

class Melody
  include Evolvable

  # Play the best melody from each generation
  def self.before_evolution(population)
    population.best_evolvable.play
  end

  # ...
end


187
# File 'lib/evolvable.rb', line 187

def before_evolution(population); end

#gene_spaceObject

Deprecated.

Will be removed in version 2.0. Use #search_space instead.



160
161
162
# File 'lib/evolvable.rb', line 160

def gene_space
  {}
end

#initialize_evolvableObject



83
84
85
# File 'lib/evolvable.rb', line 83

def initialize_evolvable
  new
end

#new_evolvable(population: nil, genome: Genome.new, generation_index: nil) ⇒ Object

Initializes a new instance. Accepts a population object, an array of gene objects, and the instance's population index. This method is useful for re-initializing instances and populations that have been saved.

It is not recommended that you override this method as it is used by Evolvable internals. If you need to customize how your instances are initialized you can override either of the following two "initialize_instance" methods.



72
73
74
75
76
77
78
79
80
81
# File 'lib/evolvable.rb', line 72

def new_evolvable(population: nil,
                  genome: Genome.new,
                  generation_index: nil)
  evolvable = initialize_evolvable
  evolvable.population = population
  evolvable.genome = genome
  evolvable.generation_index = generation_index
  evolvable.after_initialize
  evolvable
end

#new_population(keyword_args = {}) ⇒ Object

Initializes a population using configurable defaults that can be configured and optimized. Accepts the same named parameters as Population#initialize.



57
58
59
60
# File 'lib/evolvable.rb', line 57

def new_population(keyword_args = {})
  keyword_args[:evolvable_type] = self
  Population.new(**keyword_args)
end

#new_search_spaceObject



87
88
89
90
91
92
# File 'lib/evolvable.rb', line 87

def new_search_space
  space_config = search_space.empty? ? gene_space : search_space
  search_space = SearchSpace.build(space_config, self)
  search_spaces.each { |space| search_space.merge_search_space!(space) }
  search_space
end

#search_spaceHash, Array

This method is abstract.
TODO:

Define gene config attributes - name, type, count

This method is responsible for configuring the available gene types of evolvable instances. In effect, it provides the blueprint for constructing a hyperdimensional genetic space that's capable of being used and searched by evolvable objects.

Override this method with a search space config for initializing SearchSpace objects. The config can be a hash, array of arrays, or single array when there's only one type of gene.

The below example definitions could conceivably be used to generate evolvable music.

Examples:

Hash config

def search_space
  { instrument: { type: InstrumentGene, count: 1..4 },
    notes: { type: NoteGene, count: 16 } }
end

Array of arrays config

# With explicit gene names
def search_space
  [[:instrument, InstrumentGene, 1..4],
   [:notes, NoteGene, 16]]
end

# Without explicit gene names
def search_space
  [[SynthGene, 0..4], [RhythmGene, 0..8]]
end

Array config

# Available when when just one type of gene
def search_space
  [NoteGene, 1..100]
end

# With explicit gene type name.
def search_space
  ['notes', 'NoteGene', 1..100]
end

Returns:

  • (Hash, Array)

See Also:



142
143
144
# File 'lib/evolvable.rb', line 142

def search_space
  {}
end

#search_spacesArray

This method is abstract.

Override this method to define multiple search spaces



153
154
155
# File 'lib/evolvable.rb', line 153

def search_spaces
  []
end