Error when trying to specify the initial simplex of Nelder Mead method in GalacticOptim.jl

Hello,
I would need help with the following error when trying specify the a,b parameters of the initial simplex constructor. I gives the same error even if I call the constructor without arguments.
I have Julia 1.7.2.
The output of Pkg.installed() is the following:

Dict{String, VersionNumber} with 16 entries:
  "CSV"               => v"0.10.4"
  "DynamicalSystems"  => v"2.3.0"
  "Interact"          => v"0.10.4"
  "Mux"               => v"0.7.6"
  "AlgebraOfGraphics" => v"0.6.6"
  "PyCall"            => v"1.93.1"
  "GalacticOptimJL"   => v"0.1.0"
  "CairoMakie"        => v"0.7.5"
  "HDF5"              => v"0.16.7"
  "Blink"             => v"0.12.5"
  "Plots"             => v"1.28.0"
  "WebIO"             => v"0.8.17"
  "DataFrames"        => v"1.3.3"
  "GalacticOptim"     => v"3.2.1"
  "GLM"               => v"1.7.0"
  "PalmerPenguins"    => v"0.1.4"

The code that gives the error is the following:

"""Optimization trials."""

# %%
using AlgebraOfGraphics, CairoMakie
using PyCall
using GalacticOptim
using GalacticOptimJL

# %%
pushfirst!(PyVector(pyimport("sys")."path"), "")
stream = pyimport("stream")


# %%
function chi2(x,p)
    θ = x[1]
    Δθ = x[2]
    β = x[3]
    ϵ = p[1]
    ic = [1.493370985649168858e+02, 3.669966976308609219e+01, 7.917039545144660018e+00,
          -7.050282547954606294e+00, -1.254565799483599520e+01, -1.636083097847286538e+01]
    r☼ = 8.122
    return stream.chi2(θ, Δθ, β, ϵ, ic, r☼)
end

# %%
x₀ = [3.621802786938911822e+01, 2.745899649967862999e+01, 1.25e-5]
p  = [60.0]


prob = OptimizationProblem(chi2, x₀, p)

sol = solve(prob, NelderMead(;initial_simplex=AffineSimplexer(0, 0.2)))

When running I obtain the following error:

ERROR: UndefVarError: AffineSimplexer not defined

Even if I call with

sol = solve(prob, NelderMead(;initial_simplex=AffineSimplexer()))

I obtain the same error message
Thank you in advance!

I’ve never used GalacticOptim but it looks like it’s not reexporting the Optim definitions? Optim.AffineSimplexer() seems to work.

Oh we should make GalacticOptimJL reexport it.

1 Like

Thanks, it starts to run. After 4 evaluations of the the chi2 function, it gives the following error:

