From f962d4b201480b9e431f0333768a7ed58c7d8297 Mon Sep 17 00:00:00 2001 From: papalotis <36131443+papalotis@users.noreply.github.com> Date: Sat, 9 Jun 2018 14:57:05 +0200 Subject: [PATCH] Fixed issue with ga.js poolSelection The birds array needs to be shuffled, otherwise we go through the array from worse to best --- examples/neuroevolution-flappybird/ga.js | 28 ++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/examples/neuroevolution-flappybird/ga.js b/examples/neuroevolution-flappybird/ga.js index da9585a..b6a4d41 100644 --- a/examples/neuroevolution-flappybird/ga.js +++ b/examples/neuroevolution-flappybird/ga.js @@ -59,10 +59,34 @@ function normalizeFitness(birds) { } } +/** + * Since the p5 shuffle array is deprecated define a new shuffle + * Shuffles array in place. Fisher Yates + * https://stackoverflow.com/a/6274381 + * @param {Array} a items An array containing the items. + */ +function fyShuffle(a) { + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [a[i], a[j]] = [a[j], a[i]]; + } + return a; +} + // An algorithm for picking one bird from an array // based on fitness function poolSelection(birds) { + //make a copy of the birds array + //so that the function remains stateless + clone = birds.slice(); + + //shullfe the array + //otherwise we go through the array, + //from least successful to most which introduces + //a bias towards picking worse pefrorming birds + fyShuffle(clone); + // Start at 0 let index = 0; @@ -73,7 +97,7 @@ function poolSelection(birds) { // Higher probabilities will be more likely to be fixed since they will // subtract a larger number towards zero while (r > 0) { - r -= birds[index].fitness; + r -= clone[index].fitness; // And move on to the next index += 1; } @@ -84,4 +108,4 @@ function poolSelection(birds) { // Make sure it's a copy! // (this includes mutation) return birds[index].copy(); -} \ No newline at end of file +}