JuliaNonconvex / Nonconvex.jl

Toolbox for gradient-based and derivative-free non-convex constrained optimization with continuous and/or discrete variables.
https://julianonconvex.github.io/Nonconvex.jl/
MIT License
112 stars 10 forks source link

Percival fails with ERROR: outside of the trust region: ‖x‖²= NaN #55

Closed tpapp closed 2 years ago

tpapp commented 3 years ago

The MWE uses this repository for SectorModelMWE, at commit ca7e0e00.

Note that from the output it seems that the values are finite, could this be a conditioning problem?

MWE

The following script reproduces the problem:

using SectorModelMWE
using Nonconvex, ChainRulesCore, ForwardDiff, DiffResults, Logging

Logging.global_logger(SimpleLogger(stdout)) # full precision printing

const DEBUG = Ref(true)         # true: always print, false: print non-finite

function nonfinite_warn(x; kwargs...)
    isnonfinite = any(x -> any(!isfinite, last(x)), kwargs)
    if DEBUG[] || isnonfinite
        @warn (isnonfinite ? "non-finite values" : "debugging") x kwargs...
    end
end

objective(x) = objective_and_constraint(PROBLEM, x)[1]

constraint(x) = objective_and_constraint(PROBLEM, x)[2]

function ChainRulesCore.rrule(::typeof(objective), x::AbstractVector)
    result = DiffResults.GradientResult(x)
    result = ForwardDiff.gradient!(result, objective, x)
    val = DiffResults.value(result)
    grad = DiffResults.gradient(result)
    nonfinite_warn(x, objective = val, ∇ = grad)
    val, Δ -> (NO_FIELDS, Δ * grad)
end

