diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | src/genome/genome.js | 18 | ||||
-rw-r--r-- | src/genome/genome.test.js | 16 |
3 files changed, 36 insertions, 1 deletions
@@ -1,2 +1,5 @@ yarn-error.log node_modules/ +.* +*.swp +*~ diff --git a/src/genome/genome.js b/src/genome/genome.js index 71e18c1..0f7275f 100644 --- a/src/genome/genome.js +++ b/src/genome/genome.js @@ -1,5 +1,7 @@ 'use strict';
+import { network } from '../mind/topology';
+
export const mutation_type = Object.freeze({
none: 'none',
@@ -71,3 +73,19 @@ export function get_size(num_input, num_output, genome) { return max_index + 1;
}
}
+
+
+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;
+}
diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js index 0ad8d80..8152f04 100644 --- a/src/genome/genome.test.js +++ b/src/genome/genome.test.js @@ -3,6 +3,7 @@ import {
mutation_type, mutate,
get_size,
+ parse_genome,
} from './genome';
@@ -32,5 +33,18 @@ test('genome validation and size', () => { test('parse a genome into a neural net', () => {
-
+ const n = parse_genome(1, 1, [
+ [0, 1, 1],
+ [1, 1, 1],
+ [1, 2, 1]
+ ]);
+
+ console.log(n);
+
+ expect(n.input_count).toBe(1);
+ expect(n.output_count).toBe(1);
+ expect(n.compute([2], [-1])).toEqual([
+ [ Math.tanh( Math.tanh( 2-1 ) ) ],
+ [ Math.tanh( 2-1 ) ],
+ ]);
});
|