Struct metaheuristics_nature::SolverBuilder
source · pub struct SolverBuilder<'a, A: Algorithm<F>, F: ObjFunc> { /* private fields */ }
Expand description
Collect configuration and build the solver.
This type is created by Solver::build()
method.
- First, setting a fixed seed with
SolverBuilder::seed()
method to get a determined result is highly recommended. - Next is
SolverBuilder::task()
method with a termination condition. - Finally, call
SolverBuilder::solve()
method to start the algorithm.
Implementations§
source§impl<'a, A: Algorithm<F>, F: ObjFunc> SolverBuilder<'a, A, F>
impl<'a, A: Algorithm<F>, F: ObjFunc> SolverBuilder<'a, A, F>
sourcepub fn pareto_limit(self, pareto_limit: usize) -> Self
pub fn pareto_limit(self, pareto_limit: usize) -> Self
Pareto front limit.
It is not working for single-objective optimization.
use metaheuristics_nature::{Rga, Solver};
let s = Solver::build(Rga::default(), MyFunc::new())
.seed(0)
.task(|ctx| ctx.gen == 20)
.pareto_limit(10)
.solve();
§Default
By default, there is no limit. The limit is set to usize::MAX
.
sourcepub fn seed(self, seed: impl Into<SeedOpt>) -> Self
pub fn seed(self, seed: impl Into<SeedOpt>) -> Self
Set a fixed random seed to get a determined result.
§Default
By default, the random seed is auto-decided so you cannot reproduce the
result. Please print the seed via Solver::seed()
method to get the
seed that used in the algorithm.
sourcepub fn init_pool(self, pool: Pool<'a, F>) -> Self
pub fn init_pool(self, pool: Pool<'a, F>) -> Self
Initialize the pool with the pool option.
§Default
By default, the pool is generated by the uniform distribution
uniform_pool()
.
sourcepub fn task<'b, C>(self, task: C) -> SolverBuilder<'b, A, F>
pub fn task<'b, C>(self, task: C) -> SolverBuilder<'b, A, F>
Termination condition.
The task function will be check each iteration, breaks if the return is true.
use metaheuristics_nature::{Rga, Solver};
let s = Solver::build(Rga::default(), MyFunc::new())
.seed(0)
.task(|ctx| ctx.gen == 20)
.solve();
§Default
By default, the algorithm will iterate 200 generation.
sourcepub fn callback<'b, C>(self, callback: C) -> SolverBuilder<'b, A, F>
pub fn callback<'b, C>(self, callback: C) -> SolverBuilder<'b, A, F>
Set callback function.
Callback function allows to change an outer mutable variable in each iteration.
use metaheuristics_nature::{Rga, Solver};
let mut report = Vec::with_capacity(20);
let s = Solver::build(Rga::default(), MyFunc::new())
.seed(0)
.task(|ctx| ctx.gen == 20)
.callback(|ctx| report.push(ctx.best.get_eval()))
.solve();
§Default
By default, this function does nothing.
sourcepub fn solve(self) -> Solver<F>
pub fn solve(self) -> Solver<F>
Create the task and run the algorithm, which may takes a lot of time.
Generation ctx.gen
is start from 1, initialized at 0.
§Panics
Panics before starting the algorithm if the following conditions are met:
- The dimension size is zero.
- The lower bound is greater than the upper bound.
- Using the
Pool::Ready
option and the pool size or dimension size is not consistent.
Auto Trait Implementations§
impl<'a, A, F> Freeze for SolverBuilder<'a, A, F>
impl<'a, A, F> !RefUnwindSafe for SolverBuilder<'a, A, F>
impl<'a, A, F> Send for SolverBuilder<'a, A, F>
impl<'a, A, F> !Sync for SolverBuilder<'a, A, F>
impl<'a, A, F> Unpin for SolverBuilder<'a, A, F>
impl<'a, A, F> !UnwindSafe for SolverBuilder<'a, A, F>
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> 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