我是粒子群优化的新手。
我有一个二维图像的冲浪图,如下所示:
是否有可能随机激发粒子并使它们找到最大值(全局)?
如果是,任何人都可以提供算法或示例代码。
谢谢你。
编辑:做了一些编码并得到了答案,对于像 10-15 这样的少量迭代,粒子在最大值处聚集,但如果迭代超过 15,比如 50 等,粒子就会远离最大值。
有人可以提供解决方案吗,我已经包含了 matlab 代码和示例图像:
img = imread('gray_scale_img.jpg');
%img is grayscale
a=10; b=0.2; %a is convergence and b is convergence rate
% range=[xmin xmax ymin ymax zmin zmax];
range=[0 440 0 228 0 255]; %<<<---size of the image
Num_iterations = 15;%<<<----problem exists if more iterations
n = 30; % number of particles
best=zeros(Num_iterations,3);
% ----- Start Particle Swarm Optimization -----------
% generating the initial locations of n particles
xrange=range(2)-range(1);
yrange=range(4)-range(3);
zrange=range(6)-range(5);
xn=(rand(1,n)*xrange+range(1));
yn=(rand(1,n)*yrange+range(3));
% Start iterations
for k=1:Num_iterations,
% Show the contour of the function
surfc(double(img)); hold on;
% Find the current best location (xo,yo)
for d = 1:n
xn = ceil(xn);
yn = ceil(yn);
zn(d)=img(yn(d),xn(d));
end
zn_min=max(max(zn));
xo=max(max(xn(zn==zn_min)));
yo=max(max(yn(zn==zn_min)));
zo=max(max(zn(zn==zn_min)));
% Trace the paths of all roaming particles
% Display these roaming particles
plot3(xn,yn,zn,'.',xo,yo,zo,'rp'); axis(range);
% Move all the particles to new locations
nn=size(yn,2); %a=alpha, b=beta
xn=xn.*(1-b)+xo.*b+a.*(rand(1,nn)-0.5);
yn=yn.*(1-b)+yo.*b+a.*(rand(1,nn)-0.5);
%zn=zn.*(1-b)+zo.*b+a.*(rand(1,nn)-0.5);
%To make sure the particles are well within the range
nn=length(yn);
for l=1:nn,
if xn(l)<=range(1), xn(l)=range(1); end
if xn(l)>=range(2), xn(l)=range(2); end
if yn(l)<=range(3), yn(l)=range(3); end
if yn(l)>=range(4), yn(l)=range(4); end
%if zn(l)<=range(5), yn(l)=range(5); end
%if zn(l)>=range(6), yn(l)=range(6); end
end
drawnow;
hold off;
best(k,1)=xo; best(k,2)=yo; best(k,3)=zo;pause(0.2);
end %%%%% end of iterations
x_best = best(k,1);
y_best = best(k,2);
z_best = best(k,3);
global_maxima_coordinate =[x_best,y_best]
figure(3);
imshow(A,[]);
hold on
plot(x_best, y_best, 'bx')
% Draw circle of required radius around the global maxima
theta = 0:pi/50:2*pi;
radius_1 = 15;
xunit_circle_1 = radius_1 * cos(theta) + x_best;
yunit_circle_1 = radius_1 * sin(theta) + y_best;
h_1 = plot(xunit_circle_1, yunit_circle_1,'r');
hold off
title('Global maxima');