jmejia8 / Metaheuristics.jl

High-performance metaheuristics for optimization coded purely in Julia.
https://jmejia8.github.io/Metaheuristics.jl/stable/
Other
253 stars 27 forks source link

Modify a metaheuristic #76

Open jmejia8 opened 1 year ago

jmejia8 commented 1 year ago

A modified metaheuristic can extend the capability of an implemented algorithm. The idea is to define custom components (initialization, termination criterion, etc) without overwriting default methods.

For instance:

eca = ECA(N = 100)
eca_modified = modify(eca, termination = RelativeTolerance())
optimize(f, bounds, eca_modified)
aplavin commented 1 year ago

May I suggest ConstructionBase/Accessors as the interface? So that

using ConstructionBase
eca_modified = setproperties(eca, termination = RelativeTolerance())

or

using Accessors
eca_modified = @set eca.termination = RelativeTolerance()

I think both already work, aside from RelativeTolerance() not existing :)

jmejia8 commented 1 year ago

Thank you for the suggestion. The idea is to modify a Julia method for a specific metaheuristic without overwriting the implemented methods (avoid type piracy). For example, I wanted to modify ECA in order to implement o re-use some termination criteria, but the following is a problem.

julia> algo = ECA()

julia> Metaheuristics.stop_criteria!(st, params::ECA, info, opt, args...; kargs...) = error("I modified all ECA instances")

julia> optimize(sum, [-5; 5;;], algo)
ERROR: I modified all ECA instances
...

julia> optimize(sum, [-5; 5;;], ECA)
ERROR: I modified all ECA instances

Therefore, we must find a way (simple to use for the user) to modify only one instance of the algorithm. However, I can't figure out how to do it. The following is a utopian implementation:

eca_modified = modify(ECA(), some_method = SomeParams())
Metaheuristics.some_method(params::SomeParams) = println("I'm not ECA")
# original implementation
# Metaheuristics.some_method(params::ECA) = println("I'm ECA")
optimize(sum, [5;5;;], eca_modified)
# I'm not ECA
optimize(sum, [5;5;;], ECA)
# I'm ECA