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

PRIMA lib errors with AutoForwardDiff #719

Closed
soldasim opened this issue Mar 21, 2024 · 3 comments · Fixed by #721
Closed

PRIMA lib errors with AutoForwardDiff #719

soldasim opened this issue Mar 21, 2024 · 3 comments · Fixed by #721
Labels
bug Something isn't working

Comments

@soldasim
Copy link

Describe the bug 🐞

Using any algortihm from OptimizationPRIMA in conjunction with AutoForwardDiff results in an error.

Expected behavior

The unconstrained algorithms should work exactly the same way with and without AD provided. (Since they are derivative-free.)

The COBYLA algorithm apparently uses AD for automatic detection of linear/nonlinear constraints (despite being derivative-free). It should not error when AD is provided though.

Minimal Reproducible Example 👇

Consider the example from https://docs.sciml.ai/Optimization/stable/optimization_packages/prima;

using Optimization, OptimizationPRIMA
using ForwardDiff  # added by me

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

prob = OptimizationProblem(rosenbrock, x0, _p)

sol = Optimization.solve(prob, UOBYQA(), maxiters = 1000)

sol = Optimization.solve(prob, NEWUOA(), maxiters = 1000)

sol = Optimization.solve(prob, BOBYQA(), maxiters = 1000)

sol = Optimization.solve(prob, LINCOA(), maxiters = 1000)

function con2_c(res, x, p)
    res .= [x[1] + x[2], x[2] * sin(x[1]) - x[1]]
end
optprob = OptimizationFunction(rosenbrock, AutoForwardDiff(), cons = con2_c)
prob = OptimizationProblem(optprob, x0, _p, lcons = [1, -100], ucons = [1, 100])
sol = Optimization.solve(prob, COBYLA(), maxiters = 1000)

(Note that I added the missing dependency using ForwardDiff.)

Error & Stacktrace ⚠️

Without the added using ForwardDiff;

ERROR: LoadError: ArgumentError: The passed automatic differentiation backend choice is not available. Please load the corresponding AD package ForwardDiff.

Running any of the PRIMA algs with AutoForwardDiff always results in the same error;

1-element ExceptionStack:
LoadError: MethodError: no method matching length(::Optimization.ReInitCache{Vector{Float64}, Vector{Float64}})   

Closest candidates are:
  length(::BitSet)
   @ Base bitset.jl:348
  length(::Base.AsyncGenerator)
   @ Base asyncmap.jl:390
  length(::RegexMatch)
   @ Base regex.jl:285
  ...

Stacktrace:
 [1] instantiate_function(f::OptimizationFunction{…}, x::Optimization.ReInitCache{…}, adtype::AutoForwardDiff{…}, 
p::Int64, num_cons::Int64)
   @ OptimizationForwardDiffExt C:\Users\sheld\.julia\packages\OptimizationBase\rRpJs\ext\OptimizationForwardDiffExt.jl:19
 [2] instantiate_function(f::OptimizationFunction{…}, x::Optimization.ReInitCache{…}, adtype::AutoForwardDiff{…}, 
p::Int64)
   @ OptimizationForwardDiffExt C:\Users\sheld\.julia\packages\OptimizationBase\rRpJs\ext\OptimizationForwardDiffExt.jl:19
 [3] OptimizationCache(prob::OptimizationProblem{…}, opt::COBYLA, data::Base.Iterators.Cycle{…}; callback::OptimizationBase.NullCallback, maxiters::Int64, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, kwargs::@Kwargs{})
   @ OptimizationPRIMA C:\Users\sheld\.julia\packages\OptimizationPRIMA\85gFS\src\OptimizationPRIMA.jl:34
 [4] __init(prob::OptimizationProblem{…}, opt::COBYLA, data::Base.Iterators.Cycle{…}; callback::OptimizationBase.NullCallback, maxiters::Int64, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, kwargs::@Kwargs{})
   @ OptimizationBase C:\Users\sheld\.julia\packages\OptimizationBase\rRpJs\src\cache.jl:44
 [5] init(::OptimizationProblem{…}, ::COBYLA; kwargs::@Kwargs{})
   @ SciMLBase C:\Users\sheld\.julia\packages\SciMLBase\DLqDb\src\solve.jl:166
 [6] solve(::OptimizationProblem{…}, ::COBYLA; kwargs::@Kwargs{})
   @ SciMLBase C:\Users\sheld\.julia\packages\SciMLBase\DLqDb\src\solve.jl:96
 [7] top-level scope
   @ D:\julia-pkg\BOSS.jl\bugtest\test.jl:23
 [8] include(fname::String)
   @ Base.MainInclude .\client.jl:489
 [9] top-level scope
   @ REPL[3]:1
Some type information was truncated. Use `show(err)` to see complete types.
in expression starting at D:\julia-pkg\BOSS.jl\bugtest\test.jl:23

Environment:

  • Output of using Pkg; Pkg.status()
