trellis_runner/
calculation.rs

1use crate::Problem;
2
3/// Trait implemented by all problems solveable by `Trellis`
4///
5/// A calculation defines the core loop of the solver. Typically we would write a for loop,
6/// consisting of an initialisation step where the calculation is arranged, a procedure carried out
7/// on each loop iteration, and a finalisation step prior to return. This trait separates these
8/// methods so they can be called by the [`Runner`]
9pub trait Calculation<P, S> {
10    /// The error associated with the problem
11    type Error: std::error::Error + 'static;
12    /// The type returned to the caller.
13    ///
14    /// Trellis defines a data-rich [`Output`], which can be constructed from the calculation, and
15    /// internal state. In some circumstances it may be appropriate to return this type to the
16    /// caller. In other circumstances it may be preferential to bury this complexity, returning
17    /// the caller a custom datatype.
18    type Output;
19
20    /// An identifier for the calculation.
21    ///
22    /// This identifier is printed in tracing logs
23    const NAME: &'static str;
24    /// Initialisation.
25    ///
26    /// This step prepares the state object for the main calculation loop.
27    fn initialise(&mut self, _problem: &mut Problem<P>, state: S) -> Result<S, Self::Error> {
28        Ok(state)
29    }
30    /// One iteration of the core algorithm
31    fn next(&mut self, problem: &mut Problem<P>, state: S) -> Result<S, Self::Error>;
32    /// Converts the internal state to the user-facing return datatype
33    fn finalise(&mut self, problem: &mut Problem<P>, state: S)
34        -> Result<Self::Output, Self::Error>;
35}