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
688 stars 75 forks source link

TypeError: in keyword argument linesearch, expected Function #695

Closed ederag closed 5 months ago

ederag commented 5 months ago

Specifying linesearch for IPNewton fails with TypeError.

MRE adapted from https://docs.sciml.ai/Optimization/stable/optimization_packages/optim/#Local-Constraint

using Optimization, OptimizationOptimJL
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 = Optimization.OptimizationProblem(rosenbrock, x0, p)
sol = solve(prob, IPNewton(linesearch = Optim.HagerZhang()))

Error & Stacktrace ⚠️

ERROR: TypeError: in keyword argument linesearch, expected Function, got a value of type LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}
Stacktrace:
 [1] top-level scope
   @ REPL[9]:1

Environment

Status `/tmp/jl_Ah7Xqw/Project.toml`
  [7f7a1694] Optimization v3.21.2
  [36348300] OptimizationOptimJL v0.2.2
Status `/tmp/jl_Ah7Xqw/Manifest.toml`
  [47edcb42] ADTypes v0.2.6
  [1520ce14] AbstractTrees v0.4.4
  [79e6a3ab] Adapt v4.0.1
  [4fba245c] ArrayInterface v7.7.0
  [6e4b80f9] BenchmarkTools v1.4.0
  [523fee87] CodecBzip2 v0.8.2
  [944b1d66] CodecZlib v0.7.4
  [38540f10] CommonSolve v0.2.4
  [bbf7d656] CommonSubexpressions v0.3.0
  [34da2185] Compat v4.12.0
  [88cd18e8] ConsoleProgressMonitor v0.1.2
  [187b0558] ConstructionBase v1.5.4
  [9a962f9c] DataAPI v1.16.0
  [864edb3b] DataStructures v0.18.16
  [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
  [1a297f60] FillArrays v1.9.3
  [6a86dc24] FiniteDiff v2.22.0
  [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
  [682c06a0] JSON v0.21.4
  [50d2b5c4] Lazy v0.15.1
  [1d6d02ad] LeftChildRightSiblingTrees v0.2.0
  [d3d80556] LineSearches v7.2.0
  [2ab3a3ac] LogExpFunctions v0.3.26
  [e6f89c97] LoggingExtras v1.0.3
  [1914dd2f] MacroTools v0.5.13
  [b8f27783] MathOptInterface v1.25.2
  [e1d29d7a] Missings v1.1.0
  [d8a4904e] MutableArithmetics v1.4.0
  [d41bc354] NLSolversBase v7.8.3
  [77ba4419] NaNMath v1.0.2
  [429524aa] Optim v1.9.1
  [7f7a1694] Optimization v3.21.2
  [36348300] OptimizationOptimJL v0.2.2
  [bac558e1] OrderedCollections v1.6.3
  [d96e819e] Parameters v0.12.3
  [69de0a69] Parsers v2.8.1
  [85a6dd25] PositiveFactorizations v0.2.4
  [aea7be01] PrecompileTools v1.2.0
  [21216c6a] Preferences v1.4.1
  [33c8b6b6] ProgressLogging v0.1.4
  [92933f4c] ProgressMeter v1.9.0
  [3cdcf5f2] RecipesBase v1.3.4
  [731186ca] RecursiveArrayTools v3.7.0
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [7e49a35a] RuntimeGeneratedFunctions v0.5.12
  [0bca4576] SciMLBase v2.23.0
  [c0aeaf25] SciMLOperators v0.3.7
  [efcf1570] Setfield v1.1.1
  [a2af1166] SortingAlgorithms v1.2.1
  [276daf66] SpecialFunctions v2.3.1
  [1e83bf80] StaticArraysCore v1.4.2
  [82ae8749] StatsAPI v1.7.0
  [2913bbd2] StatsBase v0.34.2
  [2efcf032] SymbolicIndexingInterface v0.3.5
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.11.1
  [5d786b92] TerminalLoggers v0.1.7
  [3bb67fe8] TranscodingStreams v0.10.3
  [410a4b4d] Tricks v0.1.8
  [781d530d] TruncatedStacktraces v1.4.0
  [3a884ed6] UnPack v1.0.2
  [6e34b625] Bzip2_jll v1.0.8+1
  [efe28fd5] OpenSpecFun_jll v0.5.5+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
  [a63ad114] Mmap
  [ca575930] NetworkOptions v1.2.0
  [44cfe95a] Pkg v1.10.0
  [de0858da] Printf
  [9abbd945] Profile
  [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.0.5+1
  [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+2
  [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
julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × AMD Ryzen 9 3900X 12-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver2)
  Threads: 1 on 12 virtual cores
ChrisRackauckas commented 5 months ago

From reading the error, I presume that needs to be: IPNewton(linesearch = Optim.HagerZhang)

ederag commented 5 months ago

The linked optim doc requires an instance rather than the type.

And algo_hz = Newton(linesearch = HagerZhang), fails with

MethodError: no method matching LineSearches.HagerZhang(::NLSolversBase.TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, ::Vector{Float64}, ::Vector{Float64}, ::Float64, ::Vector{Float64}, ::Float64, ::Float64)

Closest candidates are:

LineSearches.HagerZhang(::T, !Matched::T, !Matched::T, !Matched::T, !Matched::T, !Matched::T, ::Any, !Matched::T, !Matched::Any, !Matched::Tm) where {T, Tm}

@ LineSearches ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:525

    perform_linesearch!(::Optim.NewtonState{Vector{Float64}, Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}}, ::Optim.Newton{LineSearches.InitialStatic{Float64}, UnionAll}, ::NLSolversBase.TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}})@perform_linesearch.jl:58
    update_state!(::NLSolversBase.TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, ::Optim.NewtonState{Vector{Float64}, Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}}, ::Optim.Newton{LineSearches.InitialStatic{Float64}, UnionAll})@newton.jl:79
    optimize(::NLSolversBase.TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, ::Vector{Float64}, ::Optim.Newton{LineSearches.InitialStatic{Float64}, UnionAll}, ::Optim.Options{Float64, Nothing}, ::Optim.NewtonState{Vector{Float64}, Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}})@optimize.jl:54
    optimize(::NLSolversBase.TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, ::Vector{Float64}, ::Optim.Newton{LineSearches.InitialStatic{Float64}, UnionAll}, ::Optim.Options{Float64, Nothing})@optimize.jl:36
    var"#optimize#85"(::Bool, ::Symbol, ::Base.Pairs{Symbol, Optim.Newton{LineSearches.InitialStatic{Float64}, UnionAll}, Tuple{Symbol}, @NamedTuple{method::Optim.Newton{LineSearches.InitialStatic{Float64}, UnionAll}}}, ::typeof(Optim.optimize), ::Function, ::Function, ::Function, ::Vector{Float64})@interface.jl:111
    top-level scope@[Local: 1](http://localhost:1234/edit?id=f9148700-c19a-11ee-257f-69bdb30e2f94#)[inlined]
ChrisRackauckas commented 5 months ago

We're just passing this onto Optim so whatever Optim wants is what to put there.

ederag commented 5 months ago

Sorry, I typed in a Pluto notebook instead of in the minimum example REPL. Here is the output:

julia> sol = solve(prob, IPNewton(linesearch = Optim.HagerZhang))
ERROR: TypeError: in keyword argument linesearch, expected Function, got Type{LineSearches.HagerZhang}
Stacktrace:
 [1] top-level scope

The linked optim doc requires an instance rather than the type.

ChrisRackauckas commented 5 months ago

Then it looks like this is an upstream problem with Optim.jl?

ederag commented 5 months ago

OK, let me try in barebones Optim and report back.

ederag commented 5 months ago

Ah, there isn't any bug anywhere, but the current documentation linked to an optim doc example that uses Newton rather than IPNewton.

IPNewton does not accept LineSearches yet:

As of February 2018, the line search algorithm is specialised for constrained interior-point methods. In future we hope to support more algorithms from LineSearches.jl

Hence the errors.

So the current documentation is misleading (IIUC): https://github.com/SciML/Optimization.jl/blob/7c700d0e2b1c5847fffe7e96de4e9c543ce6618f/docs/src/optimization_packages/optim.md?plain=1#L58-L67

One fix could be to remove references to LineSearch and link to the equivalent and more relevant IPNewton example or find another constrained optimization algorithm that matches those sentences. I don't know about the latter, but the former I could do if you wish.

ChrisRackauckas commented 5 months ago

I see. Could you make the PR removing the reference?