pub trait State<Observations, EvalState, Model, Error>where
Self: Sized,{
// Required methods
fn init(obs: &Observations, model: Model) -> Self;
fn step(&self) -> Result<Model, Error>;
fn eval(&self, obs: &Observations, new_model: Model) -> EvalState;
fn stop_criterion(
self,
nb_iter: usize,
eval_state: EvalState,
) -> (Self, Continue);
// Provided method
fn iterative_solve(
obs: &Observations,
initial_model: Model,
) -> Result<(Self, usize), Error> { ... }
}
Expand description
An State<Observations, EvalState, Model, Error>
is capable of iteratively minimizing an energy function,
if provided few functions that are evaluated during iterations.
It merely is a skeleton for any iterative optimizer, that I have found to be flexible enough for all my past needs. Here is a simple description of its generic types.
Self
: State of the iterative optimizer.Observations
: Data used as reference during energy evaluations.EvalState
: Data computed while evaluating a model just computed. Will typically be result successfully containing all the data needed to update the optimizer state, or an error meaning that we stopped the evaluation because the energy increased.Model
: The model of what you are trying to optimize.Error
: Custom error type for potential failures in step computation.
Required Methods§
Sourcefn init(obs: &Observations, model: Model) -> Self
fn init(obs: &Observations, model: Model) -> Self
Initialize the optimizer state.
Sourcefn step(&self) -> Result<Model, Error>
fn step(&self) -> Result<Model, Error>
Compute an iteration step from the current optimizer state. May fail, in such cases, iterations are stopped.
Sourcefn eval(&self, obs: &Observations, new_model: Model) -> EvalState
fn eval(&self, obs: &Observations, new_model: Model) -> EvalState
Evaluate the model.
You might want to short-circuit evaluation of a full new state depending on your usage
(e.g. if the energy increases).
This is why it returns an EvalState
and not Self
.
Sourcefn stop_criterion(
self,
nb_iter: usize,
eval_state: EvalState,
) -> (Self, Continue)
fn stop_criterion( self, nb_iter: usize, eval_state: EvalState, ) -> (Self, Continue)
Function deciding if iterations should continue. Also return the state that will be used for next iteration.
Provided Methods§
Sourcefn iterative_solve(
obs: &Observations,
initial_model: Model,
) -> Result<(Self, usize), Error>
fn iterative_solve( obs: &Observations, initial_model: Model, ) -> Result<(Self, usize), Error>
Iteratively solve your optimization problem, with the provided functions by the trait implementation. Return the final state and the number of iterations. May return an error if a step computation failed.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.