/
NSGA3.jl
345 lines (257 loc) · 8.15 KB
/
NSGA3.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
mutable struct NSGA3 <: AbstractParameters
N::Int
η_cr::Float64
p_cr::Float64
η_m::Float64
p_m::Float64
partitions::Int
reference_points::Array{Vector{Float64},1}
end
"""
NSGA3(;
N = 100,
η_cr = 20,
p_cr = 0.9,
η_m = 20,
p_m = 1.0 / D,
partitions = 12,
reference_points = Vector{Float64}[],
information = Information(),
options = Options(),
)
Parameters for the metaheuristic NSGA-III.
Parameters:
- `N` Population size.
- `η_cr` η for the crossover.
- `p_cr` Crossover probability.
- `η_m` η for the mutation operator.
- `p_m` Mutation probability (1/D for D-dimensional problem by default).
- `reference_points` reference points usually generated by `gen_ref_dirs`.
- `partitions` number of Das and Dennis's reference points if `reference_points` is empty.
To use NSGA3, the output from the objective function should be a 3-touple
`(f::Vector, g::Vector, h::Vector)`, where `f` contains the objective functions,
`g` and `h` are inequality, equality constraints respectively.
A feasible solution is such that `g_i(x) ≤ 0 and h_j(x) = 0`.
```julia
using Metaheuristics
# Objective function, bounds, and the True Pareto front
f, bounds, pf = Metaheuristics.TestProblems.get_problem(:DTLZ2)
# define the parameters (use `NSGA3()` for using default parameters)
nsga3 = NSGA3(p_cr = 0.9)
# optimize
status = optimize(f, bounds, nsga3)
# show results
display(status)
```
"""
function NSGA3(;
N = 100,
η_cr = 20,
p_cr = 0.9,
η_m = 20,
p_m = -1,
partitions=12,
reference_points=Vector{Float64}[],
information = Information(),
options = Options(),
)
parameters = NSGA3(N, promote( Float64(η_cr), p_cr, η_m, p_m )..., partitions,
reference_points)
Algorithm(
parameters,
information = information,
options = options,
)
end
function update_state!(
status::State,
parameters::NSGA3,
problem::AbstractProblem,
information::Information,
options::Options,
args...;
kargs...
)
Q = reproduction(status, parameters, problem)
append!(status.population, create_solutions(Q, problem))
# non-dominated sort, elitist removing via niching
environmental_selection!(status.population, parameters)
end
function environmental_selection!(population, parameters::NSGA3)
truncate_population_nsga3!(population,parameters.reference_points,parameters.N)
end
function truncate_population_nsga3!(population, reference_points, N)
fast_non_dominated_sort!(population)
k = 1
l = 1
while l <= N
k = population[l].rank
l += 1
end
let k = k
l = findlast(sol -> sol.rank == k, population)
deleteat!(population, l+1:length(population))
end
l = findfirst(sol -> sol.rank == k, population)
niching!(population, reference_points, N, l)
end
distance_point_to_rect(s, w) = @fastmath norm(s - (dot(w,s) / dot(w, w))*w )
function associate!(nich, nich_freq, distance, F, reference_points, l)
N = length(F)
# find closest nich to corresponding solution
for i = 1:N
for j = 1:length(reference_points)
d = distance_point_to_rect(F[i], reference_points[j])
distance[i] < d && continue
distance[i] = d
nich[i] = j
end
# not associate last front
if i < l
nich_freq[nich[i]] += 1
end
end
end
function hyperplane_normalization(population)
M = length(fval(population[1]))
ideal_point = ideal(population)
nadir_point = fill(Inf, length(ideal_point))
Fx = fvals(population) .- ideal_point'
# identify extreme points
extreme_points = zeros(Int, M)
w = LinearAlgebra.I + fill(1e-6, M, M)
for i in 1:M
extreme_points[i] = argmin(nadir(Fx' ./ w[i,:]))
end
# check if intercepts can be obtained
S = Fx[extreme_points,:]
if LinearAlgebra.det(S) ≈ 0 # check if soluble matrix
nadir_point = nadir(population)
else
hyperplane = S \ ones(M)
intercepts = 1 ./ hyperplane # intercepts
nadir_point = ideal_point + intercepts
end
ideal_point, nadir_point
end
function normalize(population)
ideal_point, nadir_point = hyperplane_normalization(population)
b = nadir_point - ideal_point
# prevent division by zero
mask = b .< eps()
b[mask] .= eps()
return [ (sol.f - ideal_point) ./ b for sol in population ]
end
# get_last_front(id, population) = findall(s -> s.rank == id, population)
pick_random(itr, item) = rand(findall(i -> i == item, itr))
find_item(itr, item) = findall(i -> i == item, itr)
function niching!(population, reference_points, N, l)
if length(population) == N
return
end
F = normalize(population)
k = l
# allocate memory
nich = zeros(Int, length(population))
nich_freq = zeros(Int, length(reference_points))
available_niches = ones(Bool, length(reference_points))
distance = fill(Inf, length(population))
# associate to niches
associate!(nich, nich_freq, distance, F, reference_points, l)
# keep last front
last_front_id = k:length(population)#get_last_front(population[end].rank, population)
last_front = population[last_front_id]
deleteat!(population, last_front_id)
# last front niches information
niches_last_front = nich[last_front_id]
distance_last_front = distance[last_front_id]
# save survivors
i = 1
while k <= N
mini = minimum(view(nich_freq, available_niches))
# nich to be assigned
j_hat = pick_random(nich_freq, mini)
# candidate solutions
I_j_hat = find_item( niches_last_front, j_hat )
if isempty(I_j_hat)
available_niches[j_hat] = false
continue
end
if mini == 0
ds = view(distance_last_front, I_j_hat)
s = I_j_hat[argmin(ds)]
push!(population, last_front[s])
else
s = rand(I_j_hat)
push!(population, last_front[s])
end
nich_freq[j_hat] += 1
deleteat!(last_front, s)
deleteat!(niches_last_front, s)
deleteat!(distance_last_front, s)
k += 1
end
nothing
end
function initialize!(
status,
parameters::NSGA3,
problem::AbstractProblem,
information::Information,
options::Options,
args...;
kargs...
)
D = size(problem.bounds, 2)
if parameters.p_m < 0.0
parameters.p_m = 1.0 / D
end
if options.iterations == 0
options.iterations = 500
end
if options.f_calls_limit == 0
options.f_calls_limit = options.iterations * parameters.N + 1
end
status = gen_initial_state(problem, parameters, information, options,status)
if isempty(parameters.reference_points)
options.debug && @info "Initializing reference points..."
# number of objectives
m = length(status.population[1].f)
# initialize reference points
parameters.reference_points = gen_weights(m, parameters.partitions)
end
status
end
function final_stage!(
status::State,
parameters::NSGA3,
problem::AbstractProblem,
information::Information,
options::Options,
args...;
kargs...
)
status.final_time = time()
end
###########################################
## NSGA3 reproduction
###########################################
function reproduction(status, parameters::NSGA3, problem)
@assert !isempty(status.population)
I = randperm(parameters.N)
Q = zeros(parameters.N, size(problem.bounds, 2))
for i = 1:parameters.N ÷ 2
pa = status.population[I[2i-1]]
pb = status.population[I[2i]]
c1, c2 = GA_reproduction(get_position(pa),
get_position(pb),
problem.bounds;
η_cr = parameters.η_cr,
p_cr = parameters.p_cr,
η_m = parameters.η_m,
p_m = parameters.p_m)
Q[i,:] = c1
Q[i+1,:] = c2
end
Q
end