diff --git a/js/genetic-0.1.14.js b/js/genetic-0.1.14.js index b40a230..3c48e52 100644 --- a/js/genetic-0.1.14.js +++ b/js/genetic-0.1.14.js @@ -107,89 +107,173 @@ var Genetic = Genetic || (function(){ this.userData = {}; this.internalGenState = {}; + this.callerFunctions = {}; this.entities = []; this.usingWebWorker = false; + this.mapFitnessAsync = function(entities, callback) { + var _this = this; + + var scores = []; + var nbScores = 0; + + entities.forEach(function(entity, i) { + + var setScore = function(score) { + scores[i] = score; + nbScores += 1; + if (nbScores === entities.length) { + callback(scores); + } + }; + + // Supports fitness function sync or async based on number of expected arguments + if (_this.fitness.length === 1) { + setScore(_this.fitness(entity)); + } else { + _this.fitness(entity, setScore); + } + }); + }; + + this.seriesAsync = function(nb, func) { + var self = this; + var results = []; + + var recursion = function(i) { + if(i < nb) { + func.call(self, i, function recursionCallback() { + recursion(i + 1); + }); + } + }; + + recursion(0); + }; + this.start = function() { - var i; var self = this; + // prepare wrapping around caller functions + for(var k in this.callerFunctions) { + this.callerFunctions[k] = this.wrapCallerFunction(k); + } + function mutateOrNot(entity) { // applies mutation based on mutation probability return Math.random() <= self.configuration.mutation && self.mutate ? self.mutate(Clone(entity)) : entity; } // seed the population - for (i=0;i=b},Minimize:function(a,b){return a=b},Minimize:function(a,b){return a + + + Mocha Tests + + + +
+ + + + + + + + + + + + + + \ No newline at end of file