Particle Swarm Optimization#
- class Particle(position: ndarray | list | Position | None = None, velocity=None, f: ObjectiveFunction | None = None)#
Bases:
object
Defines a particle used in PSO.
- __init__(position: ndarray | list | Position | None = None, velocity=None, f: ObjectiveFunction | None = None)#
- Parameters:
position – The initial position of the particle. If unspecified,
the position will default to a uniformly sampled vector within the bounds of the
ObjectiveFunction
(if provided). :param velocity: The initial velocity. Defaults to a vector of zeros. :param f: The objective function to be optimized.
- property velocity: ndarray#
Returns the current velocity as a Numpy array.
- update_personal_best()#
- evaluate(f: ObjectiveFunction)#
Evaluates the
Particle
’s position.
- class PSO(n_particles: int, obj_func, w: float, c1: float, c2: float)#
Bases:
Algorithm
- __init__(n_particles: int, obj_func, w: float, c1: float, c2: float)#
- Parameters:
n_particles – The number of particles in the Swarm.
obj_func – The
ObjectiveFunction
used to initialize the particles.w – Inertia weight.
c1 – Cognitive acceleration coefficient.
c2 – Social acceleration coefficient.
- iterate(obj_func) None #
The only function that must be overridden when implementing your own algorithm. This function must be the logic of one iteration of your algorithm.
- Parameters:
f – The
ObjectiveFunction
to optimize.
- collection()#
- std_velocity(particle: Particle, social_best: Position, w: float = 0.74, c1: float = 1.4, c2: float = 1.4)#
The default velocity update function.
- Parameters:
particle – The particle whose velocity is to be updated.
social_best – The social guide.
w – Inertia weight.
c1 – Cognitive acceleration coefficient.
c2 – Social acceleration coefficient.
- particles(n: int, f: ObjectiveFunction)#
- global_best(particles: List[Particle], f: ObjectiveFunction)#