summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-08-01 17:40:53 -0500
committersanine-a <sanine.not@pm.me>2023-08-01 17:40:53 -0500
commiteb03b3a341f3c8152db4ee2ee99b85cffb27796e (patch)
treeb617069542b540cc9083f8481c881fc6a3819e52
parent6aae1f6fb4cf289ecf5ed318b34c0e7df62bbf83 (diff)
add util.js
-rw-r--r--src/genome/genome.js2
-rw-r--r--src/mind/topology.js49
-rw-r--r--src/util.js10
3 files changed, 37 insertions, 24 deletions
diff --git a/src/genome/genome.js b/src/genome/genome.js
index 16de162..8e0c9f0 100644
--- a/src/genome/genome.js
+++ b/src/genome/genome.js
@@ -47,3 +47,5 @@ export function mutate(gene, type, value) {
throw new Error(`unknown mutation type: '${type}'`);
};
}
+
+
diff --git a/src/mind/topology.js b/src/mind/topology.js
index 8e59c54..576ee83 100644
--- a/src/mind/topology.js
+++ b/src/mind/topology.js
@@ -1,5 +1,7 @@
'use strict';
+import { create } from '../util';
+
const DEFAULT_WEIGHT_MAX = 4;
@@ -18,12 +20,13 @@ const network_proto = {
// create a new network
export function network(input_count, internal_count, output_count, weight_max = 4) {
const count = input_count + internal_count + output_count;
- const n = Object.create(network_proto);
- n.input_count = input_count;
- n.output_count = output_count;
- n.adjacency = new Array(count).fill([]);
- n.weight = [];
- return Object.freeze(n);
+ const n = create({
+ input_count,
+ output_count,
+ adjacency: new Array(count).fill([]),
+ weight: [],
+ }, network_proto);
+ return n;
}
@@ -53,24 +56,22 @@ function network_connect(n, source, sink, weight) {
throw new Error("attempt to use output as source");
}
- const nn = Object.create(network_proto);
- nn.input_count = n.input_count;
- nn.output_count = n.output_count;
- nn.adjacency = n.adjacency.map((row, i) => {
- if (i === source && i === sink) {
- // self-loop
- return [...row, 2];
- } else if (i === source) {
- return [...row, 1];
- } else if (i === sink) {
- return [...row, -1];
- } else {
- return [...row, 0];
- }
- });
- nn.weight = [...n.weight, weight];
-
- return Object.freeze(nn);
+ return create({
+ ...n,
+ adjacency: n.adjacency.map((row, i) => {
+ if (i === source && i === sink) {
+ // self-loop
+ return [...row, 2];
+ } else if (i === source) {
+ return [...row, 1];
+ } else if (i === sink) {
+ return [...row, -1];
+ } else {
+ return [...row, 0];
+ }
+ }),
+ weight: [...n.weight, weight],
+ }, network_proto);
}
diff --git a/src/util.js b/src/util.js
new file mode 100644
index 0000000..9f52551
--- /dev/null
+++ b/src/util.js
@@ -0,0 +1,10 @@
+'use strict';
+
+
+export function create(obj, proto=Object.prototype) {
+ const props = Object.keys(obj)
+ .map((key) => [ key, { value: obj[key], enumerable: true } ])
+ .reduce((acc, [ key, value ]) => ({ ...acc, [key]: value }), {});
+
+ return Object.create(proto, props);
+};