Você está na página 1de 7

********************** display_result*************************

function display_result(best_fit,mean_fit,K,Min_Max_flag)
best_rate=mean(best_fit);
mean_rate=mean(mean_fit);
temp=best_rate;
if Min_Max_flag == 1
% minimization
for i=2:K
if best_rate(i) < temp(i-1)
temp(i) = best_rate(i);
else
temp(i) = temp(i-1);
end
end
else
% maximization
for i=2:K
if best_rate(i) > temp(i-1)
temp(i) = best_rate(i);
else
temp(i) = temp(i-1);
end
end
end
best_sofar=temp;
plot(mean_rate),grid,title(' Meaan of fitness')
figure,plot(best_sofar),grid,title(' Best fitness ever found')
return
************************ evaluate*****************************
function fitness=evaluate(position,k,N,D,L,var,x_max,fitness,Num_func)
for i=1:N
for j=1:var
temp=position(i,(j-1)*L+1:j*L);
X(j)=decode(temp,L,x_max); %#ok<AGROW>
end
switch Num_func
case 1
result = sum (X.^2);
case 2
result = sum(abs(X)) + prod(abs(X)) ;
case 3
result = 0 ;
for ii=1:var
result = result + sum(X(1:ii)).^2;
end
case 4
result = max (abs(X));
case 5
result = 0;
for ii=1:var-1
result = result + 100*((X(ii+1)-X(ii)^2)^2+(X(ii)-1)^2);
end
end
fitness(i,k) = result ;
end
return

*****************************initialize*****************************
function
[N,K,D,L,var,w_max,w_min,c1,c2,position,p_best,g_best,fitness,p_best_fit,...
Num_func,Min_Max_flag,Gl_Lo_flag]=initialize
N = 50;

% N is the number of the particles

K = 1000;

%K is the number of iteration

var = 5; % var is number of variables


L = 15 ; % L is the lenght for each variable
D = L*var; % D is the dimension of each particle
w_min=0.1;w_max=0.6;c1=2;c2=2;% w is the inertia factor and c1 & c2 are
learning factors
position = rand(N,D)>0.5; % Generates initial population
fitness=0;
p_best = rand(N,D)>0.5;
g_best = rand(N,D)>0.5;
p_best_fit = ones(N,1);
Num_func = 1 ; % Select the number of function to be evaluated
Min_Max_flag = 1 ;

% 1 if the function must be minimized

....

2 if the

% 1 if the search is global ...............

2 if the

function must be maximized


Gl_Lo_flag = 1

search is local
Return
***************************** untitled *********************************
close all,clc,,a=1:1500;
plot(a,f7_min_BPSO,'k',a,f7_min_NBPSO,'k-.')
legend('BPSO','NBPSO','NorthWest')
title('F7 Function')
xlabel('Iteration')
ylabel('Best-so far')
% axis([0 1500 0 12])

****************************** Main ***********************************


%
%
%
%
%
%
%
%

