Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error? in OptimizationPackage/Optimization.md Documentation #837

Closed
gbarz opened this issue Sep 29, 2024 · 5 comments
Closed

Error? in OptimizationPackage/Optimization.md Documentation #837

gbarz opened this issue Sep 29, 2024 · 5 comments
Labels
question Further information is requested

Comments

@gbarz
Copy link

gbarz commented Sep 29, 2024

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)

@gbarz gbarz added the question Further information is requested label Sep 29, 2024
@gbarz gbarz changed the title Unclear re documentation examples: OptimizationPackage/Optimization.md Error? in OptimizationPackage/Optimization.md Documentation Oct 1, 2024
@Vaibhavdixit02
Copy link
Member

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

@gbarz
Copy link
Author

gbarz commented Oct 5, 2024

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
Copy link
Member

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
Copy link
Author

gbarz commented Oct 6, 2024

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants