summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/genome/genome.js16
-rw-r--r--src/genome/genome.test.js51
2 files changed, 67 insertions, 0 deletions
diff --git a/src/genome/genome.js b/src/genome/genome.js
index c288d02..7688b32 100644
--- a/src/genome/genome.js
+++ b/src/genome/genome.js
@@ -88,3 +88,19 @@ export function mut_gene_weight(weight_max, gene, r) {
clamp(new_weight, -weight_max, weight_max),
];
}
+
+
+export function mut_genome_expand(
+ [n_input, n_internal, n_output, genome], r
+) {
+ const expand_index = Math.floor(n_internal * r) + n_input;
+ const new_genome = genome.map(([source, sink, weight]) => [
+ source >= expand_index ? source+1 : source,
+ sink >= expand_index ? sink+1 : sink,
+ weight,
+ ]);
+
+ return [
+ n_input, n_internal+1, n_output, new_genome,
+ ];
+}
diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js
index e1c8711..11b2d31 100644
--- a/src/genome/genome.test.js
+++ b/src/genome/genome.test.js
@@ -6,6 +6,7 @@ import {
mut_gene_source,
mut_gene_sink,
mut_gene_weight,
+ mut_genome_expand,
} from './genome';
@@ -110,3 +111,53 @@ test('mutate gene weight', () => {
weight_max, [0, 0, 3], 0.5
)).toEqual([0, 0, (6+0)/3]);
});
+
+
+test('expand genome', () => {
+ const n_input = 1;
+ const n_internal = 3;
+ const n_output = 1;
+
+ const genome = [
+ [0, 1, 0],
+ [1, 2, 0],
+ [2, 3, 0],
+ [3, 4, 0],
+ ];
+
+ expect(mut_genome_expand([
+ n_input, n_internal, n_output, genome
+ ], 0.0)).toEqual([
+ n_input, n_internal+1, n_output,
+ [
+ [0, 2, 0],
+ [2, 3, 0],
+ [3, 4, 0],
+ [4, 5, 0],
+ ],
+ ]);
+
+ expect(mut_genome_expand([
+ n_input, n_internal, n_output, genome
+ ], 0.5)).toEqual([
+ n_input, n_internal+1, n_output,
+ [
+ [0, 1, 0],
+ [1, 3, 0],
+ [3, 4, 0],
+ [4, 5, 0],
+ ],
+ ]);
+
+ expect(mut_genome_expand([
+ n_input, n_internal, n_output, genome
+ ], 0.99)).toEqual([
+ n_input, n_internal+1, n_output,
+ [
+ [0, 1, 0],
+ [1, 2, 0],
+ [2, 4, 0],
+ [4, 5, 0],
+ ],
+ ]);
+});