evolvable 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +0,0 @@
1
- class Crossover
2
- def call(parent_genes, offspring_count)
3
- parent_gene_couples = parent_genes.combination(2).cycle
4
- Array.new(offspring_count) do
5
- p1_genes, p2_genes = parent_gene_couples.next
6
- p1_genes.merge(p2_genes) { |_k, p1_val, p2_val| [p1_val, p2_val].sample }
7
- offspring_genes = merge_parent_genes(p1_genes, p2_genes)
8
- trim_offspring_genes!(offspring_genes, p1_genes.count)
9
- offspring_genes
10
- end
11
- end
12
-
13
- def inspect
14
- "#<#{self.class.name} #{as_json.map { |a| a.join(': ') }.join(', ')} >"
15
- end
16
-
17
- def as_json
18
- { type: self.class.name }
19
- end
20
-
21
- private
22
-
23
- def merge_parent_genes(p1_genes, p2_genes)
24
- p1_genes.merge(p2_genes) { |_k, p1_val, p2_val| [p1_val, p2_val].sample }
25
- end
26
-
27
- def trim_offspring_genes!(offspring_genes, parent_genes_count)
28
- trim_genes_count = offspring_genes.count - parent_genes_count
29
- return if trim_genes_count.zero?
30
-
31
- offspring_genes.keys.sample(trim_genes_count).each do |name|
32
- offspring_genes.delete(name)
33
- end
34
- end
35
- end
@@ -1,5 +0,0 @@
1
- module Evolvable
2
- module Errors
3
- class NotImplemented < StandardError; end
4
- end
5
- end
@@ -1,45 +0,0 @@
1
- module Evolvable
2
- module HelperMethods
3
- def combine_dimensions(dimensions)
4
- dimension_lengths = dimensions.map(&:length)
5
- genes_count = dimension_lengths.inject(1) { |product, c| product * c }
6
-
7
- genes = Array.new(genes_count) { [] }
8
- element_repeat_counts = compute_element_repeat_counts(dimension_lengths)
9
- sequence_counts = compute_sequence_counts(dimension_lengths)
10
-
11
- dimensions.each_with_index do |dimension, dim_index|
12
- element_count = element_repeat_counts[dim_index]
13
- sequence = dimension.flat_map { |e| [e] * element_count }
14
- dimension_sequence = sequence * sequence_counts[dim_index]
15
- dimension_sequence.each_with_index do |element, gene_index|
16
- genes[gene_index][dim_index] = element
17
- end
18
- end
19
-
20
- genes
21
- end
22
-
23
- private
24
-
25
- def compute_element_repeat_counts(dimension_lengths)
26
- repeat_counts = Array.new(dimension_lengths.count - 1) do |n|
27
- right_side_counts = dimension_lengths[(n + 1)..-1]
28
- right_side_counts.inject(1) { |product, a| product * a }
29
- end
30
- repeat_counts << 1
31
- repeat_counts
32
- end
33
-
34
- def compute_sequence_counts(dimension_lengths)
35
- Array.new(dimension_lengths.count) do |n|
36
- if n.zero?
37
- 1
38
- else
39
- right_side_counts = dimension_lengths[0...n]
40
- right_side_counts.inject(1) { |product, a| product * a }
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,9 +0,0 @@
1
- module Evolvable
2
- module Hooks
3
- def evolvable_before_evolution(population); end
4
-
5
- def evolvable_after_select(population); end
6
-
7
- def evolvable_after_evolution(population); end
8
- end
9
- end