Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions lib/ControlSystemsBase/src/synthesis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,16 @@ end
Robust pole placement using the algorithm from
> "Robust Pole Assignment in Linear State Feedback", Kautsky, Nichols, Van Dooren

This implementation uses "method 0" for the X-step and the QR factorization for all factorizations.
This implementation uses "method 0" for the X-step and QR factorizations to
construct the input and eigenvector subspaces.
The iteration monitors the relative change in `abs(det(X))` using logarithms
and a tolerance of `sqrt(eps(real(T)))`, where `T` is the floating-point working
type. For real systems, the candidate closed-loop matrix must also be real to
working precision before stopping, including when random initialization is used
with real target poles. The limit is 200 sweeps.
These are numerical stopping criteria, not a guarantee of pole accuracy.
The iterative method seeks a nonsingular eigenvector matrix; repeated poles and
poorly controllable systems can cause it to fail or reach the iteration limit.

This function will be called automatically when [`place`](@ref) is called with a MIMO system.

Expand All @@ -417,7 +426,10 @@ function place_knvd(A::AbstractMatrix, B, λ; verbose=false, init=:s, method = 0
length(λ) == size(A, 1) == n || error("Must specify as many poles as the state dimension")
Λ = diagm(λ)
R = svdvals(B)
m = count(>(100*eps()*R[1]), R) # Rank of B
# Use the precision of the SVD, which may differ from the promoted type T.
# Retain the safety factor for nearly dependent columns, and account for size.
m = isempty(R) ? 0 : count(>(max(100, size(B)...)*eps(eltype(R))*R[1]), R)
m > 0 || throw(ArgumentError("B must have nonzero numerical rank"))
QRB = qr(B, ColumnNorm())
U0, U1 = QRB.Q[:, 1:m], QRB.Q[:, m+1:end] # TODO: check dimension
Z = (QRB.R*QRB.P')[:, 1:m]
Expand Down Expand Up @@ -475,7 +487,9 @@ function place_knvd(A::AbstractMatrix, B, λ; verbose=false, init=:s, method = 0
error("Unknown init method")
end

cond_old = float(T)(Inf)
logdet_old = real(T)(-Inf)
rtol = sqrt(eps(real(T)))
check_real = isreal(A) && isreal(B)
if method == 0
for i = 1:200
verbose && @info "Iteration $i"
Expand All @@ -487,12 +501,20 @@ function place_knvd(A::AbstractMatrix, B, λ; verbose=false, init=:s, method = 0
any(!isfinite, xj) && error("Not finite")
X[:, j] = xj
end
c = cond(X)
verbose && @info "cond(X) = $c"
if cond_old - c < 1e-14
break
# Method 0 increases abs(det(X)), not necessarily decreases cond(X).
# Logarithms avoid underflow; their difference measures relative change.
logdet = LinearAlgebra.logabsdet(X)[1]
verbose && @info "cond(X) = $(cond(X))"
stop = isfinite(logdet) && abs(logdet - logdet_old) <= rtol
if stop && check_real
# A small determinant change need not imply that discarding the
# imaginary part of the feedback preserves the assigned poles.
M = X*Λ/X
scale = norm(M)
stop = isfinite(scale) && norm(imag(M)) <= n*eps(real(T))*scale
end
cond_old = c
stop && break
logdet_old = logdet
i == 200 && @warn "Max iterations reached"
end
# elseif method == 1
Expand Down
85 changes: 83 additions & 2 deletions lib/ControlSystemsBase/test/test_synthesis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ end
]'
# p = [-0.07732, -0.01423, -0.8953, -2.841, -5.982]
p = [-0.2, -0.5, -1, -1+im, -1-im]
L = place(A, B, p; verbose=true) # with verbose, should prind cond(X) ≈ 39.4 which it does
@test allin(eigvals(A - B*L), p; tol=0.025) # Tolerance from paper
# cond(X) can increase before the iteration settles. Stopping on the first
# increase used to leave a pole error of about 0.022 in this example.
L = place(A, B, p; verbose=true)
@test allin(eigvals(A - B*L), p; tol=1e-10)
# norm(L)

## Rank deficient multi-input B
Expand All @@ -201,6 +203,85 @@ end

end

@testset "KNVD numerical rank" begin
for T in (Float32, Float64), scale in T.((1e-6, 1, 1e6))
A = T[0 1; 0 0]
B = scale .* T[1 1 1; 2 2 2]
p = T[-2, -1]
K = place(A, B, p)
# B = [1, 2]*[1 1 1]. The effective single-input gain is [1 1].
@test scale*K ≈ fill(one(T)/3, 3, 2) rtol=100eps(T)
@test A - B*K ≈ T[-1 0; -2 -2] rtol=100eps(T)
@test sort(eigvals(A - B*K)) ≈ p rtol=100eps(T)

# Promoting A must not change the precision used to determine rank(B).
K = place(Float64.(A), B, Float64.(p))
@test sort(eigvals(A - B*K)) ≈ p rtol=100eps(T)

# A square, singular B must not take the full-row-rank shortcut.
B = scale .* ones(T, 2, 2)
K = place(A, B, p)
@test sort(eigvals(A - B*K)) ≈ p rtol=100eps(T)
end

for T in (Float32, Float64)
A = T[0 1; 0 0]
@test_throws ArgumentError place(A, zeros(T, 2, 2), T[-1, -2])
@test_throws ArgumentError place(A, zeros(T, 2, 0), T[-1, -2])

# A genuinely small, independent input direction must be retained.
B = Matrix(Diagonal(T[1, 1000eps(T)]))
K = place(A, B, T[-1, -2])
@test sort(eigvals(A - B*K)) ≈ T[-2, -1] rtol=100eps(T)

B = T[1 1 1; 2 2 2]
p = Complex{T}[-1-im, -1+im]
K = place(A, B, p)
@test sort(eigvals(A - B*K); by=imag) ≈ p rtol=100eps(T)

# Repeated poles in the existing rank-one reduction use Ackermann's
# formula. Check the closed-loop matrix without root-splitting error.
K = place(A, B, T[-1, -1])
@test A - B*K ≈ T[-0.5 0.25; -1 -1.5] rtol=100eps(T)

A = zeros(T, 3, 3)
B = T[1 0; 0 1; 0 0]
@test_throws ErrorException place(A, B, T[-1, -2, -3])
end

# Rank two with redundant input columns exercises the iterative reduction.
for T in (Float32, Float64), scale in T.((1e-6, 1, 1e6))
A = T[0 1 0 0; 0 0 1 0; 0 0 0 1; 0 0 0 0]
B = scale .* T[1 0 1; 0 1 1; 1 0 1; 0 1 1]
p = T[-1, -2, -3, -4]
K = place(A, B, p)
@test sort(eigvals(A - B*K)) ≈ sort(p) rtol=100eps(T)
@test all(isfinite, K)

# Lowering the original safety factor treats these nearly dependent
# columns as independent in Float64 and yields huge, inaccurate gains.
A = T[0 1; 0 0]
B = scale .* T[1 1; 2 2+32eps(T)]
K = place(A, B, T[-1, -2])
@test sort(eigvals(A - B*K)) ≈ T[-2, -1] rtol=100eps(T)
@test norm(scale*K) < 2
end
end

@testset "KNVD random initialization with real poles" begin
for T in (Float32, Float64), seed in 1:3
A = T[2 0 2 2; 3 0 1 -3; 0 3 0 3; -3 3 1 -1]
B = T[-2 3; -3 -2; 1 3; 3 2]
p = T[-4, -3, -2, -1]
# Random initialization uses complex eigenvectors even for real poles.
# Determinant stagnation alone does not justify projecting the gain to real.
Random.seed!(seed)
K = place(A, B, p; init=:rand)
@test isreal(K)
@test sort(eigvals(A - B*K)) ≈ p rtol=100eps(T)
end
end

@testset "LQR" begin
Ts = 0.1
A = [1 Ts; 0 1]
Expand Down
Loading