diff --git a/examples/neuroevolution-flappybird/bird.js b/examples/neuroevolution-flappybird/bird.js index 2cccc5b..2cfa5db 100644 --- a/examples/neuroevolution-flappybird/bird.js +++ b/examples/neuroevolution-flappybird/bird.js @@ -62,7 +62,7 @@ class Bird { let closest = null; let record = Infinity; for (let i = 0; i < pipes.length; i++) { - let diff = pipes[i].x - this.x; + let diff = pipes[i].x - this.x + pipes[i].w; if (diff > 0 && diff < record) { record = diff; closest = pipes[i]; @@ -111,4 +111,4 @@ class Bird { // Every frame it is alive increases the score this.score++; } -} \ No newline at end of file +} diff --git a/examples/neuroevolution-flappybird/index.html b/examples/neuroevolution-flappybird/index.html index e540f29..7933238 100755 --- a/examples/neuroevolution-flappybird/index.html +++ b/examples/neuroevolution-flappybird/index.html @@ -19,6 +19,8 @@ speed: 1
high score: 0
all time high score: 0 +
generation: 0 +
birds alive: 0

diff --git a/examples/neuroevolution-flappybird/sketch.js b/examples/neuroevolution-flappybird/sketch.js index 58fe6f0..dc4ee32 100644 --- a/examples/neuroevolution-flappybird/sketch.js +++ b/examples/neuroevolution-flappybird/sketch.js @@ -25,6 +25,9 @@ let allTimeHighScoreSpan; // All time high score let highScore = 0; +// Count generations +let generations = 1 + // Training or just showing the current best let runBest = false; let runBestButton; @@ -34,6 +37,8 @@ function setup() { canvas.parent('canvascontainer'); // Access the interface elements + generationText = select('#gens'); + birdsAlive = select('#birdsalive'); speedSlider = select('#speedSlider'); speedSpan = select('#speed'); highScoreSpan = select('#hs'); @@ -159,6 +164,8 @@ function draw() { // Update DOM Elements highScoreSpan.html(tempHighScore); allTimeHighScoreSpan.html(highScore); + generationText.html(generations); + birdsAlive.html(activeBirds.length); // Draw everything! for (let i = 0; i < pipes.length; i++) { @@ -173,7 +180,8 @@ function draw() { } // If we're out of birds go to the next generation if (activeBirds.length == 0) { + generations++; nextGeneration(); } } -} \ No newline at end of file +} diff --git a/lib/nn.js b/lib/nn.js index 0cb4527..4e61c92 100644 --- a/lib/nn.js +++ b/lib/nn.js @@ -17,6 +17,11 @@ let tanh = new ActivationFunction( y => 1 - (y * y) ); +let tanh = new ActivationFunction( + x => (x<0)?0:x, + y => (x<0)?0:1 +); + class NeuralNetwork { /*