ParticleSwarm

Struct ParticleSwarm 

Source
pub struct ParticleSwarm<T: Scalar + SampleUniform, D: Dim, F>
where F: Fn(Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>) -> T, DefaultAllocator: Allocator<D>,
{ /* private fields */ }
Expand description

§Particle Swarm Global Optimiser

This struct approximates x where F(x) <= F(y) for all y in the domain S in Rn of F, where F: S -> R is an objective or cost function to be minimised. This is done using the Particle Swarm Optimization method.

Default Tolerance: 1e-6

Default Max Iterations: 50

Default Particle Count: 100

Default Inertia Weight: 0.5

Default Cognitive Coefficient: 1

Default Social Coefficient: 1

§Examples

use eqsolver::global_optimisers::ParticleSwarm;
use nalgebra::SVector;

const SIZE: usize = 16;
let rastrigin = |v: SVector<f64, SIZE>| {
    let mut total = 10. * SIZE as f64;

    for &w in v.iter() {
        total += w * w - 10. * (2. * std::f64::consts::PI * w).cos();
    }

    total
};


let guess = SVector::repeat(80.);
let bounds = SVector::repeat(100.);

let optimised_position = ParticleSwarm::new(rastrigin, -bounds, bounds)
    .unwrap()
    .solve(guess)
    .unwrap();

Implementations§

Source§

impl<T, D, F> ParticleSwarm<T, D, F>
where T: Scalar + Float + SampleUniform + ComplexField<RealField = T>, D: Dim, F: Fn(Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>) -> T, DefaultAllocator: Allocator<D>,

Source

pub fn new( f: F, lower_bounds: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>, upper_bounds: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>, ) -> SolverResult<Self>

Sets up the optimiser given the function f to optimise and the bounds of the hyperrectangle that contains the global minimum. The hyperrectangle is given as two vectors lower_bounds and upper_bounds containing the lower and upper bounds of each dimension, respectively.

As per the changes in rand version 0.9, this will fail if any bounds pair [from, to] does not fulfil from <= to

Source

pub fn with_inertia_weight(&mut self, inertia_weight: T) -> &mut Self

Set the inertia weight of the optimiser. For more information about the effect of this parameter, see Particle Swarm Optimization.

Default Inertia Weight: 0.5

§Examples
let optimised_position = ParticleSwarm::new(f, -bounds, bounds)
    .unwrap()
    .with_inertia_weight(0.8)
    .solve(guess)
    .unwrap();
Source

pub fn with_cognitive_coefficient( &mut self, cognitive_coefficient: T, ) -> &mut Self

Set the cognitive coefficient of the optimiser. For more information about the effect of this parameter, see Particle Swarm Optimization.

Default Cognitive Coefficient: 1.0

§Examples
let optimised_position = ParticleSwarm::new(f, -bounds, bounds)
    .unwrap()
    .with_cognitive_coefficient(1.5)
    .solve(guess)
    .unwrap();
Source

pub fn with_social_coefficient(&mut self, social_coefficient: T) -> &mut Self

Set the social coefficient of the optimiser. For more information about the effect of this parameter, see Particle Swarm Optimization.

Default Social Coefficient: 1.0

§Examples
let optimised_position = ParticleSwarm::new(f, -bounds, bounds)
    .unwrap()
    .with_social_coefficient(1.5)
    .solve(guess)
    .unwrap();
Source

pub fn with_tol(&mut self, tolerance: T) -> &mut Self

Set the tolerance of the optimiser. If many (exact amount specified in implementation) of the previous best global minimums found are in absolve value less than the tolerance then the optimiser terminates and returns the best value found

Default Tolerance: 1e-6

§Examples
let optimised_position = ParticleSwarm::new(f, -bounds, bounds)
    .unwrap()
    .with_tol(1e-3)
    .solve(guess)
    .unwrap();
Source

pub fn with_particle_count(&mut self, particle_count: usize) -> &mut Self

Set the number of particles in the optimiser. For more information about the effect of this parameter, see Particle Swarm Optimization.

Default Particle Count: 100

§Examples
let optimised_position = ParticleSwarm::new(f, -bounds, bounds)
    .unwrap()
    .with_particle_count(100)
    .solve(guess)
    .unwrap();
Source

pub fn with_iter_max(&mut self, iter_max: usize) -> &mut Self

Set the maximum number of iterations of the optimiser. The optimiser returns the best value found after it has iterated that amount of number of iterations.

Default Max Iterations: 50

§Examples
let optimised_position = ParticleSwarm::new(f, -bounds, bounds)
    .unwrap()
    .with_iter_max(100)
    .solve(guess)
    .unwrap();
Source

pub fn solve( &self, x0: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>, ) -> SolverResult<Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>>

Optimises the function using a given initial value (or guess) by returning an approximation of the global minimum of the objective function within the bounds.

§Examples
let optimised_position = ParticleSwarm::new(f, -bounds, bounds)
    .unwrap()
    .solve(guess)
    .unwrap();

Auto Trait Implementations§

§

impl<T, D, F> Freeze for ParticleSwarm<T, D, F>

§

impl<T, D, F> RefUnwindSafe for ParticleSwarm<T, D, F>

§

impl<T, D, F> Send for ParticleSwarm<T, D, F>

§

impl<T, D, F> Sync for ParticleSwarm<T, D, F>

§

impl<T, D, F> Unpin for ParticleSwarm<T, D, F>

§

impl<T, D, F> UnwindSafe for ParticleSwarm<T, D, F>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

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

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V