-
Notifications
You must be signed in to change notification settings - Fork 4
/
pso.m
192 lines (135 loc) · 5.66 KB
/
pso.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
clc;
clear;
close all;
%% Problem Definition
model=CreateModel();
model.n=3; % number of Handle Points
CostFunction=@(x) MyCost(x,model); % Cost Function
nVar=model.n; % Number of Decision Variables
VarSize=[1 nVar]; % Size of Decision Variables Matrix
VarMin.x=model.xmin; % Lower limit of Variables
VarMax.x=model.xmax; % Upper limit of Variables
VarMin.y=model.ymin; % Lower limit of Variables
VarMax.y=model.ymax; % Upper limit of Variables
%% PSO Parameters
MaxIt=50; % Maximum Number of Iterations
nPop=15; % Population Size (Swarm Size)
w=1; % Inertia Weight
wdamp=0.98; % Inertia Weight Damping Ratio
c1=1.5; % Personal Learning Coefficient
c2=1.5; % Global Learning Coefficient
alpha=0.1;
VelMax.x=alpha*(VarMax.x-VarMin.x); % Maximum Velocity
VelMin.x=-VelMax.x; % Minimum Velocity
VelMax.y=alpha*(VarMax.y-VarMin.y); % Maximum Velocity
VelMin.y=-VelMax.y; % Minimum Velocity
%% Initialization
% Create Empty Particle Structure
empty_particle.Position=[];
empty_particle.Velocity=[];
empty_particle.Cost=[];
empty_particle.Sol=[];
empty_particle.Best.Position=[];
empty_particle.Best.Cost=[];
empty_particle.Best.Sol=[];
% Initialize Global Best
GlobalBest.Cost=inf;
% Create Particles Matrix
particle=repmat(empty_particle,nPop,1);
% Initialization Loop
for i=1:nPop
% Initialize Position
if i > 1
particle(i).Position=CreateRandomSolution(model);
else
% Straight line from source to destination
xx = linspace(model.xs, model.xt, model.n+2);
yy = linspace(model.ys, model.yt, model.n+2);
particle(i).Position.x = xx(2:end-1);
particle(i).Position.y = yy(2:end-1);
end
% Initialize Velocity
particle(i).Velocity.x=zeros(VarSize);
particle(i).Velocity.y=zeros(VarSize);
% Evaluation
[particle(i).Cost, particle(i).Sol]=CostFunction(particle(i).Position);
% Update Personal Best
particle(i).Best.Position=particle(i).Position;
particle(i).Best.Cost=particle(i).Cost;
particle(i).Best.Sol=particle(i).Sol;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest=particle(i).Best;
end
end
% Array to Hold Best Cost Values at Each Iteration
BestCost=zeros(MaxIt,1);
%% PSO Main Loop
for it=1:MaxIt
for i=1:nPop
% x Part
% Update Velocity
particle(i).Velocity.x = w*particle(i).Velocity.x ...
+ c1*rand(VarSize).*(particle(i).Best.Position.x-particle(i).Position.x) ...
+ c2*rand(VarSize).*(GlobalBest.Position.x-particle(i).Position.x);
% Update Velocity Bounds
particle(i).Velocity.x = max(particle(i).Velocity.x,VelMin.x);
particle(i).Velocity.x = min(particle(i).Velocity.x,VelMax.x);
% Update Position
particle(i).Position.x = particle(i).Position.x + particle(i).Velocity.x;
% Velocity Mirroring
OutOfTheRange=(particle(i).Position.x<VarMin.x | particle(i).Position.x>VarMax.x);
particle(i).Velocity.x(OutOfTheRange)=-particle(i).Velocity.x(OutOfTheRange);
% Update Position Bounds
particle(i).Position.x = max(particle(i).Position.x,VarMin.x);
particle(i).Position.x = min(particle(i).Position.x,VarMax.x);
% y Part
% Update Velocity
particle(i).Velocity.y = w*particle(i).Velocity.y ...
+ c1*rand(VarSize).*(particle(i).Best.Position.y-particle(i).Position.y) ...
+ c2*rand(VarSize).*(GlobalBest.Position.y-particle(i).Position.y);
% Update Velocity Bounds
particle(i).Velocity.y = max(particle(i).Velocity.y,VelMin.y);
particle(i).Velocity.y = min(particle(i).Velocity.y,VelMax.y);
% Update Position
particle(i).Position.y = particle(i).Position.y + particle(i).Velocity.y;
% Velocity Mirroring
OutOfTheRange=(particle(i).Position.y<VarMin.y | particle(i).Position.y>VarMax.y);
particle(i).Velocity.y(OutOfTheRange)=-particle(i).Velocity.y(OutOfTheRange);
% Update Position Bounds
particle(i).Position.y = max(particle(i).Position.y,VarMin.y);
particle(i).Position.y = min(particle(i).Position.y,VarMax.y);
% Evaluation
[particle(i).Cost, particle(i).Sol]=CostFunction(particle(i).Position);
% Update Personal Best
if particle(i).Cost<particle(i).Best.Cost
particle(i).Best.Position=particle(i).Position;
particle(i).Best.Cost=particle(i).Cost;
particle(i).Best.Sol=particle(i).Sol;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest=particle(i).Best;
end
end
end
% Update Best Cost Ever Found
BestCost(it)=GlobalBest.Cost;
w=w*wdamp;
% Show Iteration Information
if GlobalBest.Sol.IsFeasible
Flag=' @';
else
Flag=[', ' num2str(GlobalBest.Sol.Violation)];
end
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it)) Flag]);
% Plot Solution
figure(1);
PlotSolution(GlobalBest.Sol,model);
pause(0.01);
end
%% Results
figure;
plot(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;