JuliaAPlavin / AccessibleOptimization.jl

MIT License
3 stars 0 forks source link

How to use a solver like Newton()? #1

Closed aplavin closed 7 months ago

aplavin commented 1 year ago

In GitLab by @alecloudenback on Jun 14, 2023, 10:19

If changing the example in the sample notebook from ECA() to Newton() (from OptimizaitonOptimJL), one gets the error:

ArgumentError: Newton is not supported as the Fminbox optimizer.

Even when limiting the model and OptArgs to one parameter, the error persists.

MWE:

struct Flat
    val::Float64
end

d = [4.0,6.0]

v = OptArgs(
    @optic(_.val) => 0..10.,
)

l(m, data) = sum(obs -> abs2(m.val - obs), data)

ops = OptProblemSpec(Base.Fix2(l, d), SVector, Flat(0.0), v)

solve(ops, Newton()).uobj
aplavin commented 1 year ago

Thanks for reporting! I haven't encountered methods that don't support parameter bounds, but Newton is such. Fixed: now, you can just omit interval bounds from OptArgs, specifying only optics.

Also, to make your example work, one needs a mutable vector (not SVector), and provide derivatives. Both are requirements of Newton.

julia> using AccessibleOptimization, OptimizationOptimJL, StaticArrays, IntervalSets

julia> struct Flat{T}
               val::T
       end
julia> d = [4.0,6.0]
julia> v = OptArgs(
              @optic(_.val),
       )
julia> l(m, data) = sum(obs -> abs2(m.val - obs), data)

julia> ops = OptProblemSpec(Base.Fix2(OptimizationFunction(l, Optimization.AutoForwardDiff()), d), Vector, Flat(0.0), v)

julia> solve(ops, Newton()).uobj
Flat{Float64}(5.0)