Struct metaheuristics_nature::utility::SolverBuilder [−][src]
Expand description
Collect configuration and build the solver.
This type is created by Solver::build
method.
Implementations
Set random seed.
Default
By default, the random seed is None
, which is decided by getrandom::getrandom
.
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.
pub 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.
Set adaptive function.
The adaptive value can be access from ObjFunc::fitness
.
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 zero.
pub 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.