-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcap_sampler.jl
214 lines (174 loc) · 5.73 KB
/
bcap_sampler.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
abstract type PopulationBasedSampler <: AbstractRNGSampler end
mutable struct BCAPSampler{R} <: PopulationBasedSampler
rng::R
population::Array
fitness::Vector{Float64}
mass::Vector{Float64}
population_size::Int
end
"""
BCAPSampler(searchspace;rng)
Define a iterator for the BCAP sampler.
"""
BCAPSampler(;seed = 989997112, rng=default_rng_ht(seed), population_size=0) = BCAPSampler(rng, [], zeros(0), zeros(0), population_size)
function Sampler(s::BCAPSampler, parameters::MixedSpace)
BCAPSampler(parameters;rng=s.rng, population_size=s.population_size)
end
function _init_BCAPSampler!(bcap, searchspace, rng)
_n = SearchSpaces.getdim(searchspace)
# TODO: is 200 the best upper bound?
N = clamp(round(Int, sqrt(_n)*_n), 10, 200)
bcap.population_size = N
bcap
end
function BCAPSampler(
searchspace::AtomicSearchSpace;
seed = 989997112,
rng=default_rng_ht(seed),
population_size = 0,
)
_init_BCAPSampler!(BCAPSampler(;rng, population_size), searchspace, rng)
end
function BCAPSampler(searchspace::MixedSpace;
seed = 989997112,
rng=default_rng_ht(seed),
population_size = 0,
)
ss = Dict(k => Sampler(BCAPSampler(searchspace.domain[k]; rng, population_size), searchspace.domain[k]) for k in keys(searchspace.domain))
Sampler(ss, searchspace, cardinality(searchspace))
end
function _center_worst(population, mass, rng)
mask = rand(rng, eachindex(population), 3)
m = mass[mask]
m /= sum(m)
sum(population[mask] .* m), population[argmin(m)]
end
function _fix_candidate!(x, bounds::BoxConstrainedSpace)
mask = x .< bounds.lb
x[mask] = bounds.lb[mask]
mask = x .> bounds.ub
x[mask] = bounds.ub[mask]
x
end
function _mut_candidate!(x, bounds, rng)
η = 15
# polynomial mutation
D = length(x)
mask = rand(rng, D) .< min(0.1, 1/D)
for i in findall(mask)
r = rand(rng)
if r < 0.5
σ = (2r)^(1/(η + 1)) - 1
else
σ = 1 - (2 - 2r)^(1/(η + 1))
end
x[i] = x[i] + σ * bounds.Δ[i]
end
x
end
function _bcap_candidate_real(population, mass, bounds, rng)
c, w = _center_worst(population, mass, rng)
x = rand(rng, population) + 1.2rand(rng)*(c - w)
_mut_candidate!(x, bounds, rng)
_fix_candidate!(x, bounds)
end
function _bcap_candidate(population, mass, bounds::BoxConstrainedSpace, rng)
_bcap_candidate_real(population, mass, bounds, rng)
end
function _bcap_candidate(population, mass, bounds::BoxConstrainedSpace{T}, rng) where T <: Integer
x = _bcap_candidate_real(population, mass, bounds, rng)
round.(T, x)
end
function _bcap_candidate(population, mass, booleans::BitArraySpace, rng)
d = SearchSpaces.getdim(booleans)
bounds = BoxConstrainedSpace(zeros(d), ones(d))
x = _bcap_candidate_real(population, mass, bounds, rng)
x .< 0.5
end
function _bcap_candidate(population, mass, searchspace::AtomicSearchSpace, rng)
rand(rng, searchspace)
end
function SearchSpaces.value(
sampler::Sampler{S, B}
) where {S<:BCAPSampler, B<:Union{BoxConstrainedSpace, BitArraySpace}}
bcap = sampler.method
population = bcap.population
searchspace = sampler.searchspace
# initialization at random
if length(population) < bcap.population_size
return rand(bcap.rng, searchspace)
end
v = _bcap_candidate(population, bcap.mass, sampler.searchspace, bcap.rng)
# TODO improve for numerical samples
return length(v) == 1 ? first(v) : v
end
function SearchSpaces.value(
sampler::Sampler{S, B}
) where {S<:BCAPSampler, B<:PermutationSpace}
bcap = sampler.method
population = bcap.population
searchspace = sampler.searchspace
# initialization at random
if length(population) < bcap.population_size
return rand(bcap.rng, searchspace)
end
k = SearchSpaces.getdim(searchspace)
# elements to transmit info
_mask = rand(bcap.rng, eachindex(population), 3)
# sort regarding maximum mass
mask = _mask[sortperm(bcap.mass[_mask], rev=true)]
U = population[mask]
ps = bcap.mass[mask]
ps /= sum(ps) # normalize
val = []
for (u, p) in zip(U, ps)
# inherit with maximum probability for each candidate
rand(bcap.rng) < p && (continue)
for v in (u isa Array ? u : [u])
v in val && (continue)
push!(val, v)
break
end
length(val) >= k && break
end
# complete permutation (if necessary)
while length(val) < k
vs = setdiff(searchspace.values, val)
push!(val, rand(bcap.rng, vs))
end
val = [ v for v in val]
# permutations with length=1, return the value not the array
if k == length(val) == 1
return first(val)
end
val
end
function _bca_update_mass!(bcap)
fitness = bcap.fitness
M = maximum(abs.(fitness))
bcap.mass = 2M .- fitness
bcap
end
function report_values_to_sampler!(
sampler::Sampler{R, P},
val_and_fvals::Vector{<:Tuple}
) where {R<:BCAPSampler, P <: AbstractSearchSpace}
bcap = sampler.method
_pre_proc(v) = v isa Number ? [v] : v
append!(bcap.population, _pre_proc.(first.(val_and_fvals)))
append!(bcap.fitness, last.(val_and_fvals))
# reduce population elements
population_size = bcap.population_size
if length(bcap.population) <= population_size
# nothing to remove
length(bcap.population) == population_size && _bca_update_mass!(bcap)
return
end
# replace worst from old and new
delete = sortperm(bcap.fitness)[population_size+1:end]
sort!(delete)
deleteat!(bcap.population, delete)
deleteat!(bcap.fitness, delete)
# update mass values
_bca_update_mass!(bcap)
end