-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsga.m
71 lines (63 loc) · 2.09 KB
/
nsga.m
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
function [best_member, F1] = nsga(X, Y)
% parameter setting
n_feature = size(X, 2);
n_obj = 3;
n_pop = min(round(n_feature / 20), 300);
n_iter = 70;
n_division = 27;
n_mutation = n_pop;
n_crossover = n_pop;
% generate reference points
zr = reference_points(n_obj, n_division);
% inner parameters
params.n_pop = n_pop;
params.zr = zr;
params.zmin = [];
params.zmax = [];
params.smin = [];
% initialization
individual.position = [];
individual.cost = [];
pop = repmat(individual, n_pop, 1);
parfor i = 1:n_pop
position = unifrnd(0, 1, 1, n_feature) > 0.5;
pop(i).position = position;
pop(i).cost = fitness(X, Y, position);
end
% init sort
[pop, F, params] = select_pop(pop, params);
F1 = pop(F{1});
Fl = pop(F{end});
% run iterations
for iter = 1:n_iter
iter_start = clock;
% crossover
popc = crossover_pop(pop, n_crossover, X, Y);
combined_pop = [pop; popc];
% mutation
popm = mutate_pop(combined_pop, n_mutation, X, Y, Fl);
combined_pop = [combined_pop; popm];
% select the next generation
[pop, F, params] = select_pop(combined_pop, params);
F1 = pop(F{1});
Fl = pop(F{end});
% analysis
iter_end = clock;
avgfit = mean([F1.cost], 2);
logger(['iter: ', num2str(iter), '/', num2str(n_iter), ' time: ', num2str(etime(iter_end, iter_start)), 's', ...
' fit: ', num2str(avgfit(1)), ', ', num2str(avgfit(2)), ', ', num2str(avgfit(3))]);
% logger(['## accuracy = ', num2str(1 - best_member.cost(1)), ', features = ', num2str(round(n_feature * best_member.cost(2)))]);
end
best_member = solution_selection(F1);
end
function best_member = solution_selection(F1)
n_F1 = numel(F1);
best_member = F1(1);
for i = 2:n_F1
if F1(i).cost(1) < best_member.cost(1)
best_member = F1(i);
elseif F1(i).cost(1) == best_member.cost(1) && F1(i).cost(2) < best_member.cost(2)
best_member = F1(i);
end
end
end