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

Sign of objective and gradient in OptimizationState is inconsistent #858

Open
sethaxen opened this issue Nov 29, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@sethaxen
Copy link
Contributor

Describe the bug 🐞

The docstring for OptimizationState states that it contains the current objective and current gradient, but it does not specify whether this is of the OptimizationFunction or, in the case of MaxSense, of the sign-flipped version. NLopt/Optimization seem to use the former, while Optim uses the latter.

Expected behavior

It would be helpful for there to be a consistent convention for these signs, since the callback may rely on these state values and their signs.

Minimal Reproducible Example 👇

This example checks assumes that grad and objective should store the gradient and value of the function wrapped by OptimizationFunction and tests for this. It fails only for Optim optimizers with sense=MaxSense.

using OptimizationOptimJL, OptimizationNLopt, OptimizationOptimisers, ForwardDiff, Test

g(x, _...) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
negg = (-)  g
∇g(x) = ForwardDiff.gradient(g, x)
∇negg(x) = -∇g(x)
x0 = [-0.1, 0.1]

fun_sense = [Optimization.MinSense => (g, ∇g), Optimization.MaxSense => (negg, ∇negg)]
optimizers = [
    Optim => LBFGS(),
    NLopt => NLopt.LD_LBFGS(),
    Optimisers => Optimisers.AdaGrad()
]

@testset "Checks" begin
    @testset "$_module" for (_module, optimizer) in optimizers
        @testset "$sense" for (sense, (f, ∇f)) in fun_sense
            obj_trace = Any[]
            grad_trace = Any[]
            obj_trace_explicit = Any[]
            grad_trace_explicit = Any[]
            fun = OptimizationFunction(f, Optimization.AutoForwardDiff())
            prob = OptimizationProblem(fun, x0; sense)
            sol = solve(prob, optimizer; maxiters=1000, callback=function(state, loss_val)
                (; u, objective, grad) = state
                push!(obj_trace, deepcopy(objective))
                push!(grad_trace, deepcopy(grad))
                push!(obj_trace_explicit, f(u))
                push!(grad_trace_explicit, ∇f(u))
                return false
            end)
            @testset "Objective" begin
                @test obj_trace  obj_trace_explicit
            end
            any(!isnothing, grad_trace) && @testset "Gradient" begin
                @test grad_trace  grad_trace_explicit
            end
        end
    end
end

Error & Stacktrace ⚠️

Objective: Test Failed at REPL[107]:19
  Expression: obj_trace  obj_trace_explicit
   Evaluated: Any[2.02, 1.276995635644458, 1.200539256315468, 1.026377859277681, 0.9669504312863325, 0.8351702807365439, 0.66147321961325, 0.23242298045600596, 0.12712002825965688, 0.1104698321388143, 0.0977839849692398, 0.06937879878722462, 0.0007154021023531066, 0.0002027519078259664, 0.00010370872209492116, 6.1022731874248694e-6, 2.7314248908662438e-8, 2.041234934458518e-12, 3.2955871460056502e-18, 8.244829054723981e-29]  Any[-2.02, -1.276995635644458, -1.200539256315468, -1.026377859277681, -0.9669504312863325, -0.8351702807365439, -0.66147321961325, -0.23242298045600596, -0.12712002825965688, -0.1104698321388143, -0.0977839849692398, -0.06937879878722462, -0.0007154021023531066, -0.0002027519078259664, -0.00010370872209492116, -6.1022731874248694e-6, -2.7314248908662438e-8, -2.041234934458518e-12, -3.2955871460056502e-18, -8.244829054723981e-29]
