Struct metaheuristics_nature::utility::SolverBuilder
source · [−]Expand description
Collect configuration and build the solver.
This type is created by Solver::build
method.
Implementations
sourceimpl<'a, S, F, R> SolverBuilder<'a, S, F, R> where
S: Setting,
F: ObjFunc,
impl<'a, S, F, R> SolverBuilder<'a, S, F, R> where
S: Setting,
F: ObjFunc,
sourcepub fn seed(self, seed: Option<u128>) -> Self
pub fn seed(self, seed: Option<u128>) -> Self
Set random seed.
Default
By default, the random seed is None
, which is decided by getrandom::getrandom
.
sourcepub fn task<'b, C>(self, task: C) -> SolverBuilder<'b, S, F, R> where
'a: 'b,
C: Fn(&Context<F>) -> bool + 'b,
pub fn task<'b, C>(self, task: C) -> SolverBuilder<'b, S, F, R> where
'a: 'b,
C: Fn(&Context<F>) -> bool + 'b,
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())
.task(|ctx| ctx.gen == 20)
.solve(MyFunc::new());
Default
By default, the algorithm will iterate 200 generation.
sourcepub fn record<'b, C, NR>(self, record: C) -> SolverBuilder<'b, S, F, NR> where
'a: 'b,
C: Fn(&Context<F>) -> NR + 'b,
pub fn record<'b, C, NR>(self, record: C) -> SolverBuilder<'b, S, F, NR> where
'a: 'b,
C: Fn(&Context<F>) -> NR + 'b,
Set record function.
The record function will be called at each generation and save the return value in the report.
Due to memory allocation, this function should record as less information as possible.
For example, return unit type ()
can totally disable this function.
After calling solve
function, you can take the report value with Solver::report
method.
The following example records generation and spent time for the report.
use metaheuristics_nature::{Rga, Solver};
let s = Solver::build(Rga::default())
.record(|ctx| (ctx.gen, ctx.adaptive))
.solve(MyFunc::new());
let report: &[(u64, f64)] = s.report();
Default
By default, this function returns unit type ()
, which allocates nothing.
sourcepub fn adaptive<'b, C>(self, adaptive: C) -> SolverBuilder<'b, S, F, R> where
'a: 'b,
C: FnMut(&Context<F>) -> f64 + 'b,
pub fn adaptive<'b, C>(self, adaptive: C) -> SolverBuilder<'b, S, F, R> where
'a: 'b,
C: FnMut(&Context<F>) -> f64 + 'b,
Set adaptive function.
The adaptive value can be access from ObjFunc::fitness
,
and can be used to enhance the fitness value.
use metaheuristics_nature::{Rga, Solver};
let s = Solver::build(Rga::default())
.adaptive(|ctx| ctx.gen as f64 / 20.)
.solve(MyFunc::new());
The adaptive function is also allow to change the external variable.
use metaheuristics_nature::{Rga, Solver};
let mut diff = None;
let s = Solver::build(Rga::default())
.adaptive(|ctx| {
if let Some(f) = diff {
let d = f - ctx.best_f;
diff = Some(d);
d
} else {
diff = Some(ctx.best_f);
ctx.best_f
}
})
.solve(MyFunc::new());
Default
By default, this function returns one.
sourcepub fn callback<'b, C>(self, callback: C) -> SolverBuilder<'b, S, F, R> where
'a: 'b,
C: FnMut(&Context<F>) + 'b,
pub fn callback<'b, C>(self, callback: C) -> SolverBuilder<'b, S, F, R> where
'a: 'b,
C: FnMut(&Context<F>) + 'b,
Set callback function.
Callback function allows to change an outer mutable variable in each iteration.
use metaheuristics_nature::{Rga, Solver};
let mut gen = 0;
let s = Solver::build(Rga::default())
.callback(|ctx| gen = ctx.gen)
.solve(MyFunc::new());
In the example below, the fields of the app
are mutable variables that changes every time.
But we still need to use its method in task
condition,
so a RwLock
/ Mutex
lock / std::sync::atomic
is required.
If you spawn the optimization process into another thread, adding a reference counter is also required.
use metaheuristics_nature::{Rga, Solver};
use std::sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
Mutex,
};
#[derive(Default)]
struct App {
is_start: AtomicBool,
gen: AtomicU64,
fitness: Mutex<f64>,
}
let app = App::default();
// Spawn the solver here!
let s = Solver::build(Rga::default())
.task(|ctx| ctx.gen == 20 || !app.is_start.load(Ordering::Relaxed))
.callback(|ctx| {
app.gen.store(ctx.gen, Ordering::Relaxed);
*app.fitness.lock().unwrap() = ctx.best_f;
})
.solve(MyFunc::new());
Default
By default, this function does nothing.
Auto Trait Implementations
impl<'a, S, F, R> !RefUnwindSafe for SolverBuilder<'a, S, F, R>
impl<'a, S, F, R> !Send for SolverBuilder<'a, S, F, R>
impl<'a, S, F, R> !Sync for SolverBuilder<'a, S, F, R>
impl<'a, S, F, R> Unpin for SolverBuilder<'a, S, F, R> where
S: Unpin,
impl<'a, S, F, R> !UnwindSafe for SolverBuilder<'a, S, F, R>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more