JuliaNLSolvers / Optim.jl

Optimization functions for Julia
Other
1.13k stars 221 forks source link

Particle Swarm #944

Closed marlloseff closed 3 years ago

marlloseff commented 3 years ago

Hello everyone. I need help to understand Particle Swarm a bit. In the following example, the answer is right, but the Status seems to contradict. And why does Particle Swarm always exhaust the number of interactions? Even if you increase the number of interactions, it will still run out. Thank you.

using Optim

function test()
    f(x) = (1.0 - x[1])^2 + (2.0 - x[2])^2
    res = optimize(f, zeros(2), ParticleSwarm(; lower=[-10.0,-10.0], upper=[10.0,10.0]))
    println(res.minimizer)
    println(res)
end

test()

# [1.0, 2.0]

# * Status: failure (reached maximum number of iterations)

# * Candidate solution
# Final objective value:     9.632189e-27

# * Found with
# Algorithm:     Particle Swarm

# * Convergence measures
# |x - x'|               = NaN ≰ 0.0e+00
# |x - x'|/|x'|          = NaN ≰ 0.0e+00
# |f(x) - f(x')|         = NaN ≰ 0.0e+00
# |f(x) - f(x')|/|f(x')| = NaN ≰ 0.0e+00
# |g(x)|                 = NaN ≰ 1.0e-08

# * Work counters
# Seconds run:   0  (vs limit Inf)
# Iterations:    1000
# f(x) calls:    4003
# ∇f(x) calls:   0
longemen3000 commented 3 years ago

this is a known behaviour, as Particle swarm will always use the maximum number of iterations. the bug is in the display of the output, not in the result. this happens because the algorithm can't know if the current best solution is the solution to the optimization problem, so its determined to be the result given at the last iteration.

marlloseff commented 3 years ago

this is a known behaviour, as Particle swarm will always use the maximum number of iterations. the bug is in the display of the output, not in the result. this happens because the algorithm can't know if the current best solution is the solution to the optimization problem, so its determined to be the result given at the last iteration.

Ok longemen3000, thanks.

pkofod commented 3 years ago

Yeah, it's a bit of a strange thing to print. Particle swarm is never meant to "settle down", it will always try to escape any local optimum (but will often return if it was indeed at the global optimum). It should be changed so I'll keep it open.

marlloseff commented 3 years ago

Thanks.