Struct Solver

Source
pub struct Solver<S, C> { /* private fields */ }
Expand description

Parallel solver for multidimensional problems.

Usual workflow:

See module level documentation for a code example.

Implementations§

Source§

impl<S, C> Solver<S, C>

Source

pub fn new<T>(search_space: Vec<SearchRange>, constructor: C) -> Self
where C: Fn(Vec<f64>) -> T + Sync,

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, Futures or Results may be returned by the closure. Note that when the closure returns Futures, then the methods extend_specimens_async and replace_worst_specimens_async must be used instead of their synchronous equivalents.

Source

pub fn dim(&self) -> usize

Dimensionality of search space.

Source

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.

Source

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.

Source

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.

Source

pub fn division_count(&self) -> usize

Number of groups into which dimensions are split when calculating covariances.

Source

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>
where S: Specimen + Send,

Source

pub fn extend_specimens<I: IntoIterator<Item = S>>(&mut self, iter: I)

Add specimens to population.

Source

pub fn replace_worst_specimens<I: IntoIterator<Item = S>>(&mut self, iter: I)

Replace worst specimens in population.

Source

pub async fn extend_specimens_async<F, I>(&mut self, iter: I)
where F: Future<Output = S> + Send, I: IntoIterator<Item = F>,

Add specimens to population asynchronously.

Source

pub async fn replace_worst_specimens_async<F, I>(&mut self, iter: I)
where F: Future<Output = S> + Send, I: IntoIterator<Item = F>,

Replace worst specimens in population asynchronously.

Source

pub fn truncate_specimens(&mut self, count: usize)

Truncate population of specimens to given count (drops worst fitting specimens).

Source

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.

Source

pub fn specimens(&mut self) -> &[S]

Sorted population of Specimens as shared slice (best first).

Source

pub fn specimens_mut(&mut self) -> &mut Vec<S>

Sorted population of Specimens as mutable Vec (best first).

The Vec may be modified and doesn’t need to be (re-)sorted by the caller after modifying.

Source

pub fn into_specimens(self) -> Vec<S>

Consume Solver and return all Specimens, ordered by fitness (best first).

Source

pub fn into_specimen(self) -> S

Consume Solver and return best Specimen.

Source§

impl<S, C, T> Solver<S, C>
where S: Specimen + Send + Sync, C: Fn(Vec<f64>) -> T + Sync, T: Send,

Source

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 Futures or Results may be returned.

Source

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 Futures or Results 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§

Source§

impl<S: Clone, C: Clone> Clone for Solver<S, C>

Source§

fn clone(&self) -> Solver<S, C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: Debug, C: Debug> Debug for Solver<S, C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, C> Freeze for Solver<S, C>
where C: Freeze,

§

impl<S, C> RefUnwindSafe for Solver<S, C>

§

impl<S, C> Send for Solver<S, C>
where C: Send, S: Send,

§

impl<S, C> Sync for Solver<S, C>
where C: Sync, S: Sync,

§

impl<S, C> Unpin for Solver<S, C>
where C: Unpin, S: Unpin,

§

impl<S, C> UnwindSafe for Solver<S, C>
where C: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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