# Struct argmin::solver::particleswarm::ParticleSwarm

source · `pub struct ParticleSwarm<P, F, R> { /* private fields */ }`

## Expand description

## Particle Swarm Optimization (PSO)

Canonical implementation of the particle swarm optimization method as outlined in [0] in chapter II, section A.

The `rayon`

feature enables parallel computation of the cost function. This can be beneficial
for expensive cost functions, but may cause a drop in performance for cheap cost functions. Be
sure to benchmark both parallel and sequential computation.

### Requirements on the optimization problem

The optimization problem is required to implement `CostFunction`

.

### References

[0] Zambrano-Bigiarini, M. et.al. (2013): Standard Particle Swarm Optimisation 2011 at CEC-2013: A baseline for future PSO improvements. 2013 IEEE Congress on Evolutionary Computation. https://doi.org/10.1109/CEC.2013.6557848

[1] https://en.wikipedia.org/wiki/Particle_swarm_optimization

## Implementations§

source§### impl<P, F> ParticleSwarm<P, F, StdRng>where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,

### impl<P, F> ParticleSwarm<P, F, StdRng>where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,

source#### pub fn new(bounds: (P, P), num_particles: usize) -> Self

#### pub fn new(bounds: (P, P), num_particles: usize) -> Self

Construct a new instance of `ParticleSwarm`

Takes the number of particles and bounds on the search space as inputs. `bounds`

is a tuple
`(lower_bound, upper_bound)`

, where `lower_bound`

and `upper_bound`

are of the same type as
the position of a particle (`P`

) and of the same length as the problem as dimensions.

The inertia weight on velocity and the social and cognitive acceleration factors can be
adapted with `with_inertia_factor`

,
`with_cognitive_factor`

and
`with_social_factor`

, respectively.

The weights and acceleration factors default to:

- inertia:
`1/(2 * ln(2))`

- cognitive:
`0.5 + ln(2)`

- social:
`0.5 + ln(2)`

##### Example

`let pso: ParticleSwarm<_, f64, _> = ParticleSwarm::new((lower_bound, upper_bound), 40);`

source§### impl<P, F, R0> ParticleSwarm<P, F, R0>where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,
R0: Rng,

### impl<P, F, R0> ParticleSwarm<P, F, R0>where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,
R0: Rng,

source#### pub fn with_rng_generator<R1: Rng>(
self,
generator: R1
) -> ParticleSwarm<P, F, R1>

#### pub fn with_rng_generator<R1: Rng>( self, generator: R1 ) -> ParticleSwarm<P, F, R1>

Set the random number generator

Defaults to `rand::rngs::StdRng::from_entropy()`

##### Example

```
let pso: ParticleSwarm<_, f64, _> =
ParticleSwarm::new((lower_bound, upper_bound), 40)
.with_rng_generator(rand_xoshiro::Xoroshiro128Plus::seed_from_u64(1729));
```

source§### impl<P, F, R> ParticleSwarm<P, F, R>where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,
R: Rng,

### impl<P, F, R> ParticleSwarm<P, F, R>where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,
R: Rng,

source#### pub fn with_inertia_factor(self, factor: F) -> Result<Self, Error>

#### pub fn with_inertia_factor(self, factor: F) -> Result<Self, Error>

Set inertia factor on particle velocity

Defaults to `1/(2 * ln(2))`

.

##### Example

```
let pso: ParticleSwarm<_, f64, _> =
ParticleSwarm::new((lower_bound, upper_bound), 40).with_inertia_factor(0.5)?;
```

source#### pub fn with_cognitive_factor(self, factor: F) -> Result<Self, Error>

#### pub fn with_cognitive_factor(self, factor: F) -> Result<Self, Error>

Set cognitive acceleration factor

Defaults to `0.5 + ln(2)`

.

##### Example

```
let pso: ParticleSwarm<_, f64, _> =
ParticleSwarm::new((lower_bound, upper_bound), 40).with_cognitive_factor(1.1)?;
```

Set social acceleration factor

Defaults to `0.5 + ln(2)`

.

##### Example

```
let pso: ParticleSwarm<_, f64, _> =
ParticleSwarm::new((lower_bound, upper_bound), 40).with_social_factor(1.1)?;
```

## Trait Implementations§

