summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-10-30 01:18:23 -0500
committersanine <sanine.not@pm.me>2023-10-30 01:18:23 -0500
commita32853e60029fa7f08d4d713ee613ee03196fbef (patch)
treee436fbf406ab0699cce74a89b53428ca223472d2
parent17fc9fe64f4de1bc58596034c54ddb487f992c9f (diff)
add mut_genome_contract
-rw-r--r--src/genome/genome.js19
-rw-r--r--src/genome/genome.test.js52
2 files changed, 71 insertions, 0 deletions
diff --git a/src/genome/genome.js b/src/genome/genome.js
index 16b5e4d..d2650b5 100644
--- a/src/genome/genome.js
+++ b/src/genome/genome.js
@@ -106,6 +106,25 @@ export function mut_genome_expand(
}
+export function mut_genome_contract(
+ [n_input, n_internal, n_output, genome], r
+) {
+ const contract_idx = Math.floor(n_internal * r) + n_input;
+ const new_source = (source) => source >= contract_idx ? source-1 : source;
+ const new_sink = (sink) => sink > contract_idx ? sink-1 : sink;
+
+ const new_genome = genome.map(([source, sink, weight]) => [
+ new_source(source),
+ new_sink(sink),
+ weight,
+ ]);
+
+ return [
+ n_input, n_internal-1, n_output, new_genome
+ ];
+}
+
+
export function mut_genome_insert(
[n_input, n_internal, n_output, genome],
weight_max,
diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js
index a021073..37bdacc 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_contract,
mut_genome_insert,
mut_genome_delete,
} from './genome';
@@ -165,6 +166,57 @@ test('expand genome', () => {
});
+test('contract genome', () => {
+ const n_input = 1;
+ const n_internal = 3;
+ const n_output = 1;
+
+ const genome = [
+ [0, 1, 0],
+ [1, 2, 1],
+ [2, 3, 2],
+ [3, 4, 3],
+ ];
+
+ expect(mut_genome_contract([
+ n_input, n_internal, n_output, genome
+ ], 0.0)).toEqual([
+ n_input, n_internal-1, n_output,
+ [
+ [0, 1, 0],
+ [0, 1, 1],
+ [1, 2, 2],
+ [2, 3, 3],
+ ],
+ ]);
+
+ expect(mut_genome_contract([
+ n_input, n_internal, n_output, genome
+ ], 0.5)).toEqual([
+ n_input, n_internal-1, n_output,
+ [
+ [0, 1, 0],
+ [1, 2, 1],
+ [1, 2, 2],
+ [2, 3, 3],
+ ],
+ ]);
+
+ expect(mut_genome_contract([
+ n_input, n_internal, n_output, genome
+ ], 0.99)).toEqual([
+ n_input, n_internal-1, n_output,
+ [
+ [0, 1, 0],
+ [1, 2, 1],
+ [2, 3, 2],
+ [2, 3, 3],
+ ],
+ ]);
+});
+
+
+
test('insert new genes', () => {
const n_input = 1;
const n_internal = 2;