Class: Evolvable::SearchSpace

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

Overview

The search space encapsulates the range of possible genes for a particular evolvable. You can think of it as the boundaries of genetic variation. It is configured via the .search_space method that you define on your evolvable class. It's used by populations to initialize new evolvables.

Evolvable provides flexibility in how you define your search space. The below example implementations for .search_space produce the exact same search space for the Hello World demo program. The different styles arguably vary in suitability for different contexts, perhaps depending on how programs are loaded and the number of different gene types.

Examples:

# All 9 of these example definitions are equivalent

# Hash syntax
{ chars: { type: 'CharGene', max_count: 100 } }
{ chars: { type: 'CharGene', min_count: 1, max_count: 100 } }
{ chars: { type: 'CharGene', count: 1..100 } }

# Array of arrays syntax
[[:chars, 'CharGene', 1..100]]
[['chars', 'CharGene',  1..100]]
[['CharGene', 1..100]]

# A single array works when there's only one type of gene
['CharGene', 1..100]
[:chars, 'CharGene', 1..100]
['chars', 'CharGene', 1..100]

Direct Known Subclasses

GeneSpace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: {}, evolvable_class: nil) ⇒ SearchSpace

Returns a new instance of SearchSpace.


53
54
55
56
# File 'lib/evolvable/search_space.rb', line 53

def initialize(config: {}, evolvable_class: nil)
  @evolvable_class = evolvable_class
  @config = normalize_config(config)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.


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

def config
  @config
end

#evolvable_classObject

Returns the value of attribute evolvable_class.


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

def evolvable_class
  @evolvable_class
end

Class Method Details

.build(config, evolvable_class = nil) ⇒ Object


43
44
45
46
47
48
49
50
# File 'lib/evolvable/search_space.rb', line 43

def build(config, evolvable_class = nil)
  if config.is_a?(SearchSpace)
    config.evolvable_class = evolvable_class if evolvable_class
    config
  else
    new(config: config, evolvable_class: evolvable_class)
  end
end

Instance Method Details

#merge_search_space(val) ⇒ Object


64
65
66
67
# File 'lib/evolvable/search_space.rb', line 64

def merge_search_space(val)
  val = val.config if val.is_a?(self.class)
  @config.merge normalize_config(val)
end

#merge_search_space!(val) ⇒ Object


69
70
71
72
# File 'lib/evolvable/search_space.rb', line 69

def merge_search_space!(val)
  val = val.config if val.is_a?(self.class)
  @config.merge! normalize_config(val)
end

#new_genomeObject


60
61
62
# File 'lib/evolvable/search_space.rb', line 60

def new_genome
  Genome.new(config: new_genome_config)
end