SciML / Optimization.jl

Mathematical Optimization in Julia. Local, global, gradient-based and derivative-free. Linear, Quadratic, Convex, Mixed-Integer, and Nonlinear Optimization in one simple, fast, and differentiable interface.
https://docs.sciml.ai/Optimization/stable/
MIT License
725 stars 84 forks source link

Error? in OptimizationPackage/Optimization.md Documentation #837

Closed gbarz closed 3 weeks ago

gbarz commented 3 weeks ago

The code provided in the document examples below does not run successfully for me in Julia 1.10. Instead the following is returned in both the unconstrained and constrained cases:

ERROR: UndefVarError: LBFGS not defined

In the unconstrained problem, if Optimization.LBFGS() is changed to just LBFGS(), it finds the solution. However, the constrained problem still returns the following:

ERROR: The algorithm LBFGS{Nothing, LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Optim.var"#19#21"} does not support constraints. Either remove the cons function passed to OptimizationFunction or use a different algorithm.

Is there something else needed to get this to run successfully?

Unconstrained rosenbrock problem

using Optimization, Zygote

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

optf = OptimizationFunction(rosenbrock, AutoZygote()) prob = Optimization.OptimizationProblem(optf, x0, p) sol = solve(prob, Optimization.LBFGS())

With nonlinear and bounds constraints

function con2_c(res, x, p) res .= [x[1]^2 + x[2]^2, (x[2] * sin(x[1]) + x[1]) - 5] end

optf = OptimizationFunction(rosenbrock, AutoZygote(), cons = con2_c) prob = OptimizationProblem(optf, x0, p, lcons = [1.0, -Inf], ucons = [1.0, 0.0], lb = [-1.0, -1.0], ub = [1.0, 1.0]) res = solve(prob, Optimization.LBFGS(), maxiters = 100)

Vaibhavdixit02 commented 3 weeks ago

What versions are you using, can you show ]st?

gbarz commented 3 weeks ago

Status ~/... /Project.toml [6e4b80f9] BenchmarkTools v1.5.0 [a93c6f00] DataFrames v1.7.0 [31c24e10] Distributions v0.25.112 [f6369f11] ForwardDiff v0.10.36 [b6b21f68] Ipopt v1.6.6 [033835bb] JLD2 v0.5.4 ⌃ [7f7a1694] Optimization v4.0.2 [fd9f6733] OptimizationMOI v0.5.0 ⌃ [36348300] OptimizationOptimJL v0.4.0 [8a4e6c94] QuasiMonteCarlo v0.3.3 [0bca4576] SciMLBase v2.55.0 [e88e6eb3] Zygote v0.6.71 [37e2e46d] LinearAlgebra

Vaibhavdixit02 commented 3 weeks ago

I am unable to recreate your issue

julia> using Optimization, Zygote
Precompiling Optimization
  2 dependencies successfully precompiled in 4 seconds. 76 already precompiled.
[ Info: Precompiling OptimizationZygoteExt [e4695e4a-3364-5f77-b8a3-892b06f3a35d]

julia> rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
rosenbrock (generic function with 1 method)

julia> x0 = zeros(2)
2-element Vector{Float64}:
 0.0
 0.0

julia> p = [1.0, 100.0]
2-element Vector{Float64}:
   1.0
 100.0

julia> optf = OptimizationFunction(rosenbrock, AutoZygote())
(::OptimizationFunction{true, AutoZygote, typeof(rosenbrock), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}) (generic function with 1 method)

julia> prob = Optimization.OptimizationProblem(optf, x0, p)
OptimizationProblem. In-place: true
u0: 2-element Vector{Float64}:
 0.0
 0.0

julia> sol = solve(prob, Optimization.LBFGS())
retcode: Success
u: 2-element Vector{Float64}:
 0.9999997057368228
 0.999999398151528

julia> function con2_c(res, x, p)
       res .= [x[1]^2 + x[2]^2, (x[2] * sin(x[1]) + x[1]) - 5]
       end
con2_c (generic function with 1 method)

julia> optf = OptimizationFunction(rosenbrock, AutoZygote(), cons = con2_c)
(::OptimizationFunction{true, AutoZygote, typeof(rosenbrock), Nothing, Nothing, Nothing, Nothing, Nothing, typeof(con2_c), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}) (generic function with 1 method)

julia> prob = OptimizationProblem(optf, x0, p, lcons = [1.0, -Inf],
       ucons = [1.0, 0.0], lb = [-1.0, -1.0],
       ub = [1.0, 1.0])
OptimizationProblem. In-place: true
u0: 2-element Vector{Float64}:
 0.0
 0.0

julia> res = solve(prob, Optimization.LBFGS(), maxiters = 100)
retcode: Success
u: 2-element Vector{Float64}:
 0.783397417853095
 0.6215211044097776

I would confirm by restarting julia, maybe you had older versions that are being used even though your st shows new versions

gbarz commented 3 weeks ago

Thanks for the review. After a full VS Code reboot, it is indeed working. I had created a new environment to ensure updated package versions but some old settings must have been lingering.

Hopefully it was not too much of your time. Navigating the options in Optimization and Jump environments has been quite a bit to process initially.

ChrisRackauckas commented 3 weeks ago

No worries at all! It happens, and we also need to make this all a bit easier. Getting better every day.