diff --git a/lib/ControlSystemsBase/src/synthesis.jl b/lib/ControlSystemsBase/src/synthesis.jl index 184cba0fb..3b2f76f78 100644 --- a/lib/ControlSystemsBase/src/synthesis.jl +++ b/lib/ControlSystemsBase/src/synthesis.jl @@ -1,20 +1,27 @@ """ - lqr(sys, Q, R; extra = Val(false)) + lqr(sys, Q, R; extra = Val(false), kwargs...) + lqr(sys, Q, R, N; extra = Val(false), kwargs...) + lqr(Continuous, A, B, Q, R, N; extra = Val(false), kwargs...) + lqr(Discrete, A, B, Q, R, N; extra = Val(false), kwargs...) lqr(Continuous, A, B, Q, R, args...; extra = Val(false), kwargs...) lqr(Discrete, A, B, Q, R, args...; extra = Val(false), kwargs...) Calculate the optimal gain matrix `K` for the state-feedback law `u = -K*x` that minimizes the cost function: -J = integral(x'Qx + u'Ru, 0, inf) for the continuous-time model `dx = Ax + Bu`. -J = sum(x'Qx + u'Ru, 0, inf) for the discrete-time model `x[k+1] = Ax[k] + Bu[k]`. +J = integral(x'Qx + 2real(x'Nu) + u'Ru, 0, inf) for the continuous-time model `dx = Ax + Bu`. +J = sum(x'Qx + 2real(x'Nu) + u'Ru, 0, inf) for the discrete-time model `x[k+1] = Ax[k] + Bu[k]`. Solve the LQR problem for state-space system `sys`. Works for both discrete and continuous time systems. -The `args...; kwargs...` are sent to the Riccati solver, allowing specification of cross-covariance etc. See `?MatrixEquations.arec / ared` for more help. +The optional state-input cross-term matrix `N` has size `(size(A, 1), size(B, 2))` +and defaults to zero. For a single input, `B` and `N` may also be vectors. +Pass it as `lqr(sys, Q, R, N)` or `lqr(Continuous, A, B, Q, R, N)` +(use `Discrete` for a discrete-time model). For real data, the cross term is `2x'Nu`. -To obtain also the solution to the Riccati equation and the eigenvalues of the closed-loop system as well, call `ControlSystemsBase.MatrixEquations.arec / ared` instead (note the different order of the arguments to these functions). +The `args...; kwargs...` are sent to the Riccati solver. In particular, `N` is +the solver's positional argument `S`. See `?MatrixEquations.arec / ared` for more help. To obtain a discrete-time approximation to a continuous-time LQR problem, the function [`c2d`](@ref) can be used to obtain corresponding discrete-time cost matrices. @@ -60,11 +67,34 @@ y, t, x, uout = lsim(sys,u,t,x0=x0) plot(t,x', lab=["Position" "Velocity"], xlabel="Time [s]") ``` +With a state-input cross term: +```jldoctest +julia> using ControlSystemsBase, LinearAlgebra + +julia> A = B = N = ones(1, 1); + +julia> lqr(Continuous, A, B, 2I, 2I, N)[1] ≈ 2 +true + +julia> lqr(Discrete, A, B, 2I, 2I, N)[1] ≈ sqrt(3) - 1 +true +``` + # FAQ -This function requires -- `Q` must be positive semi-definite +For the standard positive semi-definite LQR problem: - `R` must be positive definite -- The pair `(Q,A)` must not have any unobservable modes on the imaginary axis (cont) / unit circle (disc), e.g., there must not be any integrating modes that are not penalized by `Q`. if this condition does not hold, you may get the error "The Hamiltonian matrix is not dichotomic". +- `[Q N; N' R]` must be positive semi-definite (equivalently, `Q - N*(R\\N')` must be positive semi-definite) +- With `F = R\\N'`, `(A - B*F, B)` must be stabilizable and `(Q - N*F, A - B*F)` must be detectable. For `N = 0`, these are the usual conditions on `(A, B)` and `(Q, A)`. Unobservable modes on the imaginary axis (cont) / unit circle (disc), such as an unpenalized integrator, may cause the error "The Hamiltonian matrix is not dichotomic". + +# Extended help +Completing the square with `v = u + F*x`, where `F = R\\N'`, gives the cost +`x'*(Q - N*F)*x + v'*R*v` and dynamics with state matrix `A - B*F`. +This holds in both continuous and discrete time. If `Kbar` solves this transformed +LQR problem, the original feedback gain is `K = Kbar + F`. + +The Riccati solvers handle `N` directly. With Riccati solution `P`, the gain is +`R\\(B'*P + N')` in continuous time and `(R + B'*P*B)\\(B'*P*A + N')` +in discrete time. """ function lqr(::ContinuousType, A, B, Q, R, args...; extra::Val{E} = Val(false), kwargs...) where E S, p, K, args... = arec(A, B, R, Q, args...; kwargs...) diff --git a/lib/ControlSystemsBase/test/runtests.jl b/lib/ControlSystemsBase/test/runtests.jl index 5f63ea64b..f86f36361 100644 --- a/lib/ControlSystemsBase/test/runtests.jl +++ b/lib/ControlSystemsBase/test/runtests.jl @@ -40,6 +40,7 @@ my_tests = [ "test_analysis", "test_matrix_comps", "test_synthesis", + "test_lqr", "test_pid_design", "test_partitioned_statespace", "test_delayed_systems", diff --git a/lib/ControlSystemsBase/test/test_lqr.jl b/lib/ControlSystemsBase/test/test_lqr.jl new file mode 100644 index 000000000..16a271a14 --- /dev/null +++ b/lib/ControlSystemsBase/test/test_lqr.jl @@ -0,0 +1,114 @@ +using StaticArrays: SMatrix, SVector + +@testset "LQR cross term" begin + @testset "scalar reference solutions" begin + A = B = N = ones(1, 1) + K, P = lqr(Continuous, A, B, 2I, 2I, N; extra=Val(true)) + @test K ≈ fill(2.0, 1, 1) + @test P ≈ fill(3.0, 1, 1) + K, P = lqr(Discrete, A, B, 2I, 2I, N; extra=Val(true)) + @test K ≈ fill(sqrt(3) - 1, 1, 1) + @test P ≈ fill(sqrt(3), 1, 1) + end + + @testset "$te, $T" for te in (Continuous, Discrete), T in (Float32, Float64, ComplexF64) + A = T[0 1 0; 0 0 1; -1 -2 -3] + B = T[0 1; 1 0; 1 1] + N = T[0.2 -0.1; 0.1 0.3; -0.2 0.1] + R = T[2 0.3; 0.3 1] + if T <: Complex + A[1, 2] += 0.2im + B[2, 1] += 0.1im + N[1, 2] += 0.1im + R[1, 2] += 0.1im + R[2, 1] -= 0.1im + end + Q = Diagonal(T[2, 3, 4]) + original = deepcopy((A, B, Q, R, N)) + result = lqr(te, A, B, Q, R, N; extra=Val(true)) + K, P, p = result + tol = 100eps(real(T)) + + @test size(K) == (2, 3) + @test eltype(K) == T + @test P ≈ P' + @test lqr(te, A, B, Q, R, N) ≈ K + @test approxsetequal(p, eigvals(A - B*K); atol=tol, rtol=tol) + if te == Continuous + @test all(real.(p) .< 0) + @test K ≈ R \ (B'*P + N') + scale = 2norm(A)*norm(P) + norm(P*B + N)*norm(K) + norm(Q) + @test norm(A'*P + P*A - (P*B + N)*K + Q) <= tol*scale + else + @test all(abs.(p) .< 1) + @test K ≈ (R + B'*P*B) \ (B'*P*A + N') + scale = norm(A)^2*norm(P) + norm(P) + norm(A'*P*B + N)*norm(K) + norm(Q) + @test norm(A'*P*A - P - (A'*P*B + N)*K + Q) <= tol*scale + end + + # Complete the square using v = u + F*x, in either time domain. + F = R \ N' + Abar = A - B*F + Qbar = Hermitian(Q - N*F) + Kbar, Pbar, pbar = lqr(te, Abar, B, Qbar, R; extra=Val(true)) + @test K ≈ Kbar + F + @test P ≈ Pbar + @test approxsetequal(p, pbar; atol=tol, rtol=tol) + + sys = te == Continuous ? ss(A, B, I, 0) : ss(A, B, I, 0, 0.1) + @test lqr(sys, Q, R, N) ≈ K + @test lqr(sys, Q, R, N; extra=Val(true))[2] ≈ P + @test lqr(sys.timeevol, A, B, Q, R, N) ≈ K + @test lqr(te, A, B, Q, R, N; rtol=eps(real(T))) ≈ K + @test lqr(te, A, B, Q, R, zeros(T, size(N))) ≈ lqr(te, A, B, Q, R) + @test lqr(te, A, B, Q, R, -N) ≉ K + @test (A, B, Q, R, N) == original + + # Preserve all backend outputs when extra=Val(true), including solver metadata. + solver = te == Continuous ? ControlSystemsBase.MatrixEquations.arec : ControlSystemsBase.MatrixEquations.ared + expected = solver(A, B, R, Q, N) + @test length(result) == length(expected) + @test all(isequal.(result[4:end], expected[4:end])) + end + + @testset "single input and matrix representations ($te)" for te in (Continuous, Discrete) + A = [0.0 1; 0 0] # Also exercises a singular discrete-time state matrix. + B = [0.0; 1] + N = [0.2; -0.1] + K = lqr(te, A, B, I, I, N) + @test lqr(te, A, reshape(B, 2, 1), I, I, reshape(N, 2, 1)) ≈ K + @test lqr(te, A, B, 1, 1, N) ≈ K + @test lqr(te, A, B, Diagonal(ones(2)), Diagonal(ones(1)), N) ≈ K + @test lqr(te, A, B, Symmetric(Matrix(1.0I, 2, 2)), Hermitian(ones(1, 1)), N) ≈ K + @test lqr(te, view(A, :, :), view(B, :), I, I, view(N, :)) ≈ K + @test lqr(te, A, B, I, I, reshape(N, 1, 2)') ≈ K + @test lqr(te, A, B, I, I, SVector{2}(N)) ≈ K + @test lqr(te, A, reshape(B, 2, 1), I, I, SMatrix{2,1}(N)) ≈ K + @test lqr(te, A, B, I, I, sparse(reshape(N, 2, 1))) ≈ K + end + + @testset "semidefinite transformed cost ($te)" for te in (Continuous, Discrete) + B = Matrix(1.0I, 2, 2) + N = Diagonal([0.2, -0.1]) + R = Diagonal([2.0, 1.0]) + F = R \ N' + Q = N*F + A = (te == Continuous ? -B : 0.5B) + B*F + K, P, p = lqr(te, A, B, Q, R, N; extra=Val(true)) + @test K ≈ F + @test norm(P) < 1e-14 + @test approxsetequal(p, eigvals(A - B*F); atol=1e-14) + end + + @testset "dimensions ($te)" for te in (Continuous, Discrete) + A = [0.0 1 0; 0 0 1; -1 -2 -3] + B = [0.0 1; 1 0; 1 1] + for N in (zeros(2, 2), zeros(3, 1), zeros(2, 3), zeros(3), zeros(3, 3)) + @test_throws DimensionMismatch lqr(te, A, B, I, I, N) + end + @test_throws DimensionMismatch lqr(te, A, B[1:2, :], I, I, zeros(3, 2)) + @test_throws DimensionMismatch lqr(te, A, B, ones(2, 2), I, zeros(3, 2)) + @test_throws DimensionMismatch lqr(te, A, B, I, ones(3, 3), zeros(3, 2)) + @test_throws DimensionMismatch lqr(te, A, B[:, 1], I, I, zeros(3, 2)) + end +end