-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossover.jl
62 lines (59 loc) · 1.61 KB
/
crossover.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
include("class.jl")
function one_point_crossover(solver::ga, selected)
rng = MersenneTwisters.MT19937()
for s in selected
ia, ib = solver.population[s[1]], solver.population[s[2]]
ω = rand()
if ω < solver.cx
oa, ob = [], []
p = rand(rng, 1:length(ia))
append!(oa, ia[1:p]), append!(oa, ib[p+1:length(ib)])
append!(ob, ib[1:p]), append!(ob, ia[p+1:length(ia)])
else
oa, ob = ia, ib
end
push!(solver.next_population, oa), push!(solver.next_population, ob)
end
end
function two_point_crossover(solver::ga, selected)
rng = MersenneTwisters.MT19937()
for s in selected
ia, ib = solver.population[s[1]], solver.population[s[2]]
ω = rand()
if ω < solver.cx
oa, ob = [], []
px, py = sort!(rand(rng, 1:length(ia), 2))
append!(oa, ia[1:px-1]), append!(oa, ib[px:py]), append!(oa, ia[py+1:length(ia)])
append!(ob, ib[1:px-1]), append!(ob, ia[px:py]), append!(ob, ib[py+1:length(ib)])
else
oa, ob = ia, ib
end
push!(solver.next_population, oa), push!(solver.next_population, ob)
end
end
function blx(solver::ga, selected)
α = 0.5 # standard
for s in selected
ia, ib = solver.population[s[1]], solver.population[s[2]]
ω = rand()
if ω < solver.cx
oa, ob = [], []
for i in 1:length(ia)
Δ = abs(ia[i] - ib[i])
lb = max(solver.lb, min(ia[i], ib[i]) - α*Δ)
ub = min(solver.ub, max(ia[i], ib[i]) + α*Δ)
if Δ != 0
d = Uniform(lb, ub)
x, y = rand(d), rand(d)
else
x, y = lb, lb
end
append!(oa, x)
append!(ob, y)
end
else
oa, ob = ia, ib
end
push!(solver.next_population, oa), push!(solver.next_population, ob)
end
end