Struct metaheuristics_nature::Solver

source ·
pub struct Solver<F: ObjFunc> { /* private fields */ }
Expand description

A public API for using optimization methods.

Users can simply obtain their solution and see the result.

  • The method is a type that implemented Algorithm.
  • The objective function is a type that implement ObjFunc.
  • A basic algorithm data is hold by Ctx.

The builder of this type can infer the algorithm by AlgCfg::Algorithm.

Please use Solver::build() method to start a task.

The settings are defined in the SolverBuilder type.

Implementations§

source§

impl<F: ObjFunc> Solver<F>

source

pub fn func(&self) -> &F

Get the reference of the objective function.

It’s useful when you need to get the preprocessed data from the initialization process, which is stored in the objective function.

source

pub fn as_best_set(&self) -> &<F::Ys as Fitness>::Best<F::Ys>

Get the reference of the best set.

Use Solver::as_best() to get the best parameters and the fitness value directly.

source

pub fn as_best(&self) -> (&[f64], &F::Ys)

Get the reference of the best parameters and the fitness value.

source

pub fn as_best_xs(&self) -> &[f64]

Get the reference of the best fitness value.

source

pub fn as_best_fit(&self) -> &F::Ys

Get the reference of the best fitness value.

source

pub fn get_best_eval(&self) -> <F::Ys as Fitness>::Eval

Get the final best fitness value.

source

pub fn into_result<P, Fit: Fitness>(self) -> P
where F: ObjFunc<Ys = WithProduct<Fit, P>>, P: MaybeParallel + Clone + 'static,

Get the final best element.

source

pub fn into_err_result<P, Fit: Fitness>(self) -> (Fit::Eval, P)
where F: ObjFunc<Ys = WithProduct<Fit, P>>, P: MaybeParallel + Clone + 'static,

Get the fitness value and the final result.

source

pub fn into_err_result_func<P, Fit: Fitness>(self) -> (Fit::Eval, P, F)
where F: ObjFunc<Ys = WithProduct<Fit, P>>, P: MaybeParallel + Clone + 'static,

Get the fitness value, final result and the objective function.

source

pub fn seed(&self) -> Seed

Seed of the random number generator.

source

pub fn pool(&self) -> &[Vec<f64>]

Get the pool from the last status.

source§

impl<F: ObjFunc> Solver<F>

source

pub fn build<A: AlgCfg>( cfg: A, func: F ) -> SolverBuilder<'static, A::Algorithm<F>, F>

Start to build a solver. Take a setting and setup the configurations.

The signature is something like Solver::build(Rga::default(), MyFunc::new()). Please check the SolverBuilder type, it will help you choose your configuration.

If all things are well-setup, call SolverBuilder::solve().

The default value of each option can be found in their document.

Use Solver::build_boxed() for dynamic dispatching.

source

pub fn build_boxed<A: AlgCfg>(cfg: A, func: F) -> SolverBox<'static, F>

Start to build a solver with a boxed algorithm, the dynamic dispatching.

This method allows you to choose the algorithm at runtime and mix them with the same type.

use metaheuristics_nature as mh;

let s = if use_ga {
    mh::Solver::build_boxed(mh::Rga::default(), MyFunc::new())
} else {
    mh::Solver::build_boxed(mh::De::default(), MyFunc::new())
};

Use Solver::build() for optimized memory allocation and access.

Auto Trait Implementations§

§

impl<F> Freeze for Solver<F>
where <<F as ObjFunc>::Ys as Fitness>::Best<<F as ObjFunc>::Ys>: Freeze, F: Freeze,

§

impl<F> RefUnwindSafe for Solver<F>
where <<F as ObjFunc>::Ys as Fitness>::Best<<F as ObjFunc>::Ys>: RefUnwindSafe, F: RefUnwindSafe, <F as ObjFunc>::Ys: RefUnwindSafe,

§

impl<F> Send for Solver<F>

§

impl<F> Sync for Solver<F>

§

impl<F> Unpin for Solver<F>
where <<F as ObjFunc>::Ys as Fitness>::Best<<F as ObjFunc>::Ys>: Unpin, F: Unpin, <F as ObjFunc>::Ys: Unpin,

§

impl<F> UnwindSafe for Solver<F>
where <<F as ObjFunc>::Ys as Fitness>::Best<<F as ObjFunc>::Ys>: UnwindSafe, F: UnwindSafe, <F as ObjFunc>::Ys: 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> 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.
§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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

source§

impl<T> MaybeParallel for T
where T: Sync + Send,