Skip to content

Commit d5dbbbb

Browse files
committed
Extend code coverage also to reducing threshold and radius, fix a nasty bug where I accidentially overwrote the iterate.
1 parent 167260e commit d5dbbbb

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/solvers/gradient_sampling.jl

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mutable struct GradientSamplingState{
8686
Pr <: Union{F, AbstractManoptProblem} where {F}, St <: AbstractManoptSolverState,
8787
SP <: AbstractVector{<:P}, ST <: AbstractVector{<:T}, A <: AbstractVector{<:R},
8888
SC <: StoppingCriterion, S <: Stepsize, RTM <: AbstractRetractionMethod, VTM <: AbstractVectorTransportMethod,
89-
} <: AbstractManoptSolverState
89+
} <: AbstractGradientSolverState
9090
convex_hull_coeffs::A
9191
p::P
9292
sampled_points::SP
@@ -177,7 +177,6 @@ function GradientSamplingState(
177177
vector_transport_method = vector_transport_method, X = X, Y = copy(M, p, X),
178178
)
179179
end
180-
181180
function default_stepsize(
182181
M::AbstractManifold, ::Type{GradientSamplingState}; retraction_method = default_retraction_method(M),
183182
)
@@ -387,13 +386,13 @@ function step_solver!(
387386
# solve sub problem in convex_hull_coeffs
388387
_gradient_sampling_subsolver(M, gss)
389388
# reconstruct tangent vector from the coefficients (w_l in HU17) in Y
390-
zero_vector!(M, gss.p, gss.Y)
389+
zero_vector!(M, gss.Y, gss.p)
391390
for (λj, Xj) in zip(gss.convex_hull_coeffs, gss.sampled_vectors)
392391
gss.Y .+= λj * Xj
393392
end
394393
# Decide whether to accept the step or update radius
395394
if norm(M, gss.p, gss.Y) < gss.subgradient_norm_tolerance
396-
# do not accept
395+
# do not accept this step but decrease radius and tolerance
397396
gss.sampling_radius *= gss.sampling_radius_reduction
398397
gss.subgradient_norm_tolerance *= gss.subgradient_norm_reduction
399398
else
@@ -420,12 +419,3 @@ function _gradient_sampling_subsolver(
420419
return gss
421420
end
422421
# (c) (not yet needed / implemented) an actual sub solver call
423-
424-
425-
function (d::DebugStepsize)(
426-
dmp::P, gss::GradientSamplingState, k::Int
427-
) where {P <: AbstractManoptProblem}
428-
(k < 1) && return nothing
429-
Printf.format(d.io, Printf.Format(d.format), get_last_stepsize(dmp, gss, k))
430-
return nothing
431-
end

test/solvers/test_gradient_sampling.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ _debug_gradient_sampling = false
2626
record = [:Iteration, :Cost, RecordGradientNorm()]
2727
)
2828

29+
Random.seed!(23)
2930
m2 = gradient_sampling(
3031
M, f, grad_f, p0;
3132
return_state = true,
32-
debug = _debug_gradient_sampling ? [:Iteration, :Cost, " ", :subgradient_norm_tolerance, " ", :sampling_radius, " | ", :GradientNorm, " ", :Change, "\n", :Stop, 100] : [],
33+
debug = _debug_gradient_sampling ? [:Iteration, :Cost, " ", :subgradient_norm_tolerance, " ", :sampling_radius, " | ", :GradientNorm, " ", :Change, "\n", :Stop, 10] : [],
3334
record = [:Iteration, :Cost, RecordGradientNorm()]
3435
)
3536

@@ -40,6 +41,19 @@ _debug_gradient_sampling = false
4041
p2 = get_solver_result(s2)
4142
@test f(M, p2) < f(M, p0)
4243

44+
p3 = copy(M, p0)
45+
Random.seed!(23)
46+
gradient_sampling!(
47+
M, f, grad_f, p3;
48+
sampling_radius = 0.1,
49+
subgradient_norm_tolerance = 0.02,
50+
sub_problem = gradient_sampling_subsolver,
51+
sub_state = AllocatingEvaluation(),
52+
)
53+
# The parameters of this run are chosen so that reduction is necessary,
54+
# they hence to not work that well and we end up a bit further away.
55+
@test isapprox(M, p2, p3; atol = 2.0e-3)
56+
4357
if _debug_gradient_sampling
4458
p1 = get_solver_result(m1)
4559
p2 = get_solver_result(m2)

0 commit comments

Comments
 (0)