Skip to content

Commit

Permalink
ES: set default recombinations to average & fix mutation params
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed May 31, 2023
1 parent 0f2da1a commit b388c2c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/api/types.jl
Expand Up @@ -171,7 +171,10 @@ abstract type AbstractStrategy end

"""Empty evolution strategy"""
struct NoStrategy <: AbstractStrategy end
copy(s::NoStrategy) = NoStrategy()
copy(::NoStrategy) = NoStrategy()
Base.getproperty(s::NoStrategy, ::Symbol) = 0.0
Base.setproperty!(s::NoStrategy, ::Symbol, ::T) where {T<:Real} = 0.0


"""
Isotropic evolution strategy
Expand Down
8 changes: 4 additions & 4 deletions src/es.jl
Expand Up @@ -4,8 +4,8 @@ Implementation of Evolution Strategy: (μ/ρ(+/,)λ)-ES
The constructor takes following keyword arguments:
- `initStrategy`: an initial strategy description, (default: empty)
- `recombination`: ES recombination function for population (default: `first`), see [Crossover](@ref)
- `srecombination`: ES recombination function for strategies (default: `first`), see [Crossover](@ref)
- `recombination`: ES recombination function for population (default: [`average`](@ref)), see [Crossover](@ref)
- `srecombination`: ES recombination function for strategies (default: [`average`](@ref)), see [Crossover](@ref)
- `mutation`: [Mutation](@ref) function for population (default: [`nop`](@ref))
- `smutation`: [Mutation](@ref) function for strategies (default: [`nop`](@ref))
- `μ`/`mu`: the number of parents
Expand All @@ -27,8 +27,8 @@ struct ES{T1,T2,T3,T4} <: AbstractOptimizer
metrics::ConvergenceMetrics

ES(; initStrategy::AbstractStrategy = NoStrategy(),
recombination::T1 = first,
srecombination::T2 = first,
recombination::T1 = average,
srecombination::T2 = average,
mutation::T3 = nop,
smutation::T4 = nop,
μ::Integer = 1,
Expand Down
22 changes: 11 additions & 11 deletions src/mutations.jl
Expand Up @@ -5,14 +5,14 @@
# ======================

"""
nop(s::AbstractStrategy)
nop(x, s::AbstractStrategy; kwargs...)
This is a dummy mutation operator that does not change the strategy.
This is a dummy mutation operator of the recombinant `x` that does not change the strategy `s`.
"""
nop(recombinant::AbstractVector, s::AbstractStrategy; kwargs...) = recombinant

"""
gaussian(x, s::IsotropicStrategy)
gaussian(x, s::IsotropicStrategy; rng=Random.default_rng())
Performs Gaussian isotropic mutation of the recombinant `x` given the strategy `s` by adding Gaussian noise as follows:
Expand All @@ -27,7 +27,7 @@ function gaussian(recombinant::AbstractVector, s::IsotropicStrategy;
end

"""
gaussian(x, s::AnisotropicStrategy)
gaussian(x, s::AnisotropicStrategy; rng=Random.default_rng())
Performs Gaussian anisotropic mutation of the recombinant `x` given the strategy `s` by adding Gaussian noise as follows:
Expand All @@ -43,7 +43,7 @@ function gaussian(recombinant::AbstractVector, s::AnisotropicStrategy;
end

"""
cauchy(x, s::IsotropicStrategy)
cauchy(x, s::IsotropicStrategy; rng=Random.default_rng())
Performs isotropic mutation of the recombinant `x` given the strategy `s` by adding a noise from the Cauchy distribution as follows:
Expand All @@ -65,15 +65,15 @@ end
# ===========================

"""
nop(s::AbstractStrategy)
nop(s::AbstractStrategy; kwargs...)
This is a dummy operator that does not change strategy.
"""
nop(s::AbstractStrategy; kwargs...) = s


"""
gaussian(s::IsotropicStrategy)
gaussian(s::IsotropicStrategy; rng=Random.default_rng())
Performs in-place mutation of the isotropic strategy `s` modifying its mutated strategy parameter ``\\sigma`` with Gaussian noise as follows:
Expand All @@ -87,7 +87,7 @@ end


"""
gaussian(s::AnisotropicStrategy)
gaussian(s::AnisotropicStrategy; rng=Random.default_rng())
Performs in-place mutation of the anisotropic strategy `s` modifying its mutated strategy parameter ``\\sigma`` with Gaussian noise as follows:
Expand All @@ -107,14 +107,14 @@ end
# ---------------------

"""
genop(recombinant)
genop(recombinant; kwargs...)
This is a dummy mutation operator that does not change the `recombinant`.
"""
genop(recombinant; kwargs...) = recombinant

"""
flip(recombinant)
flip(recombinant; rng=Random.default_rng())
Returns an in-place mutated binary `recombinant` with a bit flips at random positions.
"""
Expand All @@ -131,7 +131,7 @@ end
Returns an in-place mutated binary `recombinant` with its bits inverted.
"""
bitinversion(recombinant::T) where {T <: AbstractVector{Bool}} = map!(!, recombinant, recombinant)
bitinversion(recombinant::T; kwargs...) where {T <: AbstractVector{Bool}} = map!(!, recombinant, recombinant)


# Real-valued mutations
Expand Down
6 changes: 3 additions & 3 deletions src/recombinations.jl
@@ -1,7 +1,7 @@
# Recombinations
# ==============
"""
average(population)
average(population; kwargs...)
Returns an *one* offspring individual of a multi-parent recombination by averaging `population`.
"""
Expand All @@ -15,7 +15,7 @@ function average(population::Vector{T}; kwargs...) where {T <: AbstractVector}
end

"""
marriage(population)
marriage(population; rng=Radom.default_rng())
Returns an *one* offspring individual of a multi-parent recombination by random copying from `population`.
"""
Expand All @@ -38,7 +38,7 @@ end
Returns the average value of the mutation parameter ``\\sigma`` of strategies `ss`.
"""
function average(ss::Vector{<:AbstractStrategy}; kwargs...)
function average(ss::Vector{<:AbstractStrategy})
s = copy(first(ss))
l = length(ss)
s.σ = mapreduce(s->s.σ/l, +, ss)
Expand Down
6 changes: 6 additions & 0 deletions test/issues.jl
@@ -0,0 +1,6 @@
@testset "Issues" begin
# issue #105
res = Evolutionary.optimize(sum, ones(5), ES= 40, λ = 100))
@test minimum(res) == 5

end
3 changes: 2 additions & 1 deletion test/runtests.jl
Expand Up @@ -24,7 +24,8 @@ for tests in [
"onemax.jl",
"moea.jl",
"regression.jl",
"gp.jl"
"gp.jl",
"issues.jl"
]
include(tests)
end

0 comments on commit b388c2c

Please sign in to comment.