-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpso_clustering.m
217 lines (186 loc) · 7.76 KB
/
pso_clustering.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
% Author: Augusto Luis Ballardini
% Email: [email protected]
% Website: http://www.ira.disco.unimib.it/people/ballardini-augusto-luis/
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% Permission is granted to copy, distribute and/or modify this document
% under the terms of the GNU Free Documentation License, Version 1.3
% or any later version published by the Free Software Foundation;
% with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
% A copy of the license is included in the section entitled "GNU
% Free Documentation License".
% The following code is inspired by the following paper:
% Van Der Merwe, D. W.; Engelbrecht, AP., "Data clustering using particle swarm optimization,"
% Evolutionary Computation, 2003. CEC '03. The 2003 Congress on , vol.1, no., pp.215,220 Vol.1, 8-12 Dec. 2003
% doi: 10.1109/CEC.2003.1299577
% URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1299577&isnumber=28874
clear;
close all;
%rng('default') % For reproducibility
% INIT PARTICLE SWARM
centroids = 2; % == clusters here (aka centroids)
dimensions = 2; % how many dimensions in each centroid
particles = 20; % how many particles in the swarm, aka how many solutions
iterations = 50; % iterations of the optimization alg.
simtime=0.01; % simulation delay btw each iteration
dataset_subset = 2; % for the IRIS dataset, change this value from 0 to 2
write_video = false; % enable to grab the output picture and save a video
hybrid_pso = true; % enable/disable hybrid_pso
manual_init = false; % enable/disable manual initialization (only for dimensions={2,3})
% VIDEO GRUB STUFF...
if write_video
writerObj = VideoWriter('PSO.avi');
writerObj.Quality=100;
% writerObj.FrameRate=30;
open(writerObj);
end
% LOAD DEFAULT CLUSTER (IRIS DATASET); USE WITH CARE!
load fisheriris.mat
meas = meas(:,1+dataset_subset:dimensions+dataset_subset); %RESIZE THE DATASET WITH CURRENT DIMENSIONS; USE WITH CARE!
dataset_size = size (meas);
% EXECUTE K-MEANS
if hybrid_pso
fprintf('Running Matlab K-Means Version\n');
[idx,KMEANS_CENTROIDS] = kmeans(meas,centroids, 'dist','sqEuclidean', 'display','iter','start','uniform','onlinephase','off');
fprintf('\n');
end
% GLOBAL PARAMETERS (the paper reports this values 0.72;1.49;1.49)
w = 0.72; %INERTIA
c1 = 1.49; %COGNITIVE
c2 = 1.49; %SOCIAL
% PLOT STUFF... HANDLERS AND COLORS
pc = []; txt = [];
cluster_colors_vector = rand(particles, 3);
% PLOT DATASET
fh=figure(1);
hold on;
if dimensions == 3
plot3(meas(:,1),meas(:,2),meas(:,3),'k*');
view(3);
elseif dimensions == 2
plot(meas(:,1),meas(:,2),'k*');
end
% PLOT STUFF .. SETTING UP AXIS IN THE FIGURE
axis equal;
axis(reshape([min(meas)-2; max(meas)+2],1,[]));
hold off;
% SETTING UP PSO DATA STRUCTURES
swarm_vel = rand(centroids,dimensions,particles)*0.1;
swarm_pos = rand(centroids,dimensions,particles);
swarm_best = zeros(centroids,dimensions);
c = zeros(dataset_size(1),particles);
ranges = max(meas)-min(meas); %%scale
swarm_pos = swarm_pos .* repmat(ranges,centroids,1,particles) + repmat(min(meas),centroids,1,particles);
swarm_fitness(1:particles)=Inf;
% KMEANS_INIT
if hybrid_pso
swarm_pos(:,:,1) = KMEANS_CENTROIDS;
end
% MANUAL INITIALIZATION (only for dimension 2 and 3)
if manual_init
if dimensions == 3
% MANUAL INIT ONLY FOR THE FIRST PARTICLE
swarm_pos(:,:,1) = [6 3 4; 5 3 1];
elseif dimensions == 2
% KEYBOARD INIT ONLY FOR THE FIRST PARTICLE
swarm_pos(:,:,1) = ginput(2);
end
end
for iteration=1:iterations
%CALCULATE EUCLIDEAN DISTANCES TO ALL CENTROIDS
distances=zeros(dataset_size(1),centroids,particles);
for particle=1:particles
for centroid=1:centroids
distance=zeros(dataset_size(1),1);
for data_vector=1:dataset_size(1)
%meas(data_vector,:)
distance(data_vector,1)=norm(swarm_pos(centroid,:,particle)-meas(data_vector,:));
end
distances(:,centroid,particle)=distance;
end
end
%ASSIGN MEASURES with CLUSTERS
for particle=1:particles
[value, index] = min(distances(:,:,particle),[],2);
c(:,particle) = index;
end
% PLOT STUFF... CLEAR HANDLERS
delete(pc); delete(txt);
pc = []; txt = [];
% PLOT STUFF...
hold on;
for particle=1:particles
for centroid=1:centroids
if any(c(:,particle) == centroid)
if dimensions == 3
pc = [pc plot3(swarm_pos(centroid,1,particle),swarm_pos(centroid,2,particle),swarm_pos(centroid,3,particle),'*','color',cluster_colors_vector(particle,:))];
elseif dimensions == 2
pc = [pc plot(swarm_pos(centroid,1,particle),swarm_pos(centroid,2,particle),'*','color',cluster_colors_vector(particle,:))];
end
end
end
end
set(pc,{'MarkerSize'},{12})
hold off;
%CALCULATE GLOBAL FITNESS and LOCAL FITNESS:=swarm_fitness
average_fitness = zeros(particles,1);
for particle=1:particles
for centroid = 1 : centroids
if any(c(:,particle) == centroid)
local_fitness=mean(distances(c(:,particle)==centroid,centroid,particle));
average_fitness(particle,1) = average_fitness(particle,1) + local_fitness;
end
end
average_fitness(particle,1) = average_fitness(particle,1) / centroids;
if (average_fitness(particle,1) < swarm_fitness(particle))
swarm_fitness(particle) = average_fitness(particle,1);
swarm_best(:,:,particle) = swarm_pos(:,:,particle); %LOCAL BEST FITNESS
end
end
[global_fitness, index] = min(swarm_fitness); %GLOBAL BEST FITNESS
swarm_overall_pose = swarm_pos(:,:,index); %GLOBAL BEST POSITION
% SOME INFO ON THE COMMAND WINDOW
fprintf('%3d. global fitness is %5.4f\n',iteration,global_fitness);
%uicontrol('Style','text','Position',[40 20 180 20],'String',sprintf('Actual fitness is: %5.4f', global_fitness),'BackgroundColor',get(gcf,'Color'));
pause(simtime);
% VIDEO GRUB STUFF...
if write_video
frame = getframe(fh);
writeVideo(writerObj,frame);
end
% SAMPLE r1 AND r2 FROM UNIFORM DISTRIBUTION [0..1]
r1 = rand;
r2 = rand;
% UPDATE CLUSTER CENTROIDS
for particle=1:particles
inertia = w * swarm_vel(:,:,particle);
cognitive = c1 * r1 * (swarm_best(:,:,particle)-swarm_pos(:,:,particle));
social = c2 * r2 * (swarm_overall_pose-swarm_pos(:,:,particle));
vel = inertia+cognitive+social;
swarm_pos(:,:,particle) = swarm_pos(:,:,particle) + vel ; % UPDATE PARTICLE POSE
swarm_vel(:,:,particle) = vel; % UPDATE PARTICLE VEL
end
end
% PLOT THE ASSOCIATIONS WITH RESPECT TO THE CLUSTER
hold on;
particle=index; %select the best particle (with best fitness)
cluster_colors = ['m','g','y','b','r','c','g'];
for centroid=1:centroids
if any(c(:,particle) == centroid)
if dimensions == 3
plot3(meas(c(:,particle)==centroid,1),meas(c(:,particle)==centroid,2),meas(c(:,particle)==centroid,3),'o','color',cluster_colors(centroid));
elseif dimensions == 2
plot(meas(c(:,particle)==centroid,1),meas(c(:,particle)==centroid,2),'o','color',cluster_colors(centroid));
end
end
end
hold off;
% VIDEO GRUB STUFF...
if write_video
frame = getframe(fh);
writeVideo(writerObj,frame);
close(writerObj);
end
% SAY GOODBYE
fprintf('\nEnd, global fitness is %5.4f\n',global_fitness);