summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-08-10 14:57:01 -0500
committersanine-a <sanine.not@pm.me>2023-08-10 14:57:01 -0500
commit0460985d38d137fd02db4919c66d316a83d2919e (patch)
tree736af5601d2fac8cc570c9da7a840e6b5607a688
parenteb03b3a341f3c8152db4ee2ee99b85cffb27796e (diff)
use average weight in mutation and add README.md
-rw-r--r--src/genome/README.md12
-rw-r--r--src/genome/genome.js2
-rw-r--r--src/genome/genome.test.js6
3 files changed, 16 insertions, 4 deletions
diff --git a/src/genome/README.md b/src/genome/README.md
new file mode 100644
index 0000000..1135ed0
--- /dev/null
+++ b/src/genome/README.md
@@ -0,0 +1,12 @@
+src/genome
+==========
+
+Genomes represent the neural network that underlies a creature. They are an array of tuples of the form `[ source, sink, weight ]`, where:
+
+ * `source` is an integer in the range `[0, N - num_outputs)`, representing the index of the source neuron
+ * `sink` is an integer in the range `[num_inputs, N)`, representing the index of the sink neuron
+ * `weight` is a floating-point value in the range `[-4, 4]`, representing the weight of the connection
+
+`num_input` and `num_output` are fixed by the environment (as they are the input senses and output actions of the creature, respectively).
+`N` is not fixed, but is instead determined by the maximum index present in the genome. As long as the maximum index is greater than or
+equal to `num_input + num_output`, the genome is considered valid.
diff --git a/src/genome/genome.js b/src/genome/genome.js
index 8e0c9f0..e2bf83e 100644
--- a/src/genome/genome.js
+++ b/src/genome/genome.js
@@ -41,7 +41,7 @@ export function mutate(gene, type, value) {
case mutation_type.weight:
const w = (8*value) - 4;
- return [ source, sink, w ];
+ return [ source, sink, 0.5*(w + weight) ];
default:
throw new Error(`unknown mutation type: '${type}'`);
diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js
index 853d103..ca56092 100644
--- a/src/genome/genome.test.js
+++ b/src/genome/genome.test.js
@@ -14,7 +14,7 @@ test('basic gene mutations', () => {
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, 0]);
- expect(mutate([0, 1, 2], mutation_type.weight, 0.0)).toEqual([0, 1, -4]);
- expect(mutate([0, 1, 2], mutation_type.weight, 1.0)).toEqual([0, 1, 4]);
+ 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]);
});