Status `D:\julia-pkg\BOSS.jl\bugtest\Project.toml`
  [f6369f11] ForwardDiff v0.10.36
  [7f7a1694] Optimization v3.24.2
  [72f8369c] OptimizationPRIMA v0.0.1
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Status `D:\julia-pkg\BOSS.jl\bugtest\Manifest.toml`
  [47edcb42] ADTypes v0.2.7
  [1520ce14] AbstractTrees v0.4.5
  [79e6a3ab] Adapt v4.0.4
  [4fba245c] ArrayInterface v7.9.0
  [38540f10] CommonSolve v0.2.4
  [bbf7d656] CommonSubexpressions v0.3.0
  [88cd18e8] ConsoleProgressMonitor v0.1.2
  [187b0558] ConstructionBase v1.5.5
  [9a962f9c] DataAPI v1.16.0
  [e2d170a0] DataValueInterfaces v1.0.0
  [163ba53b] DiffResults v1.1.0
  [b552c78f] DiffRules v1.15.1
  [ffbed154] DocStringExtensions v0.9.3
  [4e289a0a] EnumX v1.0.4
  [e2ba6199] ExprTools v0.1.10
  [f6369f11] ForwardDiff v0.10.36
  [069b7b12] FunctionWrappers v1.1.3
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [46192b85] GPUArraysCore v0.1.6
  [92d709cd] IrrationalConstants v0.2.2
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.5.0
  [1d6d02ad] LeftChildRightSiblingTrees v0.2.0
  [2ab3a3ac] LogExpFunctions v0.3.27
  [e6f89c97] LoggingExtras v1.0.3
  [1914dd2f] MacroTools v0.5.13
  [77ba4419] NaNMath v1.0.2
  [7f7a1694] Optimization v3.24.2
  [bca83a33] OptimizationBase v0.0.5
  [72f8369c] OptimizationPRIMA v0.0.1
  [bac558e1] OrderedCollections v1.6.3
⌅ [0a7d04aa] PRIMA v0.1.1
  [aea7be01] PrecompileTools v1.2.1
  [21216c6a] Preferences v1.4.3
  [33c8b6b6] ProgressLogging v0.1.4
  [92933f4c] ProgressMeter v1.10.0
  [3cdcf5f2] RecipesBase v1.3.4
  [731186ca] RecursiveArrayTools v3.12.0
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [7e49a35a] RuntimeGeneratedFunctions v0.5.12
  [0bca4576] SciMLBase v2.30.1
  [c0aeaf25] SciMLOperators v0.3.8
  [53ae85a6] SciMLStructures v1.1.0
  [efcf1570] Setfield v1.1.1
  [276daf66] SpecialFunctions v2.3.1
  [1e83bf80] StaticArraysCore v1.4.2
  [2efcf032] SymbolicIndexingInterface v0.3.11
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.11.1
  [5d786b92] TerminalLoggers v0.1.7
  [c3b1956e] TypeUtils v0.3.5
  [efe28fd5] OpenSpecFun_jll v0.5.5+0
  [eead6e0c] PRIMA_jll v0.7.1+0
  [0dad84c5] ArgTools v1.1.1
  [56f22d72] Artifacts
  [2a0f44e3] Base64
  [ade2ca70] Dates
  [8ba89e20] Distributed
  [f43a241f] Downloads v1.6.0
  [7b1f6079] FileWatching
  [9fa8497b] Future
  [b77e0a4c] InteractiveUtils
  [b27032c2] LibCURL v0.6.4
  [76f85450] LibGit2
  [8f399da3] Libdl
  [37e2e46d] LinearAlgebra
  [56ddb016] Logging
  [d6f4376e] Markdown
  [ca575930] NetworkOptions v1.2.0
  [44cfe95a] Pkg v1.10.0
  [de0858da] Printf
  [3fa0cd96] REPL
  [9a3f8284] Random
  [ea8e919c] SHA v0.7.0
  [9e88b42a] Serialization
  [6462fe0b] Sockets
  [2f01184e] SparseArrays v1.10.0
  [10745b16] Statistics v1.10.0
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML v1.0.3
  [a4e569a6] Tar v1.10.0
  [8dfed614] Test
  [cf7118a7] UUIDs
  [4ec0a83e] Unicode
  [e66e0078] CompilerSupportLibraries_jll v1.1.0+0
  [deac9b47] LibCURL_jll v8.4.0+0
  [e37daf67] LibGit2_jll v1.6.4+0
  [29816b5a] LibSSH2_jll v1.11.0+1
  [c8ffd9c3] MbedTLS_jll v2.28.2+1
  [14a3606d] MozillaCACerts_jll v2023.1.10
  [4536629a] OpenBLAS_jll v0.3.23+4
  [05823500] OpenLibm_jll v0.8.1+2
  [bea87d4a] SuiteSparse_jll v7.2.1+1
  [83775a58] Zlib_jll v1.2.13+1
  [8e850b90] libblastrampoline_jll v5.8.0+1
  [8e850ede] nghttp2_jll v1.52.0+1
  [3f19e933] p7zip_jll v17.4.0+2
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
  • Output of versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8 (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
Threads: 8 default, 0 interactive, 4 GC (on 8 virtual cores)
Environment:
  JULIA_NUM_THREADS = 8
  JULIA_STACKTRACE_ABBREVIATED = false
  JULIA_STACKTRACE_MINIMAL = false
  JULIA_STACKTRACE_PUBLIC = false
@soldasim
Copy link
Author

In the provided MRE, all the unconstrained algorithms work and only COBYLA fails as it is the only one using AutoForwardDiff.

However, the other algorithms also fail when provided with OptimizationFunction(rosenbrock, AutoForwardDiff()) instead of just rosenbrock.

@zaikunzhang
Copy link

zaikunzhang commented Apr 3, 2024

Have this issue and soldasim/BOSS.jl#28 been resolved? Thanks.

@soldasim
Copy link
Author

soldasim commented Apr 3, 2024

Yes, the issue is resolved. Thanks for a speedy fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants