├── Levy.m ├── cec13_0.mexw64 ├── cec13_func.cpp ├── get_fun_info_CEC2013.m ├── fitness.m ├── simplebounds.m ├── init.m ├── RouletteWheelSelection.m ├── main.m ├── main.asv ├── PSO_func.m ├── initialization.m ├── readme.txt ├── CrowSearchAlgorithm.m ├── FPA.m ├── CSA.m ├── GSA.m ├── NA.m ├── WOA.m └── ABC.m /Levy.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luwei-fu/Nomad-Algorithm/HEAD/Levy.m -------------------------------------------------------------------------------- /cec13_0.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luwei-fu/Nomad-Algorithm/HEAD/cec13_0.mexw64 -------------------------------------------------------------------------------- /cec13_func.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luwei-fu/Nomad-Algorithm/HEAD/cec13_func.cpp -------------------------------------------------------------------------------- /get_fun_info_CEC2013.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luwei-fu/Nomad-Algorithm/HEAD/get_fun_info_CEC2013.m -------------------------------------------------------------------------------- /fitness.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function ft=fitness(xn,N,pd) % Function for fitness evaluation 4 | 5 | for i=1:N 6 | ft(i)=sum(xn(i,:).^2); % Sphere function 7 | end -------------------------------------------------------------------------------- /simplebounds.m: -------------------------------------------------------------------------------- 1 | function s=simplebounds(s,Lb,Ub) 2 | 3 | ns_tmp=s; 4 | I=ns_tmpUb; 9 | ns_tmp(J)=Ub(J); 10 | % Update this new move 11 | s=ns_tmp; 12 | 13 | end 14 | 15 | -------------------------------------------------------------------------------- /init.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [x l u]=init(N,pd) % Function for initialization 4 | 5 | l=-100; u=100; % Lower and upper bounds 6 | 7 | for i=1:N % Generation of initial solutions (position of crows) 8 | for j=1:pd 9 | x(i,j)=l-(l-u)*rand; % Position of the crows in the space 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /RouletteWheelSelection.m: -------------------------------------------------------------------------------- 1 | % 2 | % Copyright (c) 2015, Yarpiz (www.yarpiz.com) 3 | % All rights reserved. Please read the "license.txt" for license terms. 4 | % 5 | % Project Code: YPEA114 6 | % Project Title: Implementation of Artificial Bee Colony in MATLAB 7 | % Publisher: Yarpiz (www.yarpiz.com) 8 | % 9 | % Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team) 10 | % 11 | % Contact Info: sm.kalami@gmail.com, info@yarpiz.com 12 | % 13 | 14 | function i=RouletteWheelSelection(P) 15 | r=rand; 16 | 17 | C=cumsum(P); 18 | 19 | i=find(r<=C,1,'first'); 20 | 21 | end -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- 1 | clear all 2 | clc 3 | %mex cec13_func.cpp -DWINDOWS 4 | D=30; 5 | Xmin=-100; 6 | Xmax=100; 7 | pop_size=30; 8 | iter_max=1000; 9 | runs=30; 10 | fhd=str2func('cec13_0'); 11 | begin=cputime; 12 | for i=1:28 13 | func_num=i 14 | for j=1:runs 15 | 16 | [position,gbest,time]=NA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 17 | %[position,gbest,time]= PSO_func(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 18 | % [position,gbest,time]=WOA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 19 | % [position,gbest,time]=GSA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 20 | % [position,gbest,time]=ABC(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 21 | % [position,gbest,time]=FPA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 22 | % [position,gbest,time]=CSA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 23 | xbest(j,:)=position; 24 | fbest(i,j)=gbest; 25 | timecost(i,j)=time; 26 | 27 | end 28 | 29 | f_value(i,1)=mean(fbest(i,:)); % mean optima of multiple runs 30 | f_value(i,2)=std(fbest(i,:)) ; % std of multiple runs 31 | f_value(i,3)=mean(timecost(i,:)); % mean value of time consumption 32 | end 33 | fprintf('\t mean \t\t STD \t\t time'); 34 | for i=1:func_num 35 | fprintf('\nF%d:\t',i); 36 | fprintf('%.2e\t',f_value(i,:)); 37 | end -------------------------------------------------------------------------------- /main.asv: -------------------------------------------------------------------------------- 1 | clear all 2 | clc 3 | %mex cec13_func.cpp -DWINDOWS 4 | D=30; 5 | Xmin=-100; 6 | Xmax=100; 7 | pop_size=30; 8 | iter_max=1000; 9 | runs=3; 10 | fhd=str2func('cec13_0'); 11 | begin=cputime; 12 | for i=1:5 13 | func_num=i 14 | for j=1:runs 15 | 16 | [position,gbest,time]=NA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 17 | %[position,gbest,time]= PSO_func(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 18 | % [position,gbest,time]=WOA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 19 | % [position,gbest,time]=GSA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 20 | % [position,gbest,time]=ABC(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 21 | % [position,gbest,time]=FPA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 22 | % [position,gbest,time]=CSA(fhd,D,pop_size,iter_max,Xmin,Xmax,func_num); 23 | xbest(j,:)=position; 24 | fbest(i,j)=gbest; 25 | timecost(i,j)=time; 26 | 27 | end 28 | 29 | f_value(i,1)=mean(fbest(i,:)); % mean optima of multiple runs 30 | f_value(i,2)=std(fbest(i,:)) ; % std of multiple runs 31 | f_value(i,3)=mean(timecost(i,:)); % mean value of time consumption 32 | end 33 | timetotal=(cputime-begin)/60 34 | time=timetotal/runs 35 | fprintf('\t mean \t\t STD \t time'); 36 | for i=1:func_num 37 | fprintf('\nF%d:\t%.2e',i,f_value(i,:)); 38 | fprintf('\nF%d:\t%.2e',i,f_value(i,:)); 39 | end -------------------------------------------------------------------------------- /PSO_func.m: -------------------------------------------------------------------------------- 1 | function [gbest,gbestval,time]= PSO_func(fhd,Dimension,Particle_Number,Max_Gen,VRmin,VRmax,varargin) 2 | %[gbest,gbestval,fitcount]= PSO_func('f8',3500,200000,30,30,-5.12,5.12) 3 | starttime=cputime; 4 | rand('state',sum(100*clock)); 5 | me=Max_Gen; 6 | ps=Particle_Number; 7 | D=Dimension; 8 | cc=[2 2]; %acceleration constants 9 | %iwt=0.9-(1:me).*(0.5./me); 10 | iwt=0.7298.*ones(1,me); 11 | if length(VRmin)==1 12 | VRmin=repmat(VRmin,1,D); 13 | VRmax=repmat(VRmax,1,D); 14 | end 15 | mv=0.5*(VRmax-VRmin); 16 | VRmin=repmat(VRmin,ps,1); 17 | VRmax=repmat(VRmax,ps,1); 18 | Vmin=repmat(-mv,ps,1); 19 | Vmax=-Vmin; 20 | pos=VRmin+(VRmax-VRmin).*rand(ps,D); 21 | 22 | e=feval(fhd,pos',varargin{:}); 23 | 24 | fitcount=ps; 25 | vel=Vmin+2.*Vmax.*rand(ps,D);%initialize the velocity of the particles 26 | pbest=pos; 27 | pbestval=e; %initialize the pbest and the pbest's fitness value 28 | [gbestval,gbestid]=min(pbestval); 29 | gbest=pbest(gbestid,:);%initialize the gbest and the gbest's fitness value 30 | gbestrep=repmat(gbest,ps,1); 31 | 32 | for i=2:me 33 | 34 | aa=cc(1).*rand(ps,D).*(pbest-pos)+cc(2).*rand(ps,D).*(gbestrep-pos); 35 | vel=iwt(i).*vel+aa; 36 | vel=(vel>Vmax).*Vmax+(vel<=Vmax).*vel; 37 | vel=(vel=Vmin).*vel; 38 | pos=pos+vel; 39 | pos=((pos>=VRmin)&(pos<=VRmax)).*pos... 40 | +(posVRmax).*(VRmax-0.25.*(VRmax-VRmin).*rand(ps,D)); 41 | e=feval(fhd,pos',varargin{:}); 42 | fitcount=fitcount+ps; 43 | tmp=(pbestval1 33 | for i=1:dim 34 | ub_i=ub(i); 35 | lb_i=lb(i); 36 | Positions(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i; 37 | end 38 | end -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | This document help users testing the performance of otpmizers. 2 | 3 | There are 6 algorithms (optimizers) implemented in this platform including 4 | Nomad Algorithm (NA, our proposal), 5 | Particle Swarm optimization (PSO), 6 | Artificial Bee Colony Algorithm (ABC), 7 | Gravitational Search Algorithm (GSA), 8 | Flower Pollination Algorithm (FPA), 9 | and Whale optimization Algorithm (WOA). 10 | 11 | CEC13 Test Function Suite (28 benchmark functions) is provided by: 12 | Jane Jing Liang (email: liangjing@zzu.edu.cn) 13 | 14 | Preliminary (importing the test functions): 15 | 16 | 17 | 1. run the following command in Matlab window: 18 | mex cec13_func.cpp -DWINDOWS 19 | 2. Then you can use the test functions as the following example: 20 | f = cec13_func(x,func_num); 21 | here x is a D*pop_size matrix. 22 | 23 | 24 | %-------------------------------------------------------------------------------- 25 | 26 | To the beginning of execution: 27 | 28 | 29 | 1. Initialization: Several controling parameters should be assigned. 30 | 31 | D: dimension of all testing functions 32 | Xmin: lower bound of search space 33 | Xmax: upper bound of search space 34 | pop_size: population size of optimizers, i.e., how many search agents are employed by optimizers 35 | iter_max: max number of iterations 36 | runs: repeating times of simulations for obtainning the statistical results 37 | 38 | 39 | 2. Selecting an optmizers to be tested: 40 | 41 | Line 16 - line 21 are 6 different optimizers (one line for one optimizer). 42 | Please keep only one optimizers executable and comment out the other five optimizers. 43 | 44 | 3. Then, run the 'main.m' in Matlab window 45 | 46 | 47 | A few moment latter (seconds to minutes), 48 | for your selected optimziers on multiple independed runs of all the 28 functions, 49 | the optmization resuls are shown as a matrix in a 28x3 matrix. 50 | 51 | 52 | Each line corresponds to a function: 53 | first column indicates mean error, 54 | second column indicates standard deviations (STD), 55 | third column indicates time consumption. 56 | 57 | */ 58 | -------------------------------------------------------------------------------- /CrowSearchAlgorithm.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % ------------------------------------------------- 4 | % Citation details: 5 | % Alireza Askarzadeh, Anovel metaheuristic method for solving constrained 6 | % engineering optimization problems: Crow search algorithm, Computers & 7 | % Structures, Vol. 169, 1-12, 2016. 8 | 9 | % Programmed by Alireza Askarzadeh at Kerman Graduate % 10 | % University of Advanced Technology (KGUT) % 11 | % Date of programming: September 2015 % 12 | % ------------------------------------------------- 13 | % This demo only implements a standard version of CSA for minimization of 14 | % a standard test function (Sphere) on MATLAB 7.6.0 (R2008a). 15 | % ------------------------------------------------- 16 | % Note: 17 | % Due to the stochastic nature of meta-heuristc algorithms, different runs 18 | % may lead to slightly different results. 19 | % ------------------------------------------------- 20 | function [g_best,ngbest,timecost]=CSA(fhd,pd,pop,Max_iteration,Lb,Ub,varargin) 21 | begin=cputime; 22 | N=50; % Flock (population) size 23 | AP=0.1; % Awareness probability 24 | fl=2; % Flight length (fl) 25 | l=Lb; u=Ub; 26 | 27 | for i=1:N % Generation of initial solutions (position of crows) 28 | for j=1:pd 29 | x(i,j)=l-(l-u)*rand; % Position of the crows in the space 30 | end 31 | end 32 | 33 | xn=x; 34 | ft= feval(fhd,xn',varargin{:}); 35 | evalu=N; 36 | 37 | mem=x; % Memory initialization 38 | fit_mem=ft; % Fitness of memory positions 39 | 40 | tmax=Max_iteration*0.6; % Maximum number of iterations (itermax) 41 | for t=1:tmax 42 | 43 | num=ceil(N*rand(1,N)); % Generation of random candidate crows for following (chasing) 44 | for i=1:N 45 | if rand>AP 46 | xnew(i,:)= x(i,:)+fl*rand*(mem(num(i),:)-x(i,:)); % Generation of a new position for crow i (state 1) 47 | else 48 | for j=1:pd 49 | xnew(i,j)=l-(l-u)*rand; % Generation of a new position for crow i (state 2) 50 | end 51 | end 52 | end 53 | 54 | xn=xnew; 55 | ft= feval(fhd,xn',varargin{:}); % Function for fitness evaluation of new solutions 56 | 57 | for i=1:N % Update position and memory 58 | if xnew(i,:)>=l & xnew(i,:)<=u 59 | x(i,:)=xnew(i,:); % Update position 60 | if ft(i)p 37 | %% L=rand; 38 | L=Levy(d); 39 | dS=L.*(Sol(i,:)-best); 40 | S(i,:)=Sol(i,:)+dS; 41 | 42 | % Check if the simple limits/bounds are OK 43 | S(i,:)=simplebounds(S(i,:),Lb,Ub); 44 | 45 | % If not, then local pollenation of neighbor flowers 46 | else 47 | epsilon=rand; 48 | % Find random flowers in the neighbourhood 49 | JK=randperm(n); 50 | % As they are random, the first two entries also random 51 | % If the flower are the same or similar species, then 52 | % they can be pollenated, otherwise, no action. 53 | % Formula: x_i^{t+1}+epsilon*(x_j^t-x_k^t) 54 | S(i,:)=S(i,:)+epsilon*(Sol(JK(1),:)-Sol(JK(2),:)); 55 | % Check if the simple limits/bounds are OK 56 | S(i,:)=simplebounds(S(i,:),Lb,Ub); 57 | end 58 | 59 | % Evaluate new solutions 60 | Fnew=feval(fhd,S(i,:)',varargin{:}); 61 | % If fitness improves (better solutions found), update then 62 | if (Fnew<=Fitness(i)) 63 | Sol(i,:)=S(i,:); 64 | Fitness(i)=Fnew; 65 | end 66 | 67 | % Update the current global best 68 | if Fnew<=fmin 69 | best=S(i,:) ; 70 | fmin=Fnew ; 71 | end 72 | end 73 | % Display results every 100 iterations 74 | 75 | Convergence_curve(t)=fmin; 76 | end 77 | best_score=fmin; 78 | 79 | time=cputime-starttime; 80 | end 81 | 82 | 83 | -------------------------------------------------------------------------------- /CSA.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % ------------------------------------------------- 4 | % Citation details: 5 | % Alireza Askarzadeh, Anovel metaheuristic method for solving constrained 6 | % engineering optimization problems: Crow search algorithm, Computers & 7 | % Structures, Vol. 169, 1-12, 2016. 8 | 9 | % Programmed by Alireza Askarzadeh at Kerman Graduate % 10 | % University of Advanced Technology (KGUT) % 11 | % Date of programming: September 2015 % 12 | % ------------------------------------------------- 13 | % This demo only implements a standard version of CSA for minimization of 14 | % a standard test function (Sphere) on MATLAB 7.6.0 (R2008a). 15 | % ------------------------------------------------- 16 | % Note: 17 | % Due to the stochastic nature of meta-heuristc algorithms, different runs 18 | % may lead to slightly different results. 19 | % ------------------------------------------------- 20 | function [g_best,fitness,timecost]=CSA(fhd,pd,pop,Max_iteration,Lb,Ub,varargin) 21 | begin=cputime; 22 | N=50; % Flock (population) size 23 | AP=0.1; % Awareness probability 24 | fl=2; % Flight length (fl) 25 | l=Lb; u=Ub; 26 | for i=1:N % Generation of initial solutions (position of crows) 27 | for j=1:pd 28 | x(i,j)=l-(l-u)*rand; % Position of the crows in the space 29 | end 30 | end 31 | 32 | xn=x; 33 | ft= feval(fhd,xn',varargin{:}); 34 | evalu=N; 35 | 36 | mem=x; % Memory initialization 37 | fit_mem=ft; % Fitness of memory positions 38 | 39 | tmax=Max_iteration*0.6; % Maximum number of iterations (itermax) 40 | ffit=zeros(1,tmax); 41 | ffit(1)=ft(fix(30*rand)+1); 42 | for t=2:tmax 43 | 44 | num=ceil(N*rand(1,N)); % Generation of random candidate crows for following (chasing) 45 | for i=1:N 46 | if rand>AP 47 | xnew(i,:)= x(i,:)+fl*rand*(mem(num(i),:)-x(i,:)); % Generation of a new position for crow i (state 1) 48 | else 49 | for j=1:pd 50 | xnew(i,j)=l-(l-u)*rand; % Generation of a new position for crow i (state 2) 51 | end 52 | end 53 | end 54 | 55 | xn=xnew; 56 | ft= feval(fhd,xn',varargin{:}); % Function for fitness evaluation of new solutions 57 | 58 | for i=1:N % Update position and memory 59 | if xnew(i,:)>=l & xnew(i,:)<=u 60 | x(i,:)=xnew(i,:); % Update position 61 | if ft(i)Ub; 35 | Tm=X(j,:)= N*Max_iteration 47 | break; 48 | end 49 | 50 | end %iteration 51 | endtime=cputime; 52 | time=endtime-starttime; 53 | 54 | function [M] = massCalculation(fitness) 55 | Fmax=max(fitness); Fmin=min(fitness); 56 | [N , ~]=size(fitness); 57 | if Fmax==Fmin 58 | M=ones(N,1); 59 | else 60 | best=Fmin;worst=Fmax; 61 | M=(fitness - worst)./(best - worst); 62 | end 63 | M=M./sum(M); 64 | 65 | function a = Gfield(M,X,G,Rpower,ElitistCheck,it,NGen) 66 | [N,dim]=size(X); 67 | final_per=2; %In the last iteration, only 2 percent of agents apply force to the others. 68 | 69 | if ElitistCheck==1 70 | kbest=final_per+(1-it/NGen)*(100-final_per); %kbest in eq. 21. 71 | kbest=round(N*kbest/100); 72 | else 73 | kbest=N; 74 | end 75 | [~ , ds]=sort(M,'descend'); 76 | for i=1:N 77 | E(i,:)=zeros(1,dim); 78 | for ii=1:kbest 79 | j=ds(ii); 80 | if j~=i 81 | R=norm(X(i,:)-X(j,:),2); %Euclidian distanse. 82 | for k=1:dim 83 | E(i,k)=E(i,k)+rand*(M(j))*((X(j,k)-X(i,k))/(R^Rpower+eps)); %note that Mp(i)/Mi(i)=1 84 | end 85 | end 86 | end 87 | end 88 | a=E.*G; % acceleration ,note that Mp(i)/Mi(i)=1 89 | 90 | -------------------------------------------------------------------------------- /NA.m: -------------------------------------------------------------------------------- 1 | % Nomad Algorithm 2 | function [position,gBest,time]=NA(fhd,dim,N,Max_iteration,lb,ub,varargin) 3 | % -----------------Initializations----------------- 4 | 5 | starttime=cputime; 6 | sigma=ub-lb; 7 | radius=sigma; 8 | w1=0.9; 9 | w2=1.1; 10 | vmin=0.4; 11 | vmax=0.9; 12 | range=vmax-vmin; 13 | gBest=inf; 14 | index=2; 15 | SF=0.08; 16 | k=0; % how many iterations has the tribe stayed 17 | cg_curve=zeros(1,Max_iteration); 18 | hersmen_curve=zeros(1,Max_iteration); 19 | % Random initialization for agents. 20 | sampledot=lb+(ub-lb)*rand(N,dim); 21 | e=feval(fhd,sampledot',varargin{:}); 22 | [pBest,pindex]=min(e); 23 | if pBestub; 40 | sampledot(i,be)=lb + rand*(ub-lb); 41 | end 42 | 43 | for i=(M_exploit+1):N 44 | sampledot(i,:)=position; 45 | j=fix(dim*rand)+1; 46 | sampledot(i,j)=position(j)+normrnd(0,sigma); 47 | if sampledot(i,j)>ub || sampledot(i,j) M_exploit 69 | sigma=ub-lb; 70 | end 71 | end 72 | 73 | for i=1:dim 74 | if position_sl(i)> ub 75 | position_sl(i)=position(i)+rand*(ub-position(i)); 76 | elseif position_sl(i)< lb 77 | position_sl(i)=lb+rand*(position(i)-lb); 78 | end 79 | end 80 | sle=feval(fhd,position_sl',varargin{:}); 81 | if sle N*vmin 91 | M_exploit=M_exploit-1; 92 | k=0; 93 | sigma=ub-lb; 94 | end 95 | else 96 | k=0; 97 | if rand < P && M_exploitub; 23 | Flag4lb=Positions(i,:) for maximization problem 32 | Leader_score=fitness; % Update alpha 33 | Leader_pos=Positions(i,:); 34 | end 35 | 36 | end 37 | 38 | a=2-t*((2)/Max_iter); % a decreases linearly fron 2 to 0 in Eq. (2.3) 39 | 40 | % a2 linearly dicreases from -1 to -2 to calculate t in Eq. (3.12) 41 | a2=-1+t*((-1)/Max_iter); 42 | 43 | % Update the Position of search agents 44 | for i=1:size(Positions,1) 45 | r1=rand(); % r1 is a random number in [0,1] 46 | r2=rand(); % r2 is a random number in [0,1] 47 | 48 | A=2*a*r1-a; % Eq. (2.3) in the paper 49 | C=2*r2; % Eq. (2.4) in the paper 50 | 51 | 52 | b=1; % parameters in Eq. (2.5) 53 | l=(a2-1)*rand+1; % parameters in Eq. (2.5) 54 | 55 | p = rand(); % p in Eq. (2.6) 56 | 57 | for j=1:size(Positions,2) 58 | 59 | if p<0.5 60 | if abs(A)>=1 61 | rand_leader_index = floor(SearchAgents_no*rand()+1); 62 | X_rand = Positions(rand_leader_index, :); 63 | D_X_rand=abs(C*X_rand(j)-Positions(i,j)); % Eq. (2.7) 64 | Positions(i,j)=X_rand(j)-A*D_X_rand; % Eq. (2.8) 65 | 66 | elseif abs(A)<1 67 | D_Leader=abs(C*Leader_pos(j)-Positions(i,j)); % Eq. (2.1) 68 | Positions(i,j)=Leader_pos(j)-A*D_Leader; % Eq. (2.2) 69 | end 70 | 71 | elseif p>=0.5 72 | 73 | distance2Leader=abs(Leader_pos(j)-Positions(i,j)); 74 | % Eq. (2.5) 75 | Positions(i,j)=distance2Leader*exp(b.*l).*cos(l.*2*pi)+Leader_pos(j); 76 | 77 | end 78 | 79 | end 80 | end 81 | 82 | t=t+1; 83 | Convergence_curve(t)=Leader_score; 84 | 85 | 86 | 87 | end 88 | 89 | endtime=cputime; 90 | time=endtime-starttime; 91 | end 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /ABC.m: -------------------------------------------------------------------------------- 1 | function [Xbest,Fbest,time] = ABC(fhd,dim,NP,NGen,Lb,Ub,varargin) 2 | format short e 3 | % make copies of parameters 4 | starttime=cputime; 5 | NP = NP; % Population Size 6 | nOnlooker =NP; % Number of Onlooker Bees 7 | L =round(0.6*dim*NP); % Abandonment Limit Parameter (Trial Limit) 8 | a = 1; % Acceleration Coefficient Upper Bound 9 | 10 | % Abandonment Counter 11 | C=zeros(NP,1); 12 | 13 | pop = Lb+(Ub-Lb).*rand(NP,dim); 14 | 15 | for j = 1:NP 16 | fitness(j,1) =feval(fhd,pop(j,:)',varargin{:}); 17 | end 18 | evalu = NP; 19 | [best , bestX]=min(fitness); % minimization 20 | Fbest=best;Xbest=pop(bestX,:); 21 | BestChart = []; 22 | BestChart=[BestChart; Fbest]; 23 | 24 | %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 25 | for it =1:NGen 26 | % Recruited Bees 27 | for j=1:NP 28 | % Choose k randomly, not equal to i 29 | K=[1:j-1 j+1:NP]; 30 | k=K(randi([1 numel(K)])); 31 | % Define Acceleration Coeff. 32 | phi=a*unifrnd(-1,+1,1,dim); 33 | % New Bee Position 34 | newbee = pop(j,:)+phi.*(pop(j,:)-pop(k,:)); 35 | % Tp=newbee>Ub; 36 | % Tm=newbeeUb; 71 | % Tm=newbee=L 89 | pop(j,:)=unifrnd(Lb,Ub,1,dim); 90 | fitness(j)= feval(fhd,pop(j,:)',varargin{:}); 91 | evalu=evalu+1; 92 | C(j)=0; 93 | end 94 | end 95 | % Update Best Solution Ever Found 96 | [best,bestX]=min(fitness); % minimization 97 | if best<=Fbest 98 | Fbest=best; 99 | Xbest=pop(bestX,:); 100 | end 101 | if evalu > NGen*NP 102 | break; 103 | end 104 | end %iteration 105 | evalu; 106 | time=cputime-starttime; 107 | --------------------------------------------------------------------------------