Skip to content

Commit

Permalink
fixed breading with many outputs. (#94)
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 May 10, 2023
1 parent d053b86 commit 257d7ba
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 21 deletions.
33 changes: 17 additions & 16 deletions src/architecture/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,14 @@ export class Network implements NetworkInternal {
console.trace();
throw indx + ") connection points to an input node";
}
const fromNode = this.getNode(c.from);
// const fromNode = this.getNode(c.from);

if (fromNode.type === "output") {
if (c.from != c.to) {
console.trace();
throw indx + ") connection from an output node";
}
}
// if (fromNode.type === "output") {
// if (c.from != c.to) {
// console.trace();
// throw indx + ") connection from an output node";
// }
// }

if (c.from < lastFrom) {
console.info(JSON.stringify(this.connections, null, 1));
Expand Down Expand Up @@ -758,14 +758,14 @@ export class Network implements NetworkInternal {
throw "to should be a non-negative integer was: " + to;
}

const firstOutputIndex = this.nodes.length - this.output;
if (from >= firstOutputIndex && from !== to) {
console.trace();
throw "from should not be from an output node (" + firstOutputIndex +
", len: " + this.nodes.length + ", output: " +
this.output +
"): " + from;
}
// const firstOutputIndex = this.nodes.length - this.output;
// if (from >= firstOutputIndex && from !== to) {
// console.trace();
// throw "from should not be from an output node (" + firstOutputIndex +
// ", len: " + this.nodes.length + ", output: " +
// this.output +
// "): " + from;
// }

if (to < this.input) {
console.trace();
Expand Down Expand Up @@ -1576,7 +1576,7 @@ export class Network implements NetworkInternal {
// Create an array of all uncreated (feedforward) connections
const available = [];

for (let i = 0; i < this.nodes.length - this.output; i++) {
for (let i = 0; i < this.nodes.length; i++) {
const node1 = this.nodes[i];

if ((node1 as NodeInternal).index != i) {
Expand Down Expand Up @@ -2028,6 +2028,7 @@ export class Network implements NetworkInternal {
pos < this.nodes.length - this.output;
pos++
) {
if (this.nodes[pos].type == "output") continue;
if (
this.fromConnections(pos).filter((c) => {
return c.from !== c.to;
Expand Down
7 changes: 2 additions & 5 deletions src/architecture/Offspring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ export class Offspring {
if (node === undefined || node.type === "output") {
const other = random < 0.5 ? network1.nodes[i] : network2.nodes[i];

if (other.type === "output") {
console.trace();
throw i + ") Should not be an 'output' node";
if (other != undefined && other.type !== "output") {
node = other;
}

node = other;
}
} else {
if (Math.random() >= 0.5) {
Expand Down
141 changes: 141 additions & 0 deletions test/OffSpring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,144 @@ function check() {
throw "Did not find " + missingUUID;
}
}

Deno.test(
"Many Outputs",
() => {
const creature: NetworkInternal = {
nodes: [
{
uuid: crypto.randomUUID(),
bias: 0,
index: 5,
type: "hidden",
squash: "IDENTITY",
},
{
uuid: crypto.randomUUID(),
bias: 0.1,
index: 6,
type: "hidden",
squash: "MAXIMUM",
},
{
uuid: crypto.randomUUID(),
bias: 0.2,
index: 7,
type: "output",
squash: "MINIMUM",
},
{
uuid: crypto.randomUUID(),
bias: 0.08,
index: 8,
type: "output",
squash: "IDENTITY",
},
{
uuid: crypto.randomUUID(),
bias: 0.09,
index: 9,
type: "output",
squash: "IDENTITY",
},
{
uuid: crypto.randomUUID(),
bias: 0.10,
index: 10,
type: "output",
squash: "IDENTITY",
},
{
uuid: crypto.randomUUID(),
bias: 0.11,
index: 11,
type: "output",
squash: "IDENTITY",
},

{
uuid: crypto.randomUUID(),
bias: 0.12,
index: 12,
type: "output",
squash: "IDENTITY",
},
{
uuid: crypto.randomUUID(),
bias: 0.13,
index: 13,
type: "output",
squash: "IDENTITY",
},
{
uuid: crypto.randomUUID(),
bias: 0.14,
index: 14,
type: "output",
squash: "IDENTITY",
},
],
connections: [
{
weight: -0.1,
from: 1,
to: 5,
},
{
weight: -0.2,
from: 2,
to: 7,
},
{
weight: -0.3,
from: 3,
to: 5,
},
{
weight: 0.2,
from: 4,
to: 7,
},
{
weight: 0.1,
from: 5,
to: 6,
},
{
weight: 0.3,
from: 6,
to: 7,
},
],
input: 5,
output: 8,
};

const n1 = Network.fromJSON(creature);
n1.fix();
n1.validate();

const n2 = Network.fromJSON(n1.exportJSON());

n2.validate();

for (let i = 0; i < 20; i++) {
n2.addNode();
n2.addConnection();
// n1.addConnection();
}

n2.validate();

for (let i = 0; i < 20; i++) {
const child = Offspring.bread(n1, n2);
child.validate();
}

for (let i = 0; i < 20; i++) {
const child = Offspring.bread(n2, n1);
child.validate();
}
},
);

0 comments on commit 257d7ba

Please sign in to comment.