Skip to content

Commit 276a278

Browse files
committed
Simplify uniformranking
1 parent f5d0512 commit 276a278

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/Evolutionary.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using Random
1212
discrete, waverage, intermediate, line,
1313
pmx, ox1, cx, ox2, pos,
1414
# GA selections
15-
ranklinear, uniformranking, roulette, sus, tournament, truncation,
15+
ranklinear, rankuniform, roulette, sus, tournament, truncation,
1616
# Optimization methods
1717
es, cmaes, ga
1818

src/selections.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function ranklinear(sp::Float64)
88
function rank(fitness::Vector{<:Real}, N::Int)
99
λ = length(fitness)
1010
rank = sortperm(fitness)
11-
11+
1212
prob = Vector{Float64}(undef, λ)
1313
for i in 1:λ
1414
prob[i] = ( 2.0- sp + 2.0*(sp - 1.0)*(rank[i] - 1.0) /- 1.0) ) / λ
@@ -20,15 +20,16 @@ function ranklinear(sp::Float64)
2020
end
2121

2222
# (μ, λ)-uniform ranking selection
23-
function uniformranking::Int)
24-
function uniformrank(fitness::Vector{<:Real}, N::Int)
25-
λ = length(fitness)
26-
@assert μ < λ "μ should equal $(λ)"
23+
function rankuniform(fitness::Vector{<:Real}, N::Int)
24+
μ = N
25+
λ = length(fitness)
26+
@assert μ <= λ "μ must be ≤ λ = $(λ)"
27+
28+
prob = zeros(λ)
29+
rank = sortperm(fitness, rev=true)
30+
prob[rank[1:μ]] .= 1/μ
2731

28-
prob = fill(1/μ, μ)
29-
return pselection(prob, N)
30-
end
31-
return uniformrank
32+
return pselection(prob, N)
3233
end
3334

3435
# Roulette wheel (proportionate selection) selection

test/selections.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
@testset "Uniform" begin
1414
Random.seed!(2);
15-
s = uniformranking(2)
16-
@test s([1.0,2.0,3.0], 2) == [2,3]
17-
@test s([1,2,3], 2) == [2,3]
18-
@test_throws AssertionError s([1.,2.], 2)
15+
@test rankuniform([1.0,2.0,3.0], 2) == [2,3]
16+
@test rankuniform([1,2,3], 2) == [2,3]
17+
@test_throws AssertionError rankuniform([1.,2.], 3)
1918
end
2019

2120
@testset "Roulette" begin

0 commit comments

Comments
 (0)