Skip to content

Commit

Permalink
Fix NelderMead trace aliasing issues (JuliaNLSolvers#1114)
Browse files Browse the repository at this point in the history
* Fix NelderMead trace aliasing issues

* Update optimize.jl
  • Loading branch information
pkofod authored and avik-pal committed Nov 19, 2024
1 parent 99e54dc commit e5409df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/multivariate/solvers/zeroth_order/nelder_mead.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ function trace!(tr, d, state, iteration, method::NelderMead, options::Options, c
dt["step_type"] = state.step_type
end
if options.trace_simplex
dt["simplex"] = state.simplex
dt["simplex_values"] = state.f_simplex
dt["simplex"] = deepcopy(state.simplex) # vector of arrays
dt["simplex_values"] = copy(state.f_simplex)
end
update!(tr,
iteration,
Expand Down
18 changes: 18 additions & 0 deletions test/general/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@
results = optimize(cos, 0.0, 2pi, method = Brent())
@test norm(Optim.minimizer(results) - pi) < 0.01
end


@testset "nm trace" begin
# https://github.com/JuliaNLSolvers/Optim.jl/issues/1112
f(x) = (x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2

x0 = [0.0, 0.0]
opt = Optim.Options(store_trace = true,
trace_simplex = true,
extended_trace = true)
res = optimize(f, x0, NelderMead(), opt)
tr = Optim.simplex_trace(res)
trval = Optim.simplex_value_trace(res)
trcent = Optim.centroid_trace(res)
@test tr[end] != tr[end-1]
@test trval[end] != trval[end-1]
@test trcent[end] != trcent[end-1]
end

0 comments on commit e5409df

Please sign in to comment.