LinearQuadraticRegulator<T>

Models & Types · AiDotNet.Control

The infinite-horizon linear quadratic regulator: the optimal state-feedback controller for a linear system with a quadratic cost.

For Beginners

You describe how the system moves, say how much you dislike being away from the target, and say how much you dislike using the controls. This works out the best possible trade-off and hands you a matrix. From then on, controlling the system is multiplying the current state by that matrix and negating it.

How It Works

Minimizes Σ (xᵀQx + uᵀRu) in discrete time, or ∫ (xᵀQx + uᵀRu) dt in continuous time, subject to the system’s dynamics. The answer is remarkable and is the reason LQR is the foundation of linear control: the optimal input is a constant linear function of the current state, u = −Kx. No lookahead, no memory, no schedule — one matrix multiply, and it is optimal over an infinite horizon.

Follows the standard development in R. E. Kalman, “Contributions to the Theory of Optimal Control”, Boletín de la Sociedad Matemática Mexicana 5, 1960, pp. 102-119. The gain comes from the algebraic Riccati equation, solved by DiscreteAlgebraicRiccatiSolver or ContinuousAlgebraicRiccatiSolver according to the time domain.

Choosing Q and R is the whole design. They are not tuning knobs bolted onto an algorithm; they are the specification. Q prices deviations of the state, R prices control effort, and only their ratio matters — scaling both by the same factor leaves the gain unchanged. Raising Q relative to R buys a faster, more aggressive response paid for with larger inputs.

API boundary: Control algorithms intentionally use this standalone, strongly typed API rather than AiModelBuilder/AiModelResult. They are online state-feedback components, not fitted prediction models: callers provide the physical system matrices once and then submit one current-state vector per control step while reading control-specific diagnostics. Keeping this boundary explicit avoids forcing real-time control state into the training facade.

Example

// A double integrator sampled at 1 Hz: position and velocity, force input.
var a = new Matrix<double>(new[,] { { 1.0, 1.0 }, { 0.0, 1.0 } });
var b = new Matrix<double>(new[,] { { 0.5 }, { 1.0 } });
var q = Matrix<double>.CreateIdentity(2);
var r = Matrix<double>.CreateIdentity(1);
            
var regulator = new LinearQuadraticRegulator<double>(a, b, q, r);
var input = regulator.ComputeControl(new Vector<double>(new[] { 5.0, 0.0 }));

Constructors

Constructor Summary
LinearQuadraticRegulator(Matrix<T> stateMatrix, Matrix<T> inputMatrix, Matrix<T> stateCost, Matrix<T> inputCost, ControlTimeDomain timeDomain, AlgebraicRiccatiSolverOptions options) Creates a linear quadratic regulator and solves for its gain.

Properties

Property Summary
AlgebraicRiccatiSolution<T> RiccatiSolution { get; } Gets the underlying Riccati solution, including its residual and whether it converged.
ControlTimeDomain TimeDomain { get; } Gets whether this regulator was designed for a discrete or continuous system.
Matrix<T> ClosedLoopMatrix { get; } Returns the closed-loop state matrix A − BK, which governs how the controlled system evolves.
Matrix<T> CostToGoMatrix { get; } Gets the cost-to-go matrix P, for which xᵀPx is the total remaining cost from state x under optimal control.
Matrix<T> Gain { get; } Gets the optimal feedback gain K.

Methods

Method Summary
T CostToGo(Vector<T> state) Returns xᵀPx, the total remaining cost of driving state to the origin under optimal control.
Vector<T> ComputeControl(Vector<T> state) Computes the optimal control input u = −Kx for the current state.