summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-10-30 00:43:26 -0500
committersanine <sanine.not@pm.me>2023-10-30 00:43:26 -0500
commit9fbeab4652b075fb545cbe54984a91f91be1d627 (patch)
treecd49b90103bbaa3c555c47a4ec6a4bcd4e8da600 /src
parentae504c5f500b523eecb51b41eaf3b51d799d8a5f (diff)
implement mut_genome_insert
Diffstat (limited to 'src')
-rw-r--r--src/genome/genome.js16
-rw-r--r--src/genome/genome.test.js25
2 files changed, 41 insertions, 0 deletions
diff --git a/src/genome/genome.js b/src/genome/genome.js
index 7688b32..d35b68c 100644
--- a/src/genome/genome.js
+++ b/src/genome/genome.js
@@ -104,3 +104,19 @@ export function mut_genome_expand(
n_input, n_internal+1, n_output, new_genome,
];
}
+
+
+export function mut_genome_insert(
+ [n_input, n_internal, n_output, genome],
+ weight_max,
+ r1, r2, r3
+) {
+ const source = Math.floor((n_input + n_internal) * r1);
+ const sink = Math.floor((n_internal + n_output) * r2) + n_input;
+ const weight = weight_max * ((2*r3)-1);
+
+ return [
+ n_input, n_internal, n_output,
+ [...genome, [source, sink, weight]],
+ ];
+}
diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js
index 11b2d31..d7dbe72 100644
--- a/src/genome/genome.test.js
+++ b/src/genome/genome.test.js
@@ -7,6 +7,7 @@ import {
mut_gene_sink,
mut_gene_weight,
mut_genome_expand,
+ mut_genome_insert,
} from './genome';
@@ -161,3 +162,27 @@ test('expand genome', () => {
],
]);
});
+
+
+test('insert new genes', () => {
+ const n_input = 1;
+ const n_internal = 2;
+ const n_output = 1;
+ const weight_max = 4;
+
+ expect(mut_genome_insert([
+ n_input, n_internal, n_output,
+ []
+ ], weight_max, 0, 0.5, 0)).toEqual([
+ n_input, n_internal, n_output,
+ [[0, 2, -4]]
+ ]);
+
+ expect(mut_genome_insert([
+ n_input, n_internal, n_output,
+ [[0, 2, -4]]
+ ], weight_max, 0.99, 0, 1)).toEqual([
+ n_input, n_internal, n_output,
+ [[0, 2, -4], [2, 1, 4]]
+ ]);
+});