Doxygen 1.9.1
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2 (development)
TasOptimization::ParticleSwarmState Class Reference

Stores the information about a particle swarm. More...

#include <tsgParticleSwarm.hpp>

Public Member Functions

 ParticleSwarmState ()=delete
 The particle swarm state must be initialized with data that defines number of particles and dimensions.
 
 ParticleSwarmState (const int num_dimensions, const int num_particles)
 Constructor for a particle swarm state with the number of particles and dimensions.
 
 ParticleSwarmState (const int num_dimensions, std::vector< double > &&pp, std::vector< double > &&pv)
 Constructor for a particle swarm state with the number of dimensions and the number of particles inferred from a set of input particle positions pp and a set of input particle velocities pv.
 
 ParticleSwarmState (const ParticleSwarmState &source)=default
 Copy constructor.
 
 ParticleSwarmState (ParticleSwarmState &&source)=default
 Move constructor.
 
ParticleSwarmStateoperator= (ParticleSwarmState &&source)=default
 Move assignment.
 
int getNumDimensions () const
 Return the number of dimensions.
 
int getNumParticles () const
 Return the number of particles.
 
void getParticlePositions (double pp[]) const
 Return the particle positions.
 
std::vector< double > getParticlePositions () const
 Return the particle positions, vector overload.
 
void getParticleVelocities (double pv[]) const
 Return the particle velocities.
 
std::vector< double > getParticleVelocities () const
 Return the particle velocities, vector overload.
 
void getBestParticlePositions (double bpp[]) const
 Return the best known particle positions. More...
 
std::vector< double > getBestParticlePositions () const
 Return the best known particle positions, vector overload.
 
void getBestPosition (double bp[]) const
 Loads the best known position in the swarm.
 
std::vector< double > getBestPosition () const
 Returns the best knows position in the swarm.
 
bool isPositionInitialized () const
 Returns true if the particle positions have been initialized.
 
bool isVelocityInitialized () const
 Returns true if the particle velocities have been initialized.
 
bool isBestPositionInitialized () const
 Returns true if the best particle positions have been initialized.
 
bool isCacheInitialized () const
 Returns true if the cache has been initialized.
 
std::vector< bool > getStateVector () const
 Return the complete state vector.
 
void setParticlePositions (const double pp[])
 Set the particle positions, raw-array variant.
 
void setParticlePositions (const std::vector< double > &pp)
 Set the particle positions, vector variant.
 
void setParticlePositions (std::vector< double > &&pp)
 Set the particle positions, with a move.
 
void setParticleVelocities (const double pv[])
 Set the particle velocities.
 
void setParticleVelocities (const std::vector< double > &pv)
 Sets the best position.
 
void setParticleVelocities (std::vector< double > &&pv)
 Sets the best position, with a move.
 
void setBestParticlePositions (const double bpp[])
 Set the previously best known particle velocities.
 
void setBestParticlePositions (const std::vector< double > &bpp)
 Sets the best position per particle.
 
void setBestParticlePositions (std::vector< double > &&bpp)
 Sets the best position per particle, allows for a move.
 
void clearBestParticles ()
 Clear the previously best known particle velocities.
 
void clearCache ()
 Clear the particle swarm cache.
 
void initializeParticlesInsideBox (const double box_lower[], const double box_upper[], const std::function< double(void)> get_random01=TasDREAM::tsgCoreUniform01)
 Randomly initializes all of the particle positions and velocities inside of a box. More...
 
void initializeParticlesInsideBox (const std::vector< double > &box_lower, const std::vector< double > &box_upper, const std::function< double(void)> get_random01=TasDREAM::tsgCoreUniform01)
 Randomly initializes all of the particles, vector API overload.
 

Friends

void ParticleSwarm (const ObjectiveFunction f, const TasDREAM::DreamDomain inside, const double inertia_weight, const double cognitive_coeff, const double social_coeff, const int num_iterations, ParticleSwarmState &state, const std::function< double(void)> get_random01)
 Applies the classic particle swarm algorithm to a particle swarm state. More...
 

Detailed Description

Stores the information about a particle swarm.

