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

Provide overoading point for arrays with custom axes #883

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

baggepinnen
Copy link
Contributor

This PR adds a function that can be overloaded for arrays with custom axes, so that these axes are preserved in the ParticleSwarmoptimization algorithm. The particular use case I have in mind is to formulate my objective function using ComponentArrays.jl, this works for most optimizers, but not for ParticleSwarm This PR introduces the function similar_axes that can be defined for custom arrays to do something sensible, e.g.,

julia> Optim.similar_axis(x::ComponentArray, n) = x .* zeros(length(x), n)

julia> a
ComponentVector{Float64}(a = [0.0, 0.0], b = [1.0, 1.0])

julia> f(x) = sum(abs2, x.a)+sum(abs, x.b)
f

julia> optimize(f, a, ParticleSwarm())
 * Status: failure (reached maximum number of iterations)

 * Candidate solution
    Final objective value:     5.214465e-84

 * 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:    5001
    ∇f(x) calls:   0

@pkofod
Copy link
Member

pkofod commented Nov 24, 2020

Thanks, I'll have a look. I'm inches from handing in my thesis though so it might be a few weeks. Please ping me if I forget :)

@pkofod
Copy link
Member

pkofod commented Dec 16, 2020

This might be a good solution, but I'm thinking that actually these should just be vectors of copies of the input array? That's how I do it in the new version https://github.com/pkofod/NLSolvers.jl/blob/65034bb76cbf91e22784bd0156f28b85416adb2f/src/optimize/randomsearch/particleswarm.jl#L28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants