From 17fc9fe64f4de1bc58596034c54ddb487f992c9f Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 30 Oct 2023 01:00:53 -0500 Subject: add mut_genome_delete --- src/genome/genome.js | 9 +++++++++ src/genome/genome.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'src') diff --git a/src/genome/genome.js b/src/genome/genome.js index d35b68c..16b5e4d 100644 --- a/src/genome/genome.js +++ b/src/genome/genome.js @@ -120,3 +120,12 @@ export function mut_genome_insert( [...genome, [source, sink, weight]], ]; } + + +export function mut_genome_delete( + [n_input, n_internal, n_output, genome], r +) { + const del_idx = Math.floor(r * genome.length); + const new_genome = genome.filter((_, idx) => idx != del_idx); + return [n_input, n_internal, n_output, new_genome]; +} diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js index d7dbe72..a021073 100644 --- a/src/genome/genome.test.js +++ b/src/genome/genome.test.js @@ -8,6 +8,7 @@ import { mut_gene_weight, mut_genome_expand, mut_genome_insert, + mut_genome_delete, } from './genome'; @@ -186,3 +187,27 @@ test('insert new genes', () => { [[0, 2, -4], [2, 1, 4]] ]); }); + + +test('remove genes', () => { + const n_input = 0; + const n_output = 0; + const n_internal = 3; + const genome = [ + [0, 1, 0], [1, 2, 0], + ]; + + expect(mut_genome_delete([ + n_input, n_internal, n_output, genome + ], 0.0)).toEqual([ + n_input, n_internal, n_output, + [[1, 2, 0]], + ]); + + expect(mut_genome_delete([ + n_input, n_internal, n_output, genome + ], 0.99)).toEqual([ + n_input, n_internal, n_output, + [[0, 1, 0]], + ]); +}); -- cgit v1.2.1