Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code to sort neurons and synapses, remove tags from neurons … #319

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/architecture/CreatureUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@ export class CreatureUtil {
return creature.uuid;
}

if (!creature.synapses) {
throw new Error("Not an object was: " + (typeof creature));
if (!creature.synapses || !creature.neurons) {
throw new Error("Not a creature was: " + (typeof creature));
}

const json = creature.internalJSON();
const json = creature.exportJSON();
json.neurons.sort((a, b) => a.uuid.localeCompare(b.uuid));
json.synapses.sort((a, b) => {
if (a.fromUUID == b.fromUUID) {
return a.toUUID.localeCompare(b.toUUID);
} else {
return a.fromUUID.localeCompare(b.fromUUID);
}
});
json.neurons.forEach(
(n: { uuid?: string }) => {
delete n.uuid;
(n) => {
delete n.tags;
},
);

json.synapses.forEach(
(s) => {
delete s.tags;
},
);

Expand Down
10 changes: 9 additions & 1 deletion src/architecture/Offspring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ export class Offspring {
const toType = offspring.neurons[toIndx].type;
if (toType == "hidden" || toType == "output") {
if (!offspring.getSynapse(fromIndx, toIndx)) {
offspring.connect(fromIndx, toIndx, c.weight, c.type);
const babySynapse = offspring.connect(
fromIndx,
toIndx,
c.weight,
c.type,
);

addTags(babySynapse, c);
}
} else {
throw new Error(
Expand Down Expand Up @@ -250,6 +257,7 @@ export class Offspring {
toUUID: creature.neurons[connection.to].uuid,
weight: connection.weight,
type: connection.type,
tags: connection.tags,
};
tmpConnections.push(c);
});
Expand Down
2 changes: 1 addition & 1 deletion test/CRISPR/From.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
assert,
assertAlmostEquals,
} from "https://deno.land/[email protected]/assert/mod.ts";
import { getTag } from "https://deno.land/x/[email protected]/src/TagsInterface.ts";
import { getTag } from "https://deno.land/x/[email protected]/mod.ts";
import { Creature } from "../../src/Creature.ts";
import { CRISPR } from "../../src/reconstruct/CRISPR.ts";

Expand Down
8 changes: 6 additions & 2 deletions test/CreatureUUID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Deno.test("knownName", async () => {
console.log("UUID", uuid);

assert(
uuid == "df3405ad-5650-549a-975d-f46211716cd4",
uuid == "49b13413-88b9-5688-92f5-ba59bb761639",
"Wrong UUID was: " + uuid,
);
});
Expand All @@ -55,18 +55,21 @@ Deno.test("ignoreTags", async () => {
uuid: crypto.randomUUID(),
neurons: [
{
uuid: "hidden-0",
bias: 0,
index: 5,
type: "hidden",
squash: "IDENTITY",
},
{
uuid: "output-0",
bias: 0.1,
index: 6,
type: "output",
squash: "IDENTITY",
},
{
uuid: "output-1",
bias: 0.2,
index: 7,
type: "output",
Expand Down Expand Up @@ -99,6 +102,7 @@ Deno.test("ignoreTags", async () => {
});

const clean = Creature.fromJSON(creature);
clean.validate();
assertEquals(
creature.uuid,
clean.uuid,
Expand Down Expand Up @@ -129,7 +133,7 @@ Deno.test("ignoreTags", async () => {

/** Manually update if needed. */
assert(
uuid2 == "87dc2a04-b5a2-5bf9-bfb5-dd9419f2f962",
uuid2 == "b08e3db1-f508-5106-b5c6-dfec158a1334",
"Wrong UUID was: " + uuid2,
);
});
Expand Down
2 changes: 1 addition & 1 deletion test/Elitism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from "https://deno.land/[email protected]/assert/mod.ts";
import { Creature } from "../src/Creature.ts";
import { CreatureInternal } from "../src/architecture/CreatureInterfaces.ts";
import { makeElitists } from "../src/architecture/ElitismUtils.ts";
import { addTag } from "https://deno.land/x/[email protected]/src/TagsInterface.ts";
import { addTag } from "https://deno.land/x/[email protected]/mod.ts";

((globalThis as unknown) as { DEBUG: boolean }).DEBUG = true;

Expand Down
2 changes: 1 addition & 1 deletion test/TagCreature.ts → test/Tag/TagCreature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "https://deno.land/[email protected]/assert/mod.ts";
import { addTag, getTag } from "https://deno.land/x/[email protected]/mod.ts";
import { Creature } from "../src/Creature.ts";
import { Creature } from "../../src/Creature.ts";

((globalThis as unknown) as { DEBUG: boolean }).DEBUG = true;

Expand Down
4 changes: 2 additions & 2 deletions test/TagNeuron.ts → test/Tag/TagNeuron.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from "https://deno.land/[email protected]/assert/mod.ts";
import { Creature } from "../src/Creature.ts";
import { Creature } from "../../src/Creature.ts";

import { CreatureInternal } from "../src/architecture/CreatureInterfaces.ts";
import { CreatureInternal } from "../../src/architecture/CreatureInterfaces.ts";

((globalThis as unknown) as { DEBUG: boolean }).DEBUG = true;

Expand Down
63 changes: 63 additions & 0 deletions test/Tag/TagSynapse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
assert,
assertEquals,
} from "https://deno.land/[email protected]/assert/mod.ts";
import { addTag, getTag } from "https://deno.land/x/[email protected]/mod.ts";
import { Creature, CreatureExport, CreatureUtil } from "../../mod.ts";
import { Offspring } from "../../src/architecture/Offspring.ts";

function makeCreature(name: string) {
const json: CreatureExport = {
neurons: [
{ type: "hidden", uuid: "hidden-3", squash: "CLIPPED", bias: 2 },

{
type: "output",
squash: "IDENTITY",
uuid: "output-0",
bias: 1,
},
{
type: "output",
squash: "IDENTITY",
uuid: "output-1",
bias: 0,
},
],
synapses: [
{ fromUUID: "input-0", toUUID: "hidden-3", weight: -0.3 },
{ fromUUID: "input-1", toUUID: "hidden-3", weight: 0.3 },

{ fromUUID: "hidden-3", toUUID: "output-0", weight: 0.6 },

{ fromUUID: "hidden-3", toUUID: "output-1", weight: 0.7 },
{ fromUUID: "input-2", toUUID: "output-1", weight: 0.8 },
],
input: 3,
output: 2,
};
addTag(json.synapses[0], "hello", name);
const creature = Creature.fromJSON(json);
creature.validate();

return creature;
}

Deno.test("TagSynapse", async () => {
const mum = makeCreature("mum");
const mumUUID = await CreatureUtil.makeUUID(mum);
const dad = makeCreature("dad");
const dadUUID = await CreatureUtil.makeUUID(dad);
const baby = Offspring.bread(mum, dad);

assert(baby);

const babyUUID = await CreatureUtil.makeUUID(baby);

const message = getTag(baby.synapses[0], "hello");
assert(message == "mum" || message == "dad", `Lost name ${message}`);

assertEquals(mumUUID, dadUUID);

assertEquals(mumUUID, babyUUID);
});