-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzzynsga2.m
201 lines (134 loc) · 4.43 KB
/
fuzzynsga2.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
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
clc;
clear;
close all;
%% Problem Definition
CostFunction=@(x) Objectives(x) %Objectives;
nVar=2; % Number of Decision Variables
VarSize=[1 nVar]; % Size of Decision Variables Matrix
VarMin=+0.2; %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=50; %100 % Maximum Number of Iterations
nPop=5; %50 % Population Size
%pCrossover=0.7; % Crossover Percentage
pCrossover=0.7;
nCrossover=2*round(pCrossover*nPop/2); % Number of Parnets (Offsprings)
%pMutation=0.4; % Mutation Percentage
pMutation=0.4;
nMutation=round(pMutation*nPop); % Number of Mutants
mu=0.05; % 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);
% Array to Hold Best Cost Values
%BestCost=zeros(It,1);
%%
% Store Worst Cost
%WorstCost=pop(end).Cost(1) + pop(end).Cost(2)
if pop(end).Cost(1) < pop(1).Cost(1)
BestCost=pop(end).Cost(1)
elseif pop(1).Cost(1) < pop(end).Cost(1)
BestCost=pop(1).Cost(1)
end
%%
%% 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);
%%
% Update Worst Cost
%WorstCost=max(WorstCost,pop(end).Cost(1)+pop(end).Cost(2))
% Update Best Cost
%BestCost=min(BestCost(it),pop(1).Cost(1)+pop(1).Cost(2))
%%
% 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});
%%
% Store Best Cost Ever Found
% BestCost(it)=pop(1).Cost(1) + pop(1).Cost(2)
%%
% Show Iteration Information
disp(['Iteration ' num2str(it) ': Number of F1 Members = ' num2str(numel(F1))]);
%% 1. Normalization of FIS Inputs
itnormalized = it / MaxIt
%for f=1: 2
% a = min (F1(1,1).Cost(1),F1(f,1).Cost(1));
%end
%a = min (a, F1(end,1).Cost(1))
% BestCostnormalized = 100 * a
if pop(end).Cost(1) < pop(1).Cost(1)
BestCost=pop(end).Cost(1)
elseif pop(1).Cost(1) < pop(end).Cost(1)
BestCost=pop(1).Cost(1)
end
BestCostnormalized2 = 200 * BestCost
if BestCostnormalized2 > 1
BestCostnormalized = 1
end
%min ( ( WorstCost - BestCost(it) ) / WorstCost )
%% 2. Read FIS file
FISMAT = readfis('Fuzzy_NSGA2_FIS.fis')
%% 3. Define Input Arguments for FIS Before Firing Rules
U = [itnormalized , BestCostnormalized]
%% 4. Fire Rules or Run Evalfis Command
Y = evalfis(U,FISMAT)
% Plot F1 Costs
%hold on;
figure(3);
PlotCosts(F1)
end
%% Results