...
Gradient: Test Failed at REPL[107]:22
  Expression: grad_trace  grad_trace_explicit
   Evaluated: Any[[1.3999999999999995, 18.0], [-1.217356490701289, 4.72397126322426], [-2.335758915213563, -0.7667751758174693], [-2.015442854533017, -3.000956096362261], [-1.6462900445839455, -3.894316143669933], [-0.2788337670716645, -5.608840724682623], [1.0598288167902634, -5.5143873896322955], [-7.006059163197108, 5.211195449161021], [-1.450964822558987, 0.574139517973915], [1.2558139656005292, -1.4109864062555988], [2.942159357062655, -2.467399611608201], [3.6588286457537835, -2.6617386056514425], [-0.8130965848198901, 0.3956701019678377], [-0.16091071662025308, 0.06755721167401862], [0.1705380821576499, -0.09513033973245211], [0.0982506272495665, -0.04868427014841359], [0.0011989019156061738, -0.0007604107179037811], [-5.661626090224616e-5, 2.8030790177879794e-5], [-1.0626897609391271e-8, 7.093836629223915e-9], [-3.5904612616377497e-13, 1.7763568394002505e-13]] ≈ Any[[-1.3999999999999995, -18.0], [1.217356490701289, -4.72397126322426], [2.335758915213563, 0.7667751758174693], [2.015442854533017, 3.000956096362261], [1.6462900445839455, 3.894316143669933], [0.2788337670716645, 5.608840724682623], [-1.0598288167902634, 5.5143873896322955], [7.006059163197108, -5.211195449161021], [1.450964822558987, -0.574139517973915], [-1.2558139656005292, 1.4109864062555988], [-2.942159357062655, 2.467399611608201], [-3.6588286457537835, 2.6617386056514425], [0.8130965848198901, -0.3956701019678377], [0.16091071662025308, -0.06755721167401862], [-0.1705380821576499, 0.09513033973245211], [-0.0982506272495665, 0.04868427014841359], [-0.0011989019156061738, 0.0007604107179037811], [5.661626090224616e-5, -2.8030790177879794e-5], [1.0626897609391271e-8, -7.093836629223915e-9], [3.5904612616377497e-13, -1.7763568394002505e-13]]
...
Test Summary:   | Pass  Fail  Total  Time
Checks          |    8     2     10  1.9s
  Optim         |    2     2      4  0.9s
    MinSense    |    2            2  0.4s
    MaxSense    |          2      2  0.5s
      Objective |          1      1  0.0s
      Gradient  |          1      1  0.0s
  NLopt         |    2            2  0.4s
  Optimisers    |    4            4  0.6s
ERROR: Some tests did not pass: 8 passed, 2 failed, 0 errored, 0 broken.

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
Status `/tmp/jl_NMIC4r/Project.toml`
  [f6369f11] ForwardDiff v0.10.38
  [4e6fcdb7] OptimizationNLopt v0.3.2
  [36348300] OptimizationOptimJL v0.4.1
  [42dfb2eb] OptimizationOptimisers v0.3.4
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Status `/tmp/jl_NMIC4r/Manifest.toml`
  [47edcb42] ADTypes v1.11.0
  [1520ce14] AbstractTrees v0.4.5
  [7d9f7c33] Accessors v0.1.38
  [79e6a3ab] Adapt v4.1.1
  [4fba245c] ArrayInterface v7.17.1
  [fa961155] CEnum v0.5.0
  [d360d2e6] ChainRulesCore v1.25.0
  [38540f10] CommonSolve v0.2.4
  [bbf7d656] CommonSubexpressions v0.3.1
  [34da2185] Compat v4.16.0
  [a33af91c] CompositionsBase v0.1.2
  [88cd18e8] ConsoleProgressMonitor v0.1.2
  [187b0558] ConstructionBase v1.5.8
  [9a962f9c] DataAPI v1.16.0
  [864edb3b] DataStructures v0.18.20
  [e2d170a0] DataValueInterfaces v1.0.0
  [163ba53b] DiffResults v1.1.0
  [b552c78f] DiffRules v1.15.1
  [a0c0ee7d] DifferentiationInterface v0.6.23
  [ffbed154] DocStringExtensions v0.9.3
  [4e289a0a] EnumX v1.0.4
  [e2ba6199] ExprTools v0.1.10
⌅ [6b7a57c9] Expronicon v0.8.5
  [9aa1b823] FastClosures v0.3.2
  [1a297f60] FillArrays v1.13.0
  [6a86dc24] FiniteDiff v2.26.2
  [f6369f11] ForwardDiff v0.10.38
  [069b7b12] FunctionWrappers v1.1.3
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [d9f16b24] Functors v0.5.2
  [46192b85] GPUArraysCore v0.2.0
  [3587e190] InverseFunctions v0.1.17
  [92d709cd] IrrationalConstants v0.2.2
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.6.1
  [5be7bae1] LBFGSB v0.4.1
  [1d6d02ad] LeftChildRightSiblingTrees v0.2.0
  [d3d80556] LineSearches v7.3.0
  [2ab3a3ac] LogExpFunctions v0.3.28
  [e6f89c97] LoggingExtras v1.1.0
  [d8e11817] MLStyle v0.4.17
  [1914dd2f] MacroTools v0.5.13
  [e1d29d7a] Missings v1.2.0
  [d41bc354] NLSolversBase v7.8.3
  [76087f3c] NLopt v1.1.1
  [77ba4419] NaNMath v1.0.2
  [429524aa] Optim v1.10.0
