propulate.propagators.pso

Module Contents

Classes

BasicPSO

Instantiate a basic PSO propagator.

VelocityClampingPSO

Instantiate a velocity clamping PSO propagator.

ConstrictionPSO

Instantiate a constriction PSO propagator.

CanonicalPSO

Initialize a canonical PSO propagator.

InitUniformPSO

Instantiate a uniform-initialization PSO propagator.

StatelessPSO

Instantiate a stateless PSO propagator.

class propulate.propagators.pso.BasicPSO(inertia: float, c_cognitive: float, c_social: float, rank: int, limits: Dict[str, Tuple[float, float]], rng: random.Random)

Bases: propulate.propagators.Propagator

Instantiate a basic PSO propagator.

In theory, it should be no problem to hand over numpy arrays instead of the float-type hyperparameters inertia, cognitive factor, and social factor. In this case, please ensure that the dimension of the passed arrays fits the search domain.

Parameters:
  • inertia (float) – The inertia weight.

  • c_cognitive (float) – The constant cognitive factor for scaling the distance to the individual’s personal best value.

  • c_social (float) – The constant social factor for scaling the distance to the swarm’s global best value.

  • rank (int) – The global rank of the worker the propagator is living on.

  • limits (Dict[str, Tuple[float, float]]) – The borders of the continuous search domain.

  • rng (random.Random) – The separate random number generator for introducing non-linearity.

__call__(individuals: List[propulate.population.Individual]) propulate.population.Individual

Apply the standard PSO update rule with inertia.

Return an Individual object containing the updated values of the youngest passed Individual or Individual that belongs to the worker the propagator is living on.

Parameters:

individuals (List[propulate.population.Individual]) – A list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the individual and the swarm, respectively, and then to update the individual based on the retrieved results.

Returns:

The updated particle.

Return type:

propulate.population.Individual

_prepare_data(individuals: List[propulate.population.Individual]) Tuple[propulate.population.Individual, propulate.population.Individual, propulate.population.Individual]

Get the particle to be updated on this rank, its current personal best, and the swarm’s current global best.

Given a list of Individual objects, determine the particle to be updated on this rank, its current personal best, and the currently known global best of the swarm to perform a particle update step.

Parameters:

individuals (List[propulate.population.Individual]) – Individual objects that shall be used as data basis for a PSO update step.

Returns:

The following particles in this very order: 1. old_p: the current particle to be updated now 2. p_best: the personal best value of this particle 3. g_best: the global best value currently known

Return type:

Tuple[propulate.population.Individual, propulate.population.Individual, propulate.population.Individual]

_make_new_particle(position: numpy.ndarray, velocity: numpy.ndarray, generation: int) propulate.population.Individual

Create a new Individual with the position dictionary set to the values provided by the numpy array.

Parameters:
  • position (np.ndarray) – The position of the particle to be created.

  • velocity (np.ndarray) – The velocity of the particle to be created.

  • generation (int) – The generation of the new particle.

Returns:

The new Individual object resulting from the PSO update step.

Return type:

propulate.population.Individual

class propulate.propagators.pso.VelocityClampingPSO(inertia: float, c_cognitive: float, c_social: float, rank: int, limits: Dict[str, Tuple[float, float]], rng: random.Random, v_limits: float | numpy.ndarray)

Bases: BasicPSO

Instantiate a velocity clamping PSO propagator.

Parameters:
  • inertia (float) – The inertia factor.

  • c_cognitive (float) – The constant cognitive factor for scaling the distance to the particle’s personal best value.

  • c_social (float) – The constant social factor for scaling the distance to the swarm’s global best value.

  • rank (int) – The global rank of the worker the propagator is living on.

  • limits (Dict[str, Tuple[float, float]]) – The borders of the continuous search domain.

  • rng (random.Random) – The separate random number generator for introducing non-linearity.

  • v_limits (Union[float, np.ndarray]) – The clamping factor to be multiplied with the clamping limit in order to reduce it further. Should be in (0, 1). If this parameter has float type, it is applied to all dimensions of the search domain; else, each of its elements is applied to the corresponding dimension of the search domain.

__call__(individuals: List[propulate.population.Individual]) propulate.population.Individual

Apply the standard PSO update rule with inertia, extended by cutting off too high velocities.

Return an Individual object containing the updated values of the youngest passed Individual or Individual that belongs to the worker the propagator is living on.

Parameters:

individuals (List[propulate.population.Individual]) – The list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the particle and the swarm, respectively, and then to update the particle based on the retrieved results. cannot be used as Individual objects are converted to particles first.

Returns:

The updated individual.

Return type:

propulate.population.Individual

class propulate.propagators.pso.ConstrictionPSO(c_cognitive: float, c_social: float, rank: int, limits: Dict[str, Tuple[float, float]], rng: random.Random)

Bases: BasicPSO

Instantiate a constriction PSO propagator.

