AutoForwardDiff() Error no method matching

Hi, I am trying to replicate the main examples for the Optimization package

https://docs.sciml.ai/Optimization/stable/getting_started/

However, when I try using the automatic differentiation Optimization.AutoForwardDiff() I get an error. Next the code I am using

using Optimization
rosenbrock(u, p) = (p[1] - u[1])^2 + p[2] * (u[2] - u[1]^2)^2
u0 = zeros(2)
p = [1.0, 100.0]
using OptimizationOptimJL
using OptimizationBBO
using ForwardDiff
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())

The last line gives the following error

ERROR: MethodError: no method matching (OptimizationFunction{true})(::typeof(rosenbrock), ::AutoForwardDiff{nothing, Nothing})
Closest candidates are:
(OptimizationFunction{iip})(::Any) where iip at ~/.julia/packages/SciMLBase/QqtZA/src/scimlfunctions.jl:3583
(OptimizationFunction{iip})(::Any, ::SciMLBase.AbstractADType; grad, hess, hv, cons, cons_j, cons_h, lag_h, hess_prototype, cons_jac_prototype, cons_hess_prototype, lag_hess_prototype, syms, paramsyms, observed, hess_colorvec, cons_jac_colorvec, cons_hess_colorvec, lag_hess_colorvec, expr, cons_expr, sys) where iip at ~/.julia/packages/SciMLBase/QqtZA/src/scimlfunctions.jl:3583
Stacktrace:
[1] OptimizationFunction(::Function, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ SciMLBase ~/.julia/packages/SciMLBase/QqtZA/src/scimlfunctions.jl:3581
[2] OptimizationFunction(::Function, ::Vararg{Any})
@ SciMLBase ~/.julia/packages/SciMLBase/QqtZA/src/scimlfunctions.jl:3581
[3] top-level scope
@ REPL[2]:1

Are you on the latest version? Julia v1.9? This seems to run fine:

using Optimization
rosenbrock(u, p) = (p[1] - u[1])^2 + p[2] * (u[2] - u[1]^2)^2
u0 = zeros(2)
p = [1.0, 100.0]
using ForwardDiff
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())

I copied and pasted the code that was posted, I do not have an error.

I have just updated to v.1.9 but I am still getting the same error.

Have you updated all of the packages and rerun the code? You can do so by entering the Pkg REPL by pressing ] and type “update” in the REPL without the “” symbol.

Done, same error.

Can you share ]st?

(@v1.9) pkg> st
Status ~/.julia/environments/v1.9/Project.toml
[336ed68f] CSV v0.10.11
[a93c6f00] DataFrames v1.6.1
⌃ [0c46a032] DifferentialEquations v7.6.0
[31c24e10] Distributions v0.25.100
[186dfeec] FEniCS v0.4.1
[f6369f11] ForwardDiff v0.10.36
[98e50ef6] JuliaFormatter v1.0.36
[7f7a1694] Optimization v3.17.0
[3e6eede4] OptimizationBBO v0.1.5
[36348300] OptimizationOptimJL v0.1.9
⌃ [1dea7af3] OrdinaryDiffEq v6.33.3
[91a5bcdd] Plots v1.39.0
[438e738f] PyCall v1.96.1
⌃ [0bca4576] SciMLBase v1.81.0
⌅ [2913bbd2] StatsBase v0.33.21
[592b5752] Trapz v2.0.3
[770da0de] UpdateJulia v0.4.2
[955f2c64] VTKView v0.1.0
[64499a7a] WriteVTK v1.18.0
[3fa0cd96] REPL
[9a3f8284] Random
Info Packages marked with ⌃ and ⌅ have new versions available, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use status --outdated

SciMLBase is in the stone age. Maybe Try ]add SciMLBase@1.97 and see what it says. And try it in a new REPL session.

It now works, thanks!

No problem. Yeah the ^ means an old package version, and usually if you’re having an issue that’s the first thing to check. The documentation always defaults to displaying the newest version, so if you’re on an older version you need to look for the older version of some documentation and… nobody does that :sweat_smile:. So most weird issues tend to just be from accidentally being on an older version for some reason or another.