I’m trying to run a genetic algorithm optimization, and to reuse Evolution.jl package instead of writing the implementation on my own.
The docs don’t show any basic example, so I tried this code in tests as a pattern, studied the arguments one by one and tried to construct the simplest possible code to run.
I use import
instead of using
, because other packages like GeoDataFrames
redefine union
and other important funcitons, and they clash in the global scope.
The basic example I’m trying to run is: generate a vector of 20 floats and take a sum of them.
import StatsBase as sb
import Evolutionary as ev
import StableRNGs as sr
P = 100
G = 20
N = 30
result = ev.optimize(
(v1) -> -sum(v1),
() -> [rand(G) for i in 1:P],
ev.GA(
populationSize = P,
selection = (fitness) -> sortperm(fitness, rev=true)[1:N],
crossover = (v1, v2) -> sb.shuffle(collect(union(v1, v2)))[1:G],
crossoverRate = 0.8,
mutation = (v1) -> rand(G),
mutationRate = 0.06,
),
ev.Options(successive_f_tol=30)
# another variant: ev.Options(rng=sr.StableRNG(42), successive_f_tol=30)
);
As I see, somewhere deep in the code it tries to call zero
with my gene type (Vector{Float64}
), but fails.
Does anyone have a simplest example of this lib to try out?
MethodError: no method matching zero(::Type{Vector{Float64}})
Closest candidates are:
zero(::Union{Type{P}, P}) where P<:Dates.Period at /snap/julia/62/share/julia/stdlib/v1.8/Dates/src/periods.jl:53
zero(::ForwardDiff.Dual) at ~/.julia/packages/ForwardDiff/pDtsf/src/dual.jl:355
zero(::LinearAlgebra.Diagonal{T, StaticArraysCore.SVector{N, T}}) where {N, T} at ~/.julia/packages/StaticArrays/PUoe1/src/SDiagonal.jl:41
...
Stacktrace:
[1] default_values(x::Vector{Vector{Float64}})
@ Evolutionary ~/.julia/packages/Evolutionary/65hL6/src/api/utilities.jl:130
[2] Evolutionary.EvolutionaryObjective(f::var"#47#53", x::Vector{Vector{Float64}}, F::Vector{Float64}; eval::Symbol)
@ Evolutionary ~/.julia/packages/Evolutionary/65hL6/src/api/objective.jl:19
[3] optimize(f::var"#47#53", constraints::Evolutionary.NoConstraints, method::Evolutionary.GA{var"#50#56", var"#51#57", var"#52#58"}, population::Vector{Vector{Vector{Float64}}}, opts::Evolutionary.Options{Nothing, Random.TaskLocalRNG})
@ Evolutionary ~/.julia/packages/Evolutionary/65hL6/src/api/optimize.jl:54
[4] optimize(f::var"#47#53", constraints::Evolutionary.NoConstraints, individual::Function, method::Evolutionary.GA{var"#50#56", var"#51#57", var"#52#58"}, opts::Evolutionary.Options{Nothing, Random.TaskLocalRNG})
@ Evolutionary ~/.julia/packages/Evolutionary/65hL6/src/api/optimize.jl:42
[5] optimize(f::var"#47#53", individual::Function, method::Evolutionary.GA{var"#50#56", var"#51#57", var"#52#58"}, opts::Evolutionary.Options{Nothing, Random.TaskLocalRNG})
@ Evolutionary ~/.julia/packages/Evolutionary/65hL6/src/api/optimize.jl:15
[6] top-level scope
@ In[16]:6
[7] eval
@ ./boot.jl:368 [inlined]
[8] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1428