summaryrefslogtreecommitdiff
path: root/src/genome/genome.test.js
blob: 8152f04c427cd9b08fba77fde46763544d487a6c (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
'use strict';

import { 
  mutation_type, mutate,
  get_size,
  parse_genome,
} 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);
  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]
  ]);

  console.log(n);

  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 ) ],
	]);
});