36.21802786938912 27.45899649967863 1.25e-05 --- chi2 =  84.18840989127727
43.46164594326694 27.45899649967863 1.25e-05 --- chi2 =  4407.345028091967
36.21802786938912 32.95080829961435 1.25e-05 --- chi2 =  8123495.394618369
36.21802786938912 27.45899649967863 2.75e-05 --- chi2 =  1478157.9699103832
ERROR: KeyError: key "x" not found
Stacktrace:
  [1] getindex(h::Dict{Any, Any}, key::String)
    @ Base ./dict.jl:481
  [2] (::GalacticOptimJL.var"#_cb#11"{GalacticOptimJL.var"#9#17", NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, Base.Iterators.Cycle{Tuple{GalacticOptim.NullData}}})(trace::OptimizationState{Float64, NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}})
    @ GalacticOptimJL ~/.julia/packages/GalacticOptimJL/isaca/src/GalacticOptimJL.jl:77
  [3] update!(tr::Vector{OptimizationState{Float64, NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}}}, iteration::Int64, f_x::Float64, grnorm::Float64, dt::Dict{Any, Any}, store_trace::Bool, show_trace::Bool, show_every::Int64, callback::GalacticOptimJL.var"#_cb#11"{GalacticOptimJL.var"#9#17", NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, Base.Iterators.Cycle{Tuple{GalacticOptim.NullData}}}, trace_simplex::Bool)
    @ Optim ~/.julia/packages/Optim/6Lpjy/src/utilities/update.jl:25
  [4] trace!(tr::Vector{OptimizationState{Float64, NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}}}, d::TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, state::Optim.NelderMeadState{Vector{Float64}, Float64, Vector{Float64}}, iteration::Int64, method::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, options::Optim.Options{Float64, GalacticOptimJL.var"#_cb#11"{GalacticOptimJL.var"#9#17", NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, Base.Iterators.Cycle{Tuple{GalacticOptim.NullData}}}}, curr_time::Float64)
    @ Optim ~/.julia/packages/Optim/6Lpjy/src/multivariate/solvers/zeroth_order/nelder_mead.jl:331
  [5] optimize(d::TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}, method::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, options::Optim.Options{Float64, GalacticOptimJL.var"#_cb#11"{GalacticOptimJL.var"#9#17", NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, Base.Iterators.Cycle{Tuple{GalacticOptim.NullData}}}}, state::Optim.NelderMeadState{Vector{Float64}, Float64, Vector{Float64}})
    @ Optim ~/.julia/packages/Optim/6Lpjy/src/multivariate/optimize/optimize.jl:50
  [6] optimize(d::TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}, method::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, options::Optim.Options{Float64, GalacticOptimJL.var"#_cb#11"{GalacticOptimJL.var"#9#17", NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, Base.Iterators.Cycle{Tuple{GalacticOptim.NullData}}}})
    @ Optim ~/.julia/packages/Optim/6Lpjy/src/multivariate/optimize/optimize.jl:36
  [7] ___solve(prob::OptimizationProblem{true, OptimizationFunction{true, SciMLBase.NoAD, typeof(chi2), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Vector{Float64}, Vector{Float64}, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, opt::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters}, data::Base.Iterators.Cycle{Tuple{GalacticOptim.NullData}}; callback::Function, maxiters::Nothing, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ GalacticOptimJL ~/.julia/packages/GalacticOptimJL/isaca/src/GalacticOptimJL.jl:136
  [8] ___solve
    @ ~/.julia/packages/GalacticOptimJL/isaca/src/GalacticOptimJL.jl:68 [inlined]
  [9] #__solve#2
    @ ~/.julia/packages/GalacticOptimJL/isaca/src/GalacticOptimJL.jl:56 [inlined]
 [10] __solve (repeats 2 times)
    @ ~/.julia/packages/GalacticOptimJL/isaca/src/GalacticOptimJL.jl:44 [inlined]
 [11] #solve#482
    @ ~/.julia/packages/SciMLBase/nbKmA/src/solve.jl:3 [inlined]
 [12] solve(::OptimizationProblem{true, OptimizationFunction{true, SciMLBase.NoAD, typeof(chi2), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Vector{Float64}, Vector{Float64}, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::NelderMead{Optim.AffineSimplexer, Optim.AdaptiveParameters})
    @ SciMLBase ~/.julia/packages/SciMLBase/nbKmA/src/solve.jl:3
 [13] top-level scope
    @ ~/casa/work/2020/halostream/sources/stream_fit/likelihood_grid/optim/optim.jl:33

What does “x” refer to?
Thanks!

Oh open an issue. Optim’s callback interface isn’t the same for all solvers so we need to fix the access there.

Fixed Check against `NelderMead` type not object for callback branching by Vaibhavdixit02 · Pull Request #249 · SciML/GalacticOptim.jl · GitHub

Thanks!
How should I proceed ? Update the GalacticOptim and GalacticOptimJL packages ?

You have to wait until that is tagged and merged, but that shouldn’t be more than an hour from now.

2 Likes

Hi, I have recently updated GalacticOptim.

[a75be94c] GalacticOptim v3.3.1
[9d3c5eb1] GalacticOptimJL v0.1.0

When running the line:

sol = solve(prob, Optim.NelderMead(; initial_simplex=AffineSimplexer(a=1.25e-5,b=0.2)))

I obtain:

ERROR: UndefVarError: AffineSimplexer not defined

What could be happening?

sol = solve(prob, Optim.NelderMead(; initial_simplex=Optim.AffineSimplexer(a=1.25e-5,b=0.2)))

?

Yes it works like that and also if I just use
(prob, NelderMead...
without making Optim explicit.

Thank you very much!