Skip to content
/ PSO.js Public

Implementation of a Particle Swarm Optimizer in Javascript

License

Notifications You must be signed in to change notification settings

nrox/PSO.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

58ef970 · Aug 31, 2013

History

6 Commits
Aug 31, 2013
Aug 31, 2013
Aug 31, 2013
Aug 31, 2013
Aug 31, 2013

Repository files navigation

PSO.js

Implementation of a Particle Swarm Optimizer in Javascript

This optimizer can produce similar results as Genetic Algorithms, and is suitable for cases where the variables to optimize are real numbers, rather than discrete quantities.

In this implementation the initial population is generated randomly, with each dimension in the interval -0.5 to +0.5. During the evolution this interval may be trespassed.

You must supply a fitness function (defaults to distance to origin), and a stop criteria function to verify if the evolution has achieved satisfactory results.

Example of usage:

var pso = new PSO({
    dimensions: 2, //position dimensions {x,y,...}
    fitness:  function(position){ //fitness function to evaluate each individual
        var sum = 0;
        for (var i = 0; i < pos.length; i++){
            sum += pos[i] * pos[i];
        }
        return Math.sqrt(sum);
    },
    criteria: function(position){ //criteria to stop
        return this.fitness(position)<0.01;
    }
});

//step into the evolution process until the result is satisfactory or the number of steps has been reached
pso.evolve();

//log the best result
console.log(pso.result());

The argument to the constructor is an object with the following defaults:

var defaults = {
    size: 20,
    dimensions: 2,
    degree: 4,
    steps: 10000,
    fitness: function (pos) {
        var sum = 0;
        for (var i = 0; i < pos.length; i++) {
            sum += pos[i] * pos[i];
        }
        return Math.sqrt(sum);
    },
    criteria: function (pos) {
        return this.fitness(pos) <= 1.0e-2;
    },
    C1: 2.05,
    C2: 2.05,
    X: 0.729,
    noise: 1.0e-12
};

Clone the repository and open test.html for a working example.

Reference: Particle Swarm Optimization, Kennedy & Eberhart

About

Implementation of a Particle Swarm Optimizer in Javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published