Class: Evolvable::Genome
- Inherits:
-
Object
- Object
- Evolvable::Genome
- Extended by:
- Forwardable
- Defined in:
- lib/evolvable/genome.rb
Overview
TODO...
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #dump(serializer: Serializer) ⇒ Object
-
#find_count_gene(key) ⇒ <Type>
. -
#find_gene(key) ⇒ <Type>
Returns the first gene with the given key.
-
#find_genes(*keys) ⇒ <Type>
Returns an array of genes that have the given key.
-
#find_genes_count(key) ⇒ <Type>
. - #gene_keys ⇒ Object
- #genes ⇒ Object
-
#initialize(config: {}) ⇒ Genome
constructor
A new instance of Genome.
- #inspect ⇒ Object
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
#config ⇒ Object (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>
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)
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)
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>
53 54 55 |
# File 'lib/evolvable/genome.rb', line 53 def find_genes_count(key) find_count_gene(key).count end |
#gene_keys ⇒ Object
70 71 72 |
# File 'lib/evolvable/genome.rb', line 70 def gene_keys @config.keys end |
#genes ⇒ Object
74 75 76 |
# File 'lib/evolvable/genome.rb', line 74 def genes @config.flat_map { |_gene_key, gene_config| gene_config[:genes] } end |
#inspect ⇒ Object
78 79 80 |
# File 'lib/evolvable/genome.rb', line 78 def inspect self.class.name end |