-
Notifications
You must be signed in to change notification settings - Fork 28
/
interface.jl
235 lines (211 loc) · 5.54 KB
/
interface.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
"""
Force (re-)evaluation of the objective value at `x`.
Returns `f(x)` and stores the value in `obj.F`
"""
function value!!(obj::AbstractObjective, x)
obj.f_calls .+= 1
copyto!(obj.x_f, x)
obj.F = obj.f(x)
value(obj)
end
"""
Evaluates the objective value at `x`.
Returns `f(x)`, but does *not* store the value in `obj.F`
"""
function value(obj::AbstractObjective, x)
obj.f_calls .+= 1
return obj.f(x)
end
"""
Evaluates the objective value at `x`.
Returns `f(x)` and stores the value in `obj.F`
"""
function value!(obj::AbstractObjective, x)
if x != obj.x_f
value!!(obj, x)
end
value(obj)
end
"""
Evaluates the gradient value at `x`
This does *not* update `obj.DF` or `obj.x_df`.
"""
function gradient(obj::AbstractObjective, x)
newdf = copy(obj.DF)
obj.df(newdf, x)
obj.df_calls .+= 1
return newdf
end
"""
Evaluates the gradient value at `x`.
Stores the value in `obj.DF`.
"""
function gradient!(obj::AbstractObjective, x)
if x != obj.x_df
gradient!!(obj, x)
end
gradient(obj)
end
"""
Force (re-)evaluation of the gradient value at `x`.
Stores the value in `obj.DF`.
"""
function gradient!!(obj::AbstractObjective, x)
obj.df_calls .+= 1
copyto!(obj.x_df, x)
obj.df(obj.DF, x)
gradient(obj)
end
function value_gradient!(obj::AbstractObjective, x)
if x != obj.x_f && x != obj.x_df
value_gradient!!(obj, x)
elseif x != obj.x_f
value!!(obj, x)
elseif x != obj.x_df
gradient!!(obj, x)
end
value(obj), gradient(obj)
end
function value_gradient!!(obj::AbstractObjective, x)
obj.f_calls .+= 1
obj.df_calls .+= 1
copyto!(obj.x_f, x)
copyto!(obj.x_df, x)
obj.F = obj.fdf(gradient(obj), x)
value(obj), gradient(obj)
end
function hessian!(obj::AbstractObjective, x)
if x != obj.x_h
hessian!!(obj, x)
end
hessian(obj)
end
function hessian!!(obj::AbstractObjective, x)
obj.h_calls .+= 1
copyto!(obj.x_h, x)
obj.h(obj.H, x)
hessian(obj)
end
# Getters are without ! and accept only an objective and index or just an objective
"Get the most recently evaluated objective value of `obj`."
value(obj::AbstractObjective) = obj.F
"Get the most recently evaluated gradient of `obj`."
gradient(obj::AbstractObjective) = obj.DF
"Get the most recently evaluated Jacobian of `obj`."
jacobian(obj::AbstractObjective) = obj.DF
"Get the `i`th element of the most recently evaluated gradient of `obj`."
gradient(obj::AbstractObjective, i::Integer) = obj.DF[i]
"Get the most recently evaluated Hessian of `obj`"
hessian(obj::AbstractObjective) = obj.H
value_jacobian!(obj, x) = value_jacobian!(obj, obj.F, obj.DF, x)
function value_jacobian!(obj, F, J, x)
if x != obj.x_f && x != obj.x_df
value_jacobian!!(obj, F, J, x)
elseif x != obj.x_f
value!!(obj, F, x)
elseif x != obj.x_df
jacobian!!(obj, J, x)
end
F, J
end
value_jacobian!!(obj, x) = value_jacobian!!(obj, obj.F, obj.DF, x)
function value_jacobian!!(obj, F, J, x)
obj.fdf(F, J, x)
copyto!(obj.x_f, x)
copyto!(obj.x_df, x)
obj.f_calls .+= 1
obj.df_calls .+= 1
obj.df_calls
F, J
end
function jacobian!(obj, x)
if x != obj.x_df
jacobian!!(obj, x)
end
jacobian(obj)
end
jacobian!!(obj, x) = jacobian!!(obj, obj.DF, x)
function jacobian!!(obj, J, x)
obj.df(J, x)
copyto!(obj.x_df, x)
obj.df_calls .+= 1
obj.df_calls
J
end
function jacobian(obj::AbstractObjective, x)
tmp = copy(obj.DF)
jacobian!!(obj, x)
newdf = copy(obj.DF)
copyto!(obj.DF, tmp)
return newdf
end
value(obj::NonDifferentiable{TF, TX}, x) where {TF<:AbstractArray, TX} = value(obj, copy(obj.F), x)
value(obj::OnceDifferentiable{TF, TDF, TX}, x) where {TF<:AbstractArray, TDF, TX} = value(obj, copy(obj.F), x)
function value(obj::AbstractObjective, F, x)
obj.f_calls .+= 1
return obj.f(F, x)
end
value!!(obj::NonDifferentiable{TF, TX}, x) where {TF<:AbstractArray, TX} = value!!(obj, obj.F, x)
value!!(obj::OnceDifferentiable{TF, TDF, TX}, x) where {TF<:AbstractArray, TDF, TX} = value!!(obj, obj.F, x)
function value!!(obj::AbstractObjective, F, x)
obj.f(F, x)
copyto!(obj.x_f, x)
obj.f_calls .+= 1
obj.f_calls
F
end
function _clear_f!(d::NLSolversBase.AbstractObjective)
d.f_calls .= 0
if typeof(d.F) <: AbstractArray
d.F .= eltype(d.F)(NaN)
else
d.F = typeof(d.F)(NaN)
end
d.x_f .= eltype(d.x_f)(NaN)
nothing
end
function _clear_df!(d::NLSolversBase.AbstractObjective)
d.df_calls .= 0
d.DF .= eltype(d.DF)(NaN)
d.x_df .= eltype(d.x_df)(NaN)
nothing
end
function _clear_h!(d::NLSolversBase.AbstractObjective)
d.h_calls .= 0
d.H .= eltype(d.H)(NaN)
d.x_h .= eltype(d.x_h)(NaN)
nothing
end
function _clear_hv!(d::NLSolversBase.AbstractObjective)
d.hv_calls .= 0
d.Hv .= eltype(d.Hv)(NaN)
d.x_hv .= eltype(d.x_hv)(NaN)
d.v_hv .= eltype(d.v_hv)(NaN)
nothing
end
clear!(d::NonDifferentiable) = _clear_f!(d)
function clear!(d::OnceDifferentiable)
_clear_f!(d)
_clear_df!(d)
nothing
end
function clear!(d::TwiceDifferentiable)
_clear_f!(d)
_clear_df!(d)
_clear_h!(d)
nothing
end
function clear!(d::TwiceDifferentiableHV)
_clear_f!(d)
_clear_df!(d)
_clear_hv!(d)
nothing
end
g_calls(d::NonDifferentiable) = 0
h_calls(d::Union{NonDifferentiable, OnceDifferentiable}) = 0
f_calls(d) = first(d.f_calls)
g_calls(d) = first(d.df_calls)
h_calls(d) = first(d.h_calls)
hv_calls(d) = 0
h_calls(d::TwiceDifferentiableHV) = 0
hv_calls(d::TwiceDifferentiableHV) = first(d.hv_calls)