-
Notifications
You must be signed in to change notification settings - Fork 1
/
NonsmoothFwdAD.jl
377 lines (311 loc) · 11 KB
/
NonsmoothFwdAD.jl
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#=
module NonsmoothFwdAD
=====================
A quick implementation of:
- the vector forward mode of automatic differentiation (AD) for generalized
derivative evaluation, developed in the article:
KA Khan and PI Barton (2015), https://doi.org/10.1080/10556788.2015.1025400
- the "compass difference" rule for generalized derivatives of scalar-valued bivariate
functions, developed in the article:
KA Khan and Y Yuan (2020), https://doi.org/10.46298/jnsao-2020-6061
This implementation applies generalized differentiation rules via operator overloading,
and is modeled on the standard "smooth" vector forward AD mode implementation described
in Chapter 6 of "Evaluating Derivatives (2nd ed.)" by Griewank and Walther (2008).
The "AFloat" struct here is analogous to Griewank and Walther's "adouble" class.
The following operators have been overloaded:
x+y, -x, x-y, x*y, inv(x), x/y, x^p, exp, log, sin, cos, abs, max, min, hypot
(where "p" is a fixed integer)
Additional operators may be overloaded in the same way; a new unary operation would be
overloaded just like "log" or "abs", and a new binary operation would be overloaded
just like "x*y" or "hypot".
Written by Kamil Khan on February 5, 2022
Edited by Maha Chaudhry in April 2022
=#
module NonsmoothFwdAD
using LinearAlgebra
export AFloat
export eval_ld_derivative,
eval_dir_derivative,
eval_gen_derivative,
eval_gen_gradient,
eval_compass_difference
# default tolerances; feel free to change.
const DEFAULT_ZTOL = 1e-8 # used to decide if we're at the kink of "abs" or "hypot"
struct AFloat
val::Float64
dot::Vector{Float64}
ztol::Float64 # used in "abs" and "hypot" to decide if quantities are 0
end
# outer constructors for when this.dot and/or this.ztol aren't provided explicitly
AFloat(val::Float64, dot::Vector{Float64}) = AFloat(val, dot, DEFAULT_ZTOL)
AFloat(val::Float64, n::Int, ztol::Float64 = DEFAULT_ZTOL) = AFloat(val, zeros(n), ztol)
# display e.g. with "println"
Base.show(io::IO, x::AFloat) = print(io, "(", x.val, "; ", x.dot, ")")
# define promotion from Float64 to AFloat.
# Base.convert can't be used, because it doesn't know the intended dimension
# of the new "dot" vector.
Base.promote(uA::AFloat, uB::Float64) = (uA, AFloat(uB, length(uA.dot), uA.ztol))
Base.promote(uA::Float64, uB::AFloat) = reverse(promote(uB, uA))
Base.promote(uA::AFloat, uB::AFloat) = (uA, uB)
# for u::AFloat, define "u[1]" to mean "u", and set length(u)=1.
# Helps handle vector/scalar outputs.
Base.getindex(u::AFloat, i::Int) = (i == 1) ? u : throw(DomainError(:i, "must be 1"))
Base.length(u::AFloat) = 1
## define high-level generalized differentiation operations, given a mathematical
## function f composed from supported elemental operations, and written as though
## its input is a Vector
# compute:
# y = the function value f(x), as a Vector, and
# yDot = the LD-derivative f'(x; xDot)
# yDot is the same type as xDot, which is either a Matrix or a Vector{Vector}
function eval_ld_derivative(
f::Function,
x::Vector{Float64},
xDot::Matrix{Float64};
kwargs...
)
# use operator overloading to compute f(x) and f'(x; xDot)
yLD = eval_AFloat_vec_output(f, x, xDot; kwargs...)
# recover outputs from AFloats
y = [vLD.val for vLD in yLD]
yDot = reduce(vcat, [vLD.dot for vLD in yLD]')
return y, yDot
end
function eval_ld_derivative(
f::Function,
x::Vector{Float64},
xDot::Vector{Vector{Float64}};
kwargs...
)
# use operator overloading to compute f(x) and f'(x; xDot)
yLD = eval_AFloat_vec_output(f, x, xDot; kwargs...)
# recover outputs from AFloats
y = [vLD.val for vLD in yLD]
yDot = [vLD.dot for vLD in yLD]
return y, yDot
end
function eval_AFloat_vec_output(
f::Function,
x::Vector{Float64},
xDot::Matrix{Float64};
ztol::Float64 = DEFAULT_ZTOL
)
# express inputs as AFloats
xLD = [AFloat(v, Vector(vDot), ztol) for (v, vDot) in zip(x, eachrow(xDot))]
# use operator overloading to compute f(x) and f'(x; xDot)
yLD = f(xLD)
if !(yLD isa Vector)
yLD = [yLD]
end
return yLD
end
function eval_AFloat_vec_output(
f::Function,
x::Vector{Float64},
xDot::Vector{Vector{Float64}};
ztol::Float64 = DEFAULT_ZTOL
)
# express inputs as AFloats
xLD = [AFloat(v, vDot, ztol) for (v, vDot) in zip(x, xDot)]
# use operator overloading to compute f(x) and f'(x; xDot)
yLD = f(xLD)
if !(yLD isa Vector)
yLD = [yLD]
end
return yLD
end
# compute:
# y = the function value f(x), as a vector, and
# yDot = the directional derivative f'(x; xDot::Vector), as a vector
function eval_dir_derivative(
f::Function,
x::Vector{Float64},
xDot::Vector{Float64};
kwargs...
)
xDotMatrix = reshape(xDot, :, 1)
(y, yDotMatrix) = eval_ld_derivative(f, x, xDotMatrix; kwargs...)
yDot = vec(yDotMatrix)
return y, yDot
end
# compute:
# y = the function value f(x), and
# yDeriv = the lexicographic derivative D_L f(x; xDot)
# (xDot defaults to I if not provided, skipping the final linear solve.)
function eval_gen_derivative(
f::Function,
x::Vector{Float64},
xDot::Matrix{Float64};
kwargs...
)
(y, yDot) = eval_ld_derivative(f, x, xDot; kwargs...)
yDeriv = yDot / xDot
return y, yDeriv
end
function eval_gen_derivative(
f::Function,
x::Vector{Float64};
kwargs...
)
(y, yDot) = eval_ld_derivative(f, x, Matrix{Float64}(I(length(x))); kwargs...)
return y, yDot
end
# for scalar-valued f (ordinarily returning a Float64), compute:
# y = the function value f(x), and
# yGrad = the lexicographic gradient grad_L f(x; xDot),
# which is a vector (and the transpose of the lex. derivative)
# (xDot defaults to I if not provided, skipping the final linear solve.)
function eval_gen_gradient(
f::Function,
x::Vector{Float64},
xDot::Matrix{Float64};
kwargs...
)
# use operator overloading to compute f(x) and f'(x; xDot)
yLD = eval_AFloat_vec_output(f, x, xDot; kwargs...)
# compute generalized gradient element
return yLD[1].val, (yLD[1].dot'/ xDot)'
end
function eval_gen_gradient(
f::Function,
x::Vector{Float64};
kwargs...
)
# use operator overloading to compute f(x) and f'(x; xDot)
yLD = eval_AFloat_vec_output(f, x, Matrix{Float64}(I(length(x))); kwargs...)
return yLD[1].val, yLD[1].dot
end
# for a scalar-valued function f, compute:
# y = the function value f(x), and
# yCompass = the compass difference of f at x.
# x must be either a scalar or vector, and yCompass is the same type as x.
function eval_compass_difference(
f::Function,
x::Vector{Float64};
kwargs...
)
y = f(x)
(length(y) == 1) ||
throw(DomainError(:f, "must be scalar-valued"))
fVec(u) = [f(u)[1]] # account for f returning either a Float64 or Vector{Float64}
yCompass = copy(x)
coordVec = zeros(length(x))
for i in eachindex(x)
coordVec[i] = 1.0
_, yDotPlus = eval_dir_derivative(fVec, x, coordVec; kwargs...)
_, yDotMinus = eval_dir_derivative(fVec, x, -coordVec; kwargs...)
yCompass[i] = 0.5*(yDotPlus[1] - yDotMinus[1])
coordVec[i] = 0.0
end
return y, yCompass
end
function eval_compass_difference(
f::Function,
x::Float64;
kwargs...
)
(y, yCompassVec) = eval_compass_difference(f, [x]; kwargs...)
return y, yCompassVec[1]
end
## define generalized differentiation rules for the simplest elemental operations
# macro to define (::AFloat, ::Float64) and (::Float64, ::AFloat) variants
# of a defined bivariate operation(::AFloat, ::AFloat).
macro define_mixed_input_variants(op)
return quote
Base.$op(uA::AFloat, uB::Float64) = $op(promote(uA, uB)...)
Base.$op(uA::Float64, uB::AFloat) = $op(promote(uA, uB)...)
end
end
# addition
function Base.:+(uA::AFloat, uB::AFloat)
vVal = uA.val + uB.val
vDot = uA.dot + uB.dot
return AFloat(vVal, vDot, uA.ztol)
end
@define_mixed_input_variants +
# negative and subtraction
Base.:-(u::AFloat) = AFloat(-u.val, -u.dot, u.ztol)
Base.:-(uA::AFloat, uB::AFloat) = uA + (-uB)
@define_mixed_input_variants -
# multiplication
function Base.:*(uA::AFloat, uB::AFloat)
vVal = uA.val * uB.val
vDot = (uB.val * uA.dot) + (uA.val * uB.dot)
return AFloat(vVal, vDot, uA.ztol)
end
@define_mixed_input_variants *
# reciprocal and division
function Base.inv(u::AFloat)
vVal = inv(u.val)
vDot = -u.dot / ((u.val)^2)
return AFloat(vVal, vDot, u.ztol)
end
Base.:/(uA::AFloat, uB::AFloat) = uA * inv(uB)
@define_mixed_input_variants /
# integer powers
function Base.:^(u::AFloat, p::Int)
if p == 0
return 0.0*u + 1.0
else
vVal = (u.val)^p
vDot = p * (u.val)^(p-1) * u.dot
return AFloat(vVal, vDot, u.ztol)
end
end
# exponential, logarithm, sine, and cosine
Base.exp(u::AFloat) = AFloat(exp(u.val), exp(u.val) * u.dot, u.ztol)
Base.log(u::AFloat) = AFloat(log(u.val), u.dot / u.val, u.ztol)
Base.sin(u::AFloat) = AFloat(sin(u.val), cos(u.val) * u.dot, u.ztol)
Base.cos(u::AFloat) = AFloat(cos(u.val), -sin(u.val) * u.dot, u.ztol)
# absolute value.
# When evaluating zDot, if any quantity "q" has abs(q) < u.ztol,
# then "q" is considered to be 0.
function Base.abs(u::AFloat)
uVec = [u.val; u.dot]
k = findfirst(!isapprox(0.0, atol=u.ztol), uVec)
s = isnothing(k) ? 0.0 : sign(uVec[k])
return AFloat(abs(u.val), s * u.dot, u.ztol)
end
# bivariate max and min.
function Base.max(uA::AFloat, uB::AFloat)
diffVec = [uA.val - uB.val; uA.dot - uB.dot]
k = findfirst(!isapprox(0.0, atol=uA.ztol), diffVec)
vVal = max(uA.val, uB.val)
vDot = isnothing(k) ? zeros(2) :
(diffVec[k] > 0.0 ? uA.dot : uB.dot)
return AFloat(vVal, vDot, uA.ztol)
end
@define_mixed_input_variants max
function Base.min(uA::AFloat, uB::AFloat)
diffVec = [uA.val - uB.val; uA.dot - uB.dot]
k = findfirst(!isapprox(0.0, atol=uA.ztol), diffVec)
vVal = min(uA.val, uB.val)
vDot = isnothing(k) ? zeros(2) :
(diffVec[k] < 0.0 ? uA.dot : uB.dot)
return AFloat(vVal, vDot, uA.ztol)
end
@define_mixed_input_variants min
# sqrt(uA^2 + uB^2); this is LinearAlgebra.hypot in Julia.
# uA.ztol is used as in abs(::AFloat)
function Base.hypot(uA::AFloat, uB::AFloat)
uVecA = [uA.val; uA.dot]
uVecB = [uB.val; uB.dot]
vVec = abs.(uVecA) .+ abs.(uVecB)
k = findfirst(!isapprox(0.0, atol=uA.ztol), vVec)
s = isnothing(k) ? zeros(2) : normalize([uVecA[k], uVecB[k]])
vVal = hypot(uA.val, uB.val)
vDot = s[1] * uA.dot + s[2] * uB.dot
return AFloat(vVal, vDot, uA.ztol)
end
@define_mixed_input_variants hypot
## overload comparisons to permit simple if-statements, but these conditions' values
## should not change under small perturbations in inputs.
Base.:<(uA::AFloat, uB::AFloat) = (uA.val < uB.val)
@define_mixed_input_variants <
Base.:>(uA::AFloat, uB::AFloat) = (uA.val > uB.val)
@define_mixed_input_variants >
Base.:>=(uA::AFloat, uB::AFloat) = (uA.val >= uB.val)
@define_mixed_input_variants >=
Base.:<=(uA::AFloat, uB::AFloat) = (uA.val <= uB.val)
@define_mixed_input_variants <=
end # module