This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assn3B5.m
101 lines (79 loc) · 2.54 KB
/
Assn3B5.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
clc;
clear;
close all;
% % Problem Definition % %
% Convergence PSO
% Set w = 0.792; c1 = c2 = 1.944
CostFunction = @CamelBackFunction; % Cost function.
nVar = 2; % Number of unknown variables.
varSize = [1 nVar]; % Size of each solution.
varMin = -5;
varMax = 5; % Min and Max for x and y (decision variables).
% % Parameters of PSO % %
maxIt = 100; % Number of iterations (stop condition).
nPop = 50; % Size of swarm.
w = 0.792; % Inertia coefficent.
c1 = 1.944; % Personal accel. coefficent.
c2 = 1.944; % Global accel. coefficent.
% % Initialization % %
% Particle template.
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];
% Create Population Array
particle = repmat(empty_particle, nPop, 1);
% Initialize global best
GlobalBest.Cost = inf; % Set to positive infinity for minimization.
BestCosts = [];
BestCosts = zeros(maxIt,1);
% Initalize Population Members
for i=1:nPop
% Generate random solution for each particle
particle(i).Position = unifrnd(varMin, varMax, varSize);
% Initalize Velocity
particle(i).Velocity = zeros(varSize);
% Evaluation of cost
particle(i).Cost = CostFunction(particle(i).Position);
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest.Cost = particle(i).Best.Cost;
GlobalBest.Position = particle(i).Best.Position;
end
end
% % Main Loop PSO % %
for it=1:maxIt
for i=1:nPop
% Velocity Equation
r1 = rand(varSize);
r2 = rand(varSize);
particle(i).Velocity = w * particle(i).Velocity ...
+ c1 * r1.*(particle(i).Best.Position - particle(i).Position) ...
+ c2 * r2.*(GlobalBest.Position - particle(i).Position);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Calculate Cost
particle(i).Cost = CostFunction(particle(i).Position);
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
end
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest.Cost = particle(i).Best.Cost;
GlobalBest.Position = particle(i).Best.Position;
end
end
% Store Best Cost Values
BestCosts(it) = GlobalBest.Cost;
end
%% Results
figure;
plot(BestCosts, 'LineWidth', 2);
title("Best Cost vs. Interation Convergence PSO")
xlabel('Iteration');
ylabel('Best Cost');
grid on;