JSwarm-PSO
JSwarm-PSO is a Particle swarm optimization package written in Java. PSO is an optimization technique used to find global optimum for complex problems. Roughly stated, it's in the same 'category' as Genetic algorithms or Simmilated annealing. If you don't know what PSO is, I recommend you to start reading wikipedia's definition. JSwarm-PSO is designed to require minimum effort to use while also highly modular.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Optimization example
This example tries to minimize Rastrigin's function which has infinie local minimum and maximum.
You can run the example just typing the command (on unix like enviroments):
java -jar jswarm-pso_VERSION.jar |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How to optimize your own function / model
This is a simple description of the code you'll find in jswarm-pso.example1:
- Create your own Fitness function. It must derive from jswarm_pso.FitnessFunction and custom evaluate method must be created.
import jswarm_pso.FitnessFunction; public class MyFitnessFunction extends FitnessFunction { public double evaluate(double position[]) { return position[0] + position[1]; } }
- Create your own particle derived from jswarm_pso.Particle
import jswarm_pso.Particle; public class MyParticle extends Particle { // Create a 2-dimentional particle public MyParticle() { super(2); } }
- Cerate a Swarm and evolve for a few iterations (use your favourite stop criteria)
// Create a swarm (using 'MyParticle' as sample particle // and 'MyFitnessFunction' as finess function) Swarm swarm = new Swarm(Swarm.DEFAULT_NUMBER_OF_PARTICLES , new MyParticle() , new MyFitnessFunction()); // Set position (and velocity) constraints. // i.e.: where to look for solutions swarm.setMaxPosition(1); swarm.setMinPosition(0); // Optimize a few times for( int i = 0; i < 20; i++ ) swarm.evolve(); // Print en results System.out.println(swarm.toStringStats())
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Usuall / Deafult settings
Optimization type:
- FitnessFunction.setMaximize(): [Deafult: true]
- true if you want to maximize your fitness function,
- false if you want to minimize it.
- Swarm.globalIncrement: Increment used to update particle's velocity towards global best position. [Default value: 0.9];
- Swarm.inertia: Particle's inertia (to keep prior velocity). [Default: 1.0]
- Swarm.particleIncrement: Increment used to update particle's velocity towards local best position. [Default value: 0.9];
- Swarm.maxPosition[]: Maximum possible position for each dimention (restricts where the particles can move)
- Swarm.minPosition[]: Minumum possible position for each dimention (restricts where the particles can move)
- Swarm.maxVelocity[]: Maximum possible velocity for each dimention (restricts how fast the particle can move)
- Swarm.minVelocity[]: Minumum possible velocity for each dimention (restricts how fast the particle can move with negative speed).
- Warning: this setting should be minVelocity[i] = - maxVelocity[i], except on very strange optimization problems.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Particle position and velocity update
Each time the swarm evolves, updates every particle's position and velocity (see jswarm-pso.Particle.update() for details).
Particle's position is updated (for each dimention i):
// Update position position[i] = position[i] + velocity[i] |
While particle's velocity is updated (for each dimention i):
// Update velocity velocity[i] = inertia * velocity[i] // Inertia + rlocal[i] * particleIncrement * (bestPosition[i] - position[i]) // Local best + rglobal[i] * globalIncrement * (globalBestPosition[i] - position[i]); // Global best |
where rlocal[i] and rglobal[i] are two random vectors, uniformly distributted on the interval [0,1].
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -