summaryrefslogtreecommitdiff
path: root/src/genome/genome.test.js
blob: e1c8711916a3573fe24144cc186b8f9fc9ea39b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';

import { 
  get_size,
  parse_genome,
  mut_gene_source,
  mut_gene_sink,
  mut_gene_weight,
} 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('parse a genome into a neural net', () => {
  const n = parse_genome(1, 1, [
    [0, 1, 1],
    [1, 1, 1],
    [1, 2, 1]
  ]);

  expect(n.input_count).toBe(1);
  expect(n.output_count).toBe(1);
	expect(n.compute([2], [-1])).toEqual([
		[ Math.tanh( Math.tanh( 2-1 ) ) ],
		[ 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]);
});