source§### impl<P: Clone, F: Clone, R: Clone> Clone for ParticleSwarm<P, F, R>

### impl<P: Clone, F: Clone, R: Clone> Clone for ParticleSwarm<P, F, R>

source§#### fn clone(&self) -> ParticleSwarm<P, F, R>

#### fn clone(&self) -> ParticleSwarm<P, F, R>

1.0.0 · source§#### fn clone_from(&mut self, source: &Self)

#### fn clone_from(&mut self, source: &Self)

`source`

. Read moresource§### impl<'de, P, F, R> Deserialize<'de> for ParticleSwarm<P, F, R>

### impl<'de, P, F, R> Deserialize<'de> for ParticleSwarm<P, F, R>

source§#### fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,

#### fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,

source§### impl<P, F, R> Serialize for ParticleSwarm<P, F, R>

### impl<P, F, R> Serialize for ParticleSwarm<P, F, R>

source§### impl<O, P, F, R> Solver<O, PopulationState<Particle<P, F>, F>> for ParticleSwarm<P, F, R>where
O: CostFunction<Param = P, Output = F> + SyncAlias,
P: Clone + SyncAlias + ArgminAdd<P, P> + ArgminSub<P, P> + ArgminMul<F, P> + ArgminZeroLike + ArgminRandom + ArgminMinMax,
F: ArgminFloat,
R: Rng,

### impl<O, P, F, R> Solver<O, PopulationState<Particle<P, F>, F>> for ParticleSwarm<P, F, R>where
O: CostFunction<Param = P, Output = F> + SyncAlias,
P: Clone + SyncAlias + ArgminAdd<P, P> + ArgminSub<P, P> + ArgminMul<F, P> + ArgminZeroLike + ArgminRandom + ArgminMinMax,
F: ArgminFloat,
R: Rng,

source§#### fn next_iter(
&mut self,
problem: &mut Problem<O>,
state: PopulationState<Particle<P, F>, F>
) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>

#### fn next_iter( &mut self, problem: &mut Problem<O>, state: PopulationState<Particle<P, F>, F> ) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>

Perform one iteration of algorithm

source§#### const NAME: &'static str = "Particle Swarm Optimization"

#### const NAME: &'static str = "Particle Swarm Optimization"

source§#### fn init(
&mut self,
problem: &mut Problem<O>,
state: PopulationState<Particle<P, F>, F>
) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>

#### fn init( &mut self, problem: &mut Problem<O>, state: PopulationState<Particle<P, F>, F> ) -> Result<(PopulationState<Particle<P, F>, F>, Option<KV>), Error>

source§#### fn terminate_internal(&mut self, state: &I) -> TerminationStatus

#### fn terminate_internal(&mut self, state: &I) -> TerminationStatus

source§#### fn terminate(&mut self, _state: &I) -> TerminationStatus

#### fn terminate(&mut self, _state: &I) -> TerminationStatus

`terminate_internal`

. Read more## Auto Trait Implementations§

### impl<P, F, R> RefUnwindSafe for ParticleSwarm<P, F, R>

### impl<P, F, R> Send for ParticleSwarm<P, F, R>

### impl<P, F, R> Sync for ParticleSwarm<P, F, R>

### impl<P, F, R> Unpin for ParticleSwarm<P, F, R>

### impl<P, F, R> UnwindSafe for ParticleSwarm<P, F, R>

## Blanket Implementations§

source§### impl<T> BorrowMut<T> for Twhere
T: ?Sized,

### impl<T> BorrowMut<T> for Twhere
T: ?Sized,

source§#### fn borrow_mut(&mut self) -> &mut T

#### fn borrow_mut(&mut self) -> &mut T

§### impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,

### impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,

§#### fn to_subset(&self) -> Option<SS>

#### fn to_subset(&self) -> Option<SS>

`self`

from the equivalent element of its
superset. Read more§#### fn is_in_subset(&self) -> bool

#### fn is_in_subset(&self) -> bool

`self`

is actually part of its subset `T`

(and can be converted to it).§#### fn to_subset_unchecked(&self) -> SS

#### fn to_subset_unchecked(&self) -> SS

`self.to_subset`

but without any property checks. Always succeeds.§#### fn from_subset(element: &SS) -> SP

#### fn from_subset(element: &SS) -> SP

`self`

to the equivalent element of its superset.