From 6aae1f6fb4cf289ecf5ed318b34c0e7df62bbf83 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 1 Aug 2023 17:03:28 -0500 Subject: add mutation_type and mutate() --- src/genome/genome.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/genome/genome.js (limited to 'src/genome/genome.js') diff --git a/src/genome/genome.js b/src/genome/genome.js new file mode 100644 index 0000000..16de162 --- /dev/null +++ b/src/genome/genome.js @@ -0,0 +1,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}'`); + }; +} -- cgit v1.2.1