Skip to content

Commit

Permalink
fixed possible infinite loop (#41)
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 Oct 21, 2022
1 parent 816c08e commit 9298c4c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/architecture/FineTune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export function fineTuneImprovement(
}

let targetJSON = (fittest as Network).toJSON();
for (let k = 0; true; k++) {
for (let k = 0; k < popsize; k++) {
for (let i = targetJSON.nodes.length; i--;) {
const fn = targetJSON.nodes[i];

Expand Down
23 changes: 12 additions & 11 deletions src/architecture/NetworkUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class NetworkUtil {
/**
* Activates the network
*/
activate(input:number[], feedbackLoop = false) {
activate(input: number[], feedbackLoop = false) {
if (input && input.length != this.network.input) {
console.trace();
throw "Activate input: " + input.length +
Expand All @@ -158,21 +158,21 @@ export class NetworkUtil {
if (!feedbackLoop) {
this.networkState.clear(this.network.input);
}
const output:number[] = new Array(this.network.output);
const output: number[] = new Array(this.network.output);
const ns = this.networkState;
for(let i=this.network.input;i--;){
for (let i = this.network.input; i--;) {
ns.node(i).activation = input[i];
}

const lastHiddenNode=this.network.nodes.length-this.network.output;
const lastHiddenNode = this.network.nodes.length - this.network.output;

/* Activate nodes chronologically */
for (let i = this.network.input; i < lastHiddenNode; i++) {
(this.network.nodes[i] as Node).activate();
}

for (let i = 0; i < this.network.output; i++) {
output[i] = (this.network.nodes[i+lastHiddenNode] as Node).activate();
output[i] = (this.network.nodes[i + lastHiddenNode] as Node).activate();
}

return output;
Expand All @@ -181,25 +181,26 @@ export class NetworkUtil {
/**
* Activates the network without calculating eligibility traces and such
*/
noTraceActivate(input:number[], feedbackLoop = false) {
noTraceActivate(input: number[], feedbackLoop = false) {
if (!feedbackLoop) {
this.networkState.clear(this.network.input);
}
const output:number[] = new Array(this.network.output);
const output: number[] = new Array(this.network.output);
const ns = this.networkState;
for(let i=this.network.input;i--;){
for (let i = this.network.input; i--;) {
ns.node(i).activation = input[i];
}

const lastHiddenNode=this.network.nodes.length-this.network.output;
const lastHiddenNode = this.network.nodes.length - this.network.output;

/* Activate nodes chronologically */
for (let i = this.network.input; i < lastHiddenNode; i++) {
(this.network.nodes[i] as Node).noTraceActivate();
}

for (let i = 0; i < this.network.output; i++) {
output[i] = (this.network.nodes[i+lastHiddenNode] as Node).noTraceActivate();
output[i] = (this.network.nodes[i + lastHiddenNode] as Node)
.noTraceActivate();
}

return output;
Expand Down
8 changes: 5 additions & 3 deletions src/architecture/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ export class Node implements TagsInterface, NodeInterface {
fix() {
delete this.squashMethodCache;

if( this.squash !== 'IF'){
if (this.squash !== "IF") {
const toList = this.util.toConnections(this.index);
toList.forEach(c => {delete c.type});
toList.forEach((c) => {
delete c.type;
});
}

if (this.type == "hidden") {
Expand Down Expand Up @@ -522,7 +524,7 @@ export class Node implements TagsInterface, NodeInterface {
switch (method) {
case Mutation.MOD_ACTIVATION.name: {
// Can't be the same squash
for (let attempts=0; attempts<12;attempts++) {
for (let attempts = 0; attempts < 12; attempts++) {
const tmpSquash = Activations
.NAMES[Math.floor(Math.random() * Activations.NAMES.length)];

Expand Down
2 changes: 0 additions & 2 deletions test/TraceAggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { NetworkUtil } from "../src/architecture/NetworkUtil.ts";
((globalThis as unknown) as { DEBUG: boolean }).DEBUG = true;

Deno.test("TraceAggregate", () => {

const json: NetworkInterface = {
nodes: [
{ bias: 0.1, type: "hidden", squash: "LOGISTIC", index: 2 },
Expand Down Expand Up @@ -35,5 +34,4 @@ Deno.test("TraceAggregate", () => {
"output",
startOut,
);

});

0 comments on commit 9298c4c

Please sign in to comment.