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
|
'use strict';
export const mutation_type = Object.freeze({
none: 'none',
source: 'source',
sink: 'sink',
weight: 'weight',
});
function positive(x) {
if (x < 0) {
return 0;
} else {
return x;
}
}
export function mutate(gene, type, value) {
const [ source, sink, weight ] = gene;
switch(type) {
case mutation_type.none:
return [...gene];
case mutation_type.source:
if (value <= 0.5) {
return [ positive(source-1), sink, weight ];
} else {
return [ source+1, sink, weight ];
}
case mutation_type.sink:
if (value <= 0.5) {
return [ source, positive(sink-1), weight ];
} else {
return [ source, sink+1, weight ];
}
case mutation_type.weight:
const w = (8*value) - 4;
return [ source, sink, w ];
default:
throw new Error(`unknown mutation type: '${type}'`);
};
}
|