metaheuristic_designer.strategies.swarm.PSO module#

Particle Swarm Optimization strategy.

class PSO(initializer, lower_bound=-100, upper_bound=100, name='PSO', w=0.7, c1=1.5, c2=1.5, encoding=None, rng=None, **kwargs)[source]#

Bases: PopulationBasedStrategy

Particle Swarm Optimization (PSO).

Each individual (particle) has a position and a velocity. The velocity is updated using personal and global bests, and the position is moved accordingly. This requires a ParameterExtendingEncoding that stores a speed vector; if not supplied, a default PSOEncoding is created.

Parameters:
initializerInitializer

Initializer for the solution part. An ExtendedInitializer is automatically created to handle the velocity parameter.

lower_boundfloat, optional

Lower bound of the search space (default -100).

upper_boundfloat, optional

Upper bound of the search space (default 100).

namestr, optional

Display name (default "PSO").

wfloat, optional

Inertia weight (default 0.7).

c1float, optional

Cognitive acceleration coefficient (default 1.5).

c2float, optional

Social acceleration coefficient (default 1.5).

encodingParameterExtendingEncoding, optional

Encoding that includes a "speed" parameter. If None, a PSOEncoding is used.

rngRNGLike, optional

Random number generator.

**kwargs

Forwarded to StaticPopulationStrategy.

Attributes:
params

Access parameter values by attribute-style lookup.

population_size

Gets the amount of individuals in the population.

Parameters:

Methods

extra_report()

Hook called at the end of the optimization (intended for subclasses).

extra_step_info()

Hook called after each generation (intended for subclasses).

gather_parameters()

Collect the current parameters from all sub-components.

get_params()

Return a copy of the current parameter dictionary.

get_state()

Gets the current state of the search strategy as a dictionary.

initialize(objfunc)

Set up the initial population and attach velocity constraints.

step(prev_population, objfunc)

Performs a single iteration of the algorithm on a given population.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Advances the state of the search by one iteration.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

reset

initialize(objfunc)[source]#

Set up the initial population and attach velocity constraints.

Parameters:
objfuncObjectiveFunc

The objective function. Its constraint handler is extended with a BounceBoundConstraint for the velocity so that speeds stay within the feasible range.

.. warning::
There is a known bug: the objective function **does not**
automatically remove the extended constraint handler after
a PSO run finishes. Reusing the same objective function
instance for other algorithms may cause unexpected
behavior. This will be resolved in a future release.
Returns:
Population

The initialized and evaluated population.

Parameters:

objfunc (ObjectiveFunc)