Skip to content

Commit 13ff14a

Browse files
committed
Unify REPL print and add a test.
1 parent 705ce1e commit 13ff14a

2 files changed

Lines changed: 22 additions & 22 deletions

File tree

src/plans/stopping_criterion.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -480,22 +480,22 @@ A stopping criterion to stop when
480480
based on Eq. (1) in [ZhuByrdLuNocedal:1997](@cite)
481481
482482
# Fields
483-
* tolerance: the threshold `tol` in the above formula.
483+
* _`threshold`: the threshold `tol` in the above formula.
484484
$(_fields([:at_iteration, :last_change]))
485-
* `last_cost``: the last cost value
485+
* `last_cost`: the last cost value
486486
487487
# Constructor
488488
489-
StopWhenRelativeAPosterioriCostChangeLessOrEqual(tolerance::F)
489+
StopWhenRelativeAPosterioriCostChangeLessOrEqual(threshold::F)
490490
491-
Initialize the stopping criterion to a threshold `tolerance` for the change of the cost function.
491+
Initialize the stopping criterion to a `threshold` for the change of the cost function.
492492
493493
StopWhenRelativeAPosterioriCostChangeLessOrEqual(; factr::Real=1.0e7)
494494
495-
Initialize tolerance to `factr * eps(factr)`, following the convention in [ZhuByrdLuNocedal:1997](@cite).
495+
Initialize threshold to `factr * eps(factr)`, following the convention in [ZhuByrdLuNocedal:1997](@cite).
496496
"""
497497
mutable struct StopWhenRelativeAPosterioriCostChangeLessOrEqual{F <: Real} <: StoppingCriterion
498-
tolerance::F
498+
threshold::F
499499
at_iteration::Int
500500
last_cost::F
501501
last_change::F
@@ -510,12 +510,12 @@ function (c::StopWhenRelativeAPosterioriCostChangeLessOrEqual)(
510510
if iteration <= 0 # reset on init
511511
c.at_iteration = -1
512512
c.last_cost = Inf
513-
c.last_change = 2 * c.tolerance
513+
c.last_change = 2 * c.threshold
514514
end
515515
current_cost = get_cost(problem, state)
516516
c.last_change = (c.last_cost - current_cost) / max(abs(c.last_cost), abs(current_cost), 1)
517517
c.last_cost = current_cost
518-
if iteration > 1 && c.last_change <= c.tolerance
518+
if iteration > 1 && c.last_change <= c.threshold
519519
c.at_iteration = iteration
520520
return true
521521
end
@@ -524,20 +524,18 @@ end
524524
indicates_convergence(c::StopWhenRelativeAPosterioriCostChangeLessOrEqual) = false
525525
function get_reason(c::StopWhenRelativeAPosterioriCostChangeLessOrEqual)
526526
if c.at_iteration >= 0
527-
return "At iteration $(c.at_iteration) the algorithm performed a step with a relative a posteriori cost change ($(abs(c.last_change))) less than or equal to $(c.tolerance)."
527+
return "At iteration $(c.at_iteration) the algorithm performed a step with a relative a posteriori cost change ($(abs(c.last_change))) less than or equal to $(c.threshold)."
528528
end
529529
return ""
530530
end
531-
function status_summary(c::StopWhenRelativeAPosterioriCostChangeLessOrEqual)
531+
function status_summary(c::StopWhenRelativeAPosterioriCostChangeLessOrEqual; context::Symbol = short)
532+
(context == :short) && return repr(c)
532533
has_stopped = (c.at_iteration >= 0)
533534
s = has_stopped ? "reached" : "not reached"
534-
return "(fₖ- fₖ₊₁)/max(|fₖ|, |fₖ₊₁|, 1) = $(abs(c.last_change))$(c.tolerance):\t$s"
535+
return (_is_inline(context) ? "(fₖ- fₖ₊₁)/max(|fₖ|, |fₖ₊₁|, 1) = $(abs(c.last_change))$(c.threshold):$(_MANOPT_INDENT)" : "A stopping criterion to stop when the relative posteriori cost change is less than $(c.threshold)\n$(_MANOPT_INDENT)") * "$s"
535536
end
536-
function Base.show(io::IO, ::MIME"text/plain", c::StopWhenRelativeAPosterioriCostChangeLessOrEqual)
537-
return print(
538-
io,
539-
"StopWhenRelativeAPosterioriCostChangeLessOrEqual with threshold $(c.tolerance).\n $(status_summary(c))",
540-
)
537+
function Base.show(io::IO, c::StopWhenRelativeAPosterioriCostChangeLessOrEqual)
538+
return print(io, "StopWhenRelativeAPosterioriCostChangeLessOrEqual($(c.threshold))")
541539
end
542540

543541
@doc """
@@ -911,10 +909,8 @@ function status_summary(c::StopWhenProjectedNegativeGradientNormLess; context::S
911909
return "A StoppingCriterion to stop when the negative projected gradient norm is less than a threshold of $(c.threshold):\n$(_MANOPT_INDENT)$s"
912910
end
913911
indicates_convergence(c::StopWhenProjectedNegativeGradientNormLess) = true
914-
function show(io::IO, c::StopWhenProjectedNegativeGradientNormLess)
915-
print(io, "StopWhenProjectedNegativeGradientNormLess(", c.threshold, "; norm = ", c.norm)
916-
!ismissing(c.outer_norm) && print(io, ", outer_norm = ", c.outer_norm)
917-
return print(io, ")")
912+
function Base.show(io::IO, c::StopWhenProjectedNegativeGradientNormLess)
913+
return print(io, "StopWhenProjectedNegativeGradientNormLess($(c.threshold); norm = $(c.norm))")
918914
end
919915

920916
"""

test/solvers/test_quasi_Newton_box.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ using RecursiveArrayTools
2323
@test Manopt.get_stepsize_bound(M, p, d, 2) Inf
2424
end
2525

26-
2726
@testset "update_fp_fpp - basic d = -g" begin
2827
M = Hyperrectangle([0.0, 1.0], [3.0, 3.0])
2928

@@ -57,7 +56,6 @@ using RecursiveArrayTools
5756
@test hv_eb_d == original_hv_eb_d
5857
end
5958

60-
6159
@testset "update_fp_fpp - basic d = [-2.0, -1.0]" begin
6260
M = Hyperrectangle([0.0, 1.0], [3.0, 3.0])
6361

@@ -317,6 +315,12 @@ using RecursiveArrayTools
317315
p_opt = quasi_Newton(M, f, grad_f, p0; stopping_criterion = StopWhenProjectedNegativeGradientNormLess(1.0e-6) | StopAfterIteration(100))
318316
@test distance(M, p_opt, ArrayPartition(px, [0 2; 0 0])) < 0.1
319317
end
318+
319+
@testset "Specialised Stopping criteria" begin
320+
sc = StopWhenProjectedNegativeGradientNormLess(1.0e-9)
321+
@test startswith(repr(sc), "StopWhenProjectedNegativeGradientNormLess(1.0e-9")
322+
@test startswith(Manopt.status_summary(sc), "A StoppingCriterion to stop when the negative projected gradient norm is less than")
323+
end
320324
end
321325

322326
@testset "MaxStepsizeInDirection" begin

0 commit comments

Comments
 (0)