Class: Evolvable::Genome

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

Overview

TODO...

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: {}) ⇒ Genome

Returns a new instance of Genome.



15
16
17
# File 'lib/evolvable/genome.rb', line 15

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/evolvable/genome.rb', line 19

def config
  @config
end

Class Method Details

.load(data, serializer: Serializer) ⇒ Object



11
12
13
# File 'lib/evolvable/genome.rb', line 11

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

Instance Method Details

#dump(serializer: Serializer) ⇒ Object



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

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

#find_count_gene(key) ⇒ <Type>

Parameters:

  • key (<Type>)

Returns:

  • (<Type>)



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

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>)



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

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 in the EvolvableClass.search_space 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>)



39
40
41
42
43
44
# File 'lib/evolvable/genome.rb', line 39

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>)



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

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

#gene_keysObject



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

def gene_keys
  @config.keys
end

#genesObject



74
75
76
# File 'lib/evolvable/genome.rb', line 74

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

#inspectObject



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

def inspect
  self.class.name
end