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

Bug in GA Implementation? #75

Closed davidreihs closed 1 year ago

davidreihs commented 1 year ago

I have noticed that every other run using the GA implementation fails on a MethodError when using it to optimize the Rosenbrock function.

I noticed it when completing the example included with Optimization.jl/(OptimizationMetaheuristics.jl) package. The following code snippet sometimes leads to the Error in question. The other times I will run into the max number of iterations. Would be great to get some insight into where this error is coming from and whether it is reproducible on other machines.

using Metaheuristics

h(x) = (1.0 - x[1])^2 + 100. * (x[2] - x[1]^2)^2

D = 2
bounds = [-ones(D) ones(D)]'

result = Metaheuristics.optimize(h, bounds, Metaheuristics.GA())

The Error I get is the following:

ERROR: MethodError: no method matching !(::Float64) Closest candidates are: !(::Function) at operators.jl:1077 !(::Bool) at bool.jl:35 !(::Missing) at missing.jl:101 ... Stacktrace: [1] _broadcast_getindex_evalf @ .\broadcast.jl:670 [inlined] [2] _broadcast_getindex @ .\broadcast.jl:643 [inlined] [3] getindex @ .\broadcast.jl:597 [inlined] [4] copy @ .\broadcast.jl:899 [inlined] [5] materialize @ .\broadcast.jl:860 [inlined] [6] mutation!(Q::Matrix{Float64}, parameters::BitFlipMutation) @ Metaheuristics C:\Users\ReihsD.julia\packages\Metaheuristics\qh0JB\src\operators\mutation.jl:13 [7] update_state!(::State{Metaheuristics.xf_solution{Vector{Float64}}}, ::GA{RandomInBounds, TournamentSelection, UniformCrossover, BitFlipMutation, ElitistReplacement}, ::Problem{Float64}, ::Information, ::Options; kargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Metaheuristics C:\Users\ReihsD.julia\packages\Metaheuristics\qh0JB\src\algorithms\GA\GA.jl:198 [8] update_state!(::State{Metaheuristics.xf_solution{Vector{Float64}}}, ::GA{RandomInBounds, TournamentSelection, UniformCrossover, BitFlipMutation, ElitistReplacement}, ::Problem{Float64}, ::Information, ::Options) @ Metaheuristics C:\Users\ReihsD.julia\packages\Metaheuristics\qh0JB\src\algorithms\GA\GA.jl:182 [9] optimize(f::Function, bounds::LinearAlgebra.Adjoint{Float64, Matrix{Float64}}, method::Metaheuristics.Algorithm{GA{RandomInBounds, TournamentSelection, UniformCrossover, BitFlipMutation, ElitistReplacement}}; logger::Metaheuristics.var"#119#121") @ Metaheuristics C:\Users\ReihsD.julia\packages\Metaheuristics\qh0JB\src\optimize.jl:91

jmejia8 commented 1 year ago

Read the documentation on GA for real encoding.

davidreihs commented 1 year ago

Ah, thank you very much, I didn't notice that the default encoding for GA is binary. With the hints from the documentation for real encoding everything works.