diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/genome/genome.js | 19 | ||||
-rw-r--r-- | src/genome/genome.test.js | 13 |
2 files changed, 31 insertions, 1 deletions
diff --git a/src/genome/genome.js b/src/genome/genome.js index e2bf83e..3445fe9 100644 --- a/src/genome/genome.js +++ b/src/genome/genome.js @@ -49,3 +49,22 @@ export function mutate(gene, type, value) { }
+export function is_valid(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 false;
+ }
+ else if (max_weight > 4.0) {
+ return false;
+ }
+ else {
+ return true;
+ }
+}
diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js index ca56092..f50531b 100644 --- a/src/genome/genome.test.js +++ b/src/genome/genome.test.js @@ -1,6 +1,9 @@ 'use strict';
-import { mutation_type, mutate } from './genome';
+import {
+ mutation_type, mutate,
+ is_valid,
+} from './genome';
test('basic gene mutations', () => {
@@ -18,3 +21,11 @@ test('basic gene mutations', () => { 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', () => {
+ expect(is_valid(0, 0, [ [ 0, 0, 1.0 ] ])).toBe(true);
+ expect(is_valid(2, 1, [ [ 0, 2, 1 ] ])).toBe(true);
+ expect(is_valid(2, 1, [ [ 0, 1, 1 ] ])).toBe(false);
+ expect(is_valid(2, 1, [ [ 0, 2, 5 ] ])).toBe(false);
+});
|