Particle Swarm Optimization Generational (GPSO)

class pagmo::pso_gen

Particle Swarm Optimization.

As opposed to the main PSO algorithm implemented in pagmo, this version of Particle Swarm Optimization is generational. In other words, the velocity is first calculated for all particles, then the position is updated.


This PSO is suitable for stochastic optimization problems. The random seed is changed at the end of each generation.


The algorithm is not suitable for multi-objective problems, nor for constrained optimization.

See also for a repository of information related to PSO

See also

pagmo::pso for the implementation of non-generational PSO

Public Types

typedef std::tuple<unsigned, unsigned long long, double, double, double, double> log_line_type

Single entry of the log (Gen, Fevals, gbest, Mean Vel., Mean lbest, Avg. Dist.)

typedef std::vector<log_line_type> log_type

The log.

Public Functions

pso_gen(unsigned gen = 1u, double omega = 0.7298, double eta1 = 2.05, double eta2 = 2.05, double max_vel = 0.5, unsigned variant = 5u, unsigned neighb_type = 2u, unsigned neighb_param = 4u, bool memory = false, unsigned seed = pagmo::random_device::next())


Allows to specify in detail all the parameters of the algorithm.

  • gen – number of generations

  • omega – particles’ inertia weight, or alternatively, the constriction coefficient (definition depends on the variant used)

  • eta1 – magnitude of the force, applied to the particle’s velocity, in the direction of its previous best position

  • eta2 – magnitude of the force, applied to the particle’s velocity, in the direction of the best position in its neighborhood

  • max_vel – maximum allowed particle velocity (as a fraction of the box bounds)

  • variant – algorithm variant to use (one of 1 .. 6)

  • neighb_type – swarm topology to use (one of 1 .. 4) [gbest, lbest, Von Neumann, adaptive random]

  • neighb_param – the neighbourhood parameter. If the lbest topology is selected (neighb_type=2), it represents each particle’s indegree (also outdegree) in the swarm topology. Particles have neighbours up to a radius of k = neighb_param / 2 in the ring. If the Randomly-varying neighbourhood topology is selected (neighb_type=4), it represents each particle’s maximum outdegree in the swarm topology. The minimum outdegree is 1 (the particle always connects back to itself). If neighb_type is 1 or 3 this parameter is ignored.

  • memory – when true the particle velocities are not reset between successive calls to evolve

  • seed – seed used by the internal random number generator (default is random)


std::invalid_argument – if omega is not in the [0,1] interval, eta1, eta2 are not in the [0,1] interval, vcoeff is not in ]0,1], variant is not one of 1 .. 6, neighb_type is not one of 1 .. 4, neighb_param is zero

population evolve(population) const

Algorithm evolve method.


pop – population to be evolved


std::invalid_argument – if the problem is multi-objective or constrained


evolved population

inline void set_verbosity(unsigned level)

Sets the algorithm verbosity.

Sets the verbosity level of the screen output and of the log returned by get_log(). level can be:

  • 0u: no verbosity

  • >=1u: will print and log one line each level generations

Example (verbosity 50u):

Gen:        Fevals:         gbest:     Mean Vel.:    Mean lbest:    Avg. Dist.:
   1             40        2.01917       0.298551        1855.03       0.394038
   51           1040     0.00436298      0.0407766         1.0704         0.1288
  101           2040    0.000228898      0.0110884       0.282699      0.0488969
  151           3040    5.53426e-05     0.00231688       0.106807      0.0167147
  201           4040    3.88181e-06    0.000972132      0.0315856     0.00988859
  251           5040    1.25676e-06    0.000330553     0.00146805     0.00397989
  301           6040    3.76784e-08    0.000118192    0.000738972      0.0018789
  351           7040    2.35193e-09    5.39387e-05    0.000532189     0.00253805
  401           8040    3.24364e-10     2.2936e-05    9.02879e-06    0.000178279
  451           9040    2.31237e-10    5.01558e-06    8.12575e-07    9.77163e-05

Gen is the generation number, Fevals the number of fitness evaluation made, gbest the global best, Mean Vel. the average mean normalized velocity of particles, Mean lbest the average of the local best fitness of particles and Avg. Dist. the average normalized distance among particles. Normalization is made with respect to the problem bounds.


level – verbosity level

inline unsigned get_verbosity() const

Gets the verbosity level.


the verbosity level

void set_bfe(const bfe &b)

Sets the batch function evaluation scheme.


b – batch function evaluation object

void set_seed(unsigned)

Sets the seed.


seed – the seed controlling the algorithm stochastic behaviour

inline unsigned get_seed() const

Gets the seed.


the seed controlling the algorithm stochastic behaviour

inline std::string get_name() const

Algorithm name.

One of the optional methods of any user-defined algorithm (UDA).


a string containing the algorithm name

std::string get_extra_info() const

Extra info.

One of the optional methods of any user-defined algorithm (UDA).


a string containing extra info on the algorithm

inline const log_type &get_log() const

Get log.

A log containing relevant quantities monitoring the last call to evolve. Each element of the returned std::vector is a pso_gen::log_line_type containing: Gen, Fevals, gbest, Mean Vel., Mean lbest, Avg. Dist. as described in pso_gen::set_verbosity


an std::vector of pso_gen::log_line_type containing the logged values