jswarm_pso
Class Swarm

java.lang.Object
  extended byjswarm_pso.Swarm

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_OTHER_PARTICLE_INCREMENT
           
static double DEFAULT_PARTICLE_INCREMENT
           
static double DEFAULT_RANDOM_INCREMENT
           
(package private)  FitnessFunction fitnessFunction
          Fitness function for this swarm
(package private)  double globalIncrement
          Global increment (for velocity update)
(package private)  double inertia
          Inertia (for velocity update)
(package private)  double[] maxPosition
          Maximum position (for each dimentions)
(package private)  double[] maxVelocity
          Maximum Velocity (for each dimentions)
(package private)  double[] minPosition
          Minimum position (for each dimentions)
(package private)  double[] minVelocity
          Minimum Velocity for each dimention.
(package private)  int numberOfEvaliations
          How many times 'particle.evaluate()' is called?
(package private)  int numberOfParticles
          Number of particles in this swarm
(package private)  double otherParticleIncrement
          Other particle increment
(package private)  double particleIncrement
          Particle's increment (for velocity update)
(package private)  Particle[] particles
          Particles in this swarm
(package private)  double randomIncrement
          Random increment
(package private)  Particle sampleParticle
          A sample particles: Build other particles based on this one
(package private)  boolean useRepulsiveAlgorithm
          Use Repulsive Algorithm
 
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()
           
 double getOtherParticleIncrement()
           
 double getParticleIncrement()
           
 Particle[] getParticles()
           
 double getRandomIncrement()
           
 Particle getSampleParticle()
           
 void init()
          Initialize every particle Warning: maxPosition[], minPosition[], maxVelocity[], minVelocity[] must be initialized and setted
 boolean isUseRepulsiveAlgorithm()
           
 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 setOtherParticleIncrement(double otherParticleIncrement)
           
 void setParticleIncrement(double particleIncrement)
           
 void setParticles(Particle[] particle)
           
 void setRandomIncrement(double randomIncrement)
           
 void setSampleParticle(Particle sampleParticle)
           
 void setUseRepulsiveAlgorithm(boolean useRepulsiveAlgorithm)
           
 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
 java.lang.String toString()
           
 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_OTHER_PARTICLE_INCREMENT

public static double DEFAULT_OTHER_PARTICLE_INCREMENT

DEFAULT_PARTICLE_INCREMENT

public static double DEFAULT_PARTICLE_INCREMENT

DEFAULT_RANDOM_INCREMENT

public static double DEFAULT_RANDOM_INCREMENT

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)


inertia

double inertia
Inertia (for velocity update)


maxPosition

double[] maxPosition
Maximum position (for each dimentions)


maxVelocity

double[] maxVelocity
Maximum Velocity (for each dimentions)


minPosition

double[] minPosition
Minimum position (for each dimentions)


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()' is called?


numberOfParticles

int numberOfParticles
Number of particles in this swarm


otherParticleIncrement

double otherParticleIncrement
Other particle increment


particleIncrement

double particleIncrement
Particle's increment (for velocity update)


particles

Particle[] particles
Particles in this swarm


randomIncrement

double randomIncrement
Random increment


sampleParticle

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


useRepulsiveAlgorithm

boolean useRepulsiveAlgorithm
Use Repulsive Algorithm

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()

getOtherParticleIncrement

public double getOtherParticleIncrement()

getParticleIncrement

public double getParticleIncrement()

getParticles

public Particle[] getParticles()

getRandomIncrement

public double getRandomIncrement()

getSampleParticle

public Particle getSampleParticle()

init

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


isUseRepulsiveAlgorithm

public boolean isUseRepulsiveAlgorithm()

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)

setOtherParticleIncrement

public void setOtherParticleIncrement(double otherParticleIncrement)

setParticleIncrement

public void setParticleIncrement(double particleIncrement)

setParticles

public void setParticles(Particle[] particle)

setRandomIncrement

public void setRandomIncrement(double randomIncrement)

setSampleParticle

public void setSampleParticle(Particle sampleParticle)

setUseRepulsiveAlgorithm

public void setUseRepulsiveAlgorithm(boolean useRepulsiveAlgorithm)

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?

toString

public java.lang.String toString()

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