-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
22 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters