pub trait Observe<I> {
    // Provided methods
    fn observe_init(&mut self, _name: &str, _kv: &KV) -> Result<(), Error> { ... }
    fn observe_iter(&mut self, _state: &I, _kv: &KV) -> Result<(), Error> { ... }
}
Expand description

An interface which every observer is required to implement

Example

use argmin::core::{Error, KV, State};
use argmin::core::observers::Observe;

struct MyObserver {}

impl<I> Observe<I> for MyObserver
where
    // Optional constraint on `I`. The `State` trait, which every state used in argmin needs to
    // implement, offers a range of methods which can be useful.
    I: State,
{
    fn observe_init(&mut self, name: &str, kv: &KV) -> Result<(), Error> {
        // Do something with `name` and/or `kv`
        // Is executed after initialization of a solver
        Ok(())
    }

    fn observe_iter(&mut self, state: &I, kv: &KV) -> Result<(), Error> {
        // Do something with `state` and/or `kv`
        // Is executed after each iteration of a solver
        Ok(())
    }
}

Provided Methods§

source

fn observe_init(&mut self, _name: &str, _kv: &KV) -> Result<(), Error>

Called once after initialization of the solver.

Has access to the name of the solver via name and to a key-value store kv with entries specific for each solver.

source

fn observe_iter(&mut self, _state: &I, _kv: &KV) -> Result<(), Error>

Called at every iteration of the solver

Has access to the current state of the solver (which always implements State) and to a key-value store kv with entries specific for each solver.

Implementors§

source§

impl<I> Observe<I> for WriteToFilewhere I: State, <I as State>::Param: Serialize,

WriteToFile only implements observer_iter and not observe_init to avoid saving the initial parameter vector. It will only save if there is a parameter vector available in the state, otherwise it will skip saving silently.

source§

impl<I> Observe<I> for SlogLoggerwhere I: State,

source§

impl<I: State> Observe<I> for Observers<I>

Implementing Observe for Observers allows to use it like a single observer. In its implementation it will loop over all stored observers, checks if the conditions for observing are met and calls the actual observers if required.