This release is 136 versions behind 0.168.1 — the latest version of @stsoftware/neat-ai. Jump to latest
@stsoftware/neat-ai@0.121.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
NEAT Neural Network. This project is a unique implementation of a neural network based on the NEAT (NeuroEvolution of Augmenting Topologies) algorithm, written in DenoJS using TypeScript.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import { assert, assertAlmostEquals } from "jsr:@std/assert@^1.0.8"; import { Creature } from "../src/Creature.ts"; import { createBackPropagationConfig } from "../src/propagate/BackPropagation.ts"; import type { CreatureInternal } from "../src/architecture/CreatureInterfaces.ts"; import { SparseConfig } from "../src/propagate/sparse/SparseConfig.ts"; ((globalThis as unknown) as { DEBUG: boolean }).DEBUG = true; Deno.test("TraceAggregateMINIMUM", () => { const json: CreatureInternal = { neurons: [ { bias: 0.1, type: "hidden", squash: "LOGISTIC", index: 2 }, { bias: -0.2, type: "hidden", squash: "LOGISTIC", index: 3 }, { bias: 0.3, type: "hidden", squash: "MINIMUM", index: 4 }, { bias: -0.4, type: "output", squash: "LOGISTIC", index: 5 }, { bias: 0.5, type: "output", squash: "LOGISTIC", index: 6 }, ], synapses: [ { weight: 0.1, from: 0, to: 2 }, { weight: -0.2, from: 1, to: 3 }, { weight: 0.3, from: 2, to: 4 }, { weight: -0.4, from: 3, to: 4 }, { weight: -0.5, from: 4, to: 5 }, { weight: 0.6, from: 4, to: 6 }, ], input: 2, output: 2, }; const creature = Creature.fromJSON(json); creature.validate(); Deno.writeTextFileSync( "test/data/.a.json", JSON.stringify(creature.exportJSON(), null, 2), ); const input = new Float32Array([0.1, 0.2]); creature.activate(input); const sparseConfig = new SparseConfig( creature.exportJSON(), createBackPropagationConfig({ sparseRatio: 1 }), ); const aOut = creature.activateAndTrace(input, false, sparseConfig); const changed = creature.applyLearnings( createBackPropagationConfig({ trainingMutationRate: 1 }), sparseConfig, ); assert(changed, "should have changed"); const dOut = creature.activate(input); Deno.writeTextFileSync( "test/data/.d.json", JSON.stringify(creature.exportJSON(), null, 2), ); assertAlmostEquals(aOut[0], dOut[0], 0.0001); assertAlmostEquals(aOut[1], dOut[1], 0.0001); }); Deno.test("TraceAggregateMAXIMUM", () => { const json: CreatureInternal = { neurons: [ { bias: 0.1, type: "hidden", squash: "LOGISTIC", index: 2 }, { bias: -0.2, type: "hidden", squash: "LOGISTIC", index: 3 }, { bias: 0.3, type: "hidden", squash: "MAXIMUM", index: 4 }, { bias: -0.4, type: "output", squash: "LOGISTIC", index: 5 }, { bias: 0.5, type: "output", squash: "LOGISTIC", index: 6 }, ], synapses: [ { weight: 0.1, from: 0, to: 2 }, { weight: -0.2, from: 1, to: 3 }, { weight: 0.3, from: 2, to: 4 }, { weight: -0.4, from: 3, to: 4 }, { weight: -0.5, from: 4, to: 5 }, { weight: 0.6, from: 4, to: 6 }, ], input: 2, output: 2, }; const creature = Creature.fromJSON(json); creature.validate(); Deno.writeTextFileSync( "test/data/.A.json", JSON.stringify(creature.exportJSON(), null, 2), ); const input = new Float32Array([0.1, 0.2]); creature.activate(input); const sparseConfig = new SparseConfig( creature.exportJSON(), createBackPropagationConfig({ sparseRatio: 1, }), ); const aOut = creature.activateAndTrace(input, false, sparseConfig); const changed = creature.applyLearnings( createBackPropagationConfig({ trainingMutationRate: 1 }), sparseConfig, ); assert(changed, "should have changed"); const dOut = creature.activate(input); Deno.writeTextFileSync( "test/data/.B.json", JSON.stringify(creature.exportJSON(), null, 2), ); assertAlmostEquals(aOut[0], dOut[0], 0.0001); assertAlmostEquals(aOut[1], dOut[1], 0.0001); }); Deno.test("TraceAggregateIF", () => { const json: CreatureInternal = { neurons: [ { bias: 0.1, type: "hidden", squash: "LOGISTIC", index: 2 }, { bias: -0.2, type: "hidden", squash: "LOGISTIC", index: 3 }, { bias: 0.3, type: "hidden", squash: "IF", index: 4 }, { bias: -0.4, type: "output", squash: "LOGISTIC", index: 5 }, { bias: 0.5, type: "output", squash: "LOGISTIC", index: 6 }, ], synapses: [ { weight: 0.1, from: 0, to: 2 }, { weight: -0.2, from: 1, to: 3 }, { weight: 0.15, from: 1, to: 4, type: "condition" }, { weight: 0.3, from: 2, to: 4, type: "positive" }, { weight: -0.4, from: 3, to: 4, type: "negative" }, { weight: -0.5, from: 4, to: 5 }, { weight: 0.6, from: 4, to: 6 }, ], input: 2, output: 2, }; const creature = Creature.fromJSON(json); creature.validate(); Deno.writeTextFileSync( "test/data/.a.json", JSON.stringify(creature.exportJSON(), null, 2), ); const input = new Float32Array([0.1, 0.2]); creature.activate(input); const sparseConfig = new SparseConfig( creature.exportJSON(), createBackPropagationConfig({ sparseRatio: 1 }), ); const aOut = creature.activateAndTrace(input, false, sparseConfig); const changed = creature.applyLearnings( createBackPropagationConfig({ trainingMutationRate: 1 }), sparseConfig, ); assert(changed, "should have changed"); const dOut = creature.activate(input); Deno.writeTextFileSync( "test/data/.d.json", JSON.stringify(creature.exportJSON(), null, 2), ); assertAlmostEquals(aOut[0], dOut[0], 0.0001); assertAlmostEquals(aOut[1], dOut[1], 0.0001); });