pub struct Solver<S, C> { /* private fields */ }
Expand description
Parallel solver for multidimensional problems.
Usual workflow:
Solver::new
Solver::set_speed_factor
- Pass result of
Solver::random_specimens
toSolver::extend_specimens
- In a loop:
- Inspect first element (or more elements) of
Solver::specimens
, e.g. for a break condition - Pass result of
Solver::recombined_specimens
toSolver::replace_worst_specimens
- Inspect first element (or more elements) of
- Extract best specimen, e.g. using
Solver::into_specimen
See module level documentation for a code example.
Implementations§
Source§impl<S, C> Solver<S, C>
impl<S, C> Solver<S, C>
Sourcepub fn new<T>(search_space: Vec<SearchRange>, constructor: C) -> Self
pub fn new<T>(search_space: Vec<SearchRange>, constructor: C) -> Self
Create Solver
for search space and Specimen
constructor
closure.
The closure takes a Vec<f64>
as argument, which contains the
coefficients/parameters, and it returns an S: Specimen
.
See module level documentation for a code example.
Alternatively, Future
s or Result
s may be returned by the
closure. Note that when the closure returns Future
s, then the methods
extend_specimens_async
and replace_worst_specimens_async
must
be used instead of their synchronous equivalents.
Sourcepub fn set_division_count(&mut self, division_count: usize)
pub fn set_division_count(&mut self, division_count: usize)
Simplify calculation by dividing dimensions into a given number of groups.
Divides dimensions into (almost) equally sized groups when calculating covariances to reduce computation time. The number of groups is given as an integer.
See Solver::set_speed_factor
for a high-level interface.
Sourcepub fn set_speed_factor(&mut self, speed_factor: f64)
pub fn set_speed_factor(&mut self, speed_factor: f64)
Simplify calculation by dividing dimensions according to speed factor.
This method is a high-level interface for
Solver::set_division_count
.
Uses a speed_factor
to divide dimensions into (almost) equally sized
groups when calculating covariances to reduce computation time. Higher
speed factors generally result in faster (but possibly inaccurate)
calculation.
Speed factors range from 0.0
to 1.0
, however, a factor greater than
0.75
is not recommended due to the introduced overhead.
Sourcepub fn set_max_division_size(&mut self, max_division_size: usize)
pub fn set_max_division_size(&mut self, max_division_size: usize)
Simply calculation by dividing dimensions into groups of specified maximum size.
This method is an alternative interface for
Solver::set_division_count
where the maximum size of each division
is specified.
See Solver::set_speed_factor
for a high-level interface.
Sourcepub fn division_count(&self) -> usize
pub fn division_count(&self) -> usize
Number of groups into which dimensions are split when calculating covariances.
Sourcepub fn min_population(&self) -> usize
pub fn min_population(&self) -> usize
Minimum required population.
The number depends on the number of dimensions and the number of groups into which the dimensions are split when calculating covariances.
Source§impl<S, C> Solver<S, C>
impl<S, C> Solver<S, C>
Sourcepub fn extend_specimens<I: IntoIterator<Item = S>>(&mut self, iter: I)
pub fn extend_specimens<I: IntoIterator<Item = S>>(&mut self, iter: I)
Add specimens to population.
Sourcepub fn replace_worst_specimens<I: IntoIterator<Item = S>>(&mut self, iter: I)
pub fn replace_worst_specimens<I: IntoIterator<Item = S>>(&mut self, iter: I)
Replace worst specimens in population.
Sourcepub async fn extend_specimens_async<F, I>(&mut self, iter: I)
pub async fn extend_specimens_async<F, I>(&mut self, iter: I)
Add specimens to population asynchronously.
Sourcepub async fn replace_worst_specimens_async<F, I>(&mut self, iter: I)
pub async fn replace_worst_specimens_async<F, I>(&mut self, iter: I)
Replace worst specimens in population asynchronously.
Sourcepub fn truncate_specimens(&mut self, count: usize)
pub fn truncate_specimens(&mut self, count: usize)
Truncate population of specimens to given count
(drops worst fitting specimens).
Sourcepub fn converged(&mut self) -> bool
pub fn converged(&mut self) -> bool
Return true if specimens have converged.
Note that this method only returns true if the cost of all specimens is exactly equal. Additional, more sensitive termination conditions may be required in practice in order to avoid endless optimization.
Sourcepub fn specimens(&mut self) -> &[S]
pub fn specimens(&mut self) -> &[S]
Sorted population of Specimen
s as shared slice (best first).
Sourcepub fn specimens_mut(&mut self) -> &mut Vec<S>
pub fn specimens_mut(&mut self) -> &mut Vec<S>
Sourcepub fn into_specimens(self) -> Vec<S>
pub fn into_specimens(self) -> Vec<S>
Sourcepub fn into_specimen(self) -> S
pub fn into_specimen(self) -> S
Source§impl<S, C, T> Solver<S, C>
impl<S, C, T> Solver<S, C>
Sourcepub fn random_specimens(&self, count: usize) -> Vec<T>
pub fn random_specimens(&self, count: usize) -> Vec<T>
Create random specimens
Note that depending on the return type of the closure passed to
Solver::new
, a Vec
of Future
s or Result
s may be
returned.
Sourcepub fn recombined_specimens(
&mut self,
children_count: usize,
local_factor: f64,
) -> Vec<T>
pub fn recombined_specimens( &mut self, children_count: usize, local_factor: f64, ) -> Vec<T>
Create recombined specimens.
Note that depending on the return type of the closure passed to
Solver::new
, a Vec
of Future
s or Result
s may be
returned.
Setting the local_factor
to a value greater than 0.0
(but smaller
than 1.0
) selects a particular specimen with a correspondingly
proportional chance to be modified. This allows performing more
localized searches. A reasonable value seems to be
0.01 / self.dim() as f64
.
Trait Implementations§
Auto Trait Implementations§
impl<S, C> Freeze for Solver<S, C>where
C: Freeze,
impl<S, C> RefUnwindSafe for Solver<S, C>where
C: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, C> Send for Solver<S, C>
impl<S, C> Sync for Solver<S, C>
impl<S, C> Unpin for Solver<S, C>
impl<S, C> UnwindSafe for Solver<S, C>where
C: UnwindSafe,
S: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more