-
Notifications
You must be signed in to change notification settings - Fork 31
/
plotHimmelblau.m
113 lines (100 loc) · 3.53 KB
/
plotHimmelblau.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
function plotHimmelblau(dataLog, iter)
% plotHimmelblau(dataLog, iter)
%
% Used to display progress as the optimization progresses
%
% dataLog(iter) = struct array with data from each iteration
% .X = [n,m] = current position of each particle
% .V = [n,m] = current "velocity" of each particle
% .F = [1,m] = value of each particle
% .X_Best = [n,m] = best point for each particle
% .F_Best = [1,m] = value of the best point for each particle
% .X_Global = [n,1] = best point ever (over all particles)
% .F_Global = [1,1] = value of the best point ever
% .I_Global = [1,1] = index of the best point ever
%
global HimmelblauContourHandle HimmelblauPopulationHandle
global HimmelblauPopBestHandle HimmelblauGlobalHandle
global HimmelblauSaveAnimation HimmelblauFrameId
figure(200); hold on;
%%%% Plot the function to be optimized
if isempty(HimmelblauContourHandle)
x = linspace(-5,5,50);
y = linspace(-5,5,50);
[XX,YY] = meshgrid(x,y);
xx = reshape(XX,1,numel(XX));
yy = reshape(YY,1,numel(YY));
zz = [xx;yy];
ff = Himmelblau(zz);
FF = reshape(ff,50,50);
HimmelblauContourHandle = contour(XX,YY,FF,15);
% Plot the solution:
plot(3.0, 2.0,'rx','LineWidth',3,'MarkerSize',20);
plot(-2.805118, 3.131312,'rx','LineWidth',3,'MarkerSize',20);
plot(-3.779310, -3.283186,'rx','LineWidth',3,'MarkerSize',20);
plot(3.584428, -1.848126,'rx','LineWidth',3,'MarkerSize',20);
end
%%%% Plot current position
if isempty(HimmelblauPopulationHandle)
x = dataLog.X(1,:); %First dimension
y = dataLog.X(2,:); %Second dimension
HimmelblauPopulationHandle = plot(x,y,'k.','MarkerSize',10);
else
x = dataLog.X(1,:); %First dimension
y = dataLog.X(2,:); %Second dimension
set(HimmelblauPopulationHandle,...
'xData',x, 'yData',y);
end
%%%% Plot best position for each particle
if isempty(HimmelblauPopBestHandle)
x = dataLog.X_Best(1,:); %First dimension
y = dataLog.X_Best(2,:); %Second dimension
HimmelblauPopBestHandle = plot(x,y,'ko','MarkerSize',5,'LineWidth',1);
else
x = dataLog.X_Best(1,:); %First dimension
y = dataLog.X_Best(2,:); %Second dimension
set(HimmelblauPopBestHandle,...
'xData',x, 'yData',y);
end
%%%% Plot best ever position of the entire swarm
if isempty(HimmelblauGlobalHandle)
x = dataLog.X_Global(1,:); %First dimension
y = dataLog.X_Global(2,:); %Second dimension
HimmelblauGlobalHandle = plot(x,y,'bo','MarkerSize',8,'LineWidth',2);
else
x = dataLog.X_Global(1,:); %First dimension
y = dataLog.X_Global(2,:); %Second dimension
set(HimmelblauGlobalHandle,...
'xData',x, 'yData',y);
end
%%%% Annotations
title(sprintf('Iteration: %d, ObjVal: %6.3g', iter, dataLog.F_Global));
xlabel('x1');
ylabel('x2');
%%%% Format the axis so things look right:
axis equal; axis(5*[-1,1,-1,1]);
%%%% Push the draw commands through the plot buffer
drawnow;
pause(0.05); %Slow down animation
%%%% Save animation if desired:
animationFileName = 'PSO_Himmelblau_Animation.gif';
if isempty(HimmelblauFrameId)
HimmelblauFrameId = 1;
else
HimmelblauFrameId = HimmelblauFrameId + 1;
end
if isempty(HimmelblauSaveAnimation)
HimmelblauSaveAnimation = false;
else
if HimmelblauSaveAnimation
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if HimmelblauFrameId == 1;
imwrite(imind,cm,animationFileName,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,animationFileName,'gif','WriteMode','append');
end
end
end
end