From 444b2b5abfbb70473b0785b38eaba1df4197ae69 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 12 Nov 2023 05:17:19 -0600 Subject: refactor genome to include size information --- src/genome/genome.test.js | 185 +++++++++++++++++++++++++--------------------- 1 file changed, 101 insertions(+), 84 deletions(-) (limited to 'src/genome/genome.test.js') diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js index 37bdacc..bc64e4e 100644 --- a/src/genome/genome.test.js +++ b/src/genome/genome.test.js @@ -1,7 +1,14 @@ 'use strict'; +// genome structure +// { +// genes: gene[] +// n_input, n_internal, n_output +// } + + import { - get_size, + validate_genome, parse_genome, mut_gene_source, mut_gene_sink, @@ -13,20 +20,39 @@ import { } from './genome'; -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); - expect(get_size(2, 1, [ [ 0, 1, 1 ] ])).toBe(-1); - expect(get_size(2, 1, [ [ 0, 2, 5 ] ])).toBe(-1); +test('genome validation', () => { + expect(validate_genome({ + n_input: 0, n_internal: 1, n_output: 0, + genes: [[0, 0, 1.0]], + })).toBe(true); + expect(validate_genome({ + n_input: 2, n_internal: 0, n_output: 1, + genes: [[0, 2, 1]], + })).toBe(true); + expect(validate_genome({ + n_input: 2, n_internal: 0, n_output: 1, + genes: [[2, 0, 1]], + })).toBe(false); + expect(validate_genome({ + n_input: 2, n_internal: 0, n_output: 1, + genes: [[2, 2, 1]], + })).toBe(false); + expect(validate_genome({ + n_input: 2, n_internal: 1, n_output: 1, + genes: [[3, 2, 1]], + })).toBe(false); }); test('parse a genome into a neural net', () => { - const n = parse_genome(1, 1, [ - [0, 1, 1], - [1, 1, 1], - [1, 2, 1] - ]); + const n = parse_genome({ + n_input: 1, n_internal: 1, n_output: 1, + genes: [ + [0, 1, 1], + [1, 1, 1], + [1, 2, 1] + ] + }); expect(n.input_count).toBe(1); expect(n.output_count).toBe(1); @@ -121,48 +147,45 @@ test('expand genome', () => { 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, - [ + const genome = { + n_input, n_internal, n_output, + genes: [ + [0, 1, 0], + [1, 2, 0], + [2, 3, 0], + [3, 4, 0], + ], + }; + + expect(mut_genome_expand(genome, 0.0)).toEqual({ + n_input, n_internal: n_internal+1, n_output, + genes: [ [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, - [ + expect(mut_genome_expand(genome, 0.5)).toEqual({ + n_input, n_internal: n_internal+1, n_output, + genes: [ [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, - [ + expect(mut_genome_expand(genome, 0.99)).toEqual({ + n_input, n_internal: n_internal+1, n_output, + genes: [ [0, 1, 0], [1, 2, 0], [2, 4, 0], [4, 5, 0], ], - ]); + }); }); @@ -171,48 +194,45 @@ test('contract genome', () => { 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, - [ + const genome = { + n_input, n_internal, n_output, + genes: [ + [0, 1, 0], + [1, 2, 1], + [2, 3, 2], + [3, 4, 3], + ], + }; + + expect(mut_genome_contract(genome, 0.0)).toEqual({ + n_input, n_internal: n_internal-1, n_output, + genes: [ [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, - [ + expect(mut_genome_contract(genome, 0.5)).toEqual({ + n_input, n_internal: n_internal-1, n_output, + genes: [ [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, - [ + expect(mut_genome_contract(genome, 0.99)).toEqual({ + n_input, n_internal: n_internal-1, n_output, + genes: [ [0, 1, 0], [1, 2, 1], [2, 3, 2], [2, 3, 3], ], - ]); + }); }); @@ -223,21 +243,21 @@ test('insert new genes', () => { const n_output = 1; const weight_max = 4; - expect(mut_genome_insert([ + expect(mut_genome_insert({ n_input, n_internal, n_output, - [] - ], weight_max, 0, 0.5, 0)).toEqual([ + genes: [] + }, weight_max, 0, 0.5, 0)).toEqual({ n_input, n_internal, n_output, - [[0, 2, -4]] - ]); + genes: [[0, 2, -4]] + }); - expect(mut_genome_insert([ + expect(mut_genome_insert({ n_input, n_internal, n_output, - [[0, 2, -4]] - ], weight_max, 0.99, 0, 1)).toEqual([ + genes: [[0, 2, -4]] + }, weight_max, 0.99, 0, 1)).toEqual({ n_input, n_internal, n_output, - [[0, 2, -4], [2, 1, 4]] - ]); + genes: [[0, 2, -4], [2, 1, 4]] + }); }); @@ -245,21 +265,18 @@ test('remove genes', () => { const n_input = 0; const n_output = 0; const n_internal = 3; - const genome = [ - [0, 1, 0], [1, 2, 0], - ]; + const genome = { + n_input, n_internal, n_output, + genes: [[0, 1, 0], [1, 2, 0]], + }; - expect(mut_genome_delete([ - n_input, n_internal, n_output, genome - ], 0.0)).toEqual([ + expect(mut_genome_delete(genome, 0.0)).toEqual({ n_input, n_internal, n_output, - [[1, 2, 0]], - ]); + genes: [[1, 2, 0]], + }); - expect(mut_genome_delete([ - n_input, n_internal, n_output, genome - ], 0.99)).toEqual([ + expect(mut_genome_delete(genome, 0.99)).toEqual({ n_input, n_internal, n_output, - [[0, 1, 0]], - ]); + genes: [[0, 1, 0]], + }); }); -- cgit v1.2.1