Skip to main content

LqRegulator

Struct LqRegulator 

Source
pub struct LqRegulator<const N: usize, const M: usize> {
    pub a: Vec<f64>,
    pub b: Vec<f64>,
    pub c: Vec<f64>,
    pub q: Vec<f64>,
    pub q_terminal: Vec<f64>,
    pub r: Vec<f64>,
    pub horizon: f64,
    /* private fields */
}
Expand description

Finite-horizon linear-quadratic regulator with constant coefficients.

Matrices are stored row-major as Vec<f64>. The control dimension M is part of the type so the Control associated type can be a fixed-length array.

See the module-level documentation for the formulation and exact solution.

Fields§

§a: Vec<f64>

State-drift coupling matrix A (row-major, n x n).

§b: Vec<f64>

Control-input matrix B (row-major, n x m).

§c: Vec<f64>

Diffusion matrix C (row-major, n x n).

§q: Vec<f64>

Running state cost Q (row-major, n x n).

§q_terminal: Vec<f64>

Terminal state cost Q_T (row-major, n x n).

§r: Vec<f64>

Running control cost R (row-major, m x m).

§horizon: f64

Remaining horizon T.

Implementations§

Source§

impl<const N: usize, const M: usize> LqRegulator<N, M>

Source

pub fn new( a: &[f64], b: &[f64], c: &[f64], q: &[f64], q_terminal: &[f64], r: &[f64], horizon: f64, ) -> Self

Creates an LQ regulator.

Matrices are row-major. All matrix sizes are asserted to match N and M.

§Panics

Panics if horizon is not positive or any matrix has the wrong length.

§Examples
use solver::models::lq_regulator::LqRegulator;
let lq = LqRegulator::<2, 1>::new(
    &[0.0, 1.0, 0.0, 0.0],
    &[0.0, 1.0],
    &[0.0, 0.0, 0.0, 0.0],
    &[1.0, 0.0, 0.0, 0.0],
    &[1.0, 0.0, 0.0, 0.0],
    &[1.0],
    1.0,
);
assert_eq!(lq.horizon, 1.0);
Source

pub fn riccati_solution(&self) -> (Vec<f64>, f64)

Solves the backward Riccati ODE with an implicit Euler scheme.

Returns (p, q) where p is the flat row-major solution of P(t) at t = 0 and q is the scalar noise-correction term q(0).

The number of time steps is fixed by Self::riccati_steps.

§Examples
use solver::models::lq_regulator::LqRegulator;
let lq = LqRegulator::<1, 1>::new(
    &[0.0], &[1.0], &[0.0], &[1.0], &[1.0], &[1.0], 1.0,
);
let (p, q) = lq.riccati_solution();
assert!(p[0] > 0.0);
assert!(q >= 0.0);
Source

pub fn riccati_solution_with_steps(&self, steps: usize) -> (Vec<f64>, f64)

Solves the backward Riccati ODE for the first steps fixed-size backward steps from the terminal condition.

Each step has size horizon / riccati_steps, so steps selects the remaining horizon steps * horizon / riccati_steps at which P and q are evaluated. steps = 0 returns the terminal condition (Q_T, 0); steps = riccati_steps() returns the solution at the start of the horizon.

Source

pub fn riccati_steps(&self) -> usize

Number of Riccati backward steps.

The Riccati ODE is independent of the spatial grid and, for constant coefficients, its solution is essentially independent of the step size once dt is small. A fixed modest step count keeps the reference cheap and deterministic.

Source

pub fn feedback_gain(&self, t: f64) -> Vec<f64>

Optimal feedback gain K = -R^{-1} B' P(t) at forward time t.

Returns a row-major m x n matrix K so that u = K x.

Source

pub fn exact_value(&self, state: &[f64; N], tau: f64) -> f64

Exact value V(t, x) = -x' P(t) x - q(t) for remaining horizon tau.

Source

pub fn exact_control(&self, state: &[f64; N], tau: f64) -> [f64; M]

