pyswarms.handlers package

This package implements different handling strategies for the optimiziation using boundary conditions. The strategies help avoiding that particles leave the defined search space. There are two Handler classes that provide these functionalities, the BoundaryHandler and the VelocityHandler.

pyswarms.handlers class

Handlers

This module provides Handler classes for the position as well as the velocity of particles. This is necessary when boundary conditions are imposed on the PSO algorithm. Particles that do not stay inside these boundary conditions have to be handled by either adjusting their position after they left the bounded search space or adjusting their velocity when it would position them outside the search space. In particular, this approach is important if the optimium of a function is near the boundaries. For the following documentation let \(x_{i, t, d}\) be the \(d\) th coordinate of the particle \(i\) ‘s position vector at the time \(t\), \(lb\) the vector of the lower boundaries and \(ub\) the vector of the upper boundaries. The algorithms in this module are adapted from [SH2010]

[SH2010] Sabine Helwig, “Particle Swarms for Constrained Optimization”, PhD thesis, Friedrich-Alexander Universität Erlangen-Nürnberg, 2010.

class pyswarms.backend.handlers.BoundaryHandler(strategy)[source]

Bases: pyswarms.backend.handlers.HandlerMixin

__call__(position, bounds, **kwargs)[source]

Apply the selected strategy to the position-matrix given the bounds

Parameters:
  • position (numpy.ndarray) – The swarm position to be handled
  • bounds (tuple of numpy.ndarray or list) – a tuple of size 2 where the first entry is the minimum bound while the second entry is the maximum bound. Each array must be of shape (dimensions,)
  • kwargs (dict) –
Returns:

the adjusted positions of the swarm

Return type:

numpy.ndarray

__init__(strategy)[source]

A BoundaryHandler class

This class offers a way to handle boundary conditions. It contains methods to repair particle positions outside of the defined boundaries. Following strategies are available for the handling:

  • Nearest:
    Reposition the particle to the nearest bound.
  • Random:
    Reposition the particle randomly in between the bounds.
  • Shrink:
    Shrink the velocity of the particle such that it lands on the bounds.
  • Reflective:
    Mirror the particle position from outside the bounds to inside the bounds.
  • Intermediate:
    Reposition the particle to the midpoint between its current position on the bound surpassing axis and the bound itself. This only adjusts the axes that surpass the boundaries.

The BoundaryHandler can be called as a function to use the strategy that is passed at initialization to repair boundary issues. An example for the usage:

from pyswarms.backend import operators as op
from pyswarms.backend.handlers import BoundaryHandler

bh = BoundaryHandler(strategy="reflective")
ops.compute_position(swarm, bounds, handler=bh)

By passing the handler, the compute_position() function now has the ability to reset the particles by calling the BoundaryHandler inside.

strategy

str – The strategy to use. To see all available strategies, call BoundaryHandler.strategies

intermediate(position, bounds, **kwargs)[source]

Set the particle to an intermediate position

This method resets particles that exceed the bounds to an intermediate position between the boundary and their earlier position. Namely, it changes the coordinate of the out-of-bounds axis to the middle value between the previous position and the boundary of the axis. The following equation describes this strategy:

\[\begin{split}x_{i, t, d} = \begin{cases} \frac{1}{2} \left (x_{i, t-1, d} + lb_d \right) & \quad \text{if }x_{i, t, d} < lb_d \\ \frac{1}{2} \left (x_{i, t-1, d} + ub_d \right) & \quad \text{if }x_{i, t, d} > ub_d \\ x_{i, t, d} & \quad \text{otherwise} \end{cases}\end{split}\]
nearest(position, bounds, **kwargs)[source]

Set position to nearest bound

This method resets particles that exceed the bounds to the nearest available boundary. For every axis on which the coordiantes of the particle surpasses the boundary conditions the coordinate is set to the respective bound that it surpasses. The following equation describes this strategy:

\[x_{i, t, d} = egin{cases} lb_d & \quad ext{if }x_{i, t, d} < lb_d \ ub_d & \quad ext{if }x_{i, t, d} > ub_d \ x_{i, t, d} & \quad ext{otherwise} \end{cases}\]
periodic(position, bounds, **kwargs)[source]

Sets the particles a periodic fashion

This method resets the particles that exeed the bounds by using the modulo function to cut down the position. This creates a virtual, periodic plane which is tiled with the search space. The following equation describtes this strategy:

