forked from JuliaManifolds/Manopt.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.jl
More file actions
1477 lines (1295 loc) · 52.2 KB
/
Copy pathdebug.jl
File metadata and controls
1477 lines (1295 loc) · 52.2 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@doc """
DebugAction
A `DebugAction` is a small functor to print/issue debug output. The usual call is given by
`(p::AbstractManoptProblem, s::AbstractManoptSolverState, k) -> s`, where `i` is
the current iterate.
By convention `i=0` is interpreted as "For Initialization only," only debug
info that prints initialization reacts, `i<0` triggers updates of variables
internally but does not trigger any output.
# Fields (assumed by subtypes to exist)
* `print` method to perform the actual print. Can for example be set to a file export,
or to @info. The default is the `print` function on the default `Base.stdout`.
"""
abstract type DebugAction <: AbstractStateAction end
@doc """
DebugSolverState <: AbstractManoptSolverState
The debug state appends debug to any state, they act as a decorator pattern.
Internally a dictionary is kept that stores a [`DebugAction`](@ref) for several occasions
using a `Symbol` as reference.
The original options can still be accessed using the [`get_state`](@ref) function.
# Fields
* `options`: the options that are extended by debug information
* `debugDictionary`: a `Dict{Symbol,DebugAction}` to keep track of Debug for different actions
# Constructors
DebugSolverState(o,dA)
construct debug decorated options, where `dD` can be
* a [`DebugAction`](@ref), then it is stored within the dictionary at `:Iteration`
* an `Array` of [`DebugAction`](@ref)s.
* a `Dict{Symbol,DebugAction}`.
* an Array of Symbols, String and an Int for the [`DebugFactory`](@ref)
"""
mutable struct DebugSolverState{S <: AbstractManoptSolverState} <: AbstractManoptSolverState
state::S
debugDictionary::Dict{Symbol, <:DebugAction}
function DebugSolverState{S}(
st::S, dA::Dict{Symbol, <:DebugAction}
) where {S <: AbstractManoptSolverState}
return new(st, dA)
end
end
function DebugSolverState(st::S, dD::D) where {S <: AbstractManoptSolverState, D <: DebugAction}
return DebugSolverState{S}(st, Dict(:Iteration => dD))
end
function DebugSolverState(
st::S, dD::Array{<:DebugAction, 1}
) where {S <: AbstractManoptSolverState}
return DebugSolverState{S}(st, Dict(:Iteration => DebugGroup(dD)))
end
function DebugSolverState(
st::S, dD::Dict{Symbol, <:DebugAction}
) where {S <: AbstractManoptSolverState}
return DebugSolverState{S}(st, dD)
end
function DebugSolverState(
st::S, format::Array{<:Any, 1}
) where {S <: AbstractManoptSolverState}
return DebugSolverState{S}(st, DebugFactory(format))
end
function DebugSolverState( # a function: callback
st::S,
callback::Function,
) where {S <: AbstractManoptSolverState}
return DebugSolverState{S}(st, DebugFactory([callback]))
end
"""
set_parameter!(ams::DebugSolverState, ::Val{:Debug}, args...)
Set certain values specified by `args...` into the elements of the `debugDictionary`
"""
function set_parameter!(dss::DebugSolverState, ::Val{:Debug}, args...)
for d in values(dss.debugDictionary)
set_parameter!(d, args...)
end
return dss
end
# all other pass through
function set_parameter!(dss::DebugSolverState, v::Val{T}, args...) where {T}
return set_parameter!(dss.state, v, args...)
end
function set_parameter!(dss::DebugSolverState, v::Val{:StoppingCriterion}, args...)
return set_parameter!(dss.state, v, args...)
end
# all other pass through
function get_parameter(dss::DebugSolverState, v::Val{T}, args...) where {T}
return get_parameter(dss.state, v, args...)
end
function status_summary(dst::DebugSolverState)
if length(dst.debugDictionary) > 0
s = ""
for (k, v) in dst.debugDictionary
s = "$s\n :$k = $(status_summary(v))"
end
return "$(dst.state)\n\n## Debug$s"
else # for length 1 the group is equivalent to the summary of the single state
return status_summary(dst.state)
end
end
function show(io::IO, dst::DebugSolverState)
return print(io, status_summary(dst))
end
dispatch_state_decorator(::DebugSolverState) = Val(true)
#
# Meta Debug Actions
#
"""
DebugGroup <: DebugAction
group a set of [`DebugAction`](@ref)s into one action, where the internal prints
are removed by default and the resulting strings are concatenated
# Constructor
DebugGroup(g)
construct a group consisting of an Array of [`DebugAction`](@ref)s `g`,
that are evaluated `en bloque`; the method does not perform any print itself,
but relies on the internal prints. It still concatenates the result and returns
the complete string
"""
mutable struct DebugGroup{D <: DebugAction} <: DebugAction
group::Vector{D}
end
function (d::DebugGroup)(p::AbstractManoptProblem, st::AbstractManoptSolverState, k)
for di in d.group
di(p, st, k)
end
return
end
function status_summary(dg::DebugGroup)
str = join(["$(status_summary(di))" for di in dg.group], ", ")
return "[ $str ]"
end
function show(io::IO, dg::DebugGroup)
s = join(["$(di)" for di in dg.group], ", ")
return print(io, "DebugGroup([$s])")
end
function set_parameter!(dg::DebugGroup, v::Val, args...)
for di in dg.group
set_parameter!(di, v, args...)
end
return dg
end
function set_parameter!(dg::DebugGroup, e::Symbol, args...)
set_parameter!(dg, Val(e), args...)
return dg
end
@doc """
DebugEvery <: DebugAction
evaluate and print debug only every ``k``th iteration. Otherwise no print is performed.
Whether internal variables are updates is determined by `always_update`.
This method does not perform any print itself but relies on it's children's print.
It also sets the sub solvers active parameter, see |`DebugWhenActive`}(#ref).
Here, the `activation_offset` can be used to specify whether it refers to _this_ iteration,
the `i`th, when this call is _before_ the iteration, then the offset should be 0,
for the _next_ iteration, that is if this is called _after_ an iteration, it has to be set to 1.
Since usual debug is happening after the iteration, 1 is the default.
# Constructor
DebugEvery(d::DebugAction, every=1, always_update=true, activation_offset=1)
"""
mutable struct DebugEvery <: DebugAction
debug::DebugAction
every::Int
always_update::Bool
activation_offset::Int
function DebugEvery(
d::DebugAction, every::Int = 1, always_update::Bool = true; activation_offset = 1
)
return new(d, every, always_update, activation_offset)
end
end
function (d::DebugEvery)(p::AbstractManoptProblem, st::AbstractManoptSolverState, k)
if (rem(k, d.every) == 0)
d.debug(p, st, k)
elseif d.always_update
d.debug(p, st, -1)
end
# set activity for this iterate in sub solvers
set_parameter!(
st,
:SubState,
:Debug,
:Activity,
!(k < 1) && (rem(k + d.activation_offset, d.every) == 0),
)
return nothing
end
function show(io::IO, de::DebugEvery)
return print(
io,
"DebugEvery($(de.debug), $(de.every), $(de.always_update); activation_offset=$(de.activation_offset))",
)
end
function status_summary(de::DebugEvery)
s = ""
if de.debug isa DebugGroup
s = status_summary(de.debug)[3:(end - 2)]
else
s = "$(de.debug)"
end
return "[$s, $(de.every)]"
end
function set_parameter!(de::DebugEvery, e::Symbol, args...)
set_parameter!(de, Val(e), args...)
return de
end
function set_parameter!(de::DebugEvery, args...)
set_parameter!(de.debug, args...)
return de
end
#
# Special single ones
#
@doc """
DebugCallback <: DebugAction
Debug for a simple callback function, mainly for compatibility to other solvers and if
a user already has a callback function or functor available
The expected format of the is that it is a function with signature `(problem, state, iteration) -> nothing`
A simple callback of the signature `() -> nothing` can be specified by `simple=true`. In this case the callback is wrapped in a function of the generic form
!!! note
This is for now an internal struct, since its name might still change before
it is made public. The functionality with the factory (`callback=f`) will still work,
but this debug actions name might still change its name in the future.
# Constructor
DebugCallback(callback; simple=false)
"""
struct DebugCallback{CB} <: DebugAction
callback::CB
function DebugCallback(callback; simple::Bool = false)
_cb = simple ? (problem, state, k) -> callback() : callback
return new{typeof(_cb)}(_cb)
end
end
function (d::DebugCallback)(
problem::AbstractManoptProblem, state::AbstractManoptSolverState, k
)
d.callback(problem, state, k)
return nothing
end
function show(io::IO, dc::DebugCallback{CB}) where {CB}
return print(io, "DebugCallback containing a $(CB) callback $(dc.callback)")
end
function status_summary(dc::DebugCallback)
return "$(dc.callback)"
end
@doc """
DebugChange(M=DefaultManifold(); kwargs...)
debug for the amount of change of the iterate (stored in `get_iterate(o)` of the [`AbstractManoptSolverState`](@ref))
during the last iteration. See [`DebugEntryChange`](@ref) for the general case
# Keyword parameters
* `storage=`[`StoreStateAction`](@ref)`( [:Gradient] )` storage of the previous action
* `prefix="Last Change:"`: prefix of the debug output (ignored if you set `format`)
* `io=stdout`: default stream to print the debug to.
$(_kwargs(:inverse_retraction_method))
the inverse retraction
to be used for approximating distance.
"""
mutable struct DebugChange{IR <: AbstractInverseRetractionMethod} <: DebugAction
io::IO
format::String
storage::StoreStateAction
inverse_retraction_method::IR
function DebugChange(
M::AbstractManifold = DefaultManifold();
storage::Union{Nothing, StoreStateAction} = nothing,
io::IO = stdout,
prefix::String = "Last Change: ",
format::String = "$(prefix)%f",
inverse_retraction_method::AbstractInverseRetractionMethod = default_inverse_retraction_method(
M
),
)
irm = inverse_retraction_method
# Deprecated, remove in Manopt 0.5
if isnothing(storage)
if M isa DefaultManifold
storage = StoreStateAction(M; store_fields = [:Iterate])
else
storage = StoreStateAction(M; store_points = Tuple{:Iterate})
end
end
return new{typeof(irm)}(io, format, storage, irm)
end
end
function (d::DebugChange)(mp::AbstractManoptProblem, st::AbstractManoptSolverState, k)
M = get_manifold(mp)
(k > 0) && Printf.format(
d.io,
Printf.Format(d.format),
distance(
M,
get_iterate(st),
get_storage(d.storage, PointStorageKey(:Iterate)),
d.inverse_retraction_method,
),
)
d.storage(mp, st, k)
return nothing
end
function show(io::IO, dc::DebugChange)
return print(
io,
"DebugChange(; format=\"$(escape_string(dc.format))\", inverse_retraction=$(dc.inverse_retraction_method))",
)
end
status_summary(dc::DebugChange) = "(:Change, \"$(escape_string(dc.format))\")"
@doc """
DebugCost <: DebugAction
print the current cost function value, see [`get_cost`](@ref).
# Constructors
DebugCost()
# Parameters
* `format="\$prefix %f"`: format to print the output
* `io=stdout`: default stream to print the debug to.
* `long=false`: short form to set the format to `f(x):` (default) or `current cost: ` and the cost
* `at_init=true`: whether to print also at initialization
"""
mutable struct DebugCost <: DebugAction
io::IO
format::String
at_init::Bool
function DebugCost(;
long::Bool = false, io::IO = stdout, format = long ? "current cost: %f" : "f(x): %f",
at_init::Bool = true,
)
return new(io, format, at_init)
end
end
function (d::DebugCost)(p::AbstractManoptProblem, st::AbstractManoptSolverState, k::Int)
(k >= (d.at_init ? 0 : 1)) && Printf.format(d.io, Printf.Format(d.format), get_cost(p, get_iterate(st)))
return nothing
end
function show(io::IO, di::DebugCost)
return print(io, "DebugCost(; format=\"$(escape_string(di.format))\", at_init=$(di.at_init))")
end
status_summary(di::DebugCost) = "(:Cost, \"$(escape_string(di.format))\")"
@doc """
DebugDivider <: DebugAction
print a small divider (default `" | "`).
# Constructor
DebugDivider(div, io=stdout, at_init=true)
"""
mutable struct DebugDivider{TypeIO <: IO} <: DebugAction
io::TypeIO
divider::String
at_init::Bool
DebugDivider(divider = " | "; io::IO = stdout, at_init::Bool = true) = new{typeof(io)}(io, divider, at_init)
end
function (d::DebugDivider)(::AbstractManoptProblem, ::AbstractManoptSolverState, k::Int)
if k >= (d.at_init ? 0 : 1) && !isempty(d.divider)
print(d.io, d.divider)
end
return nothing
end
function show(io::IO, di::DebugDivider)
return print(io, "DebugDivider(; divider=\"$(escape_string(di.divider))\", at_init=$(di.at_init))")
end
status_summary(di::DebugDivider) = "\"$(escape_string(di.divider))\""
@doc """
DebugEntry <: DebugAction
print a certain fields entry during the iterates, where a `format` can be specified
how to print the entry.
# Additional fields
* `field`: symbol the entry can be accessed with within [`AbstractManoptSolverState`](@ref)
* `at_init`: whether to print also at initialization
# Constructor
DebugEntry(f; prefix="\$f:", format = "\$prefix %s", io=stdout, at_init=true)
"""
mutable struct DebugEntry <: DebugAction
io::IO
format::String
field::Symbol
at_init::Bool
function DebugEntry(f::Symbol; prefix = "$f:", format = "$prefix %s", io::IO = stdout, at_init::Bool = true)
return new(io, format, f, at_init)
end
end
function (d::DebugEntry)(::AbstractManoptProblem, st::AbstractManoptSolverState, k)
(k >= (d.at_init ? 0 : 1)) && Printf.format(d.io, Printf.Format(d.format), getfield(st, d.field))
return nothing
end
function show(io::IO, di::DebugEntry)
return print(io, "DebugEntry(:$(di.field); format=\"$(escape_string(di.format))\", at_init=$(di.at_init))")
end
"""
DebugFeasibility <: DebugAction
Display information about the feasibility of the current iterate
# Fields
* `format`: a vector of symbols and string formatting the output
* `io`: default stream to print the debug to.
* `at_init`: whether to print also at initialization
The following symbols are filled with values
* `:Feasible` display true or false depending on whether the iterate is feasible
* `:FeasibleEq` display `=` or `≠` equality constraints are fulfilled or not
* `:FeasibleIneq` display `≤` or `≰` inequality constraints are fulfilled or not
* `:NumEq` display the number of equality constraints infeasible
* `:NumEqNz` display the number of equality constraints infeasible if exists
* `:NumIneq` display the number of inequality constraints infeasible
* `:NumIneqNz` display the number of inequality constraints infeasible if exists
* `:TotalEq` display the sum of how much the equality constraints are violated
* `:TotalInEq` display the sum of how much the inequality constraints are violated
format to print the output.
# Constructor
DebugFeasibility(
format=["feasible: ", :Feasible];
io::IO=stdout,
at_init::Bool=true,
)
"""
mutable struct DebugFeasibility <: DebugAction
format::Vector{Union{String, Symbol}}
io::IO
at_init::Bool
function DebugFeasibility(format = ["feasible: ", :Feasible]; io::IO = stdout, atol = NaN, at_init::Bool = true)
isnan(atol) || (@warn "Providing atol= directly to DebugFeasibility is deprecated. Use the keyword for the ConstrainedObjective instead. The value provided here ($(atol)) is ignored")
return new(format, io, at_init)
end
end
function (d::DebugFeasibility)(
mp::AbstractManoptProblem, st::AbstractManoptSolverState, k::Int
)
s = ""
cmo = get_objective(mp)
p = get_iterate(st)
eqc = get_equality_constraint(mp, p, :)
eqc_nz = eqc[abs.(eqc) .> cmo.atol]
ineqc = get_inequality_constraint(mp, p, :)
ineqc_pos = ineqc[ineqc .> cmo.atol]
feasible = (length(eqc_nz) == 0) && (length(ineqc_pos) == 0)
n_eq = length(eqc_nz)
n_ineq = length(ineqc_pos)
for f in d.format
(f isa String) && (s *= f)
(f === :Feasible) && (s *= feasible ? "Yes" : "No")
(f === :FeasibleEq) && (s *= n_eq == 0 ? "=" : "≠")
(f === :FeasibleIneq) && (s *= n_ineq == 0 ? "≤" : "≰")
(f === :NumEq) && (s *= "$(n_eq)")
(f === :NumEqNz) && (s *= n_eq == 0 ? "" : "$(n_eq)")
(f === :NumIneq) && (s *= "$(n_ineq)")
(f === :NumIneqNz) && (s *= n_ineq == 0 ? "" : "$(n_ineq)")
(f === :TotalEq) && (s *= "$(sum(abs.(eqc_nz); init = 0.0))")
(f === :TotalInEq) && (s *= "$(sum(ineqc_pos; init = 0.0))")
end
print(d.io, (k >= (d.at_init ? 0 : 1)) ? s : "")
return nothing
end
function show(io::IO, d::DebugFeasibility)
sf = "[" * (join([e isa String ? "\"$e\"" : ":$e" for e in d.format], ", ")) * "]"
return print(io, "DebugFeasibility($sf, at_init=$(d.at_init))")
end
function status_summary(d::DebugFeasibility)
sf = "[" * (join([e isa String ? "\"$e\"" : ":$e" for e in d.format], ", ")) * "]"
return "(:Feasibility, $sf)"
end
@doc """
DebugIfEntry <: DebugAction
Issue a warning, info, or error if a certain field does _not_ pass a the `check`.
The `message` is printed in this case. If it contains a `@printf` argument identifier,
that one is filled with the value of the `field`.
That way you can print the value in this case as well.
# Fields
* `io`: an `IO` stream
* `check`: a function that takes the value of the `field` as input and returns a boolean
* `field`: symbol the entry can be accessed with within [`AbstractManoptSolverState`](@ref)
* `msg`: if the `check` fails, this message is displayed
* `type`: symbol specifying the type of display, possible values `:print`, `: warn`, `:info`, `:error`,
where `:print` prints to `io`.
* `at_init`: whether to print also at initialization
# Constructor
DebugIfEntry(field, check=(>(0)); type=:warn, message=":\$f is nonnegative", io=stdout, at_init=true)
"""
mutable struct DebugIfEntry{F} <: DebugAction
io::IO
check::F
field::Symbol
msg::String
type::Symbol
at_init::Bool
function DebugIfEntry(
f::Symbol, check::F = (>(0)); type = :warn, message = ":\$f nonpositive.", io::IO = stdout, at_init::Bool = true
) where {F}
return new{F}(io, check, f, message, type, at_init)
end
end
function (d::DebugIfEntry)(::AbstractManoptProblem, st::AbstractManoptSolverState, k)
if (k >= (d.at_init ? 0 : 1)) && (!d.check(getfield(st, d.field)))
format = Printf.Format(d.msg)
msg = !('%' ∈ d.msg) ? d.msg : Printf.format(format, getfield(st, d.field))
d.type === :warn && (@warn "$(msg)")
d.type === :info && (@info "$(msg)")
d.type === :error && error(msg)
d.type === :print && print(d.io, msg)
end
return nothing
end
function show(io::IO, di::DebugIfEntry)
return print(io, "DebugIfEntry(:$(di.field), $(di.check); type=:$(di.type), at_init=$(di.at_init))")
end
@doc """
DebugEntryChange{T} <: DebugAction
print a certain entries change during iterates
# Additional fields
* `print`: function to print the result
* `prefix`: prefix to the print out
* `format`: format to print (uses the `prefix` by default and scientific notation)
* `field`: Symbol the field can be accessed with within [`AbstractManoptSolverState`](@ref)
* `distance`: function (p,o,x1,x2) to compute the change/distance between two values of the entry
* `storage`: a [`StoreStateAction`](@ref) to store the previous value of `:f`
# Constructors
DebugEntryChange(f,d)
## Keyword arguments
* `io=stdout`: an `IOStream` used for the debug
* `prefix="Change of \$f"`: the prefix
* `storage=StoreStateAction((f,))`: a [`StoreStateAction`](@ref)
* `initial_value=NaN`: an initial value for the change of `o.field`.
* `format="\$prefix %e"`: format to print the change
"""
mutable struct DebugEntryChange <: DebugAction
distance::Any
field::Symbol
format::String
io::IO
storage::StoreStateAction
function DebugEntryChange(
f::Symbol,
d;
storage::StoreStateAction = StoreStateAction([f]),
prefix::String = "Change of \$f:",
format::String = "$prefix%s",
io::IO = stdout,
initial_value::Any = NaN,
)
if !isa(initial_value, Number) || !isnan(initial_value) #set initial value
update_storage!(storage, Dict(f => initial_value))
end
return new(d, f, format, io, storage)
end
end
function (d::DebugEntryChange)(
p::AbstractManoptProblem, st::AbstractManoptSolverState, k::Int
)
if k == 0
# on init if field not present -> generate
!has_storage(d.storage, d.field) && d.storage(p, st, k)
return nothing
end
x = get_storage(d.storage, d.field)
v = d.distance(p, st, getproperty(st, d.field), x)
(k > 0) && Printf.format(d.io, Printf.Format(d.format), v)
d.storage(p, st, k)
return nothing
end
function show(io::IO, dec::DebugEntryChange)
return print(
io,
"DebugEntryChange(:$(dec.field), $(dec.distance); format=\"$(escape_string(dec.format))\")",
)
end
@doc """
DebugGradientChange()
debug for the amount of change of the gradient (stored in `get_gradient(o)` of the [`AbstractManoptSolverState`](@ref) `o`)
during the last iteration. See [`DebugEntryChange`](@ref) for the general case
# Keyword parameters
* `storage=`[`StoreStateAction`](@ref)`( (:Gradient,) )`: storage of the action for previous data
* `prefix="Last Change:"`: prefix of the debug output (ignored if you set `format`:
* `io=stdout`: default stream to print the debug to.
* `format="\$prefix %f"`: format to print the output
"""
mutable struct DebugGradientChange{VTR <: AbstractVectorTransportMethod} <: DebugAction
io::IO
format::String
storage::StoreStateAction
vector_transport_method::VTR
function DebugGradientChange(
M::AbstractManifold = DefaultManifold();
storage::Union{Nothing, StoreStateAction} = nothing,
io::IO = stdout,
prefix::String = "Last Change: ",
format::String = "$(prefix)%f",
vector_transport_method::VTR = default_vector_transport_method(M),
) where {VTR <: AbstractVectorTransportMethod}
if isnothing(storage)
if M isa DefaultManifold
storage = StoreStateAction(M; store_fields = [:Iterate, :Gradient])
else
storage = StoreStateAction(
M; store_points = [:Iterate], store_vectors = [:Gradient]
)
end
end
return new{VTR}(io, format, storage, vector_transport_method)
end
end
function (d::DebugGradientChange)(
pm::AbstractManoptProblem, st::AbstractManoptSolverState, k
)
if k > 0
M = get_manifold(pm)
p_old = get_storage(d.storage, PointStorageKey(:Iterate))
X_old = get_storage(d.storage, VectorStorageKey(:Gradient))
p = get_iterate(st)
X = get_gradient(st)
l = norm(
M, p, X - vector_transport_to(M, p_old, X_old, p, d.vector_transport_method)
)
Printf.format(d.io, Printf.Format(d.format), l)
end
d.storage(pm, st, k)
return nothing
end
function show(io::IO, dgc::DebugGradientChange)
return print(
io,
"DebugGradientChange(; format=\"$(escape_string(dgc.format))\", vector_transport_method=$(dgc.vector_transport_method))",
)
end
function status_summary(di::DebugGradientChange)
return "(:GradientChange, \"$(escape_string(di.format))\")"
end
@doc """
DebugIterate <: DebugAction
debug for the current iterate (stored in `get_iterate(o)`).
# Constructor
DebugIterate(; kwargs...)
# Keyword arguments
* `io=stdout`: default stream to print the debug to.
* `format="\$prefix %s"`: format how to print the current iterate
* `long=false`: whether to have a long (`"current iterate:"`) or a short (`"p:"`) prefix default
* `prefix`: (see `long` for default) set a prefix to be printed before the iterate
* `at_init=true`: whether to print also at initialization
"""
mutable struct DebugIterate <: DebugAction
io::IO
format::String
at_init::Bool
function DebugIterate(;
io::IO = stdout,
long::Bool = false,
prefix = long ? "current iterate:" : "p:",
format = "$prefix %s",
at_init::Bool = false,
)
return new(io, format, at_init)
end
end
function (d::DebugIterate)(::AbstractManoptProblem, st::AbstractManoptSolverState, k::Int)
(k >= (d.at_init ? 0 : 1)) && Printf.format(d.io, Printf.Format(d.format), get_iterate(st))
return nothing
end
function show(io::IO, di::DebugIterate)
return print(io, "DebugIterate(; format=\"$(escape_string(di.format))\", at_init=$(di.at_init))")
end
status_summary(di::DebugIterate) = "(:Iterate, \"$(escape_string(di.format))\")"
@doc """
DebugIteration <: DebugAction
# Constructor
DebugIteration()
# Keyword parameters
* `format="# %-6d"`: format to print the output
* `io=stdout`: default stream to print the debug to.
debug for the current iteration (prefixed with `#` by )
"""
mutable struct DebugIteration <: DebugAction
io::IO
format::String
DebugIteration(; io::IO = stdout, format = "# %-6d") = new(io, format)
end
function (d::DebugIteration)(::AbstractManoptProblem, ::AbstractManoptSolverState, k::Int)
(k == 0) && print(d.io, "Initial ")
(k > 0) && Printf.format(d.io, Printf.Format(d.format), k)
return nothing
end
function show(io::IO, di::DebugIteration)
return print(io, "DebugIteration(; format=\"$(escape_string(di.format))\")")
end
status_summary(di::DebugIteration) = "(:Iteration, \"$(escape_string(di.format))\")"
@doc """
DebugMessages <: DebugAction
An [`AbstractManoptSolverState`](@ref) or one of its sub steps like a
[`Stepsize`](@ref) might generate warnings throughout their computations.
This debug can be used to `:print` them display them as `:info` or `:warnings` or even `:error`,
depending on the message type.
# Constructor
DebugMessages(mode=:Info, warn=:Once; io::IO=stdout)
Initialize the messages debug to a certain `mode`. Available modes are
* `:Error`: issue the messages as an error and hence stop at any issue occurring
* `:Info`: issue the messages as an `@info`
* `:Print`: print messages to the steam `io`.
* `:Warning`: issue the messages as a warning
The `warn` level can be set to `:Once` to only display only the first message,
to `:Always` to report every message, one can set it to `:No`,
to deactivate this, then this [`DebugAction`](@ref) is inactive.
All other symbols are handled as if they were `:Always:`
"""
mutable struct DebugMessages <: DebugAction
io::IO
mode::Symbol
status::Symbol
function DebugMessages(mode::Symbol = :Info, warn::Symbol = :Once; io::IO = stdout)
return new(io, mode, warn)
end
end
function (d::DebugMessages)(::AbstractManoptProblem, st::AbstractManoptSolverState, k::Int)
if d.status !== :No
msg = get_message(st)
(k < 0 || length(msg) == 0) && (return nothing)
(d.mode == :Warning) && (@warn msg)
(d.mode == :Error) && (@error msg)
(d.mode == :Print) && (print(d.io, msg))
(d.mode == :Info) && (@info msg)
if d.status === :Once
@warn "Further warnings will be suppressed, use DebugMessages(:$(d.mode), :Always) to get all warnings."
d.status = :No
end
end
return nothing
end
show(io::IO, d::DebugMessages) = print(io, "DebugMessages(:$(d.mode), :$(d.status))")
function status_summary(d::DebugMessages)
(d.mode == :Warning) && return "(:WarningMessages, :$(d.status))"
(d.mode == :Error) && return "(:ErrorMessages, :$(d.status))"
# default
# (d.mode == :Info) && return "(:InfoMessages, $(d.status)"
return "(:Messages, :$(d.status))"
end
@doc """
DebugStoppingCriterion <: DebugAction
print the Reason provided by the stopping criterion. Usually this should be
empty, unless the algorithm stops.
# Fields
* `prefix=""`: format to print the output
* `io=stdout`: default stream to print the debug to.
# Constructor
DebugStoppingCriterion(prefix = ""; io::IO=stdout)
"""
mutable struct DebugStoppingCriterion <: DebugAction
io::IO
prefix::String
DebugStoppingCriterion(prefix = ""; io::IO = stdout) = new(io, prefix)
end
function (d::DebugStoppingCriterion)(
::AbstractManoptProblem, st::AbstractManoptSolverState, k::Int
)
print(d.io, (k > 0) ? "$(d.prefix)$(get_reason(st))" : "")
return nothing
end
function show(io::IO, c::DebugStoppingCriterion)
s = length(c.prefix) > 0 ? "\"$(c.prefix)\"" : ""
return print(io, "DebugStoppingCriterion($s)")
end
function status_summary(c::DebugStoppingCriterion)
return length(c.prefix) == 0 ? ":Stop" : "(:Stop, \"$(c.prefix)\")"
end
@doc """
DebugWhenActive <: DebugAction
evaluate and print debug only if the active boolean is set.
This can be set from outside and is for example triggered by [`DebugEvery`](@ref)
on debugs on the subsolver.
This method does not perform any print itself but relies on it's children's prints.
For now, the main interaction is with [`DebugEvery`](@ref) which might activate or
deactivate this debug
# Fields
* `active`: a boolean that can (de-)activated from outside to turn on/off debug
* `always_update`: whether or not to call the order debugs with iteration `<=0` inactive state
# Constructor
DebugWhenActive(d::DebugAction, active=true, always_update=true)
"""
mutable struct DebugWhenActive{D <: DebugAction} <: DebugAction
debug::D
active::Bool
always_update::Bool
function DebugWhenActive(
d::D, active::Bool = true, always_update::Bool = true
) where {D <: DebugAction}
return new{D}(d, active, always_update)
end
end
function (dwa::DebugWhenActive)(p::AbstractManoptProblem, st::AbstractManoptSolverState, k)
return if dwa.active
dwa.debug(p, st, k)
elseif (k < 0) && (dwa.always_update)
dwa.debug(p, st, k)
end
end
function show(io::IO, dwa::DebugWhenActive)
return print(io, "DebugWhenActive($(dwa.debug), $(dwa.active), $(dwa.always_update))")
end
function status_summary(dwa::DebugWhenActive)
return repr(dwa)
end
function set_parameter!(dwa::DebugWhenActive, v::Val, args...)
set_parameter!(dwa.debug, v, args...)
return dwa
end
function set_parameter!(dwa::DebugWhenActive, ::Val{:Activity}, v)
return dwa.active = v
end
@doc """
DebugTime()
Measure time and print the intervals. Using `start=true` you can start the timer on construction,
for example to measure the runtime of an algorithm overall (adding)
The measured time is rounded using the given `time_accuracy` and printed after [canonicalization](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.canonicalize).
# Keyword parameters
* `io=stdout`: default stream to print the debug to.
* `format="\$prefix %s"`: format to print the output, where `%s` is the canonicalized time`.
* `mode=:cumulative`: whether to display the total time or reset on every call using `:iterative`.
* `prefix="Last Change:"`: prefix of the debug output (ignored if you set `format`:
* `start=false`: indicate whether to start the timer on creation or not.
Otherwise it might only be started on first call.
* `time_accuracy=Millisecond(1)`: round the time to this period before printing the canonicalized time
"""
mutable struct DebugTime <: DebugAction
io::IO
format::String
last_time::Nanosecond
time_accuracy::Period
mode::Symbol
function DebugTime(;
start = false,
io::IO = stdout,
prefix::String = "time spent:",
format::String = "$(prefix) %s",
mode::Symbol = :cumulative,
time_accuracy::Period = Millisecond(1),
)
return new(io, format, Nanosecond(start ? time_ns() : 0), time_accuracy, mode)
end
end
function (d::DebugTime)(::AbstractManoptProblem, ::AbstractManoptSolverState, k)
if k == 0 || d.last_time == Nanosecond(0) # init
d.last_time = Nanosecond(time_ns())
elseif k > 0
t = time_ns()
p = Nanosecond(t) - d.last_time
Printf.format(
d.io, Printf.Format(d.format), canonicalize(round(p, d.time_accuracy))
)
if d.mode == :iterative
d.last_time = Nanosecond(time_ns())
end
end
return nothing
end
function show(io::IO, di::DebugTime)
return print(
io, "DebugTime(; format=\"$(escape_string(di.format))\", mode=:$(di.mode))"
)
end
function status_summary(di::DebugTime)
if di.mode === :iterative
return "(:IterativeTime, \"$(escape_string(di.format))\")"
end
return "(:Time, \"$(escape_string(di.format))\")"
end
"""
reset!(d::DebugTime)
reset the internal time of a [`DebugTime`](@ref), that is start from now again.
"""
function reset!(d::DebugTime)
d.last_time = Nanosecond(time_ns())
return d
end
"""
stop!(d::DebugTime)
stop the reset the internal time of a [`DebugTime`](@ref), that is set the time to 0 (undefined)
"""
function stop!(d::DebugTime)
d.last_time = Nanosecond(0)
return d
end
#
# Debugs that warn about something
#
@doc """
DebugWarnIfCostIncreases <: DebugAction
print a warning if the cost increases.
Note that this provides an additional warning for gradient descent
with its default constant step size.
# Constructor
DebugWarnIfCostIncreases(warn=:Once; tol=1e-13)
Initialize the warning to warning level (`:Once`) and introduce a tolerance for the test of `1e-13`.
The `warn` level can be set to `:Once` to only warn the first time the cost increases,