class ParticleSwarmState
A state class associated with the Particle Swarm State algorithm.
Constructors and Copy/Move assignment
All constructors requires the number of dimensions and particles, those cannot be modified but can be retrieved by get methods. The class is movable and copyable by constructor or operator=.
Data layout
The logical organization of the particle data follows conventions similar to the Sparse Grid module. Namely, the data is organized into strips of size num_dimensions and there is one strip for each particle. Each particle has several vectors: position, best-known position and velocity; as well as cached values for the objective functional at the position and best-position.
Set and Get Particle Positions and Velocities
Each particle of the swarm is associated with a (possibly empty) position and velocity vector. Methods are also available to determine if the positions and velocities are empty (uninitialized) or non-empty (initialized).
Set and Get Best Particle Positions
A best known position will be assigned to each particle during the execution of the algorithm. The best know position for the entire swarm is also available and it is the current "solution", i.e., the point with smallest observed functional.
Initializing a Random Swarm
Helper methods are available for initializing random particle positions and velocities.
Particle Swarm Algorithm
If all of particles of the swarm are initialized with velocities and positions, then the particle swarm optimization algorithm can be used on the swarm with the goal of minimizing a particular objective function. The best point, with respect to the given objective function, can be obtained from the swarm using the getBestPosition() method.
More information about the particle swarm algorithm can be found in the following paper:

M. R. Bonyadi and Z. Michalewicz, "Particle Swarm Optimization for Single Objective Continuous Space Problems: A Review," in Evolutionary Computation, vol. 25, no. 1, pp. 1-54, March 2017, doi: 10.1162/EVCO_r_00180.

Clearing the Cache
After an optimization method is applied to the swarm, certain data related to the objective function are stored in cache variables inside this class. If an optimization method needs to be applied to the swarm with a different objective function than the one used to generate the cache, the cache must be cleared.

Member Function Documentation

◆ getBestParticlePositions()

void TasOptimization::ParticleSwarmState::getBestParticlePositions ( double  bpp[]) const
inline

Return the best known particle positions.

The method actually returns positions with one more than the total number of particles, the last num_dimensions entries of this vector contain the best particle position of the entire swarm.

◆ initializeParticlesInsideBox()

void TasOptimization::ParticleSwarmState::initializeParticlesInsideBox ( const double  box_lower[],
const double  box_upper[],
const std::function< double(void)>  get_random01 = TasDREAM::tsgCoreUniform01 
)

Randomly initializes all of the particle positions and velocities inside of a box.

The i-th component of each particle's position is uniformly sampled from the interval [box_lower[i], box_upper[i]]. The i-th velocity of each particle's velocity is uniformly sampled from the interval [-R, R] where R = abs(box_upper[i] - box_lower[i]). The uniform [0,1] random number generator used in the sampling is specified by get_random01.

Friends And Related Function Documentation

◆ ParticleSwarm

void ParticleSwarm ( const ObjectiveFunction  f,
const TasDREAM::DreamDomain  inside,
const double  inertia_weight,
const double  cognitive_coeff,
const double  social_coeff,
const int  num_iterations,
ParticleSwarmState state,
const std::function< double(void)>  get_random01 
)
friend

Applies the classic particle swarm algorithm to a particle swarm state.

Runs num_iterations of the particle swarm algorithm to a particle swarm state to minimize the function f over the domain inside. The parameters of the algorithm are inertia_weight , cognitive_coeff , and social_coeff. The uniform [0,1] random number generator used by the algorithm is get_random01.

Parameters
fObjective function to be minimized
insideindicates whether a given point is inside or outside of the domain of interest
inertia_weightinertial weight for the particle swarm algorithm
cognitive_coeffcognitive coefficient for the particle swarm algorithm
social_coeffsocial coefficient for the particle swarm algorithm
num_iterationsnumber of iterations to perform
stateholds the state of the particles, e.g., positions and velocities, see TasOptimization::ParticleSwarmState
get_random01random number generator, defaults to rand()
Exceptions
std::runtime_errorif either the positions or the velocities of the state have not been initialized

The documentation for this class was generated from the following file: