Trait rsgenetic::sim::Simulation
[−]
[src]
pub trait Simulation<'a, T, F> where
T: Phenotype<F>,
F: Fitness, { type B: Builder<Self>; fn builder(population: &'a mut Vec<T>) -> Self::B
where
Self: Sized; fn run(&mut self) -> RunResult; fn step(&mut self) -> StepResult; fn checked_step(&mut self) -> StepResult; fn get(&'a self) -> SimResult<'a, T>; fn time(&self) -> Option<NanoSecond>; fn iterations(&self) -> u64; fn population(&self) -> Vec<T>; }
A Simulation
is an execution of a genetic algorithm.
Associated Types
Required Methods
fn builder(population: &'a mut Vec<T>) -> Self::B where
Self: Sized,
Self: Sized,
Create a Builder
to create an instance.
population
is a required parameter of any Simulation
, which
is why it is a parameter of this function.
fn run(&mut self) -> RunResult
Run the simulation completely.
fn step(&mut self) -> StepResult
: To encourage checking the StepResult
while maintaining backwards compatibility, this function has been deprecated in favour of checked_step
.
Make one step in the simulation. This function returns a StepResult
:
StepResult::Success
when a step was successful, but the simulation is not done.StepResult::Failure
when an error occurred. Check the result ofget()
.StepResult::Done
on convergence or reaching the maximum iterations.
Be careful to check for failures when running step()
in a loop,
to avoid infinite loops. To run the simulation until convergence or until
reaching a maximum number of iterations, consider using run()
instead:
fn checked_step(&mut self) -> StepResult
Make one step in the simulation. This function returns a StepResult
:
StepResult::Success
when a step was successful, but the simulation is not done.StepResult::Failure
when an error occurred. Check the result ofget()
.StepResult::Done
on convergence or reaching the maximum iterations.
Unlike step
, this function will panic if it is called after a failure.
To avoid this panic, match StepResult
for StepResult::Failure
and exit gracefully.
fn get(&'a self) -> SimResult<'a, T>
Get the result of the latest step or of a complete run.
This function will either return the best performing individual, or an error string indicating what went wrong.
fn time(&self) -> Option<NanoSecond>
Get the number of nanoseconds spent running, or None
in case of an overflow.
When Self
is par::Simulator
, i.e. a parallel simulator is used,
the duration is the average duration of all child simulators.
fn iterations(&self) -> u64
Get the number of iterations the Simulator
has executed so far.
When Self
is par::Simulator
, i.e. a parallel simulator is used,
this returns the number of iterations made by the parallel simulator itself.
fn population(&self) -> Vec<T>
Get the current population.
Using this function clones the population out of the Simulation
, so use
it sparingly.
Implementors
impl<'a, T, F> Simulation<'a, T, F> for Simulator<'a, T, F> where
T: Phenotype<F>,
F: Fitness, type B = SimulatorBuilder<'a, T, F>;