net.sourceforge.jswarm_pso
Class Swarm

java.lang.Object
  extended by net.sourceforge.jswarm_pso.Swarm
Direct Known Subclasses:
SwarmRepulsive

public class Swarm
extends java.lang.Object

A swarm of particles

Author:
Pablo Cingolani

Field Summary
(package private)  double bestFitness
          Best fitness so far (global best)
(package private)  int bestParticleIndex
          Index of best particle so far
(package private)  double[] bestPosition
          Best position so far (global best)
static double DEFAULT_GLOBAL_INCREMENT
           
static double DEFAULT_INERTIA
           
static int DEFAULT_NUMBER_OF_PARTICLES
           
static double DEFAULT_PARTICLE_INCREMENT
           
(package private)  FitnessFunction fitnessFunction
          Fitness function for this swarm
(package private)  double globalIncrement
          Global increment (for velocity update), usually called 'c2' constant
(package private)  double inertia
          Inertia (for velocity update), usually called 'w' constant
(package private)  double[] maxPosition
          Maximum position (for each dimention)
(package private)  double[] maxVelocity
          Maximum Velocity (for each dimention)
(package private)  double[] minPosition
          Minimum position (for each dimention)
(package private)  double[] minVelocity
          Minimum Velocity for each dimention.
(package private)  int numberOfEvaliations
          How many times 'particle.evaluate()' has been called?
(package private)  int numberOfParticles
          Number of particles in this swarm
(package private)  double particleIncrement
          Particle's increment (for velocity update), usually called 'c1' constant
(package private)  Particle[] particles
          Particles in this swarm
(package private)  ParticleUpdate particleUpdate
          Particle update strategy
(package private)  Particle sampleParticle
          A sample particles: Build other particles based on this one
(package private)  VariablesUpdate variablesUpdate
          Variables update
static double VELOCITY_GRAPH_FACTOR
           
 
Constructor Summary
Swarm(int numberOfParticles, Particle sampleParticle, FitnessFunction fitnessFunction)
          Create a Swarm and set default values
 
Method Summary
 void evaluate()
          Evaluate fitness function for every particle Warning: particles[] must be initialized and fitnessFunction must be setted
 void evolve()
          Make an iteration: - evaluates the swarm - updates positions and velocities - applies positions and velocities constraints
 double getBestFitness()
           
 Particle getBestParticle()
           
 int getBestParticleIndex()
           
 double[] getBestPosition()
           
 FitnessFunction getFitnessFunction()
           
 double getGlobalIncrement()
           
 double getInertia()
           
 double[] getMaxPosition()
           
 double[] getMaxVelocity()
           
 double[] getMinPosition()
           
 double[] getMinVelocity()
           
 int getNumberOfEvaliations()
           
 int getNumberOfParticles()
           
 Particle getParticle(int i)
           
 double getParticleIncrement()
           
 Particle[] getParticles()
           
 ParticleUpdate getParticleUpdate()
           
 Particle getSampleParticle()
           
 VariablesUpdate getVariablesUpdate()
           
 void init()
          Initialize every particle Warning: maxPosition[], minPosition[], maxVelocity[], minVelocity[] must be initialized and setted
 void setBestParticleIndex(int bestParticle)
           
 void setBestPosition(double[] bestPosition)
           
 void setFitnessFunction(FitnessFunction fitnessFunction)
           
 void setGlobalIncrement(double globalIncrement)
           
 void setInertia(double inertia)
           
 void setMaxMinVelocity(double maxVelocity)
          Sets every maxVelocity[] and minVelocity[] to 'maxVelocity' and '-maxVelocity' respectively
 void setMaxPosition(double maxPosition)
          Sets every maxPosition[] to 'maxPosition'
 void setMaxPosition(double[] maxPosition)
           
 void setMaxVelocity(double[] maxVelocity)
           
 void setMinPosition(double minPosition)
          Sets every minPosition[] to 'minPosition'
 void setMinPosition(double[] minPosition)
           
 void setMinVelocity(double[] minVelocity)
           
 void setNumberOfEvaliations(int numberOfEvaliations)
           
 void setNumberOfParticles(int numberOfParticles)
           
 void setParticleIncrement(double particleIncrement)
           
 void setParticles(Particle[] particle)
           
 void setParticleUpdate(ParticleUpdate particleUpdate)
           
 void setSampleParticle(Particle sampleParticle)
           
 void setVariablesUpdate(VariablesUpdate variablesUpdate)
           
 void show(java.awt.Graphics graphics, java.awt.Color foreground, int width, int height, int dim0, int dim1, boolean showVelocity)
          Show a swarm in a graph
 int size()
          Swarm size (number of particles)
 java.lang.String toString()
          Printable string
 java.lang.String toStringStats()
          Return a string with some (very basic) statistics
 void update()
          Update every particle's position and velocity, also apply position and velocity constraints (if any) Warning: Particles must be already evaluated
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_GLOBAL_INCREMENT

public static double DEFAULT_GLOBAL_INCREMENT

DEFAULT_INERTIA

public static double DEFAULT_INERTIA

DEFAULT_NUMBER_OF_PARTICLES

public static int DEFAULT_NUMBER_OF_PARTICLES

DEFAULT_PARTICLE_INCREMENT

public static double DEFAULT_PARTICLE_INCREMENT

VELOCITY_GRAPH_FACTOR

public static double VELOCITY_GRAPH_FACTOR

bestFitness

double bestFitness
Best fitness so far (global best)


bestParticleIndex

int bestParticleIndex
Index of best particle so far


bestPosition

double[] bestPosition
Best position so far (global best)


fitnessFunction

