Skip to content

Selections refactor #41

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Evolutionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using Random
discrete, waverage, intermediate, line,
pmx, ox1, cx, ox2, pos,
# GA selections
ranklinear, uniformranking, roulette, sus, tournament, truncation,
ranklinear, rankuniform, roulette, sus, tournament, truncation,
# Optimization methods
es, cmaes, ga

Expand Down
65 changes: 38 additions & 27 deletions src/selections.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# GA seclections
# GA selections
# ==============

# Rank-based fitness assignment
Expand All @@ -7,29 +7,29 @@ function ranklinear(sp::Float64)
@assert 1.0 <= sp <= 2.0 "Selective pressure has to be in range [1.0, 2.0]."
function rank(fitness::Vector{<:Real}, N::Int)
λ = length(fitness)
idx = sortperm(fitness)
ranks = zeros(λ)
rank = sortperm(fitness)

prob = Vector{Float64}(undef, λ)
for i in 1:λ
ranks[i] = ( 2 - sp + 2*(sp - 1)*(idx[i] - 1) / (λ - 1) ) / λ
prob[i] = ( 2.0- sp + 2.0*(sp - 1.0)*(rank[i] - 1.0) / (λ - 1.0) ) / λ
end
return pselection(ranks, N)

return pselection(prob, N)
end
return rank
end

# (μ, λ)-uniform ranking selection
function uniformranking(μ::Int)
function uniformrank(fitness::Vector{<:Real}, N::Int)
λ = length(fitness)
idx = sortperm(fitness, rev=true)
@assert μ < λ "μ should be less then $(λ)"
ranks = similar(fitness, Float64)
for i in 1:μ
ranks[idx[i]] = 1/μ
end
return pselection(ranks, N)
end
return uniformrank
function rankuniform(fitness::Vector{<:Real}, N::Int)
μ = N
λ = length(fitness)
@assert μ <= λ "μ must be ≤ λ = $(λ)"

prob = zeros(λ)
rank = sortperm(fitness, rev=true)
prob[rank[1:μ]] .= 1/μ

return pselection(prob, N)
end

# Roulette wheel (proportionate selection) selection
Expand All @@ -40,11 +40,13 @@ end

# Stochastic universal sampling (SUS)
function sus(fitness::Vector{<:Real}, N::Int)
selected = Vector{Int}(undef, N)

F = sum(fitness)
P = F/N

start = P*rand()
pointers = [start+P*i for i = 0:(N-1)]
selected = Array{Int}(undef, N)
i = c = 1
for P in pointers
while sum(fitness[1:i]) < P
Expand All @@ -53,6 +55,7 @@ function sus(fitness::Vector{<:Real}, N::Int)
selected[c] = i
c += 1
end

return selected
end

Expand All @@ -65,10 +68,10 @@ function truncation(fitness::Vector{<:Real}, N::Int)
end

# Tournament selection
function tournament(groupSize :: Int)
function tournament(groupSize::Int)
@assert groupSize > 0 "Group size must be positive"
function tournamentN(fitness::Vector{<:Real}, N::Int)
selection = fill(0,N)
selection = Vector{Int}(undef, N)

nFitness = length(fitness)

Expand Down Expand Up @@ -98,16 +101,24 @@ end

# Utils: selection
function pselection(prob::Vector{<:Real}, N::Int)
selected = Vector{Int}(undef, N)

cp = cumsum(prob)
selected = Array{Int}(undef, N)
@assert cp[end] ≈ 1 "Sum of probability vector must equal 1"

for i in 1:N
j = 1
r = rand()
while cp[j] < r
j += 1
end
selected[i] = j
selected[i] = vlookup(cp, rand())
end
return selected
end

# Utils: vlookup
function vlookup(range::Vector{<:Number}, value::Number)
for i in eachindex(range)
if range[i] >= value
return i
end
end

return -1
end
2 changes: 1 addition & 1 deletion test/rastrigin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
test_result(result, fitness, N, 1e-1)

# Testing: GA
selections = [roulette, sus, ranklinear(1.5)]
selections = [roulette, sus, ranklinear(1.5), rankuniform, tournament(4)]
crossovers = [discrete, intermediate(0.), intermediate(0.25), line(0.2)]
mutations = [domainrange(fill(0.5,N)), domainrange(fill(1.0,N))]

Expand Down
7 changes: 3 additions & 4 deletions test/selections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
end

@testset "Uniform" begin
s = uniformranking(2)
@test sort(unique(s([1.0,2.0,3.0], 10))) == [2,3]
@test sort(unique(s([5,2,3], 5))) == [1,3]
@test_throws AssertionError s([1.,2.], 2)
@test sort(unique(rankuniform([1.0,2.0,3.0], 2))) == [2,3]
@test sort(unique(rankuniform([1,2,3], 2))) == [2,3]
@test_throws AssertionError rankuniform([1.,2.], 3)
end

@testset "Roulette" begin
Expand Down