Skip to content

Commit a4fc81d

Browse files
authored
Slight rewrite of update_g and update_fg as well as some SAMIN fixes (#1155)
1 parent d1ca027 commit a4fc81d

File tree

3 files changed

+23
-52
lines changed

3 files changed

+23
-52
lines changed

src/multivariate/optimize/optimize.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
update_g!(d, state, method) = nothing
2-
function update_g!(d, state, method::M) where {M<:Union{FirstOrderOptimizer,Newton}}
2+
function update_g!(d, state, method::FirstOrderOptimizer)
3+
# Update the function value and gradient
4+
value_gradient!(d, state.x)
5+
project_tangent!(method.manifold, gradient(d), state.x)
6+
end
7+
function update_g!(d, state, method::Newton)
38
# Update the function value and gradient
49
value_gradient!(d, state.x)
5-
if M <: FirstOrderOptimizer #only for methods that support manifold optimization
6-
project_tangent!(method.manifold, gradient(d), state.x)
7-
end
810
end
911
update_fg!(d, state, method) = nothing
1012
update_fg!(d, state, method::ZerothOrderOptimizer) = value!(d, state.x)
11-
function update_fg!(d, state, method::M) where {M<:Union{FirstOrderOptimizer,Newton}}
13+
function update_fg!(d, state, method::FirstOrderOptimizer)
14+
value_gradient!(d, state.x)
15+
project_tangent!(method.manifold, gradient(d), state.x)
16+
end
17+
function update_fg!(d, state, method::Newton)
1218
value_gradient!(d, state.x)
13-
if M <: FirstOrderOptimizer #only for methods that support manifold optimization
14-
project_tangent!(method.manifold, gradient(d), state.x)
15-
end
1619
end
1720

1821
# Update the Hessian

src/multivariate/solvers/constrained/samin.jl

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function optimize(
8282
(;nt, ns, t0, rt, r_expand, bounds_ratio, neps, coverage_ok, verbosity) = method
8383
verbose = verbosity > 0
8484

85-
x_tol, f_tol = options.f_abstol, options.x_abstol
85+
x_tol, f_tol = options.x_abstol, options.f_abstol
8686

8787
x0 = copy(x)
8888
n = size(x, 1) # dimension of parameter
@@ -213,7 +213,6 @@ function optimize(
213213
end
214214
converge = 0
215215
termination_code = TerminationCode.NotImplemented
216-
217216
return MultivariateOptimizationResults(
218217
method,
219218
x0,# initial_x,
@@ -236,7 +235,7 @@ function optimize(
236235
h_calls(d),
237236
options.time_limit,
238237
_time - time0,
239-
NamedTuple(),
238+
(;x_converged = x_absΔ <0.0, f_converged = f_absΔ <= 0.0, g_converged = false, iterations = f_calls(d) >= options.iterations, time_limit = _time - time0 > options.time_limit, callback = stopped_by_callback, f_increased = false, ls_failed = false),
240239
# not hit ever since stopped_by were not here?
241240
termination_code,
242241
)
@@ -361,39 +360,6 @@ function optimize(
361360
x = xopt
362361
end
363362
end
364-
termination_code = TerminationCode.NotImplemented
365-
return MultivariateOptimizationResults(
366-
method,
367-
x0,# initial_x,
368-
xopt, #pick_best_x(f_incr_pick, state),
369-
fopt, # pick_best_f(f_incr_pick, state, d),
370-
f_calls(d), #iteration,
371-
f_calls(d) >= options.iterations, #iteration == options.iterations,
372-
x_converged, # x_converged,
373-
x_tol,#T(options.x_tol),
374-
0.0,#T(options.x_tol),
375-
x_absΔ,# x_abschange(state),
376-
NaN,# x_relchange(state),
377-
f_converged, # f_converged,
378-
f_tol,#T(options.f_tol),
379-
0.0,#T(options.f_tol),
380-
f_absΔ,#f_abschange(d, state),
381-
NaN,#f_relchange(d, state),
382-
false,#g_converged,
383-
0.0,#T(options.g_tol),
384-
NaN,#g_residual(d),
385-
false, #f_increased,
386-
tr,
387-
f_calls(d),
388-
g_calls(d),
389-
h_calls(d),
390-
true,
391-
options.time_limit,
392-
_time - time0,
393-
NamedTuple(),
394-
termination_code,
395-
)
396-
397363
end
398364

399365
# TODO
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
using Random
2+
Random.seed!(3288)
13
@testset "SAMIN" begin
24
@testset "SAMIN $i" for i in keys(MVP.UnconstrainedProblems.examples)
35
prob = MVP.UnconstrainedProblems.examples[i]
4-
if i !== "Himmelblau"
6+
xtrue = prob.solutions
7+
if length(xtrue) > 10 || i == "Powell" || i == "Parabola"
58
continue
69
end
7-
xtrue = prob.solutions
810
f = OptimTestProblems.MultivariateProblems.objective(prob)
911
x0 = prob.initial_x
1012
res = optimize(
1113
f,
12-
x0 ./ 1.1 .- 2.0,
13-
x0 .* 1.1 .+ 2.0,
14-
x0,
15-
Optim.SAMIN(t0 = 2.0, r_expand = 9.0, verbosity = 0),
16-
Optim.Options(iterations = 100000),
14+
xtrue ./ 1.1,
15+
xtrue .* 1.1,
16+
xtrue .* 1.02,
17+
Optim.SAMIN(t0 = 1.0, r_expand = 2.0, verbosity = 0),
18+
Optim.Options(iterations = 10000),
1719
)
18-
@test Optim.minimum(res) < 1e-5
20+
@test abs(prob.minimum-Optim.minimum(res)) < 1e-5
1921
end
2022
end

0 commit comments

Comments
 (0)