JuliaNLSolvers / Optim.jl

Optimization functions for Julia
Other
1.11k stars 213 forks source link

For ParticleSwarm, `upper` default to type that errors if `lower` is passed #1033

Closed KronosTheLate closed 1 year ago

KronosTheLate commented 1 year ago

This works:

julia> sol = optimize(lossfunc, p0, ParticleSwarm(n_particles=50), Optim.Options(iterations=10_000))

Adding a lower bound in the particle swarm causes a type error on upper:

julia> sol = optimize(lossfunc, p0, ParticleSwarm(lower=[Inf, 0, Inf], n_particles=50), Optim.Options(iterations=10_000))
ERROR: MethodError: no method matching ParticleSwarm(::Vector{Float64}, ::Vector{Any}, ::Int64)

Closest candidates are:
  ParticleSwarm(::Vector{T}, ::Vector{T}, ::Int64) where T
   @ Optim ~/.julia/packages/Optim/tP8PJ/src/multivariate/solvers/zeroth_order/particle_swarm.jl:2

Stacktrace:
 [1] ParticleSwarm(; lower::Vector{Float64}, upper::Vector{Any}, n_particles::Int64)
   @ Optim ~/.julia/packages/Optim/tP8PJ/src/multivariate/solvers/zeroth_order/particle_swarm.jl:36
 [2] top-level scope
   @ REPL[7]:1

I hardly think it is relevant, but here is the

code context ``` data_p3 = readdlm(joinpath(dir_exc, "METALS_Gold_Johnson_Christy.txt")) # Just 3 columns of numbers λs, ns, ks = data_p3|>eachcol λs .*= 1e-6 c = 3e8 fs = c ./ λs ωs = 2π*fs ñ = ns .+ im .* ks ϵ_rs = @. ns^2 - ks^2 ϵ_is = @. 2ns*ks ϵs = @. ϵ_rs + im*ϵ_is function lossfunc(p, ϵs=ϵs) ϵ∞, ωₚ, γ = p predictions_ϵ = [ϵ∞ - ωₚ^2/(ω^2+γ^2) + im*ωₚ^2*γ/(ω*(ω^2+γ^2)) for ω in ωs] residuals = predictions_ϵ - ϵs sum(abs2, residuals) end ## p0 = [1, 1e12, 1e1] ```
KronosTheLate commented 1 year ago

It must be because the struct requires the types to match:

struct ParticleSwarm{T} <: ZerothOrderOptimizer
    lower::Vector{T}
    upper::Vector{T}
    n_particles::Int
end