forked from GuilhermeCaeiro/dopt_ga_julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialization.jl
More file actions
104 lines (86 loc) · 3.71 KB
/
initialization.jl
File metadata and controls
104 lines (86 loc) · 3.71 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using StatsBase
function binary_random(environment::Environment)
population = Vector{Individual}()
for i in 1:environment.population_size
chromosome = zeros(Int64, environment.n)
indices = sample(1:environment.n, environment.s, replace=false)
chromosome[indices] .= 1
individual = Individual(environment, chromosome)
push!(population, individual)
end
return population
end
function binary_biased(environment::Environment, min_diff)
chromosomes = Vector{Vector{Int64}}()
while size(chromosomes, 1) < environment.population_size
chromosome = zeros(Int64, environment.n)
chromosome[sample(1:size(chromosome, 1), environment.s, replace = false)] .= 1
found_close = false
for existing_chromosome in chromosomes
if mean(abs.(existing_chromosome - chromosome)) < min_diff
found_close = true
break
end
end
if !found_close
push!(chromosomes, chromosome)
end
end
return [Individual(environment, chromosome) for chromosome in chromosomes]
end
function binary_biasedweighted(environment::Environment)
println("binary_biasedweighted")
chromosomes = Vector{Vector{Int64}}()
chromosome = zeros(Int64, environment.n)
chromosome[sample(1:size(chromosome, 1), environment.s, replace = false)] .= 1
push!(chromosomes, chromosome)
while size(chromosomes, 1) < environment.population_size
weights = 1 ./ (sum(chromosomes) .+ 1) # the sum() is by default row-wise, producing a Vector.
weights = weights ./ sum(weights)
chromosome = zeros(Int64, environment.n)
chromosome[sample(1:size(chromosome, 1), Weights(weights), environment.s, replace = false)] .= 1
push!(chromosomes, chromosome)
end
return [Individual(environment, chromosome) for chromosome in chromosomes]
end
function binary_guided(environment::Environment)
R = map(Int64, environment.R)
m, n = size(environment.A)
s = environment.s
U, = svd(environment.A; full=true)
x_bar = vec(sum(U .^ 2; dims = 2))
x_bar[R] .= 0.0
chromosomes = Vector{Vector{Int64}}(undef, environment.population_size)
x = zeros(m)
x[R] .= 1.0 # choose independent rows
phi = partialsortperm(x_bar, (1:s-n); rev = true)
x[phi] .= 1.0
zeros_idx = findall(x .== 0.0)
ones_idx = findall(x .== 1.0)
changes = Int(floor(m / 10))
chromosomes[1] = deepcopy(x)
for i in 2:environment.population_size
_x = deepcopy(x)
_x[sample(zeros_idx, changes, replace = false)] .= 1.0
_x[sample(ones_idx, changes, replace = false)] .= 0.0
chromosomes[i] = deepcopy(_x)
end
return [Individual(environment, chromosome) for chromosome in chromosomes]
end
function initialize_population(environment::Environment)
population = Vector{Individual}()
if environment.initialization_method == "binary_random"
population = binary_random(environment)
elseif environment.initialization_method == "binary_biased"
# initialization_params is assumed to be a float
population = binary_biased(environment, environment.initialization_params[1])
elseif environment.initialization_method == "binary_biasedweighted"
# initialization_params is assumed to be a float
population = binary_biasedweighted(environment)
elseif environment.initialization_method == "binary_guided"
population = binary_guided(environment)
else
println("Unknow initialization method ", environment.initialization_method)
end
return population
end