summaryrefslogtreecommitdiff
path: root/src/genome/genome.js
blob: bc569ff0be39c266e7a71a71a126f7edf5ea675f (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';

import { random_choice } from '../util.js';
import { network } from '../mind/topology.js';


// check if a given genome is valid and compute its size
export function get_size(num_input, num_output, genome) {
  const [ max_index, max_weight ] = genome.reduce(
    ([max_index, max_weight ], [ source, sink, weight]) => [
      Math.max(max_index, source, sink),
      Math.max(max_weight, Math.abs(weight)),
    ],
    [ 0, 0 ]
  );

  if (max_index < num_input + num_output - 1) {
    return -1;
  }
  else if (max_weight > 4.0) {
    return -1;
  }
  else {
    return max_index + 1;
  }
}


// parse a genome into a useable neural net
export function parse_genome(num_input, num_output, genome) {
  const size = get_size(num_input, num_output, genome);
  if (size < 0) {
    // bad genome
    throw new Error('invalid genome sequence!');
  }

  const n = genome.reduce(
    (acc, [source, sink, weight]) => acc.connect(source, sink, weight),
    network(num_input, size-num_input-num_output, num_output)
  );

  return n;
}


// --===== mutations =====--

function clamp(value, min, max) {
  if (value > max) { return max; }
  if (value < min) { return min; }
  return value;
}

// adjust the source input of a gene
export function mut_gene_source(n_input, n_internal, n_output, gene, r) {
  const [source, sink, weight] = gene;

  const new_source = r < 0.5 ? source-1 : source+1;

  return [
    clamp(new_source, 0, n_input+n_internal-1), 
    sink, 
    weight,
  ];
}


// adjust the sink target of a gene
export function mut_gene_sink(n_input, n_internal, n_output, gene, r) {
  const [source, sink, weight] = gene;

  const new_sink = r < 0.5 ? sink-1 : sink+1;

  return [
    source, 
    clamp(new_sink,  n_input+n_internal, n_input+n_internal+n_output-1), 
    weight,
  ];
}


// modify a gene's weight
// only adjusts the weight by performing a weighted average, so as to
// more gently modify the generated net
export function mut_gene_weight(weight_max, gene, r) {
  const [source, sink, weight] = gene;
  
  const rr = (2*r)-1;
  const move = weight_max * rr;
  const new_weight = (2*weight + move)/3;

  return [
    source,
    sink,
    clamp(new_weight, -weight_max, weight_max),
  ];
}



// expand the size of the neural net encoded by the genome
// relabels internal indices so that there is one extra internal neuron
export function mut_genome_expand(
  [n_input, n_internal, n_output, genome], r
) {
  const expand_index = Math.floor(n_internal * r) + n_input;
  const new_genome = genome.map(([source, sink, weight]) => [
    source >= expand_index ? source+1 : source,
    sink >= expand_index ? sink+1 : sink,
    weight,
  ]);

  return [
    n_input, n_internal+1, n_output, new_genome,
  ];
}


// contract the size of the neural net encoded by the genome
// relabels internal indices so that there is one less internal neuron
export function mut_genome_contract(
  [n_input, n_internal, n_output, genome], r
) {
  const contract_idx = Math.floor(n_internal * r) + n_input;

  // decrement sources on the contract index too, to prevent invalid genomes
  const new_source = (source) => source >= contract_idx ? source-1 : source;
  // decrement sinks only after the contract index
  const new_sink   = (sink)   => sink > contract_idx ? sink-1 : sink;

  const new_genome = genome.map(([source, sink, weight]) => [
    new_source(source), 
    new_sink(sink), 
    weight,
  ]);

  return [
    n_input, n_internal-1, n_output, new_genome
  ];
}


// append a newly generated gene to the end of the genome
export function mut_genome_insert(
  [n_input, n_internal, n_output, genome], 
  weight_max, 
  r1, r2, r3
) {
  const source = Math.floor((n_input + n_internal) * r1);
  const sink   = Math.floor((n_internal + n_output) * r2) + n_input;
  const weight = weight_max * ((2*r3)-1);

  return [
    n_input, n_internal, n_output,
    [...genome, [source, sink, weight]],
  ];
}


// delete a gene from the genome
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];
}


function mut_gene(
  [n_input, n_internal, n_output, genome], 
  weight_max, r1, r2, r3
) {
  const gene_idx = Math.floor(genome.length * r1);
  const mod = random_choice(['source', 'sink', 'weight'], r2);
  let new_gene;
  if (mod == 'source') {
    new_gene = mut_gene_source(
      n_input, n_internal, n_output, 
      genome[gene_idx],
      r3
    );
  } else if (mod == 'sink') {
    new_gene = mut_gene_sink(
      n_input, n_internal, n_output, 
      genome[gene_idx],
      r3
    );
  } else {
    new_gene = mut_gene_weight(
      weight_max, genome[gene_idx], r3
    );
  }

  const new_genome = genome.map((gene, idx) => {
    if (idx == gene_idx) { return new_gene; }
    return gene;
  });

  return [
    n_input, n_internal, n_output, new_genome
  ];
}


export function mutate_genome(obj, weight_max) {
  const mut = random_choice([
    'gene', 'gene', 'gene',
    'gene', 'gene', 'gene',
    'gene', 'gene', 'gene',
    'insert', 'delete',
    'insert', 'delete',
    'expand', 'contract',
  ], Math.random());

  if (mut == 'gene') {
    return mut_gene(
      obj, weight_max, 
      Math.random(), Math.random(), Math.random()
    );
  } else if (mut == 'insert') {
    return mut_genome_insert(
      obj, weight_max, 
      Math.random(), Math.random(), Math.random()
    );
  } else if (mut == 'delete') {
    return mut_genome_delete(obj, Math.random());
  } else if (mut == 'expand') {
    return mut_genome_expand(obj, Math.random());
  } else if (mut == 'contract') {
    return mut_genome_contract(obj, Math.random());
  } else {
    throw new Error(`bad mut value: ${mut}`);
  }
}