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

Callback signature doesn't match with the docs for PRIMA #743

Closed opiateblush closed 1 month ago

opiateblush commented 1 month ago

Describe the bug 🐞

The docs say that the first argument passed to the callback function is an OptimizationState. But if I run the optimization with NEWUOA of PRIMA then it's actually an Array.

Expected behavior

First argument passed to the callback function is an OptimizationState.

Minimal Reproducible Example πŸ‘‡

using Optimization, OptimizationPRIMA

function problem(x, p)
    return sum(x .^ 2) 
end

function main()
    x = randn(10)

    callback = function (state, obj, others...)
        println("Iteration: $(state.iter)")
        return false
    end

    p = OptimizationProblem(problem, x, callback = callback)

    solve(p, NEWUOA())
end

main()

Error & Stacktrace ⚠️

ERROR: LoadError: type Array has no field iter
Stacktrace:
  [1] getproperty
    @ ./Base.jl:37 [inlined]
  [2] (::var"#1#2")(::Vector{Float64}, ::Float64)
    @ Main ~/Workspaces/OptimizationBug/main.jl:11
  [3] (::OptimizationPRIMA.var"#3#6"{OptimizationCache{OptimizationFunction{true, SciMLBase.NoAD, typeof(problem), 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}, OptimizationBase.ReInitCache{Vector{Float64}, SciMLBase.NullParameters}, Nothing, Nothing, Nothing, Nothing, Nothing, NEWUOA, Base.Iterators.Cycle{Tuple{OptimizationBase.NullData}}, Bool, var"#1#2"}})(ΞΈ::Vector{Float64})
    @ OptimizationPRIMA ~/.julia/packages/OptimizationPRIMA/85gFS/src/OptimizationPRIMA.jl:123
  [4] _objfun_wrapper(x_ptr::Ptr{Float64}, f_ptr::Ptr{Float64})
    @ PRIMA ~/.julia/packages/PRIMA/8IzVw/src/PRIMA.jl:510
  [5] prima_newuoa(calfun::Ptr{Nothing}, n::Int64, x::Vector{Float64}, f::Base.RefValue{Float64}, nf::Base.RefValue{Int32}, rhobeg::Float64, rhoend::Float64, ftarget::Float64, maxfun::Int64, npt::Int64, iprint::PRIMA.Message)
    @ PRIMA ~/.julia/packages/PRIMA/8IzVw/src/wrappers.jl:46
  [6] newuoa!(f::Function, x::Vector{Float64}; rhobeg::Float64, rhoend::Float64, ftarget::Float64, maxfun::Int64, npt::Int64, iprint::PRIMA.Message)
    @ PRIMA ~/.julia/packages/PRIMA/8IzVw/src/PRIMA.jl:325
  [7] newuoa!
    @ ~/.julia/packages/PRIMA/8IzVw/src/PRIMA.jl:307 [inlined]
  [8] #newuoa#2
    @ ~/.julia/packages/PRIMA/8IzVw/src/PRIMA.jl:29 [inlined]
  [9] newuoa(f::Function, x0::Vector{Float64})
    @ PRIMA ~/.julia/packages/PRIMA/8IzVw/src/PRIMA.jl:27
 [10] __solve(cache::OptimizationCache{OptimizationFunction{true, SciMLBase.NoAD, typeof(problem), 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}, OptimizationBase.ReInitCache{Vector{Float64}, SciMLBase.NullParameters}, Nothing, Nothing, Nothing, Nothing, Nothing, NEWUOA, Base.Iterators.Cycle{Tuple{OptimizationBase.NullData}}, Bool, var"#1#2"})
    @ OptimizationPRIMA ~/.julia/packages/OptimizationPRIMA/85gFS/src/OptimizationPRIMA.jl:177
 [11] solve!
    @ ~/.julia/packages/SciMLBase/hSv8d/src/solve.jl:188 [inlined]
 [12] #solve#623
    @ ~/.julia/packages/SciMLBase/hSv8d/src/solve.jl:96 [inlined]
 [13] solve
    @ ~/.julia/packages/SciMLBase/hSv8d/src/solve.jl:93 [inlined]
 [14] main()
    @ Main ~/Workspaces/OptimizationBug/main.jl:17
 [15] top-level scope
    @ ~/Workspaces/OptimizationBug/main.jl:20

Environment:

  [7f7a1694] Optimization v3.25.0
  [72f8369c] OptimizationPRIMA v0.0.1
  [47edcb42] ADTypes v1.1.0
  [1520ce14] AbstractTrees v0.4.5
  [7d9f7c33] Accessors v0.1.36
  [79e6a3ab] Adapt v4.0.4
  [4fba245c] ArrayInterface v7.10.0
  [38540f10] CommonSolve v0.2.4
  [a33af91c] CompositionsBase v0.1.2
  [88cd18e8] ConsoleProgressMonitor v0.1.2
  [187b0558] ConstructionBase v1.5.5
  [9a962f9c] DataAPI v1.16.0
  [e2d170a0] DataValueInterfaces v1.0.0
  [ffbed154] DocStringExtensions v0.9.3
  [4e289a0a] EnumX v1.0.4
  [e2ba6199] ExprTools v0.1.10
  [069b7b12] FunctionWrappers v1.1.3
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [46192b85] GPUArraysCore v0.1.6
  [3587e190] InverseFunctions v0.1.14
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.5.0
  [5be7bae1] LBFGSB v0.4.1
  [1d6d02ad] LeftChildRightSiblingTrees v0.2.0
  [e6f89c97] LoggingExtras v1.0.3
  [1914dd2f] MacroTools v0.5.13
  [7f7a1694] Optimization v3.25.0
  [bca83a33] OptimizationBase v0.0.7
  [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.16.0
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [7e49a35a] RuntimeGeneratedFunctions v0.5.13
  [0bca4576] SciMLBase v2.36.1
  [c0aeaf25] SciMLOperators v0.3.8
  [53ae85a6] SciMLStructures v1.1.0
  [efcf1570] Setfield v1.1.1
  [1e83bf80] StaticArraysCore v1.4.2
  [2efcf032] SymbolicIndexingInterface v0.3.21
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.11.1
  [5d786b92] TerminalLoggers v0.1.7
  [c3b1956e] TypeUtils v0.3.8
  [81d17ec3] L_BFGS_B_jll v3.0.1+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.1+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
  [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 Version 1.10.3
Commit 0b4590a5507 (2024-04-30 10:59 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 Γ— Intel(R) Xeon(R) W-11955M CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, tigerlake)
Threads: 1 default, 0 interactive, 1 GC (on 16 virtual cores)
opiateblush commented 1 month ago

Well, seems to be fixed in OptimizationPRIMA.jl v0.0.2. However, the state.iter field is always 0. That cannot be correct, right?