From 6a4e4c96434d029b2b2e85703eacd92cc76c31da Mon Sep 17 00:00:00 2001 From: aprowe Date: Sun, 24 Jan 2016 16:55:31 -0600 Subject: [PATCH] removed old examples updated XOR --- examples/XOR.js | 67 ++++++++++++--------------------------- examples/letters.js | 53 ------------------------------- examples/metapool.js | 74 -------------------------------------------- package.json | 5 +-- 4 files changed, 22 insertions(+), 177 deletions(-) delete mode 100644 examples/letters.js delete mode 100644 examples/metapool.js diff --git a/examples/XOR.js b/examples/XOR.js index 28ee77c..5d65b09 100644 --- a/examples/XOR.js +++ b/examples/XOR.js @@ -1,59 +1,30 @@ +/* + * This example trains a neural network to solve the XOR problem + */ +var evo = require('evo-js'); -var evo = require('../evo.js'); +var pool = evo.pool(); -var pool = evo.pool({ - n_genes: 10, // Number of genes of each object - size: 400, // Size of pool - - cross_rate: 0.10, - mutate_rate: 0.7, - mutate_amount: 1.85, - - ratios: { - top: 1.00, - cross: 0.33, - mutate: 2.00, - fresh: 0.20, - meld: 0.75, - random: 1.25, - } - -}); - -pool.on ('spawn', function(genes){ +pool.on('run', function(genes){ + // Create a neural network var net = evo.network('feedforward', genes, { output_nodes: 1, - hidden_nodes: 2, + hidden_nodes: 2, input_nodes: 2 }); - return net; -}); - -// Show the average each generation -pool.on('breed', function(){ - // console.log(this.average); + // Create a score based on the networks output + var score = 0; + score += net.calc([-1, -1]) > 0 ? 1 : 0; + score += net.calc([ 1, 1]) > 0 ? 1 : 0; + score += net.calc([ 1, -1]) < 0 ? 1 : 0; + score += net.calc([-1, 1]) < 0 ? 1 : 0; + return score; }); -pool.on('run', function(){ - var net = this.spawn() - - // Max score is 4 if it correctly classifies all points - net.score += net.calc([-1, -1]) > 0 ? 1 : 0; - net.score += net.calc([ 1, 1]) > 0 ? 1 : 0; - net.score += net.calc([ 1, -1]) < 0 ? 1 : 0; - net.score += net.calc([-1, 1]) < 0 ? 1 : 0; - - this.report(net) -}); - -// Run 300 generations -// pool.run(300); - - -// Run until function returns true -pool.run(function(){ - return this.average > 3.50 +// Run until score is 3.5 or greater +pool.run({ + score: 3.5 }); -console.log('Took ' + pool.generation + " generations to reach a score of " + pool.average); +console.log("Took " + pool.generation + " generations to reach a score of " + pool.average); diff --git a/examples/letters.js b/examples/letters.js deleted file mode 100644 index 6c411cc..0000000 --- a/examples/letters.js +++ /dev/null @@ -1,53 +0,0 @@ - -var evo = require('../evo.js'); - -var pool = evo.pool({ - n_genes: 10, // Number of genes of each object - size: 400, // Size of pool - - ratios: { - top: 0.25, - cross: 0.20, - mutate: 0.40, - meld: 0.1, - fresh: 0.05 - } - -}); - -pool.seed = function(){ - return evo.util.sample('abcdefghijklmnopqrstuvwxyz'); -} - -pool.mutate = function(genes) { - var g = []; - for (var i = 0; i < genes.length; i++) { - g.push(genes[i]); - if (Math.random() < this.config.mutate_rate){ - return evo.util.sample('abcdefghijklmnopqrstuvwxyz'); - } - } - return g; -} - -// Show the average each generation -pool.on('breed', function(){ - // console.log(this.average); -}); - -pool.on('run', function(){ - var genes = this.next(); - var score = 0; - - for (var i = 0; i < genes.length-1; i++) { - score += genes[i] == genes[i+1]; - } - - pool.report(genes, score); -}); - -// Run 300 generations -pool.run(10); - -console.log('Took ' + pool.generation + " generations to reach a score of " + pool.average); - diff --git a/examples/metapool.js b/examples/metapool.js deleted file mode 100644 index 3de2b87..0000000 --- a/examples/metapool.js +++ /dev/null @@ -1,74 +0,0 @@ -/* ********************************************** - * This example breeds a pool to achieve the best - * Parameters for breeding a pool on the XOR problem. - * See XOR.js for the XOR network pool - *********************************************/ - - -var evo = require('../evo.js'); - -var smallpool = function(g){ - copy = g.slice(0).reverse(); - return evo.pool({ - n_genes: 10, // Number of genes of each object - size: 100, // Size of pool - cross_rate: Math.abs(copy.pop())/10, - mutate_rate: Math.abs(copy.pop())/10, - mutate_amount: Math.abs(copy.pop()), - autospawn: true, - ratios: { - top: Math.abs(copy.pop()), - cross: Math.abs(copy.pop()), - mutate: Math.abs(copy.pop()), - fresh: Math.abs(copy.pop()), - meld: Math.abs(copy.pop()), - random: Math.abs(copy.pop()) - }, - on_spawn: function(genes){ - var net = evo.network('feedforward', genes, { - output_nodes: 1, - hidden_nodes: 2, - input_nodes: 2 - }); - return net; - }, - on_run: function(net){ - net.score += net.calc([ 0, 0]) > 0.5 ? 1 : 0; - net.score += net.calc([ 1, 1]) > 0.5 ? 1 : 0; - net.score += net.calc([ 1, 0]) < 0.5 ? 1 : 0; - net.score += net.calc([ 0, 1]) < 0.5 ? 1 : 0; - }, - }); -}; - -var metapool = evo.pool({ - n_genes: 9, // Number of genes of each object - size: 100, // Size of pool - - cross_rate: 0.14, - mutate_rate: 0.05, - mutate_amount: 1.0, - autospawn: true, - - ratios: { - top: 0.23, - cross: 2.02, - mutate: 0.71, - meld: 0.75, - fresh: 0.18, - random: 0.10 - }, - on_spawn: function(genes) { - return smallpool(genes); - }, - on_run: function(pool) { - pool.run(function(){return this.generation > 300 || this.average > 3.5}); - pool.score = -pool.generation - }, - on_breed: function(){ - console.log("Gen "+this.generation+": "+this.average); - } - -}); - -// metapool.run(1) \ No newline at end of file diff --git a/package.json b/package.json index 5bd8bcc..63a5931 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "evo-js", "version": "0.2.1", - "description": "Genetic Algorithm Calculator with ANN", + "description": "Evolutionary Algorithm Tool wrapped with ANN", "main": "evo.js", "scripts": { "test": "grunt test" @@ -33,5 +33,6 @@ "grunt-contrib-uglify": "~0.5.0", "grunt-contrib-watch": "^0.6.1", "grunt-execute": "^0.2.2" - } + }, + "tonicExampleFilename":"examples/XOR.js" }