summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/mind/topology.test.js28
-rw-r--r--src/protein/protein.js10
-rw-r--r--src/protein/protein.test.js29
3 files changed, 53 insertions, 14 deletions
diff --git a/src/mind/topology.test.js b/src/mind/topology.test.js
index 9776132..52c196f 100644
--- a/src/mind/topology.test.js
+++ b/src/mind/topology.test.js
@@ -175,20 +175,20 @@ test('arbitrary hidden neurons', () => {
.connect(1, 2, -1)
.connect(2, 3, 2)
- const [output, state] = n.compute([1], [0, 0]);
-
- expect(output).toEqual([
- Math.tanh(
- 2*Math.tanh(
- -1*Math.tanh(1)
- )
- )
- ]);
-
- expect(state).toEqual([
- Math.tanh(1),
- Math.tanh( -Math.tanh(1) ),
- ]);
+ const [output, state] = n.compute([1], [0, 0]);
+
+ expect(output).toEqual([
+ Math.tanh(
+ 2*Math.tanh(
+ -1*Math.tanh(1)
+ )
+ )
+ ]);
+
+ expect(state).toEqual([
+ Math.tanh(1),
+ Math.tanh( -Math.tanh(1) ),
+ ]);
});
diff --git a/src/protein/protein.js b/src/protein/protein.js
new file mode 100644
index 0000000..0223203
--- /dev/null
+++ b/src/protein/protein.js
@@ -0,0 +1,10 @@
+const xnor = (a, b) => a === b ? 1 : 0;
+
+export function compare(pa, pb) {
+ if (pa.length !== pb.length) {
+ throw new Error(`attempted to compare proteins with different lengths: ${pa.length} vs ${pb.length}`);
+ }
+ return (1/pa.length) * pa
+ .map((_, i) => xnor(pa[i], pb[i]))
+ .reduce((acc, val) => acc + val, 0)
+}
diff --git a/src/protein/protein.test.js b/src/protein/protein.test.js
new file mode 100644
index 0000000..61c4896
--- /dev/null
+++ b/src/protein/protein.test.js
@@ -0,0 +1,29 @@
+'use strict';
+
+import { compare } from './protein';
+
+test('compare size-1 proteins', () => {
+ expect(compare([0], [0])).toBe(1);
+ expect(compare([0], [1])).toBe(0);
+ expect(compare([1], [0])).toBe(0);
+ expect(compare([1], [1])).toBe(1);
+});
+
+
+test('compare larger proteins', () => {
+ expect(compare([0,0], [0,0])).toBe(1);
+ expect(compare([1,1], [1,1])).toBe(1);
+ expect(compare([0,1], [1,1])).toBeCloseTo(0.5);
+ expect(compare([0,1,0], [1,1,1])).toBeCloseTo(1/3);
+ expect(compare([0,1,0,1], [1,1,1,1])).toBeCloseTo(0.5);
+ expect(compare([0,1,0,1], [0,1,1,1])).toBeCloseTo(0.75);
+ expect(compare([0,1,0,1], [0,1,1,0])).toBeCloseTo(0.5);
+ expect(compare([0,1,0,0], [0,1,1,0])).toBeCloseTo(0.75);
+ expect(compare([0,1,1,0], [0,1,1,0])).toBeCloseTo(1);
+});
+
+
+test('comparisons between differently-sized proteins throw', () => {
+ expect(() => compare([0], [0, 0])).toThrow();
+ expect(() => compare([0, 0], [0])).toThrow();
+});