Class: Evolvable::Genome

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/evolvable/genome.rb

Overview

The Genome class represents the fully instantiated genetic blueprint of an evolvable instance. It stores all gene data in a structured, accessible form and provides methods to inspect, manipulate, and serialize that genetic information.

A genome consists of:

  • A hash of gene configurations organized by key
  • Count genes that determine how many instances of each gene type are present
  • The actual gene instances used by the evolvable

The genome acts as the bridge between the gene space (definition) and the evolvable instance (implementation), enabling flexible gene access and supporting dynamic mutation or crossover behavior.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: {}) ⇒ Genome

Returns a new instance of Genome.



29
30
31
# File 'lib/evolvable/genome.rb', line 29

def initialize(config: {})
  @config = config
end

Instance Attribute Details

#configObject (readonly) Also known as: to_h

Returns the value of attribute config.



33
34
35
# File 'lib/evolvable/genome.rb', line 33

def config
  @config
end

Class Method Details

.load(data, serializer: Serializer) ⇒ Object



25
26
27
# File 'lib/evolvable/genome.rb', line 25

def self.load(data, serializer: Serializer)
  new(config: serializer.load(data))
end

Instance Method Details

#dump(serializer: Serializer) ⇒ Object



110
111
112
# File 'lib/evolvable/genome.rb', line 110

def dump(serializer: Serializer)
  serializer.dump @config
end

#find_count_gene(key) ⇒ <Type>

Parameters:

  • key (<Type>)

Returns:

  • (<Type>)



88
89
90
# File 'lib/evolvable/genome.rb', line 88

def find_count_gene(key)
  @config.dig(key, :count_gene)
end

#find_gene(key) ⇒ <Type>

Returns the first gene with the given key. In the Melody example above, the instrument gene has the key :instrument so we might write something like:

instrument_gene = melody.find_gene(instrument)

Parameters:

  • key (<Type>)

Returns:

  • (<Type>)



49
50
51
# File 'lib/evolvable/genome.rb', line 49

def find_gene(key)
  @config.dig(key, :genes, 0)
end

#find_genes(*keys) ⇒ <Type>

Returns an array of genes that have the given key. Gene keys are defined using the EvolvableClass.gene macro method. In the Melody example above, the key for the note genes would be :notes. The following would return an array of them: note_genes = melody.find_genes(:notes)

Parameters:

  • *keys (<Type>)

Returns:

  • (<Type>)



63
64
65
66
67
68
# File 'lib/evolvable/genome.rb', line 63

def find_genes(*keys)
  keys.flatten!
  return @config.dig(keys.first, :genes) if keys.count <= 1

  @config.values_at(*keys).flat_map { _1&.fetch(:genes, []) || [] }
end

#find_genes_count(key) ⇒ <Type>

Parameters:

  • key (<Type>)

Returns:

  • (<Type>)



77
78
79
# File 'lib/evolvable/genome.rb', line 77

def find_genes_count(key)
  find_count_gene(key).count
end

#gene_keysObject



94
95
96
# File 'lib/evolvable/genome.rb', line 94

def gene_keys
  @config.keys
end

#genesObject



98
99
100
# File 'lib/evolvable/genome.rb', line 98

def genes
  @config.flat_map { |_gene_key, gene_config| gene_config[:genes] }
end

#inspectObject



106
107
108
# File 'lib/evolvable/genome.rb', line 106

def inspect
  self.class.name
end

#merge!(other_genome) ⇒ Object



102
103
104
# File 'lib/evolvable/genome.rb', line 102

def merge!(other_genome)
  @config.merge!(other_genome.config)
end