can anyone point out the error in this code ......

Status
Not open for further replies.

Noor Us Sabah

Newbie level 4
Joined
Jun 15, 2014
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
16
% eg: firefly_demo( [25,20]);
function [best]=firefly_demo(instr)
% n=number of f i r e f l i es
y. MaxGeneration=number of pseudo time steps
if nargin<l, instr=[25 20]; end
n = i n s t r ( l ) ; MaxGeneration=instr(2);
r a n d ( ' s t a t e ' , 0 ) ; % Reset the random generator
% Four peak functions
funstr='(abs(x)+abs)*exp(-0.0625*(x~2+y~2))';
y, Converting to an inline function
f=vectorize(inline(funstr));
% range= [xmin xmax ymin ymax] ;
range=[-5 5 - 5 5];
%
alpha=0.2; % Randomness 0—1 (highly random)
gamma=1.0; % Absorption coefficient

% Grid values are used for display only
Ndiv=100;
dx=(range(2)-range(1))/Ndiv; dy=(range(4)-range(3))/Ndiv;
[x,y]=meshgrid(range(l):dx:range(2),range(3):dy:range(4));
z=f(x,y);
% Display the shape of the objective function
figure(l); surfc(x,y,z);
%
% generating the initial locations of n fireflies
[xn,yn,Lightn]=init_ffa(n,range);

% Display the paths of fireflies in a figure with
% contours of the function to be optimized
figure(2);
%'A I t e r a t i o n s or pseudo time marching
for i=l:MaxGeneration, °/°/°/°/,°l. s t a r t i t e r a t i o ns
% Show the contours of the function
contour(x,y,z,15); hold on;
% Evaluate new solutions
zn=f(xn.yn);
% Ranking the fireflies by their light intensity
[Lightn,Index]=sort(zn);
xn=xn(Index); yn=yn(Index);
xo=xn; yo=yn; Lighto=Lightn;
% Trace the paths of all roaming fireflies
plot(xn,yn,'.','markersize',10,'markerfacecolor','g');
% Move all fireflies to the better locations
[xn,yn]=ffa_move(xn,yn,Lightn,xo.yo,...
Lighto,alpha,gamma,range);
drawnow;
% Use "hold on" to show the paths of fireflies
hold off;
end V/°/°/X
end of iterations
best,l)=xo'; best,2)=yo'; best,3)=Lighto';
% All subfunctions are listed here
% The initial locations of n fireflies
function [xn,yn,Lightn]=init_ffa(n,range)
xrange=range(2)-range(l);
yrange=range(4)-range(3);
xn=rand(1,n)*xrange+range(1);
yn=rand(1,n)*yrange+range(3);
Lightn=zeros(size(yn));
%Move all fireflies toward brighter ones
function [xn,yn]=ffa_move(xn,yn,Lightn,xo.yo,...
Lighto,alpha,gamma,range)
ni=size(yn,2); nj=size(yo,2);
for i=l:ni,
% The attractiveness parameter beta=exp(-gamma*r)
for j=l:nj,
r=sqrt((xn(i)-xo(j))~2+(yn(i)-yo(j))~2) ;
if Lightn(i)<Lighto(j) , */, Brighter and more attractive
beta0=l; beta=betaO*exp(-gamma*r."2);
xn(i)=xn(i).*(l-beta)+xo(j).*beta+alpha.*(rand-0.5);
yn(i)=yn(i).*(l-beta)+yo(j).*beta+alpha.*(rand-0.5);
end
end 7, end for j

end */, end for i
[xn,yn]=findrange(xn,yn,range);
% Make sure the f i r e f l i e s are within the range
function [xn,yn]=findrange(xn,yn,range)
for i=l:length(yn),
if xn(i)<=range(l), xn(i)=range(l); end
if xn(i)>=range(2), xn(i)=range(2); end
if yn(i)<=range(3), yn(i)=range(3); end
if yn(i)>=range(4), yn(i)=range(4); end
end
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…