-
Notifications
You must be signed in to change notification settings - Fork 1
/
nsga2.m
147 lines (87 loc) · 3.05 KB
/
nsga2.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
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
clc;
clear;
close all;
%% Problem Definition
CostFunction=@(x) Objectives(x) %Objectives;
nVar=3; %1 % Number of Decision Variables
VarSize=[1 nVar]; % Size of Decision Variables Matrix
VarMin=1.5; %1.5 %az -5 % Lower Bound of Variables
VarMax=2; %5 % Upper Bound of Variables
% Number of Objective Functions
nObj=numel(CostFunction(unifrnd(VarMin,VarMax,VarSize)));
%% NSGA-II Parameters
MaxIt=100 %50; %100 % Maximum Number of Iterations
nPop=5 %50 % Population Size
pCrossover=0.4; %0.7 % Crossover Percentage
nCrossover=2*round(pCrossover*nPop/2); % Number of Parnets (Offsprings)
pMutation=0.7; %0.4 % Mutation Percentage
nMutation=round(pMutation*nPop); % Number of Mutants
mu=0.02; %0.02 % Mutation Rate
sigma=0.1*(VarMax-VarMin); % Mutation Step Size
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
empty_individual.Rank=[];
empty_individual.DominationSet=[];
empty_individual.DominatedCount=[];
empty_individual.CrowdingDistance=[];
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
pop(i).Cost=CostFunction(pop(i).Position);
end
% Non-Dominated Sorting
[pop F]=NonDominatedSorting(pop);
% Calculate Crowding Distance
pop=CalcCrowdingDistance(pop,F);
% Sort Population
[pop F]=SortPopulation(pop);
%% NSGA-II Main Loop
for it=1:MaxIt
% Crossover
popc=repmat(empty_individual,nCrossover/2,2);
for k=1:nCrossover/2
i1=randi([1 nPop]);
p1=pop(i1);
i2=randi([1 nPop]);
p2=pop(i2);
[popc(k,1).Position popc(k,2).Position]=Crossover(p1.Position,p2.Position);
popc(k,1).Cost=CostFunction(popc(k,1).Position);
popc(k,2).Cost=CostFunction(popc(k,2).Position);
end
popc=popc(:);
% Mutation
popm=repmat(empty_individual,nMutation,1);
for k=1:nMutation
i=randi([1 nPop]);
p=pop(i);
popm(k).Position=Mutate(p.Position,mu,sigma);
popm(k).Cost=CostFunction(popm(k).Position);
end
% Merge
pop=[pop
popc
popm];
% Non-Dominated Sorting
[pop F]=NonDominatedSorting(pop);
% Calculate Crowding Distance
pop=CalcCrowdingDistance(pop,F);
% Sort Population
[pop F]=SortPopulation(pop);
% Truncate
pop=pop(1:nPop);
% Non-Dominated Sorting
[pop F]=NonDominatedSorting(pop);
% Calculate Crowding Distance
pop=CalcCrowdingDistance(pop,F);
% Sort Population
[pop F]=SortPopulation(pop);
% Store F1
F1=pop(F{1});
% Show Iteration Information
disp(['Iteration ' num2str(it) ': Number of F1 Members = ' num2str(numel(F1))]);
% Plot F1 Costs
figure(1);
PlotCosts(F1);
end
%% Results