-
Notifications
You must be signed in to change notification settings - Fork 3
/
LinearAlgebraStopping.jl
348 lines (288 loc) · 11.1 KB
/
LinearAlgebraStopping.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
"""
Type: LAStopping
Methods: `start!`, `stop!`, `update_and_start!`, `update_and_stop!`, `fill_in!`, `reinit!`, `status`,
`linear_system_check`, `normal_equation_check`
Specialization of GenericStopping. Stopping structure for linear algebra
solving either
``Ax = b``
or
```math
min\\_{x} \\tfrac{1}{2}\\|Ax - b\\|^2
```
Attributes:
- `pb` : a problem using, for instance, either `LLSModel` (designed for linear least square problem, see https://github.com/JuliaSmoothOptimizers/LLSModels.jl ) or `LinearSystem`.
- `current_state` : The information relative to the problem, see `GenericState`.
- (opt) `meta` : Metadata relative to stopping criteria, see `StoppingMeta`.
- (opt) `main_stp` : Stopping of the main loop in case we consider a Stopping
of a subproblem.
If not a subproblem, then `VoidStopping`.
- (opt) `listofstates` : ListofStates designed to store the history of States.
- (opt) `stopping_user_struct` : Contains a structure designed by the user.
Constructors:
- `LAStopping(pb, meta::AbstractStoppingMeta, stop_remote::AbstractStopRemoteControl, state::AbstractState; main_stp::AbstractStopping=VoidStopping(), list::AbstractListofStates = VoidListofStates(), user_struct::AbstractDict = Dict(), zero_start::Bool = false)`
The default constructor.
- `LAStopping(pb, meta::AbstractStoppingMeta, state::AbstractState; main_stp::AbstractStopping=VoidStopping(), list::AbstractListofStates = VoidListofStates(), user_struct::AbstractDict = Dict(), zero_start::Bool = false, kwargs...)`
The one passing the `kwargs` to the `stop_remote`.
- `LAStopping(pb, state::AbstractState; stop_remote::AbstractStopRemoteControl = StopRemoteControl(), main_stp::AbstractStopping=VoidStopping(), list::AbstractListofStates = VoidListofStates(), user_struct::AbstractDict = Dict(), zero_start::Bool = false, kwargs...)`
The one passing the `kwargs` to the `meta`.
- `LAStopping(:: Union{AbstractLinearOperator, AbstractMatrix}, :: AbstractVector; sparse::Bool = true, n_listofstates::Int = 0, kwargs...)`
The one setting up a default problem (`sparse ? LLSModel(A, b) : LinearSystem(A, b)`), a default `GenericState` using x, and initializing the list of states if `n_listofstates>0`.
- `LAStopping(:: Union{AbstractLinearOperator, AbstractMatrix}, :: AbstractVector, :: AbstractState; sparse::Bool = true, kwargs...)`
The one setting up a default problem (`sparse ? LLSModel(A, b) : LinearSystem(A, b)`).
Notes:
- No specific State targeted
- State don't necessarily keep track of evals
- Evals are checked only for `pb.A` being a LinearOperator
- `zero_start` is true if 0 is the initial guess (not check automatically)
- `LLSModel` counter follow `NLSCounters` (see `init_max_counters_NLS`)
- By default, `meta.max_cntrs` is initialized with an NLSCounters
See also `GenericStopping`, `NLPStopping`, `linear_system_check`, `normal_equation_check`
"""
mutable struct LAStopping{Pb, M, SRC, T, MStp, LoS} <: AbstractStopping{Pb, M, SRC, T, MStp, LoS}
# problem
pb::Pb
# Common parameters
meta::M
stop_remote::SRC
# current state of the problem
current_state::T
# Stopping of the main problem, or nothing
main_stp::MStp
# History of states
listofstates::LoS
# User-specific structure
stopping_user_struct::AbstractDict
#zero is initial point
zero_start::Bool
end
get_pb(stp::LAStopping) = stp.pb
get_meta(stp::LAStopping) = stp.meta
get_remote(stp::LAStopping) = stp.stop_remote
get_state(stp::LAStopping) = stp.current_state
get_main_stp(stp::LAStopping) = stp.main_stp
get_list_of_states(stp::LAStopping) = stp.listofstates
get_user_struct(stp::LAStopping) = stp.stopping_user_struct
function LAStopping(
pb::Pb,
meta::M,
stop_remote::SRC,
current_state::T;
main_stp::AbstractStopping = VoidStopping(),
list::AbstractListofStates = VoidListofStates(),
user_struct::AbstractDict = Dict(),
zero_start::Bool = false,
) where {Pb <: Any, M <: AbstractStoppingMeta, SRC <: AbstractStopRemoteControl, T <: AbstractState}
return LAStopping(pb, meta, stop_remote, current_state, main_stp, list, user_struct, zero_start)
end
function LAStopping(
pb::Pb,
meta::M,
current_state::T;
main_stp::AbstractStopping = VoidStopping(),
list::AbstractListofStates = VoidListofStates(),
user_struct::AbstractDict = Dict(),
zero_start::Bool = false,
kwargs...,
) where {Pb <: Any, M <: AbstractStoppingMeta, T <: AbstractState}
stop_remote = StopRemoteControl(; kwargs...) #main_stp == VoidStopping() ? StopRemoteControl() : cheap_stop_remote_control()
return LAStopping(pb, meta, stop_remote, current_state, main_stp, list, user_struct, zero_start)
end
function LAStopping(
pb::Pb,
current_state::T;
stop_remote::AbstractStopRemoteControl = StopRemoteControl(), #main_stp == VoidStopping() ? StopRemoteControl() : cheap_stop_remote_control()
main_stp::AbstractStopping = VoidStopping(),
list::AbstractListofStates = VoidListofStates(),
user_struct::AbstractDict = Dict(),
zero_start::Bool = false,
kwargs...,
) where {Pb <: Any, T <: AbstractState}
if :max_cntrs in keys(kwargs)
mcntrs = kwargs[:max_cntrs]
elseif Pb <: LLSModel
mcntrs = init_max_counters_NLS()
else
mcntrs = init_max_counters_linear_operators()
end
if :optimality_check in keys(kwargs)
oc = kwargs[:optimality_check]
else
oc = linear_system_check
end
meta = StoppingMeta(; max_cntrs = mcntrs, optimality_check = oc, kwargs...)
return LAStopping(pb, meta, stop_remote, current_state, main_stp, list, user_struct, zero_start)
end
function LAStopping(
A::TA,
b::Tb;
x::Tb = zeros(eltype(Tb), size(A, 2)),
sparse::Bool = true,
n_listofstates::Int = 0,
kwargs...,
) where {TA <: Any, Tb <: AbstractVector}
pb = sparse ? LLSModel(A, b) : LinearSystem(A, b)
state = GenericState(x)
mcntrs = sparse ? init_max_counters_NLS() : init_max_counters_linear_operators()
if n_listofstates > 0 && :list ∉ keys(kwargs)
list = ListofStates(n_listofstates, Val{typeof(state)}())
return LAStopping(pb, state, max_cntrs = mcntrs, list = list; kwargs...)
end
return LAStopping(pb, state, max_cntrs = mcntrs; kwargs...)
end
function LAStopping(
A::TA,
b::Tb,
state::S;
sparse::Bool = true,
kwargs...,
) where {TA <: Any, Tb <: AbstractVector, S <: AbstractState}
pb = sparse ? LLSModel(A, b) : LinearSystem(A, b)
mcntrs = sparse ? init_max_counters_NLS() : init_max_counters_linear_operators()
return LAStopping(pb, state, max_cntrs = mcntrs; kwargs...)
end
"""
Type: LACounters
"""
mutable struct LACounters{T <: Int}
nprod::T
ntprod::T
nctprod::T
sum::T
function LACounters(nprod::T, ntprod::T, nctprod::T, sum::T) where {T <: Int}
return new{T}(nprod, ntprod, nctprod, sum)
end
end
function LACounters(; nprod::Int64 = 0, ntprod::Int64 = 0, nctprod::Int64 = 0, sum::Int64 = 0)
return LACounters(nprod, ntprod, nctprod, sum)
end
"""
init\\_max\\_counters\\_linear\\_operators: counters for LinearOperator
`init_max_counters_linear_operators(; allevals :: T = 20000, nprod = allevals, ntprod = allevals, nctprod = allevals, sum = 11 * allevals)`
"""
function init_max_counters_linear_operators(;
allevals::T = 20000,
nprod::T = allevals,
ntprod::T = allevals,
nctprod::T = allevals,
sum::T = allevals * 11,
) where {T <: Int}
cntrs =
Dict{Symbol, T}([(:nprod, nprod), (:ntprod, ntprod), (:nctprod, nctprod), (:neval_sum, sum)])
return cntrs
end
"""
LinearSystem: Minimal structure to store linear algebra problems
`LinearSystem(:: Union{AbstractLinearOperator, AbstractMatrix}, :: AbstractVector)`
Note:
Another option is to convert the `LinearSystem` as an `LLSModel`.
"""
mutable struct LinearSystem{
TA <: Union{AbstractLinearOperator, AbstractMatrix},
Tb <: AbstractVector,
}
A::TA
b::Tb
counters::LACounters
function LinearSystem(
A::TA,
b::Tb;
counters::LACounters = LACounters(),
kwargs...,
) where {TA <: Union{AbstractLinearOperator, AbstractMatrix}, Tb <: AbstractVector}
return new{TA, Tb}(A, b, counters)
end
end
function LAStopping(
A::TA,
b::Tb;
x::Tb = zeros(eltype(Tb), size(A, 2)),
kwargs...,
) where {TA <: AbstractLinearOperator, Tb <: AbstractVector}
return LAStopping(A, b, GenericState(x), kwargs...)
end
function LAStopping(
A::TA,
b::Tb,
state::AbstractState;
kwargs...,
) where {TA <: AbstractLinearOperator, Tb <: AbstractVector}
return LAStopping(
LinearSystem(A, b),
state,
max_cntrs = init_max_counters_linear_operators(),
kwargs...,
)
end
"""
\\_resources\\_check!: check if the optimization algorithm has
exhausted the resources. This is the Linear Algebra specialized version.
Note:
- function does _not_ keep track of the evals in the state
- check `:nprod`, `:ntprod`, and `:nctprod` in the `LinearOperator` entries
"""
function _resources_check!(stp::LAStopping, x::T) where {T}
#GenericState has no field evals.
#_smart_update!(stp.current_state, evals = cntrs)
# check all the entries in the counter
# global user limit diagnostic
stp.meta.resources = _counters_loop!(stp.pb.counters, stp.meta.max_cntrs)
return stp.meta.resources
end
function _counters_loop!(cntrs::LACounters{T}, max_cntrs::Dict{Symbol, T}) where {T}
sum, max_f = 0, false
for f in (:nprod, :ntprod, :nctprod)
ff = getfield(cntrs, f)
max_f = max_f || (ff > max_cntrs[f])
sum += ff
end
return max_f || (sum > max_cntrs[:neval_sum])
end
function _counters_loop!(cntrs::NLSCounters, max_cntrs::Dict{Symbol, T}) where {T}
sum, max_f = 0, false
for f in intersect(fieldnames(NLSCounters), keys(max_cntrs))
max_f = f != :counters ? (max_f || (getfield(cntrs, f) > max_cntrs[f])) : max_f
end
for f in intersect(fieldnames(Counters), keys(max_cntrs))
max_f = max_f || (getfield(cntrs.counters, f) > max_cntrs[f])
end
return max_f || (sum > max_cntrs[:neval_sum])
end
"""
linear\\_system\\_check: return ||Ax-b||_p
`linear_system_check(:: Union{LinearSystem, LLSModel}, :: AbstractState; pnorm :: Real = Inf, kwargs...)`
Note:
- Returns the p-norm of state.res
- state.res is filled in if nothing.
"""
function linear_system_check(pb::LinearSystem, state::AbstractState; pnorm::Real = Inf, kwargs...)
pb.counters.nprod += 1
if length(state.res) == 0
update!(state, res = pb.A * state.x - pb.b)
end
return norm(state.res, pnorm)
end
function linear_system_check(pb::LLSModel, state::AbstractState; pnorm::Real = Inf, kwargs...)
if length(state.res) == 0
Axmb = if xtype(state) <: SparseVector
sparse(residual(pb, state.x))
else
residual(pb, state.x)
end
update!(state, res = Axmb)
end
return norm(state.res, pnorm)
end
"""
normal\\_equation\\_check: return ||A'Ax-A'b||_p
`normal_equation_check(:: Union{LinearSystem, LLSModel}, :: AbstractState; pnorm :: Real = Inf, kwargs...)`
Note: pb must have A and b entries
"""
function normal_equation_check(pb::LinearSystem, state::AbstractState; pnorm::Real = Inf, kwargs...)
pb.counters.nprod += 1
pb.counters.ntprod += 1
return norm(pb.A' * (pb.A * state.x) - pb.A' * pb.b, pnorm)
end
function normal_equation_check(pb::LLSModel, state::AbstractState; pnorm::Real = Inf, kwargs...)
nres = jtprod_residual(pb, state.x, residual(pb, state.x))
return norm(nres, pnorm)
end