Getting started example not running Optimization.jl

I’m assuming I’m missing something stupid, but I am just copy pasting code from the getting started section Getting Started with Optimization in Julia · Optimization.jl

The very first block of code runs fine. The first block of code in the Controlling Gradient Calculations (Automatic Differentiation) does not:

using ForwardDiff
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())
prob = OptimizationProblem(optf, u0, p)
sol = solve(prob, BFGS())

Gives error:

ERROR: MethodError: no method matching (OptimizationFunction{true})(::typeof(rosenbrock), ::AutoForwardDiff{nothing, Nothing})

Am I missing something?

Full code that I am trying to run:

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]

prob = OptimizationProblem(rosenbrock, u0, p)

# Import a solver package and solve the optimization problem
using OptimizationOptimJL
sol = solve(prob, NelderMead())

# Import a different solver package and solve the optimization problem a different way
using OptimizationBBO
prob = OptimizationProblem(rosenbrock, u0, p, lb = [-1.0, -1.0], ub = [1.0, 1.0])
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited())

using ForwardDiff
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())
prob = OptimizationProblem(optf, u0, p)
sol = solve(prob, BFGS())

I am sorry, I cannot reproduce the error. If I copy and paste the full code you are trying to run, it works:

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]

prob = OptimizationProblem(rosenbrock, u0, p)

# Import a solver package and solve the optimization problem
using OptimizationOptimJL
sol = solve(prob, NelderMead())

# Import a different solver package and solve the optimization problem a different way
using OptimizationBBO
prob = OptimizationProblem(rosenbrock, u0, p, lb = [-1.0, -1.0], ub = [1.0, 1.0])
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited())

using ForwardDiff
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())
prob = OptimizationProblem(optf, u0, p)
sol = solve(prob, BFGS())

and I get:
image

If you just want the example from the second link, this should do it:

using ForwardDiff, OptimizationOptimJL

rosenbrock(u, p) = (p[1] - u[1])^2 + p[2] * (u[2] - u[1]^2)^2
u0 = zeros(2)
p = [1.0, 100.0]

optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())
prob = OptimizationProblem(optf, u0, p)
sol = solve(prob, BFGS())

Maybe restart julia and try running it again?

Weird, I’ve tried restarting julia, maybe I’ll try reinstalling packages or running in a new environment…

Running in a fresh environment and making sure you’ve got the latest versions of all packages would eliminate some corner case issues. Can you post the full stack trace of the error that gets printed?