Important note: c_cognitive and c_social have to sum up to a number greater than 4!

Parameters:
  • c_cognitive (float) – The constant cognitive factor for scaling the distance to the particle’s personal best value. Has to sum up with ``c_social`` to a number greater than 4!

  • c_social (float) – The constant social factor for scaling the distance to the swarm’s global best value. Has to sum up with ``c_cognitive`` to a number greater than 4!

  • rank (int) – The global rank of the worker the propagator is living on.

  • limits (Dict[str, Tuple[float, float]]) – The borders of the continuous search domain.

  • rng (random.Random) – The random number generator for introducing non-linearity.

Raises:

ValueError – If c_social and c_cognitive do not sum up to a number greater than 4.

__call__(individuals: List[propulate.population.Individual]) propulate.population.Individual

Apply the constriction PSO update rule.

Return an Individual object containing the updated values of the youngest passed Individual or Individual that belongs to the worker the propagator is living on.

Parameters:

individuals (List[propulate.population.Individual]) – A list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the particle and the swarm, respectively, and then to update the particle based on the retrieved results.

Returns:

The updated particle.

Return type:

propulate.population.Individual

class propulate.propagators.pso.CanonicalPSO(c_cognitive: float, c_social: float, rank: int, limits: Dict[str, Tuple[float, float]], rng: random.Random)

Bases: ConstrictionPSO

Initialize a canonical PSO propagator.

In theory, it should be no problem to hand over numpy arrays instead of the float-type hyperparameters inertia, cognitive factor, and social factor. In this case, please ensure that the dimension of the passed arrays fits the search domain.

Parameters:
  • c_cognitive (float) – The constant cognitive factor for scaling the distance to the particle’s personal best value.

  • c_social (float) – The constant social factor to scaling the distance to the swarm’s global best value.

  • rank (int) – The global rank of the worker the propagator is living on.

  • limits (Dict[str, Tuple[float, float]]) – The borders of the continuous search domain.

  • rng (random.Random) – The random number generator for introducing non-linearity.

__call__(individuals: List[propulate.population.Individual]) propulate.population.Individual

Apply the canonical PSO variant update rule.

Return an Individual object containing the updated values of the youngest passed Individual or Individual that belongs to the worker the propagator is living on.

Parameters:

individuals (List[propulate.population.Individual]) – The list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the particle and the swarm, respectively, and then to update the particle based on the retrieved results. Individuals that cannot be used as Individual objects are converted to particles first.

Returns:

The updated particle.

Return type:

propulate.population.Individual

class propulate.propagators.pso.InitUniformPSO(limits: Dict[str, Tuple[float, float]], rank: int, parents: int = 0, probability: float = 1.0, rng: random.Random | None = None, v_init_limit: float | numpy.ndarray = 0.1)

Bases: propulate.propagators.Stochastic

Instantiate a uniform-initialization PSO propagator.

In case of parents > 0 and probability < 1., call returns input individual without change.

Parameters:
  • limits (Dict[str, Tuple[float, float]]) – The limits of the search space, i.e., the limits of (hyper-)parameters to be optimized.

  • rank (int) – The rank of the worker in the Propulate communicator

  • parents (int, optional) – The number of input individuals (-1 for any). Default is 0.

  • probability (float, optional) – The probability of creating a completely new individual. Default is 1.0.

  • rng (random.Random, optional) – The separate random number generator for the Propulate optimization.

  • v_init_limit (float | np.ndarray, optional) – The multiplicative constant to reduce initial random velocity values. Default is 0.1.

__call__(individuals: List[propulate.population.Individual]) propulate.population.Individual

Apply the uniform-initialization propagator.

Parameters:

individuals (List[propulate.population.Individual]) – The individuals the propagator is applied to.

Returns:

A single individual object.

Return type:

propulate.population.Individual

class propulate.propagators.pso.StatelessPSO(c_cognitive: float, c_social: float, rank: int, limits: Dict[str, Tuple[float, float]], rng: random.Random)

Bases: propulate.propagators.Propagator

Instantiate a stateless PSO propagator.

Parameters:
  • c_cognitive (float) – The constant cognitive factor for scaling the individual’s personal best value.

  • c_social (float) – The constant social factor for scaling the swarm’s global best value.

  • rank (int) – The global rank of the worker the propagator is living on.

  • limits (Dict[str, Tuple[float, float]) – The borders of the continuous search domain.

  • rng (random.Random) – The random number generator required for non-linearity of the update.

__call__(individuals: List[propulate.population.Individual]) propulate.population.Individual

Apply the standard PSO update without inertia and old velocity.

Parameters:

individuals (List[propulate.population.Individual]) – The individuals used as data basis for the PSO update.

Returns:

The updated individual.

Return type:

propulate.population.Individual

Raises:

ValueError – If the individuals list passed is empty and the propagator thus has no data to work on.