FitnessFunction fitnessFunction
Fitness function for this swarm


globalIncrement

double globalIncrement
Global increment (for velocity update), usually called 'c2' constant


inertia

double inertia
Inertia (for velocity update), usually called 'w' constant


maxPosition

double[] maxPosition
Maximum position (for each dimention)


maxVelocity

double[] maxVelocity
Maximum Velocity (for each dimention)


minPosition

double[] minPosition
Minimum position (for each dimention)


minVelocity

double[] minVelocity
Minimum Velocity for each dimention. WARNING: Velocity is no in Abs value (so setting minVelocity to 0 is NOT correct!)


numberOfEvaliations

int numberOfEvaliations
How many times 'particle.evaluate()' has been called?


numberOfParticles

int numberOfParticles
Number of particles in this swarm


particleIncrement

double particleIncrement
Particle's increment (for velocity update), usually called 'c1' constant


particles

Particle[] particles
Particles in this swarm


particleUpdate

ParticleUpdate particleUpdate
Particle update strategy


sampleParticle

Particle sampleParticle
A sample particles: Build other particles based on this one


variablesUpdate

VariablesUpdate variablesUpdate
Variables update

Constructor Detail

Swarm

public Swarm(int numberOfParticles,
             Particle sampleParticle,
             FitnessFunction fitnessFunction)
Create a Swarm and set default values

Parameters:
numberOfParticles - : Number of particles in this swarm (should be greater than 0). If unsure about this parameter, try Swarm.DEFAULT_NUMBER_OF_PARTICLES or greater
sampleParticle - : A particle that is a sample to build all other particles
fitnessFunction - : Fitness function used to evaluate each particle
Method Detail

evaluate

public void evaluate()
Evaluate fitness function for every particle Warning: particles[] must be initialized and fitnessFunction must be setted


evolve

public void evolve()
Make an iteration: - evaluates the swarm - updates positions and velocities - applies positions and velocities constraints


getBestFitness

public double getBestFitness()

getBestParticle

public Particle getBestParticle()

getBestParticleIndex

public int getBestParticleIndex()

getBestPosition

public double[] getBestPosition()

getFitnessFunction

public FitnessFunction getFitnessFunction()

getGlobalIncrement

public double getGlobalIncrement()

getInertia

public double getInertia()

getMaxPosition

public double[] getMaxPosition()

getMaxVelocity

public double[] getMaxVelocity()

getMinPosition

public double[] getMinPosition()

getMinVelocity

public double[] getMinVelocity()

getNumberOfEvaliations

public int getNumberOfEvaliations()

getNumberOfParticles

public int getNumberOfParticles()

getParticle

public Particle getParticle(int i)

getParticleIncrement

public double getParticleIncrement()

getParticles

public Particle[] getParticles()

getParticleUpdate

public ParticleUpdate getParticleUpdate()

getSampleParticle

public Particle getSampleParticle()

getVariablesUpdate

public VariablesUpdate getVariablesUpdate()

init

public void init()
Initialize every particle Warning: maxPosition[], minPosition[], maxVelocity[], minVelocity[] must be initialized and setted


setBestParticleIndex

public void setBestParticleIndex(int bestParticle)

setBestPosition

public void setBestPosition(double[] bestPosition)

setFitnessFunction

public void setFitnessFunction(FitnessFunction fitnessFunction)

setGlobalIncrement

public void setGlobalIncrement(double globalIncrement)

setInertia

public void setInertia(double inertia)

setMaxMinVelocity

public void setMaxMinVelocity(double maxVelocity)
Sets every maxVelocity[] and minVelocity[] to 'maxVelocity' and '-maxVelocity' respectively

Parameters:
maxVelocity -

setMaxPosition

public void setMaxPosition(double maxPosition)
Sets every maxPosition[] to 'maxPosition'

Parameters:
maxPosition -

setMaxPosition

public void setMaxPosition(double[] maxPosition)

setMaxVelocity

public void setMaxVelocity(double[] maxVelocity)

setMinPosition

public void setMinPosition(double minPosition)
Sets every minPosition[] to 'minPosition'

Parameters:
minPosition -

setMinPosition

public void setMinPosition(double[] minPosition)

setMinVelocity

public void setMinVelocity(double[] minVelocity)

setNumberOfEvaliations

public void setNumberOfEvaliations(int numberOfEvaliations)

setNumberOfParticles

public void setNumberOfParticles(int numberOfParticles)

setParticleIncrement

public void setParticleIncrement(double particleIncrement)

setParticles

public void setParticles(Particle[] particle)

setParticleUpdate

public void setParticleUpdate(ParticleUpdate particleUpdate)

setSampleParticle

public void setSampleParticle(Particle sampleParticle)

setVariablesUpdate

public void setVariablesUpdate(VariablesUpdate variablesUpdate)

show

public void show(java.awt.Graphics graphics,
                 java.awt.Color foreground,
                 int width,
                 int height,
                 int dim0,
                 int dim1,
                 boolean showVelocity)
Show a swarm in a graph

Parameters:
graphics - : Grapics object
foreground - : foreground color
width - : graphic's width
height - : graphic's height
dim0 - : Dimention to show ('x' axis)
dim1 - : Dimention to show ('y' axis)
showVelocity - : Show velocity tails?

size

public int size()
Swarm size (number of particles)


toString

public java.lang.String toString()
Printable string

Overrides:
toString in class java.lang.Object

toStringStats

public java.lang.String toStringStats()
Return a string with some (very basic) statistics

Returns:
A string

update

public void update()
Update every particle's position and velocity, also apply position and velocity constraints (if any) Warning: Particles must be already evaluated