Main function for NBPSO algorithm. The function called here are :
initialize : gives the initial parameters
range_func : depend on the Num_func, determines ranges for pos & Velocity
evaluate : depend on the Num_func, evaluates the function
renewp_best : finds the Particle_best
renewg_best : finds the Global_best
update_v_p : updates position and velocities of particles
display_result : displays the results
clear,close all,clc %#ok<DUALC>
for j = 1:50
[N,K,D,L,var,w_max,w_min,c1,c2,position,p_best,g_best,fitness,p_best_fit,...
Num_func,Min_Max_flag,Gl_Lo_flag] = initialize;
[v_max,x_max,velocity,] = range_func(Num_func,N,D) ;
for k=1:K
w = w_max+(((w_min-w_max)*(k-1))/(K-1));
fitness = evaluate(position,k,N,D,L,var,x_max,fitness,Num_func);
[p_best,p_best_fit] =
renewp_best(D,fitness,p_best,N,k,position,p_best_fit,Min_Max_flag);
g_best=renewg_best(p_best,p_best_fit,N,Min_Max_flag,Gl_Lo_flag);
[position,velocity]=update_v_p(D,N,c1,c2,w,p_best,g_best,position,vel
ocity,v_max,Gl_Lo_flag);
end
if Min_Max_flag == 1
best_fit(j,:) = min(fitness);
%#ok<SAGROW>
else
best_fit(j,:) = min(fitness); %#ok<SAGROW>
end
mean_fit(j,:) = mean(fitness); %#ok<SAGROW>
disp([' End of run ' , num2str(j) , '
==> Total run is 50'])
end
display_result(best_fit,mean_fit,K,Min_Max_flag)
************************** range_func***************************
function [v_max,x_max,velocity] = range_func(Num_func,N,D)
switch Num_func
case 1
x_max = 100 ;
% x_min=-x_max (here)
case 2
x_max = 10 ;
% x_min=-x_max (here)
case 3
x_max = 100 ;
% x_min=-x_max (here)
case 4
x_max = 100 ;
% x_min=-x_max (here)
case 5
x_max = 30 ;
% x_min=-x_max (here)
end
v_max = 0.5 * x_max; % it can be tuned
velocity = -v_max + 2*v_max*rand(N,D);

return

************************ renewg_best****************************
function g_best=renewg_best(p_best,p_best_fit,N,Min_Max_flag,Gl_Lo_flag)
if Gl_Lo_flag == 1

%%% This part is for Global model

if Min_Max_flag == 1
[m_g place_g]=min(p_best_fit);

% finding minimum

g_best=p_best(place_g,:);
else
[m_g place_g]=max(p_best_fit);

% finding maximum

g_best=p_best(place_g,:);
end
else
if Min_Max_flag == 1 %%% This part is for local model
for i=1:N
if i==1
temp_g=p_best_fit(N);
temp_g(2:3)=p_best_fit(i:i+1,1);
[min_g,place_g]=min(temp_g);

% finding minimum

switch place_g
case 1
g_best(i,:)=p_best(N,:);
case {2,3}
g_best(i,:)=p_best(place_g+i-2,:);
end
elseif i==N
temp_g=p_best_fit(1);
temp_g(2:3)=p_best_fit(i-1:i,1);
[min_g,place_g]=min(temp_g);

% finding minimum

switch place_g
case 1
g_best(i,:)=p_best(1,:);
case {2,3}
g_best(i,:)=p_best(place_g+i-3,:);
end
else
[min_g,place_g]=min(p_best_fit(i-1:i+1,1));
g_best(i,:)=p_best(place_g+i-2,:);

% finding minimum

end
end
else
for i=1:N
if i==1
temp_g=p_best_fit(N);
temp_g(2:3)=p_best_fit(i:i+1,1);
[max_g,place_g]=max(temp_g);

% finding maximum

switch place_g
case 1
g_best(i,:)=p_best(N,:);
case {2,3}
g_best(i,:)=p_best(place_g+i-2,:);
end
elseif i==N
temp_g=p_best_fit(1);
temp_g(2:3)=p_best_fit(i-1:i,1);
[max_g,place_g]=max(temp_g);

% finding maximum

switch place_g
case 1
g_best(i,:)=p_best(1,:);
case {2,3}
g_best(i,:)=p_best(place_g+i-3,:);
end
else
[max_g,place_g]=max(p_best_fit(i-1:i+1,1));
g_best(i,:)=p_best(place_g+i-2,:);
end
end
end
end

% finding maximum

************************* renewp_best**************************
function
[p_best,p_best_fit]=renewp_best(D,fitness,p_best,N,k,position,p_best_fit,Min_
Max_flag)
if Min_Max_flag == 1
for n=1:N
if k==1
min_p=fitness(n,k);
p_best(n,:)=position(n,:);
p_best_fit(n,1)=min_p;
elseif p_best_fit(n,1) > fitness(n,k)
p_best_fit(n,1)=fitness(n,k);
p_best(n,:)=position(n,:);
end
end
else
for n=1:N
if k==1
min_p=fitness(n,k);
p_best(n,:)=position(n,:);
p_best_fit(n,1)=min_p;
elseif p_best_fit(n,1) < fitness(n,k)
p_best_fit(n,1)=fitness(n,k);
p_best(n,:)=position(n,:);
end
end
end
return

************************** update********************************
function
[position,velocity]=update_v_p(D,N,c1,c2,w,p_best,g_best,position,velocity,v_
max,Gl_Lo_flag)
if Gl_Lo_flag==1
for i=1:N
temp(i,:)=g_best-position(i,:); %#ok<AGROW>
end
velocity=w*velocity+c1*rand*(p_best-position)+c2*rand*temp;

For

global model
else
velocity=w*velocity+c1*rand*(p_best-position)+c2*rand*(g_best-position);
% For loval model
end
for d=1:D
for n=1:N
if velocity(n,d)>v_max
velocity(n,d)=v_max;
elseif velocity(n,d)<-v_max
velocity(n,d)=-v_max;
end
end
end
pro_velocity=abs(2*(logsig(velocity)-0.5));
for n=1:N
for d=1:D
if rand<pro_velocity(n,d)
position(n,d)=xor(position(n,d),1);
else
position(n,d)=position(n,d);
end
end
end
return

Você também pode gostar