-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathControlSystemsBaseImplicitDifferentiationExt.jl
More file actions
262 lines (207 loc) · 7.03 KB
/
ControlSystemsBaseImplicitDifferentiationExt.jl
File metadata and controls
262 lines (207 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
module ControlSystemsBaseImplicitDifferentiationExt
using ControlSystemsBase
import ControlSystemsBase: kalman, lqr, are, ContinuousType, DiscreteType
using ForwardDiff
using ForwardDiff: Dual
using ImplicitDifferentiation
using ComponentArrays
using LinearAlgebra
function forward_arec(pars)
(; A,B,Q,R) = pars
ControlSystemsBase.are(Continuous, A, B, Q, R), 0
end
function conditions_arec(pars, X, noneed)
(; A,B,Q,R) = pars
C = A'X
C .+= C'
C .+= Q
XB = X*B
mul!(C, XB, R\XB', -1, 1)
# C .+ X .- X' # Does not seem to be needed
end
const implicit_arec = ImplicitFunction(forward_arec, conditions_arec)
"""
are(::Continuous, A, B, Q, R::AbstractMatrix{<:Dual}; kwargs)
To make the ARE solver work with dual numbers, make sure that the `R` matrix has the dual number element type.
"""
function are(::ContinuousType, A::AbstractMatrix, B, Q, R::AbstractMatrix{<:Dual}; kwargs...)
pars = ComponentVector(; A,B,Q,R)
X0, _ = implicit_arec(pars)
X = X0 isa AbstractMatrix ? X0 : reshape(X0, size(A))
X
end
"""
lqr(::Continuous, A, B, Q, R::AbstractMatrix{<:Dual})
To make the LQR solver work with dual numbers, make sure that the `R` matrix has the dual number element type.
"""
function lqr(::ContinuousType, A, B, Q, R::AbstractMatrix{<:Dual})
X = are(Continuous, A, B, Q, R)
R\(B'X)
end
"""
kalman(::Continuous, A, C, Q, R::AbstractMatrix{<:Dual})
To make the Kalman solver work with dual numbers, make sure that the `R` matrix has the dual number element type.
"""
function kalman(::ContinuousType, A, C, Q, R::AbstractMatrix{<:Dual})
X = are(Continuous, A', C', Q, R)
(R\(C*X))'
end
## Discrete
function forward_ared(pars)
(; A,B,Q,R) = pars
# Q = reshape(Q0, size(A))
ControlSystemsBase.are(Discrete, A, B, Q, R), 0
end
function conditions_ared(pars, X, noneed)
# A'XA - X - (A'XB+S)(R+B'XB)^(-1)(B'XA+S') + Q = 0
# A'X*A - X - (A'X*B)*((R+B'X*B)\(B'X*A)) + Q
(; A,B,Q,R) = pars
AX = A'X
C = AX*A
C .+= Q .- X'
AXB = AX*B
C .-= AXB*(((B'X*B) .+= R)\AXB')
# C .+= X .- X'
C
end
const implicit_ared = ImplicitFunction(forward_ared, conditions_ared)
"""
are(::Discrete, A, B, Q, R::AbstractMatrix{<:Dual}; kwargs)
To make the ARE solver work with dual numbers, make sure that the `R` matrix has the dual number element type.
"""
function are(::DiscreteType, A::AbstractMatrix, B, Q, R::AbstractMatrix{<:Dual}; kwargs...)
pars = ComponentVector(; A,B,Q,R)
X0, _ = implicit_ared(pars)
X = X0 isa AbstractMatrix ? X0 : reshape(X0, size(A))
X
end
"""
lqr(::Discrete, A, B, Q, R::AbstractMatrix{<:Dual})
To make the LQR solver work with dual numbers, make sure that the `R` matrix has the dual number element type.
"""
function lqr(::DiscreteType, A, B, Q, R::AbstractMatrix{<:Dual})
X = are(Discrete, A, B, Q, R)
BX = B'X
(R+BX*B)\(BX*A)
end
"""
kalman(::Discrete, A, C, Q, R::AbstractMatrix{<:Dual})
To make the Kalman solver work with dual numbers, make sure that the `R` matrix has the dual number element type.
"""
function kalman(::DiscreteType, A, C, Q, R::AbstractMatrix{<:Dual})
X = are(Discrete, A', C', Q, R)
CX = C*X
(R+CX*C')\(CX*A')
end
## Lyap
function forward_lyapc(pars)
(; A,Q) = pars
# Q = reshape(Q0, size(A))
ControlSystemsBase.lyapc(A, Q), 0
end
function conditions_lyapc(pars, X, noneed)
(; A,Q) = pars
AX = A*X
O = AX .+ AX' .+ Q
O .+ (X .- X')
end
# linear_solver = (A, b) -> (Matrix(A) \ b, (solved=true,))
const implicit_lyapc = ImplicitFunction(forward_lyapc, conditions_lyapc)
"""
ControlSystemsBase.lyap(nothing::ContinuousType, A::AbstractMatrix, Q::AbstractMatrix{<:Dual}; kwargs)
To make the Lyapunov solver work with dual numbers, make sure that the `Q` matrix has the dual number element type.
The returned gradient may not be symmetric, but the trick `(X + X') ./ 2` results in the correct symmetric gradient.
# Example:
```julia
using ControlSystemsBase, ImplicitDifferentiation, ForwardDiff, FiniteDifferences, ComponentArrays, Test
fdgrad(f, x) = FiniteDifferences.grad(central_fdm(3, 1), f, x) |> first
P = ssrand(1, 1, 2)
function difffun(q)
Q = reshape(q, 2, 2)
sum(ControlSystemsBase.lyap(P, Q))
end
q = [2.0 1; 1 2] |> vec
J1 = ForwardDiff.gradient(difffun, q) # Non-symmetric gradient
J1 = reshape(J1, 2,2)
J1 = vec((J1 + J1') ./ 2) # Symmetric gradient
J2 = fdgrad(difffun, q)
@test J1 ≈ J2 rtol = 1e-6
```
"""
function ControlSystemsBase.lyap(::ContinuousType, A::AbstractMatrix, Q::AbstractMatrix{<:Dual}; kwargs...)
pars = ComponentVector(; A,Q)
X0, _ = implicit_lyapc(pars)
X0 isa AbstractMatrix ? X0 : reshape(X0, size(A))
end
# plyap
function forward_plyapc(pars)
(; A,Q) = pars
Matrix(ControlSystemsBase.plyapc(A, Q)), 0
end
function conditions_plyapc(pars, Xc, noneed)
(; A,Q) = pars
Q = Q*Q'
X = Xc*Xc'
AX = A*X
O = AX .+ AX' .+ Q
O .+ (Xc .- UpperTriangular(Xc))
end
# linear_solver = (A, b) -> (Matrix(A) \ b, (solved=true,))
const implicit_plyapc = ImplicitFunction(forward_plyapc, conditions_plyapc)
function ControlSystemsBase.plyap(::ContinuousType, A::AbstractMatrix, Q::AbstractMatrix{<:Dual}; kwargs...)
pars = ComponentVector(; A,Q)
X0, _ = implicit_plyapc(pars)
X0 isa AbstractMatrix ? X0 : reshape(X0, size(A))
end
## Hinf norm
import ControlSystemsBase: hinfnorm
function forward_hinfnorm(pars; kwargs...)
(; A,B,C,D) = pars
sys = ss(A,B,C,D)
γ, w = hinfnorm(sys; kwargs...)
return [γ], w
end
function conditions_hinfnorm(pars, γ_vec, w; tol=1e-10)
γ = only(γ_vec)
(; A,B,C,D) = pars
sys = ss(A,B,C,D)
[opnorm(freqresp(sys, w)) - γ]
end
const implicit_hinfnorm = ImplicitFunction(forward_hinfnorm, conditions_hinfnorm)
"""
hinfnorm(sys::StateSpace{Continuous, <:Dual}; kwargs)
The H∞ norm can be differentiated through using ForwardDiff.jl, but at the time of writing, is limited to systems with *either* a single input *or* a single output.
A reverse-differentiation rule is defined in RobustAndOptimalControl.jl, which means that hinfnorm is differentiable using, e.g., Zygote in reverse mode.
"""
function hinfnorm(sys::StateSpace{Continuous, <:Dual}; kwargs...)
A,B,C,D = ssdata(sys)
pars = ComponentVector(; A,B,C,D)
γ_vec, w = implicit_hinfnorm(pars)
only(γ_vec), w
end
# ## Schur
function forward_schur(A)
F = schur(A)
ComponentVector(; F.Z, F.T), F
end
function conditions_schur(A, F, s)
(; Z, T) = F
if all(isreal, s.values)
ComponentVector(; Z = Z' * A * Z - T, T = Z' * Z - I + LowerTriangular(T) - Diagonal(T))
else
ComponentVector(; Z = Z' * A * Z - T, T = Z' * Z - I + UpperTriangular(T) - Diagonal(T))
end
end
const implicit_schur = ImplicitFunction(forward_schur, conditions_schur)
# vectors = Z
# Schur = T
# A = F.vectors * F.Schur * F.vectors'
# A = Z * T * Z'
function LinearAlgebra.schur(A::AbstractMatrix{<:Dual})
ZT, F = implicit_schur(A)
n = length(A)
Z = reshape(ZT[1:n], size(A))
T = reshape(ZT[n+1:end], size(A))
Schur(T, Z, F.values)
end
end # module