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:
PopulationBasedStrategyParticle 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
ParameterExtendingEncodingthat stores a speed vector; if not supplied, a defaultPSOEncodingis created.- Parameters:
- initializerInitializer
Initializer for the solution part. An
ExtendedInitializeris 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. IfNone, aPSOEncodingis used.- rngRNGLike, optional
Random number generator.
- **kwargs
Forwarded to
StaticPopulationStrategy.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
lower_bound (float)
upper_bound (float)
name (str)
encoding (Optional[ParameterExtendingEncoding])
rng (Optional[RNGLike])
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
BounceBoundConstraintfor 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)