⌅ [3bd65402] Optimisers v0.3.4
  [7f7a1694] Optimization v4.0.5
  [bca83a33] OptimizationBase v2.4.0
  [4e6fcdb7] OptimizationNLopt v0.3.2
  [36348300] OptimizationOptimJL v0.4.1
  [42dfb2eb] OptimizationOptimisers v0.3.4
  [bac558e1] OrderedCollections v1.7.0
  [90014a1f] PDMats v0.11.31
  [d96e819e] Parameters v0.12.3
  [85a6dd25] PositiveFactorizations v0.2.4
  [aea7be01] PrecompileTools v1.2.1
  [21216c6a] Preferences v1.4.3
  [33c8b6b6] ProgressLogging v0.1.4
  [92933f4c] ProgressMeter v1.10.2
  [3cdcf5f2] RecipesBase v1.3.4
  [731186ca] RecursiveArrayTools v3.27.4
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [7e49a35a] RuntimeGeneratedFunctions v0.5.13
  [0bca4576] SciMLBase v2.65.0
  [c0aeaf25] SciMLOperators v0.3.12
  [53ae85a6] SciMLStructures v1.6.1
  [efcf1570] Setfield v1.1.1
  [a2af1166] SortingAlgorithms v1.2.1
  [9f842d2f] SparseConnectivityTracer v0.6.9
  [0a514795] SparseMatrixColorings v0.4.10
  [276daf66] SpecialFunctions v2.4.0
  [1e83bf80] StaticArraysCore v1.4.3
  [10745b16] Statistics v1.11.1
  [82ae8749] StatsAPI v1.7.0
  [2913bbd2] StatsBase v0.34.3
  [2efcf032] SymbolicIndexingInterface v0.3.35
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.12.0
  [5d786b92] TerminalLoggers v0.1.7
  [3a884ed6] UnPack v1.0.2
  [81d17ec3] L_BFGS_B_jll v3.0.1+0
  [079eb43e] NLopt_jll v2.8.0+0
  [efe28fd5] OpenSpecFun_jll v0.5.5+0
  [0dad84c5] ArgTools v1.1.2
  [56f22d72] Artifacts v1.11.0
  [2a0f44e3] Base64 v1.11.0
  [ade2ca70] Dates v1.11.0
  [8ba89e20] Distributed v1.11.0
  [f43a241f] Downloads v1.6.0
  [7b1f6079] FileWatching v1.11.0
  [9fa8497b] Future v1.11.0
  [b77e0a4c] InteractiveUtils v1.11.0
  [b27032c2] LibCURL v0.6.4
  [76f85450] LibGit2 v1.11.0
  [8f399da3] Libdl v1.11.0
  [37e2e46d] LinearAlgebra v1.11.0
  [56ddb016] Logging v1.11.0
  [d6f4376e] Markdown v1.11.0
  [ca575930] NetworkOptions v1.2.0
  [44cfe95a] Pkg v1.11.0
  [de0858da] Printf v1.11.0
  [9a3f8284] Random v1.11.0
  [ea8e919c] SHA v0.7.0
  [9e88b42a] Serialization v1.11.0
  [6462fe0b] Sockets v1.11.0
  [2f01184e] SparseArrays v1.11.0
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML v1.0.3
  [a4e569a6] Tar v1.10.0
  [cf7118a7] UUIDs v1.11.0
  [4ec0a83e] Unicode v1.11.0
  [e66e0078] CompilerSupportLibraries_jll v1.1.1+0
  [deac9b47] LibCURL_jll v8.6.0+0
  [e37daf67] LibGit2_jll v1.7.2+0
  [29816b5a] LibSSH2_jll v1.11.0+1
  [c8ffd9c3] MbedTLS_jll v2.28.6+0
  [14a3606d] MozillaCACerts_jll v2023.12.12
  [4536629a] OpenBLAS_jll v0.3.27+1
  [05823500] OpenLibm_jll v0.8.1+2
  [bea87d4a] SuiteSparse_jll v7.7.0+0
  [83775a58] Zlib_jll v1.2.13+1
  [8e850b90] libblastrampoline_jll v5.11.0+0
  [8e850ede] nghttp2_jll v1.59.0+0
  [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.11.1
Commit 8f5b7ca12ad (2024-10-16 10:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, tigerlake)
Threads: 8 default, 0 interactive, 4 GC (on 8 virtual cores)
Environment:
  JULIA_NUM_THREADS = auto
  JULIA_EDITOR = cursor
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
Development

No branches or pull requests

1 participant