-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticalSwarmOptimization.m
187 lines (172 loc) · 7.11 KB
/
ParticalSwarmOptimization.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
function [bestGlobalPos, CurrentEps] = ParticalSwarmOptimization(RulesCount, FactorDim, InitCount, InertionCoef, MemoryCoef, CoopCoef, Eps, MaxIter, InitialConditions, ModelParameters, IrisRules, IrisFactors)
% RulesCount - êîëè÷åñòâî êëàññîâ
% FactorDim - êîëè÷åñòâî ïðèçíàêîâ êëàññà
% InitCount - êîëè÷åñòâî ÷àñòèö â ðîå
% InertionCoef - êîýôèöèåíò èíåðöèè ÷àñòèö (W)
% MemoryCoef - êîýôèöèåíò ïàìÿòè (C1)
% CoopCoef - ôàêòîð ñîòðóäíè÷åñòâà (Ñ2)
% Eps - íåîáõîäèìàÿ òî÷íîñòü ðåøåíèÿ
% MaxIter - ìàêñèìàëüíîå êîëè÷åñòâî èòåðàöèé
% InitialConditions - Íà÷àëüíîå ðàñïðåäåëåíèå ÷àñòèö ëèáî ïóñòîå, ëèáî íåò
% ModelParameters - Ïàðàìåòðû ìîäåëè W=<a, c, b> äëÿ 1îé ÷àñòèöû
% IrisRules - êëàññû i ñòðîêà ñîîòâåòñòâóåò i ñòðîêå â IrisFactors
% IrisFactors - ïðèçíàêè
TaskDimension = RulesCount*(2*FactorDim+1);% 3*(2*4+1) = 27
oldSwarmPosition = zeros(InitCount, TaskDimension);%ïðåäûäóùåå ïîëîæåíèå ÷àñòèö â ðîå
newSwarmPosition = zeros(InitCount, TaskDimension);%òåêóùåå ïîëîæåíèå ÷àñòèö â ðîå
%çàäàåì íà÷àëüíîå ðàñïðåäåëåíèå
if isempty(InitialConditions)
%îäíà ÷àñòèöà ýòî W=<a, c, b>
oldSwarmPosition(1,:) = ModelParameters;
%äðóãèå ñëó÷àéíî, êàæäàÿ êîîðäèíàòà îò 0 äî 1.
oldSwarmPosition(2:InitCount,:) = rand(InitCount-1, TaskDimension);
else
oldSwarmPosition = InitialConditions;
end;
%íà÷àëüíàÿ ñêîðîñòü íóëåâàÿ
oldV = zeros(InitCount, TaskDimension);
%ëó÷øåå ïîëîæåíèå êàæäîé ÷àñòèöû
bestIndividPos = oldSwarmPosition;
%ëó÷øåå ïîëîæåíèÿ ðîÿ
bestGlobalPos = FindBestGlobalPosition(InitCount, oldSwarmPosition, IrisFactors, IrisRules);
[A_par, C_par, B_par] = DecodeParameters(bestGlobalPos);
CurrentEps = Verification(IrisFactors, IrisRules, A_par, C_par, B_par);
i = 0;
while i <= MaxIter && CurrentEps > Eps
for j = 1:InitCount
%îáíîâëÿåì ñêîðîñòü
newV(j,:) = CalculateVelocity(oldV(j,:), oldSwarmPosition(j, :), bestIndividPos(j,:), bestGlobalPos, InertionCoef, MemoryCoef, CoopCoef);
%îáíîâëÿåì ïîëîæåíèå
newSwarmPosition(j,:) = oldSwarmPosition(j, :) + newV(j,:);
%Íóæíî ëè îáíîâèòü íàèëó÷øåå ïîëîæåíèå ÷àñòèöû?
[bestPos, whichBetter] = ChooseBestPosition(IrisFactors, IrisRules, newSwarmPosition(j,:), bestIndividPos(j,:));
if whichBetter == 1
bestIndividPos(j,:) = bestPos;
%íóæíî ëè îáíîâèòü íàèëó÷øåå ïîëîæåíèå ðîÿ?
[bestPos, whichGlobal] = ChooseBestPosition(IrisFactors, IrisRules, bestIndividPos(j,:), bestGlobalPos);
if whichGlobal == 1
bestGlobalPos = bestPos;
%Ïîäñ÷åò òî÷íîñòè ñ íîâûì ëó÷øèì ïîëîæåíèåì
[A_par, C_par, B_par] = DecodeParameters(bestGlobalPos);
CurrentEps = Verification(IrisFactors, IrisRules, A_par, C_par, B_par);
end
end
end
oldSwarmPosition = newSwarmPosition;
oldV = newV;
i=i+1;
end;
end
function [newVelocity] = CalculateVelocity(currentVel, currentPos, BestIP, BestGP, InertionCoef, MemoryCoef, CoopCoef)
% currentVel - òåêóùàÿ ñêîðîñòü ÷àñòèöû (1xd), N-êîëè÷åñòâî ÷àñòèö, d-ðàçìåðíîñòü ïðîñòðàíñòâà
% currentPos - òåêóùàÿ ïîçèöèÿ ÷àñòèöû (1xd)
% BestIP - òåêóùåå ëó÷øåå ïîëîæåíèå äëÿ ÷àñòèöû (1xd)
% BestGP - òåêóùåå ëó÷øåå ãëîáàëüíîå ïîëîæåíèå (1õd)
newVelocity = InertionCoef*currentVel;
newVelocity = newVelocity + MemoryCoef*rand(size(currentVel)).*(BestIP - currentPos);
newVelocity = newVelocity + CoopCoef*rand(size(currentVel)).*(BestGP - currentPos);
end
%Ôóíêöèÿ ïðèíàäëåæíîñòè ìþ
function [answer] = MuGauss(X, C, A)
answer = exp(-(X-C).^2./(2*A));
end
%Ôóíêöèîíàë ýìïèðè÷åñêîãî ðèñêà
function [answer] = EmpirikRiskFunction(IrisFactors, IrisRules, A_par, C_par, B_par)
% IrisFactors - õàðàêòåðèñòèêè èðèñîâ
% IrisRules - òèï èðèñà i ñòðîêà ñîîòâåòñòâóåò i-îé ñòðîêå â IrisFactors
% A_par - A_ik
% C_par - Öåíòðû
% B_par - B_0k
K = 3;
Norm = zeros(length(IrisRules),1);
for j = 1:length(IrisRules)
Alphas = zeros(K,1);
for i=1:K
%prod(A) - ïðîèçâåäåíèå âñåõ ýëåìåíòîâ ìàññèâà À
Alphas(i) = prod(MuGauss(IrisFactors(j,:), C_par(i,:), A_par(i,:)));
end
Alphas_dot_B = Alphas.*B_par;
response = sum(Alphas_dot_B)/sum(Alphas);
Norm(j) = (response - IrisRules(j))^2;
end
answer = sum(Norm)/length(Norm);
end
function [A_par, C_par, B_par] = DecodeParameters(currentPartPos)
A_par = zeros(3, 4);
C_par = zeros(3, 4);
B_par = zeros(3, 1);
A_par(1,:) = currentPartPos(1:4);
A_par(2,:) = currentPartPos(5:8);
A_par(3,:) = currentPartPos(9:12);
C_par(1,:) = currentPartPos(13:16);
C_par(2,:) = currentPartPos(17:20);
C_par(3,:) = currentPartPos(21:24);
B_par(1:3) = currentPartPos(25:27);
end
%ïîèñê íàèëó÷øåãî ïîëîæåíèÿ ðîÿ
function [bestGlobalPos] = FindBestGlobalPosition(InitCount, SwarmPosition, IrisFactors, IrisRules)
currentPartPos = SwarmPosition(1,:);
%Äåêîäèðîâàíèå ïàðàìåòðîâ
[A_par, C_par, B_par] = DecodeParameters(currentPartPos);
%ïîäñ÷åò ôóíêöèè ýìïåðè÷åñêîãî ðèñêà è çíà÷åíèÿ äëÿ òåêóùåãî ëó÷øåãî
%ïîëîæåíèÿ
bestGlobalValue = EmpirikRiskFunction(IrisFactors, IrisRules, A_par, C_par, B_par);
bestGlobalPos = currentPartPos;
for i = 2:InitCount
currentPartPos = SwarmPosition(i,:);
%Äåêîäèðîâàíèå ïàðàìåòðîâ
[A_par, C_par, B_par] = DecodeParameters(currentPartPos);
%çíà÷åíèå ôóíêöèè ýìïåðè÷åñêîãî ðèñêà äëÿ ýòîé ÷àñòèöû
currentPartValue = EmpirikRiskFunction(IrisFactors, IrisRules, A_par, C_par, B_par);
%åñëè çíà÷åíèå ëó÷øå, òî ñ÷èòàåì ïîëîæåíèå ýòîé ÷àñòèöû - ëó÷øèì
%ïîëîæåíèåì ðîÿ
if currentPartValue < bestGlobalValue
bestGlobalPos = currentPartPos;
end
end
end
%âûáîð íàèëó÷øåé ÷àñòèöû èç äâóõ
function [bestPos, type] = ChooseBestPosition(IrisFactors, IrisRules, firstPart, secondPart)
currentPartPos = firstPart;
%Äåêîäèðîâàíèå ïàðàìåòðîâ
[A_par, C_par, B_par] = DecodeParameters(currentPartPos);
firstValue = EmpirikRiskFunction(IrisFactors, IrisRules, A_par, C_par, B_par);
currentPartPos = secondPart;
%Äåêîäèðîâàíèå ïàðàìåòðîâ
[A_par, C_par, B_par] = DecodeParameters(currentPartPos);
secondValue = EmpirikRiskFunction(IrisFactors, IrisRules, A_par, C_par, B_par);
if firstValue < secondValue
bestPos = firstPart;
type = 1;
else
bestPos = secondPart;
type = 2;
end
end
%Âåðèôèêàöèÿ
function [answer] = Verification(IrisFactors, IrisRules, A_par, C_par, B_par)
% IrisFactors - õàðàêòåðèñòèêè èðèñîâ
% IrisRules - òèï èðèñà i ñòðîêà ñîîòâåòñòâóåò i-îé ñòðîêå â IrisFactors
% A_par - A_ik
% C_par - Öåíòðû
% B_par - B_0k
K = 3;
Norm = zeros(length(IrisRules),1);
response = zeros(length(IrisRules),1);
for j = 1:length(IrisRules)
Alphas = zeros(K,1);
for i=1:K
%prod(A) - ïðîèçâåäåíèå âñåõ ýëåìåíòîâ ìàññèâà À
Alphas(i) = prod(MuGauss(IrisFactors(j,:), C_par(i,:), A_par(i,:)));
end
Alphas_dot_B = Alphas.*B_par;
response(j) = sum(Alphas_dot_B)/sum(Alphas);
end
CorrectAnswers = 0;
for i = 1:length(IrisRules)
if abs(response(i) - IrisRules(i))< 0.5
CorrectAnswers = CorrectAnswers + 1;
end
end
answer = CorrectAnswers/length(IrisRules);
end