Skip to content

Commit

Permalink
Refactor neuron sorting logic in Offspring class (#321)
Browse files Browse the repository at this point in the history
Co-authored-by: Nigel (MacBook) <nigel@laptop>
  • Loading branch information
nleck and Nigel (MacBook) authored Mar 18, 2024
1 parent 3705295 commit 4ebbfb8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
70 changes: 37 additions & 33 deletions src/architecture/Offspring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,24 @@ export class Offspring {
if (neuron.type == "input") childMap.set(neuron.uuid, indx);
});

const motherMap = new Map<string, number>();
const fatherMap = new Map<string, number>();
const mumMap = new Map<string, number>();
const dadMap = new Map<string, number>();

mother.forEach((node, indx) => {
motherMap.set(node.uuid, indx);
mumMap.set(node.uuid, indx);
});

father.forEach((node, indx) => {
fatherMap.set(node.uuid, indx);
dadMap.set(node.uuid, indx);
});

let firstMap = mumMap;
let secondMap = dadMap;
if (mumMap.size < dadMap.size) {
firstMap = dadMap;
secondMap = mumMap;
}

/* Sort output to the end and input to the beginning */
child.sort((a: Neuron, b: Neuron) => {
if (a.type == "output") {
Expand All @@ -299,32 +306,29 @@ export class Offspring {
} else if (b.type == "output") {
return -1;
}
if (a.index == b.index) {
if (a.uuid == b.uuid) {
throw new Error(`Duplicate uuid ${a.uuid}`);
}
const mumIndxA = motherMap.get(a.uuid);
if (mumIndxA == undefined) {
return -1;
}
const dadIndxA = fatherMap.get(a.uuid);
if (dadIndxA == undefined) {
return 1;
}
const mumIndxB = motherMap.get(b.uuid);
if (mumIndxB == undefined) {
return -1;
}

const dadIndxB = fatherMap.get(b.uuid);
if (dadIndxB == undefined) {
return 1;
}
// if (a.index == b.index) {
if (a.uuid == b.uuid) {
throw new Error(`Duplicate uuid ${a.uuid}`);
}
let indxA = firstMap.get(a.uuid);
if (indxA == undefined) {
indxA = secondMap.get(a.uuid);
if (indxA == undefined) throw new Error(`Can't find ${a.uuid}`);
indxA += 0.1;
}

return mumIndxA - mumIndxB;
let indxB = firstMap.get(b.uuid);
if (indxB == undefined) {
indxB = secondMap.get(b.uuid);
if (indxB == undefined) throw new Error(`Can't find ${b.uuid}`);
indxB += 0.1;
}
return a.index - b.index;

if (indxA == indxB) throw new Error(`Duplicate index ${indxA}`);

return indxA - indxB;
});

const usedIndx = new Set<number>();
let missing = true;
for (let attempts = 0; missing && attempts < child.length; attempts++) {
Expand All @@ -334,14 +338,14 @@ export class Offspring {
const uuid = neuron.uuid;

if (!childMap.has(uuid)) {
const motherIndx = motherMap.get(uuid);
const fatherIndx = fatherMap.get(uuid);
const firstIndx = firstMap.get(uuid);
const secondIndx = secondMap.get(uuid);

let indx = 0;
if (motherIndx !== undefined) {
indx = motherIndx;
} else if (fatherIndx !== undefined) {
indx = fatherIndx;
if (firstIndx !== undefined) {
indx = firstIndx;
} else if (secondIndx !== undefined) {
indx = secondIndx;
} else {
throw new Error(
`Can't find ${uuid} in father or mother creatures!`,
Expand Down
2 changes: 1 addition & 1 deletion test/Offspring/SortNeurons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Deno.test(
assertEquals(sorted[4].uuid, "dad-a");
assertEquals(sorted[5].uuid, "mum-a");

assertEquals(sorted[sorted.length - 3].uuid, "dad-c");
assertEquals(sorted[sorted.length - 3].uuid, "common-d");
assertEquals(sorted[sorted.length - 2].uuid, "output-0");
assertEquals(sorted[sorted.length - 1].uuid, "output-1");
},
Expand Down

0 comments on commit 4ebbfb8

Please sign in to comment.