function ChainRulesCore.rrule(::typeof(constraint), x::AbstractVector)
    result = DiffResults.JacobianResult(zeros(5), x)
    result = ForwardDiff.jacobian!(result, constraint, x)
    val = DiffResults.value(result)
    jac = DiffResults.jacobian(result)
    nonfinite_warn(x, constraint = val, ∂ = jac)
    val, Δ -> (NO_FIELDS, jac' * Δ)
end

m = Model(objective)
addvar!(m, LOWER_BOUNDS_X, UPPER_BOUNDS_X)
add_eq_constraint!(m, FunctionWrapper(constraint, 5))

alg = AugLag()
options = Nonconvex.AugLagOptions()
x0 = [0.4683960639229081, 0.8400753712868766, 0.8194473520749728, 1.7190740064948666,
      0.49831460023812674, 2681.10696006373, 2881.4771575869295, 2994.7180619903943,
      457.97811450658014, 457.97811450657997]
sol = Nonconvex.optimize(m, alg, x0, options = options)

Output and backtrace

julia> sol = Nonconvex.optimize(m, alg, x0, options = options)
┌ Warning: debugging
│   x = [0.4683960639229081, 0.8400753712868766, 0.8194473520749728, 1.7190740064948666, 0.49831460023812674, 2681.10696006373, 2881.4771575869295, 2994.7180619903943, 457.97811450658014, 457.97811450657997]
│   objective = 562753.0708490529
│   ∇ = [0.008539714056223478, 0.002001338855565355, 3920.2751620731724, -442.1630506615913, -0.004737052357618956, -0.044891831971265866, -0.08206262647627585, -0.11671199733245007, 0.12183315396074128, 0.12183330181925056]
└ @ Main REPL[10]:4
┌ Warning: debugging
│   x = [0.4683960639229081, 0.8400753712868766, 0.8194473520749728, 1.7190740064948666, 0.49831460023812674, 2681.10696006373, 2881.4771575869295, 2994.7180619903943, 457.97811450658014, 457.97811450657997]
│   objective = 562753.0708490529
│   ∇ = [0.008539714056223478, 0.002001338855565355, 3920.2751620731724, -442.1630506615913, -0.004737052357618956, -0.044891831971265866, -0.08206262647627585, -0.11671199733245007, 0.12183315396074128, 0.12183330181925056]
└ @ Main REPL[10]:4
┌ Warning: debugging
│   x = [0.4683960639229081, 0.8400753712868766, 0.8194473520749728, 1.7190740064948666, 0.49831460023812674, 2681.10696006373, 2881.4771575869295, 2994.7180619903943, 457.97811450658014, 457.97811450657997]
│   constraint = [-7.2062078526613504e-12, -1.2265133353395186e-11, 1.2269019133981374e-11, 1.1719579300073502e-12, 1.1719574963264812e-12]
│   ∂ = [-0.7630419681616327 -0.2784502755416528 0.11735966895154426 0.08701055894920123 0.23894566568271322 0.0005589769512999752 -0.00023496642357380388 -0.00031055881069954943 -2.410770638450196e-5 -2.410770638450198e-5; -0.518080796729811 -0.224923523544702 1.0935488866927146 0.14823992865381563 0.047940065294448185 -0.0001935167002294156 0.0007701811115232878 -0.0005537467149087483 -4.107231027595321e-5 -4.107231027595325e-5; -0.07360966940883162 -0.08732715078168107 2.217373334206754 0.21313243716568314 -0.22508608761678872 -0.00027277884950435656 -0.0005861791295426577 0.0008958873541771344 -5.903652183877688e-5 -5.903652183877689e-5; 1.8605352062881548 0.8461575891768994 -1.285850809792808 -0.7052551923129947 0.03321209851756579 1.4723408584320038e-5 2.69172065888774e-5 -0.0004383814498827035 0.00020436346099006513 0.00019474037990484683; 1.8605352062881553 0.8461575891768996 -1.285850809792808 -0.7052551923129948 0.03321209851756579 1.472340858432004e-5 2.6917206588877407e-5 -0.00043838144988270363 0.00019474037990484683 0.0002043634609900652]
└ @ Main REPL[10]:4
┌ Info:   iter        fx    normgp    normcx         μ     normy    sumc     inner_status        iter_type  
└ @ Percival /home/tamas/.julia/packages/Percival/k19Y2/src/method.jl:130
┌ Info:      0   5.6e+05   4.4e+02   1.9e-11   1.0e+01   2.2e+00       5
└ @ Percival /home/tamas/.julia/packages/Percival/k19Y2/src/method.jl:132
┌ Warning: debugging
│   x = [0.4683960639229081, 0.8400753712868766, 0.8194473520749728, 1.7190740064948666, 0.49831460023812674, 2681.10696006373, 2881.4771575869295, 2994.7180619903943, 457.97811450658014, 457.97811450657997]
│   objective = 562753.0708490529
│   ∇ = [0.008539714056223478, 0.002001338855565355, 3920.2751620731724, -442.1630506615913, -0.004737052357618956, -0.044891831971265866, -0.08206262647627585, -0.11671199733245007, 0.12183315396074128, 0.12183330181925056]
└ @ Main REPL[10]:4
┌ Warning: debugging
│   x = [0.4683960639229081, 0.8400753712868766, 0.8194473520749728, 1.7190740064948666, 0.49831460023812674, 2681.10696006373, 2881.4771575869295, 2994.7180619903943, 457.97811450658014, 457.97811450657997]
│   constraint = [-7.2062078526613504e-12, -1.2265133353395186e-11, 1.2269019133981374e-11, 1.1719579300073502e-12, 1.1719574963264812e-12]
│   ∂ = [-0.7630419681616327 -0.2784502755416528 0.11735966895154426 0.08701055894920123 0.23894566568271322 0.0005589769512999752 -0.00023496642357380388 -0.00031055881069954943 -2.410770638450196e-5 -2.410770638450198e-5; -0.518080796729811 -0.224923523544702 1.0935488866927146 0.14823992865381563 0.047940065294448185 -0.0001935167002294156 0.0007701811115232878 -0.0005537467149087483 -4.107231027595321e-5 -4.107231027595325e-5; -0.07360966940883162 -0.08732715078168107 2.217373334206754 0.21313243716568314 -0.22508608761678872 -0.00027277884950435656 -0.0005861791295426577 0.0008958873541771344 -5.903652183877688e-5 -5.903652183877689e-5; 1.8605352062881548 0.8461575891768994 -1.285850809792808 -0.7052551923129947 0.03321209851756579 1.4723408584320038e-5 2.69172065888774e-5 -0.0004383814498827035 0.00020436346099006513 0.00019474037990484683; 1.8605352062881553 0.8461575891768996 -1.285850809792808 -0.7052551923129948 0.03321209851756579 1.472340858432004e-5 2.6917206588877407e-5 -0.00043838144988270363 0.00019474037990484683 0.0002043634609900652]
└ @ Main REPL[10]:4
┌ Info:      1   5.6e+05   4.4e+02   1.9e-11   1.0e+01   2.2e+00      10      first_order         update_y
└ @ Percival /home/tamas/.julia/packages/Percival/k19Y2/src/method.jl:181
┌ Warning: debugging
│   x = [0.010000000000000009, 1.606224146826592, 0.01, 51.03948944477472, 0.5796373532789317, 2681.146672053711, 2881.5286636105243, 2994.778570288716, 457.90191714570767, 457.9019171297725]
│   objective = 553855.8007479684
│   ∇ = [-0.02215531149216976, 0.0001634457038592099, 5180.659352482244, -3.333453274387851, -0.03453233410733292, -2.477035911808962, -3.0274552620835298, -3.3913465015315207, 4.447918797302337, 4.447918878121673]
└ @ Main REPL[10]:4
┌ Warning: debugging
│   x = [0.010000000000000009, 1.606224146826592, 0.01, 51.03948944477472, 0.5796373532789317, 2681.146672053711, 2881.5286636105243, 2994.778570288716, 457.90191714570767, 457.9019171297725]
│   constraint = [0.1613835436318019, 0.3036593877668866, 0.4614854164212876, 0.012550477553153819, 0.012550477553088395]
│   ∂ = [-0.6909136306433854 0.01368402062623054 0.01816695985477588 -3.7606854825204524e-5 0.19085315590665766 0.00040505667418997825 -0.00018902118734475494 -0.0003228427337963879 4.9498053373934286e-5 4.9498053413023305e-5; -1.6171722253941776 0.029070670444224005 0.039829347858303096 3.3689155258822995e-6 0.0099295978272783 -0.0001533463758404125 0.0007590363491046999 -0.0006097210707321685 -5.7803211026089575e-6 -5.780321106082496e-6; -2.6475821563028816 0.045214040753104284 0.06273766504281464 7.902157526120232e-5 -0.3350465410067529 -0.00023311068964983176 -0.0005422772921189344 0.0009668119486967581 -0.00010739286249439469 -0.00010739286257646042; 0.045174868709954574 -0.000333267183514278 -7.792027425304856e-5 -1.488480624138309e-5 0.07041172285197775 -6.064828348088269e-6 -1.3958360827905089e-5 -2.370331014808855e-5 2.3916607660948753e-5 1.980991159959415e-5; 0.04517486870979145 -0.00033326718351309127 -7.792027425276729e-5 -1.488480624132922e-5 0.07041172285172355 -6.064828348056595e-6 -1.3958360827832254e-5 -2.370331014796491e-5 1.980991158405562e-5 2.391660767625915e-5]
└ @ Main REPL[10]:4
┌ Info:      2   5.5e+05   8.9e+00   5.8e-01   1.0e+02   2.2e+00      21      first_order         update_μ
└ @ Percival /home/tamas/.julia/packages/Percival/k19Y2/src/method.jl:181
ERROR: outside of the trust region: ‖x‖²=    NaN, Δ²=7.6e+02
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] to_boundary(x::Vector{Float64}, d::Vector{Float64}, radius::Float64; flip::Bool, xNorm2::Float64, dNorm2::Float64)
    @ Krylov ~/.julia/packages/Krylov/XqTOU/src/krylov_utils.jl:164
  [3] cg(A::LinearOperators.LinearOperator{Float64}, b::Vector{Float64}; M::LinearOperators.opEye, atol::Float64, rtol::Float64, itmax::Int64, radius::Float64, linesearch::Bool, verbose::Int64, history::Bool)
    @ Krylov ~/.julia/packages/Krylov/XqTOU/src/cg.jl:103
  [4] projected_newton!(x::Vector{Float64}, H::LinearOperators.LinearOperator{Float64}, g::Vector{Float64}, Δ::Float64, cgtol::Float64, s::Vector{Float64}, ℓ::Vector{Float64}, u::Vector{Float64}; max_cgiter::Int64)
    @ JSOSolvers ~/.julia/packages/JSOSolvers/w21mV/src/tron.jl:333
  [5] (::JSOSolvers.var"#12#13"{Vector{Float64}, Int64, Float64, Vector{Float64}, Vector{Float64}, Vector{Float64}, LinearOperators.LinearOperator{Float64}, Float64})()
    @ JSOSolvers ~/.julia/packages/JSOSolvers/w21mV/src/tron.jl:100
  [6] with_logstate(f::Function, logstate::Any)
    @ Base.CoreLogging ./logging.jl:491
  [7] with_logger
    @ ./logging.jl:603 [inlined]
  [8] tron(::Val{:Newton}, nlp::NLPModelsModifiers.LBFGSModel; subsolver_logger::Base.CoreLogging.NullLogger, x::Vector{Float64}, μ₀::Float64, μ₁::Float64, σ::Float64, max_eval::Int64, max_time::Float64, max_cgiter::Int64, use_only_objgrad::Bool, cgtol::Float64, atol::Float64, rtol::Float64, fatol::Float64, frtol::Float64)
    @ JSOSolvers ~/.julia/packages/JSOSolvers/w21mV/src/tron.jl:99
  [9] tron(nlp::NLPModelsModifiers.LBFGSModel; variant::Symbol, kwargs::Base.Iterators.Pairs{Symbol, Any, NTuple{7, Symbol}, NamedTuple{(:x, :cgtol, :rtol, :atol, :max_time, :max_eval, :max_cgiter), Tuple{Vector{Float64}, Float64, Float64, Float64, Float64, Int64, Int64}}})
    @ JSOSolvers ~/.julia/packages/JSOSolvers/w21mV/src/tron.jl:6
 [10] (::Percival.var"#7#10"{Float64, Nonconvex.var"#183#184"{Int64}, Int64, Dict{Symbol, Int64}, Percival.AugLagModel{ADNLPModels.ADNLPModel, Float64, Vector{Float64}}})()
    @ Percival ~/.julia/packages/Percival/k19Y2/src/method.jl:141
 [11] with_logstate(f::Function, logstate::Any)
    @ Base.CoreLogging ./logging.jl:491
 [12] with_logger
    @ ./logging.jl:603 [inlined]
 [13] percival(::Val{:equ}, nlp::ADNLPModels.ADNLPModel; μ::Float64, max_iter::Int64, max_time::Float64, max_eval::Int64, atol::Float64, rtol::Float64, ctol::Float64, subsolver_logger::Base.CoreLogging.NullLogger, inity::Vector{Float64}, subproblem_modifier::Nonconvex.var"#183#184"{Int64}, subsolver_max_eval::Int64, subsolver_kwargs::Dict{Symbol, Int64})
    @ Percival ~/.julia/packages/Percival/k19Y2/src/method.jl:140
 [14] _percival(nlp::ADNLPModels.ADNLPModel; μ::Float64, max_iter::Int64, max_time::Float64, max_eval::Int64, atol::Float64, rtol::Float64, ctol::Float64, first_order::Bool, memory::Int64, subsolver_logger::Base.CoreLogging.NullLogger, inity::Vector{Float64}, max_cgiter::Int64, subsolver_max_eval::Int64, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Nonconvex ~/.julia/packages/Nonconvex/prdTV/src/wrappers/percival.jl:71
 [15] optimize!(workspace::Nonconvex.PercivalWorkspace{Model{Vector{Float64}}, ADNLPModels.ADNLPModel, Vector{Float64}, PercivalOptions{NamedTuple{(:first_order, :memory, :inity), Tuple{Bool, Int64, typeof(ones)}}}, Base.RefValue{Int64}})
    @ Nonconvex ~/.julia/packages/Nonconvex/prdTV/src/wrappers/percival.jl:42
 [16] optimize(::Model{Vector{Float64}}, ::Vararg{Any, N} where N; kwargs::Base.Iterators.Pairs{Symbol, PercivalOptions{NamedTuple{(:first_order, :memory, :inity), Tuple{Bool, Int64, typeof(ones)}}}, Tuple{Symbol}, NamedTuple{(:options,), Tuple{PercivalOptions{NamedTuple{(:first_order, :memory, :inity), Tuple{Bool, Int64, typeof(ones)}}}}}})
    @ Nonconvex ~/.julia/packages/Nonconvex/prdTV/src/algorithms/mma_algorithm.jl:183
 [17] top-level scope
    @ REPL[19]:1
tpapp commented 3 years ago

Manifest:

(SectorModelMWE) pkg> st --manifest
     Project SectorModelMWE v0.1.0
      Status `~/research/SectorModelMWE/Manifest.toml`
  [dce04be8] ArgCheck v2.1.0
  [d360d2e6] ChainRulesCore v0.9.41
  [bbf7d656] CommonSubexpressions v0.3.0
  [34da2185] Compat v3.28.0
  [163ba53b] DiffResults v1.0.3
  [b552c78f] DiffRules v1.0.2
  [ffbed154] DocStringExtensions v0.8.4
  [f6369f11] ForwardDiff v0.10.18
  [692b3bcd] JLLWrappers v1.3.0
  [0b1a1467] KrylovKit v0.5.2
  [1914dd2f] MacroTools v0.5.6
  [77ba4419] NaNMath v0.3.5
  [21216c6a] Preferences v1.2.1
  [276daf66] SpecialFunctions v1.3.0
  [90137ffa] StaticArrays v1.1.2
  [14486370] TrustRegionMethods v0.3.2
  [3a884ed6] UnPack v1.0.2
  [efe28fd5] OpenSpecFun_jll v0.5.4+0
  [0dad84c5] ArgTools
  [56f22d72] Artifacts
  [2a0f44e3] Base64
  [ade2ca70] Dates
  [8bb1440f] DelimitedFiles
  [8ba89e20] Distributed
  [f43a241f] Downloads
  [b77e0a4c] InteractiveUtils
  [b27032c2] LibCURL
  [76f85450] LibGit2
  [8f399da3] Libdl
  [37e2e46d] LinearAlgebra
  [56ddb016] Logging
  [d6f4376e] Markdown
  [a63ad114] Mmap
  [ca575930] NetworkOptions
  [44cfe95a] Pkg
  [de0858da] Printf
  [3fa0cd96] REPL
  [9a3f8284] Random
  [ea8e919c] SHA
  [9e88b42a] Serialization
  [1a1011a3] SharedArrays
  [6462fe0b] Sockets
  [2f01184e] SparseArrays
  [10745b16] Statistics
  [fa267f1f] TOML
  [a4e569a6] Tar
  [8dfed614] Test
  [cf7118a7] UUIDs
  [4ec0a83e] Unicode
  [e66e0078] CompilerSupportLibraries_jll
  [deac9b47] LibCURL_jll
  [29816b5a] LibSSH2_jll
  [c8ffd9c3] MbedTLS_jll
  [14a3606d] MozillaCACerts_jll
  [83775a58] Zlib_jll
  [8e850ede] nghttp2_jll
  [3f19e933] p7zip_jll
tpapp commented 3 years ago

This is actually an upstream issue, reported it as JuliaSmoothOptimizers/Percival.jl#50.

mohamed82008 commented 2 years ago

Closing this issue since it is an upstream issue.