% NBPSO code v1.0% Generated by Majid Rostami Shahrbabaki, 2010. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5% Particle swarm optimization (PSO) is one of the modern heuristic algorithms that can be applied to continuous and discrete optimization problems.% The original binary PSO (BPSO) has got some disadvantages that make the algorithm not to converge well.% To deal with these disadvantages, a new BPSO (NBPSO) is introduced. The results provided in the following papers show the superiority of the NBPSO.% % [1]. M.Rostami Shahrbabak and H.Nezamabadi-pour, " A New Approach to Binary PSO Algorithm" 14th Iranian Conference on Electrical Engineering, may 2006.% [2]. H.Nezamabadi-pour, M.Rostami Shahrbabaki and M.Maghfoori Farsangi "Binary Particle Swarm Optimization: Challenges and new% Solutions"The CSI Journal on Computer Science and Engineering Vol. 6, No. 1 (a), pp. 21-32, 2008.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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 position and 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 resultsclear,close all,clc %#okfor 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,velocity,v_max,Gl_Lo_flag); end if Min_Max_flag == 1 best_fit(j,:) = min(fitness); %#ok else best_fit(j,:) = min(fitness); %#ok end mean_fit(j,:) = mean(fitness); %#ok disp([' End of run ' , num2str(j) , ' ==> Total run is 50'])enddisplay_result(best_fit,mean_fit,K,Min_Max_flag)
function X=decode(temp,L,x_max)x=0;for i=1:L x=x+temp(i)*2^(i-1);endx=x/(2^L-1);X=((2*x_max)/(1-0))*(x-0)-x_max;return
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 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 ; endreturn
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]=initializeN = 50; % N is the number of the particlesK = 1000; %K is the number of iterationvar = 5; % var is number of variablesL = 15 ; % L is the lenght for each variableD = L*var; % D is the dimension of each particlew_min=0.1;w_max=0.6;c1=2;c2=2;% w is the inertia factor and c1 & c2 are learning factorsposition = rand(N,D)>0.5; % Generates initial populationfitness=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 evaluatedMin_Max_flag = 1 ; % 1 if the function must be minimized .... 2 if the function must be maximizedGl_Lo_flag = 1 ; % 1 if the search is global ............... 2 if the search is localreturn
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 tunedvelocity = -v_max + 2*v_max*rand(N,D);return
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 endelse 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 endendreturn
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,:); endelse 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)); % finding minimum g_best(i,:)=p_best(place_g+i-2,:); 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)); % finding maximum g_best(i,:)=p_best(place_g+i-2,:); end end end end
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 end velocity=w*velocity+c1*rand*(p_best-position)+c2*rand*temp; % For global modelelse velocity=w*velocity+c1*rand*(p_best-position)+c2*rand*(g_best-position); % For loval modelendfor d=1:D for n=1:N if velocity(n,d)>v_max velocity(n,d)=v_max; elseif velocity(n,d) velocity(n,d)=-v_max; end endendpro_velocity=abs(2*(logsig(velocity)-0.5));for n=1:N for d=1:D if rand position(n,d)=xor(position(n,d),1); else position(n,d)=position(n,d); end endendreturn
![6a16e68de7223dec455cd64eaa40d0bf.png](https://i-blog.csdnimg.cn/blog_migrate/41b2f14cec4ef2560d3b544eedb56938.png)
往期回顾>>>>>>
【模式识别】Matlab指纹识别【优化求解】A*算法解决三维路径规划问题【优化求解】模拟退火遗传实现带时间窗的车辆路径规划问题【数学建模】Matlab实现SEIR模型【优化求解】基于NSGA-2的求解多目标柔性车间调度算法
【优化求解】蚁群算法求最优值
【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割
【优化算法】基于粒子群算法的充电站最优布局
【优化求解】粒子群算法之电线布局优化
【优化求解】遗传算法解决背包问题
【优化求解】基于粒子群之机器人栅格路径规划
![4067f974007e91bdf1f9cff501fa9cca.png](https://i-blog.csdnimg.cn/blog_migrate/cc2e05d478b90a77c9d91e8da2cf404f.jpeg)