From 3793cddeb12993da492f9934b59849442c68aa12 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 30 Oct 2023 00:05:00 -0500 Subject: refactor gene mutations --- src/genome/genome.test.js | 102 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 20 deletions(-) (limited to 'src/genome/genome.test.js') diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js index 8152f04..e1c8711 100644 --- a/src/genome/genome.test.js +++ b/src/genome/genome.test.js @@ -1,29 +1,14 @@ 'use strict'; import { - mutation_type, mutate, get_size, parse_genome, + mut_gene_source, + mut_gene_sink, + mut_gene_weight, } from './genome'; -test('basic gene mutations', () => { - expect(mutate([0, 1, 2], mutation_type.none, 0)).toEqual([0, 1, 2]); - - expect(mutate([0, 1, 2], mutation_type.source, 0.2)).toEqual([0, 1, 2]); - expect(mutate([1, 1, 2], mutation_type.source, 0.2)).toEqual([0, 1, 2]); - expect(mutate([0, 1, 2], mutation_type.source, 0.8)).toEqual([1, 1, 2]); - - expect(mutate([0, 1, 2], mutation_type.sink, 0.2)).toEqual([0, 0, 2]); - expect(mutate([0, 1, 2], mutation_type.sink, 0.8)).toEqual([0, 2, 2]); - expect(mutate([0, 0, 2], mutation_type.sink, 0.2)).toEqual([0, 0, 2]); - - expect(mutate([0, 1, 2], mutation_type.weight, 0.5)).toEqual([0, 1, 1]); - expect(mutate([0, 1, 2], mutation_type.weight, 0.0)).toEqual([0, 1, -1]); - expect(mutate([0, 1, 2], mutation_type.weight, 1.0)).toEqual([0, 1, 3]); -}); - - test('genome validation and size', () => { expect(get_size(0, 0, [ [ 0, 0, 1.0 ] ])).toBe(1); expect(get_size(2, 1, [ [ 0, 2, 1 ] ])).toBe(3); @@ -39,8 +24,6 @@ test('parse a genome into a neural net', () => { [1, 2, 1] ]); - console.log(n); - expect(n.input_count).toBe(1); expect(n.output_count).toBe(1); expect(n.compute([2], [-1])).toEqual([ @@ -48,3 +31,82 @@ test('parse a genome into a neural net', () => { [ Math.tanh( 2-1 ) ], ]); }); + + +test('mutate gene source', () => { + const n_input = 3; + const n_internal = 4; + const n_output = 5; + + expect(mut_gene_source( + n_input, n_internal, n_output, + [0, 4, 0], + 0.0 + )).toEqual([0, 4, 0]); + + expect(mut_gene_source( + n_input, n_internal, n_output, + [0, 4, 0], + 1.0 + )).toEqual([1, 4, 0]); + + expect(mut_gene_source( + n_input, n_internal, n_output, + [6, 4, 0], + 0.0 + )).toEqual([5, 4, 0]); + + expect(mut_gene_source( + n_input, n_internal, n_output, + [6, 4, 0], + 1.0 + )).toEqual([6, 4, 0]); +}); + + +test('mutate gene sink', () => { + const n_input = 3; + const n_internal = 4; + const n_output = 5; + + expect(mut_gene_sink( + n_input, n_internal, n_output, + [0, 7, 0], + 0.0 + )).toEqual([0, 7, 0]); + + expect(mut_gene_sink( + n_input, n_internal, n_output, + [0, 7, 0], + 1.0 + )).toEqual([0, 8, 0]); + + expect(mut_gene_sink( + n_input, n_internal, n_output, + [6, 11, 0], + 0.0 + )).toEqual([6, 10, 0]); + + expect(mut_gene_sink( + n_input, n_internal, n_output, + [6, 11, 0], + 1.0 + )).toEqual([6, 11, 0]); +}); + + +test('mutate gene weight', () => { + const weight_max = 4.0; + + expect(mut_gene_weight( + weight_max, [0, 0, 1], 0.0 + )).toEqual([0, 0, (2 - 4)/3]); + + expect(mut_gene_weight( + weight_max, [0, 0, -4], 1.0 + )).toEqual([0, 0, (-8 + 4)/3]); + + expect(mut_gene_weight( + weight_max, [0, 0, 3], 0.5 + )).toEqual([0, 0, (6+0)/3]); +}); -- cgit v1.2.1