JuliaAPlavin / AccessibleOptimization.jl

MIT License
3 stars 0 forks source link

MethodError: no method matching zero(::Tuple{Float64}) #9

Closed jariji closed 1 week ago

jariji commented 1 week ago

What do I mean here?

julia> let data = rand()
           f2((;β), x) = β⋅x
           θ₀ = (;β=1.0)
           vars = OptArgs(@o _.β)
           optfunc = OptimizationFunction(f2, Optimization.AutoFiniteDiff())
           ops = OptProblemSpec((@o optfunc(_, data)), θ₀, vars,)
           @time "solve" soln = solve(ops, BFGS())
       end
ERROR: MethodError: no method matching zero(::Tuple{Float64})

Stacktrace:
  [1] FiniteDiff.GradientCache(df::Tuple{…}, x::Tuple{…}, fdtype::Val{…}, returntype::Type, inplace::Val{…})
    @ FiniteDiff ~/.julia/packages/FiniteDiff/vund7/src/gradients.jl:53
  [2] FiniteDiff.GradientCache(df::Tuple{Float64}, x::Tuple{Float64}, fdtype::Val{:forward})
    @ FiniteDiff ~/.julia/packages/FiniteDiff/vund7/src/gradients.jl:25
  [3] instantiate_function(f::OptimizationFunction{…}, cache::OptimizationBase.ReInitCache{…}, adtype::AutoFiniteDiff{…}, num_cons::Int64)
    @ OptimizationFiniteDiffExt ~/.julia/packages/OptimizationBase/KIIy3/ext/OptimizationFiniteDiffExt.jl:132
  [4] OptimizationCache(prob::OptimizationProblem{…}, opt::BFGS{…}, data::Base.Iterators.Cycle{…}; callback::Function, maxiters::Nothing, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, structural_analysis::Bool, manifold::Nothing, kwargs::@Kwargs{})
    @ OptimizationBase ~/.julia/packages/OptimizationBase/KIIy3/src/cache.jl:38
  [5] __init(prob::OptimizationProblem{…}, opt::BFGS{…}, data::Base.Iterators.Cycle{…}; callback::Function, maxiters::Nothing, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, kwargs::@Kwargs{})
    @ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/hDX5k/src/OptimizationOptimJL.jl:112
  [6] __init(prob::OptimizationProblem{…}, opt::BFGS{…}, data::Base.Iterators.Cycle{…})
    @ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/hDX5k/src/OptimizationOptimJL.jl:77
  [7] init(::OptimizationProblem{…}, ::BFGS{…}; kwargs::@Kwargs{})
    @ SciMLBase ~/.julia/packages/SciMLBase/tEuIM/src/solve.jl:172
  [8] init(::OptimizationProblem{…}, ::BFGS{…})
    @ SciMLBase ~/.julia/packages/SciMLBase/tEuIM/src/solve.jl:170
  [9] solve(::OptimizationProblem{…}, ::BFGS{…}; kwargs::@Kwargs{})
    @ SciMLBase ~/.julia/packages/SciMLBase/tEuIM/src/solve.jl:94
 [10] solve(::OptimizationProblem{…}, ::BFGS{…})
    @ SciMLBase ~/.julia/packages/SciMLBase/tEuIM/src/solve.jl:91
 [11] solve(s::OptProblemSpec{…}, args::BFGS{…}; kwargs::@Kwargs{})
    @ AccessibleOptimization ~/.julia/packages/AccessibleOptimization/rNUrm/src/AccessibleOptimization.jl:144
 [12] macro expansion
    @ ./timing.jl:279 [inlined]
 [13] top-level scope
aplavin commented 1 week ago

Running this example after using AccessibleOptimization gives "UndefVarError: BFGS not defined", so probably more imports are needed?

Anyway, my guess is that you should specify the vector type to use, OptProblemSpec((@o optfunc(_, data)), Vector, θ₀, vars,) (or other like SVector, MVector). By default, AccessibleOptimization passes whatever it receives from Accessors.getall to Optimization, and it may be a Tuple.