Exact control u* = -R^{-1} B' P(t) x for remaining horizon tau.

Trait Implementations§

Source§

impl<const N: usize, const M: usize> Clone for LqRegulator<N, M>

Source§

fn clone(&self) -> LqRegulator<N, M>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize, const M: usize> ControlProblem<N> for LqRegulator<N, M>

Source§

type Control = [f64; M]

The action type. For Merton this is a scalar portfolio fraction; for market making it is a pair of bid/ask intensities.
Source§

fn optimize( &self, t: f64, state: &[f64; N], _derivs: &StateDerivatives<N>, ) -> Self::Control

Returns the control that maximizes the driver at (t, state).
Source§

fn running_reward( &self, _t: f64, state: &[f64; N], control: &Self::Control, ) -> f64

Returns the running reward f(t,x,u).
Source§

fn generator( &self, _t: f64, state: &[f64; N], control: &Self::Control, derivs: &StateDerivatives<N>, ) -> f64

Returns the infinitesimal generator L^u V for the given control and derivative bundle.
Source§

fn terminal(&self, state: &[f64; N]) -> f64

Terminal value g(x) at the horizon.
Source§

fn next_step( &self, t: f64, state: &[f64; N], dt: f64, noise: &[f64; N], ) -> [f64; N]

Advances the state one step under the optimal control at forward time t.
Source§

fn is_diffusion_dimension(&self, _dim: usize) -> bool

Whether a dimension is driven by Brownian diffusion.
Source§

fn driver( &self, t: f64, state: &[f64; N], control: &Self::Control, derivs: &StateDerivatives<N>, ) -> f64

Returns the full HJB driver f(t,x,u) + L^u V. Read more
Source§

fn bsde_driver( &self, t: f64, state: &[f64; N], control: &Self::Control, _derivs: &StateDerivatives<N>, _dt: f64, ) -> f64

Returns the backward driver consumed by the BSDE regression solver. Read more
Source§

fn apply_constraint(&self, _state: &[f64; N], value: f64) -> f64

Optional pointwise constraint on the value, e.g. the early-exercise obstacle V >= payoff for an American option. Read more
Source§

fn discount_rate(&self, _state: &[f64; N]) -> f64

Discount rate r(t,x). Defaults to zero.
Source§

fn constant_discount_rate(&self) -> Option<f64>

Constant discount rate hint. Defaults to None (state dependent).
Source§

fn next_step_controlled( &self, t: f64, state: &[f64; N], _control: &Self::Control, dt: f64, noise: &[f64; N], ) -> [f64; N]

Advances the state one step under an explicitly supplied control. Read more
Source§

fn is_reduced_value(&self) -> bool

Whether the value this problem solves is a reduced value. Read more
Source§

fn gradient_step(&self, _dim: usize) -> f64

Physical finite-difference step for a dimension, used by mesh-free gradient stencils.
Source§

impl<const N: usize, const M: usize> Debug for LqRegulator<N, M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize, const M: usize> !Freeze for LqRegulator<N, M>

§

impl<const N: usize, const M: usize> RefUnwindSafe for LqRegulator<N, M>
where OnceLock<RiccatiCache<N, M>>: RefUnwindSafe,

§

impl<const N: usize, const M: usize> Send for LqRegulator<N, M>
where OnceLock<RiccatiCache<N, M>>: Send,

§

impl<const N: usize, const M: usize> Sync for LqRegulator<N, M>
where OnceLock<RiccatiCache<N, M>>: Sync,

§

impl<const N: usize, const M: usize> Unpin for LqRegulator<N, M>
where OnceLock<RiccatiCache<N, M>>: Unpin,

§

impl<const N: usize, const M: usize> UnsafeUnpin for LqRegulator<N, M>
where OnceLock<RiccatiCache<N, M>>: UnsafeUnpin,

§

impl<const N: usize, const M: usize> UnwindSafe for LqRegulator<N, M>
where OnceLock<RiccatiCache<N, M>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V