Skip to content

Commit 3220985

Browse files
committed
(#3) Introduce biased uniform cross-over operator for BRKGA
1 parent e1172ce commit 3220985

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/randomkeyga.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ function onepoint_crossover(ch::Chromosome, ch2::Chromosome)::Chromosome
3939
return Chromosome(vals1)
4040
end
4141

42+
function uniform_crossover(elite::Chromosome, other::Chromosome; alpha::Float64 = 0.7)::Chromosome
43+
L = length(elite.values)
44+
vals1 = [rand() < alpha ? elite.values[i] : other.values[i] for i in 1:L]
45+
return Chromosome(vals1)
46+
end
47+
48+
function create_mutant(len::Int)::Chromosome
49+
return Chromosome(rand(len), Inf64)
50+
end
51+
52+
4253
function random_mutation(ch::Chromosome)::Chromosome
4354
luckyindex = rand(1:length(ch.values))
4455
newch = Chromosome(copy(ch.values))

test/testrandomkeyga.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
@testset "Random Key Genetic Algorithm" verbose = true begin
22

3+
@testset "Uniform Biased Crossover" begin
4+
elite = Chromosome([0.1, 0.2, 0.3, 0.4, 0.5])
5+
other = Chromosome([0.5, 0.4, 0.3, 0.2, 0.1])
6+
alpha = 0.7
7+
8+
offspring = OperationsResearchModels.RandomKeyGA.uniform_crossover(elite, other; alpha)
9+
10+
@test length(offspring.values) == length(elite.values)
11+
for i in 1:length(elite.values)
12+
@test offspring.values[i] == elite.values[i] || offspring.values[i] == other.values[i]
13+
end
14+
end
15+
16+
@testset "Create Mutant" begin
17+
len = 5
18+
mutant = OperationsResearchModels.RandomKeyGA.create_mutant(len)
19+
20+
@test length(mutant.values) == len
21+
for val in mutant.values
22+
@test 0.0 <= val <= 1.0
23+
end
24+
end
25+
326
@testset "Example with chsize = 5" begin
427

528
# Correct order is 1 2 3 4 5

0 commit comments

Comments
 (0)