\begin{gather*} x_{i, t, d} = \begin{cases} ub_d - (lb_d - x_{i, t, d}) \mod s_d & \quad \text{if }x_{i, t, d} < lb_d \\ lb_d + (x_{i, t, d} - ub_d) \mod s_d & \quad \text{if }x_{i, t, d} > ub_d \\ x_{i, t, d} & \quad \text{otherwise} \end{cases}\\ \\ \text{with}\\ \\ s_d = |ub_d - lb_d| \end{gather*}
random(position, bounds, **kwargs)[source]

Set position to random location

This method resets particles that exeed the bounds to a random position inside the boundary conditions.

reflective(position, bounds, **kwargs)[source]

Reflect the particle at the boundary

This method reflects the particles that exceed the bounds at the respective boundary. This means that the amount that the component which is orthogonal to the exceeds the boundary is mirrored at the boundary. The reflection is repeated until the position of the particle is within the boundaries. The following algorithm describes the behaviour of this strategy:

egin{gather*} ext{while } x_{i, t, d}

otin left[lb_d,,ub_d ight]

ext{ do the following:}

x_{i, t, d} = egin{cases}

2cdot lb_d - x_{i, t, d} & quad ext{if } x_{i, t, d} < lb_d 2cdot ub_d - x_{i, t, d} & quad ext{if } x_{i, t, d} > ub_d x_{i, t, d} & quad ext{otherwise}

end{cases}

end{gather*}

shrink(position, bounds, **kwargs)[source]

Set the particle to the boundary

This method resets particles that exceed the bounds to the intersection of its previous velocity and the boundary. This can be imagined as shrinking the previous velocity until the particle is back in the valid search space. Let \(\sigma_{i, t, d}\) be the \(d\) th shrinking value of the \(i\) th particle at the time \(t\) and \(v_{i, t}\) the velocity of the \(i\) th particle at the time \(t\). Then the new position is computed by the following equation:

egin{gather*} \mathbf{x}_{i, t} = \mathbf{x}_{i, t-1} + \sigma_{i, t} \mathbf{v}_{i, t} \ \ ext{with} \ \ \sigma_{i, t, d} = egin{cases}

rac{lb_d-x_{i, t-1, d}}{v_{i, t, d}} & quad ext{if } x_{i, t, d} < lb_d

rac{ub_d-x_{i, t-1, d}}{v_{i, t, d}} & quad ext{if } x_{i, t, d} > ub_d
1 & quad ext{otherwise}

end{cases}

ext{and}

sigma_{i, t} = min_{d=1…n} sigma_{i, t, d} end{gather*}

class pyswarms.backend.handlers.HandlerMixin[source]

Bases: object

A HandlerMixing class

This class offers some basic functionality for the Handlers.

class pyswarms.backend.handlers.VelocityHandler(strategy)[source]

Bases: pyswarms.backend.handlers.HandlerMixin

__call__(velocity, clamp, **kwargs)[source]

Apply the selected strategy to the velocity-matrix given the bounds

Parameters:
  • velocity (numpy.ndarray) – The swarm position to be handled
  • clamp (tuple of numpy.ndarray or list) – a tuple of size 2 where the first entry is the minimum clamp while the second entry is the maximum clamp. Each array must be of shape (dimensions,)
  • kwargs (dict) –
Returns:

the adjusted positions of the swarm

Return type:

numpy.ndarray

__init__(strategy)[source]

A VelocityHandler class

This class offers a way to handle velocities. It contains methods to repair the velocities of particles that exceeded the defined boundaries. Following strategies are available for the handling:

  • Unmodified:
    Returns the unmodified velocites.
  • Adjust
    Returns the velocity that is adjusted to be the distance between the current and the previous position.
  • Invert
    Inverts and shrinks the velocity by the factor -z.
  • Zero
    Sets the velocity of out-of-bounds particles to zero.
adjust(velocity, clamp=None, **kwargs)[source]

Adjust the velocity to the new position

The velocity is adjusted such that the following equation holds: .. math:

\mathbf{v_{i,t}} = \mathbf{x_{i,t}} - \mathbf{x_{i,t-1}}

Note

This method should only be used in combination with a position handling operation.

invert(velocity, clamp=None, **kwargs)[source]

Invert the velocity if the particle is out of bounds

The velocity is inverted and shrinked. The shrinking is determined by the kwarg z. The default shrinking factor is 0.5. For all velocities whose particles are out of bounds the following equation is applied: .. math:

\mathbf{v_{i,t}} = -z\mathbf{v_{i,t}}
unmodified(velocity, clamp=None, **kwargs)[source]

Leaves the velocity unchanged

zero(velocity, clamp=None, **kwargs)[source]

Set velocity to zero if the particle is out of bounds