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>,
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>,
Sourcepub 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>
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
Sourcepub fn with_inertia_weight(&mut self, inertia_weight: T) -> &mut Self
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();Sourcepub fn with_cognitive_coefficient(
&mut self,
cognitive_coefficient: T,
) -> &mut Self
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();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();Sourcepub fn with_tol(&mut self, tolerance: T) -> &mut Self
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();Sourcepub fn with_particle_count(&mut self, particle_count: usize) -> &mut Self
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();Sourcepub fn with_iter_max(&mut self, iter_max: usize) -> &mut Self
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();Sourcepub fn solve(
&self,
x0: Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>,
) -> SolverResult<Vector<T, D, <DefaultAllocator as Allocator<D>>::Buffer<T>>>
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>where
DefaultAllocator: Sized,
F: RefUnwindSafe,
T: RefUnwindSafe,
D: RefUnwindSafe,
<T as SampleUniform>::Sampler: RefUnwindSafe,
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>where
DefaultAllocator: Sized,
F: UnwindSafe,
T: UnwindSafe,
D: UnwindSafe,
<T as SampleUniform>::Sampler: UnwindSafe,
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§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).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.