/* Particle Swarm Algorithm Class blprnt@blprnt.com July, 2005 This class is an implementation of the particle swarm algorithm (PSA). In a particle swarm, the particles know two things: 1) The best fit that they have found 2) The best fit that the group has found Fits are stored inside of arrays, where the first index is the fitness values and the following indexes are the properties that gave that value. I am using zero as the best fit. ie. if an object finds a fitness of 0.5 at an x position of 300 and a y position of 12, the fit would be stored as [0.5, 300, 12] */ class pswarm.Swarm { var swarm_array:Array; var gbest_array:Array; var best_obj:Object; function Swarm() { }; function initSwarm() :Void { gbest_array = [10000,Stage.width/2,Stage.height/2]; best_obj = {}; for (var n in swarm_array) { best_obj[swarm_array[n]] = [10000,swarm_array[n]._x,swarm_array[n]._y]; init(swarm_array[n]); }; setInterval(control, 30, this); }; function control(target) :Void { target.doSwarm(); }; function doSwarm() :Void { setTarget(); for (var n in swarm_array) { var p = swarm_array[n]; // the particle we're testing - not typed because it could theoretically be anything var f:Array = checkFitness(p); // gets a fitness array (see the note at top) best_obj[p] = (f[0] < best_obj[p][0]) ? (f):(best_obj[p]); // set the individual best for the particle gbest_array = (f[0] < gbest_array[0]) ? (f):(gbest_array); // set the group best for the particle swarmAction(p); }; }; function init(particle) :Void {}; function setTarget() :Void {}; function checkFitness(particle) :Array{ return([0]); }; function swarmAction(particle) {}; };