Skip to main content

solver/numeric/
result.rs

1//! Generic output from a numerical HJB solver.
2
3/// Output from a numerical HJB solver, parameterized by the control type.
4///
5/// Contains the value function and the optimal control at a query state.
6/// The optional grid stores the full value function when the solver works on
7/// a finite-difference grid.
8#[derive(Debug, Clone)]
9pub struct GridSolution<const N: usize, C> {
10    /// Value function `V(t, x)` at the query state.
11    pub value: f64,
12
13    /// Optimal control at the query state.
14    pub control: C,
15
16    /// Full value function on the grid (`Some` for FD solvers, `None` for
17    /// grid-free solvers such as BSDE).
18    pub value_grid: Option<Vec<f64>>,
19}
20
21impl<const N: usize, C> GridSolution<N, C> {
22    /// Creates a [`GridSolution`] from raw components.
23    ///
24    /// # Examples
25    ///
26    /// ```
27    /// use solver::numeric::result::GridSolution;
28    ///
29    /// let solution = GridSolution::<1, f64>::new(1.5, 0.25, None);
30    /// assert_eq!(solution.value, 1.5);
31    /// assert_eq!(solution.control, 0.25);
32    /// assert!(solution.value_grid.is_none());
33    /// ```
34    pub fn new(value: f64, control: C, value_grid: Option<Vec<f64>>) -> Self {
35        Self {
36            value,
37            control,
38            value_grid,
39        }
40    }
41}