%%%%%%%% pso_inertial.m %%%%%%%% %%%%%Particle Swarm Adjustment%%%%%%%%%% Optimization with Inertial Weight %In particle Swarm Optimization the inertial weight which decides the %weightage of previous later in the improved velocity value was introduced %version of PSO. %The inertial weightage value is taken to be linearly decreasing and in %some cases may even provide better and faster optimizing results %Look at the example below %% Double Boundary Control Strategy %It is used to have some variation in the population which has violated the %boundary consitions %Method I % If it violates it becomes equal to boundary value %Method II % If violated the boundary upper and lower bound condition a value between % is chosen %% Example % Type this in your command window :% low = [-10;-10;-10]; % up = low+20; % pso_inertial('rosenbrock',40,3,low,up,9,1,10e+6); % Whenever you feel the minimum value has been attained Just Press ---> Ctrl+C % The globalminimum value and globalminimizer will be there in your workspace % PSO_inertial can also be checked benchmark functions - Ackley, Rastrigin for different % Grienwank and FoxHoles. Only in the case of FoxHoles change dim = 2 %% code for PSO function pso_inertial(fnc,popsize,dim,low,up,wf,wi,epoch) %% Initialization and Poulation Generation pop=GeneratePopulation(popsize,dim,low,up); fitnesspop=feval(fnc,pop); PBest=pop; PBest_value=fitnesspop; [GBest_value,avain]=min(fitnesspop); for k=1:popsize GBest(k,:)=pop(avain,:); end v=zeros(popsize,dim); c1=1; c2=2; for i=1:epoch w = wi+((wi-wf)*i)/epoch; mp=1; % working on hybridization of PSO hence mp variable has been used vel = w.*v + pop)+(c2*rand).*(GBest-pop)); offspring = vel + pop; mp.*((c1*rand).*(PBest- % BOUNDARY CONTROL IS ALWAYS NECESSARY offspring=BoundaryControl(offspring,low,up); % Cost Value of offspring fitness_offspring=feval(fnc,offspring); % updating the value of PBest and GBest ind = fitness_offspring<PBest_value; PBest(ind,:)=offspring(ind,:); PBest_value(ind,:)=fitness_offspring(ind,:); [pop_best,ind1]=min(fitness_offspring); if pop_best<GBest_value for j=1:popsize GBest(j,:)=offspring(ind1,:); end GBest_value=pop_best; end % the new co-ordinates and the velocity pop = offspring; v = vel; assignin('base','globalminimizer',GBest(1,:)); assignin('base','globalminimum',GBest_value); fprintf('PSO_byBhanu|%5.0f %9.16f\n',i,GBest_value); end return -----> %$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$ %%%%%%% pso_nb.m %%%%%%%%%%% %%%%%Particle Swarm Optimization%%%%%%%%%% %%It is the most basic code of PSO %hence makes it easy to apply on atleast unconstrained problems %% Double Boundary Control Strategy %It is used to have some variation in the population which has violated the %boundary consitions %Method I % If it violates it becomes equal to boundary value %Method II % If violated the boundary upper and lower bound condition a value between % is chosen %% It is best for Newbies % The code is vectorized and Hence makes it even faster. % Syntax pso_nb('filename',popsize,dim,low,up,epoch) % popsize - It is the number of swarms or basically the total population % In swarm intelligence a population of maximum 80 works for every problem % dim - It refers to the number of variables or the dimensions of the search space % low - A column matrix depicting the lower boundary for each variable % up - upper boundary level % epoch - The max number of iterations. %% Example % Type this in your command window :% low = [-10;-10;-10]; % up = low+20; % pso_nb('rosenbrock',40,3,low,up,10e+6); % Whenever you feel the minimum value has been attained Just Press ---> Ctrl+C % The globalminimum value and globalminimizer will be there in your workspace % PSO can also be checked functions - Ackley, Rastrigin for different benchmark % Grienwank and FoxHoles. Only in the case of FoxHoles change dim = 2 %% code for PSO function pso_nb(fnc,popsize,dim,low,up,epoch) %% Initialization and Poulation Generation pop=GeneratePopulation(popsize,dim,low,up); fitnesspop=feval(fnc,pop); PBest=pop; PBest_value=fitnesspop; [GBest_value,avain]=min(fitnesspop); for k=1:popsize GBest(k,:)=pop(avain,:); end v=zeros(popsize,dim); c1=1; c2=2; w=2; for i=1:epoch mp=1; % working on hybridization of PSO hence mp variable has been used vel = w.*v + pop)+(c2*rand).*(GBest-pop)); mp.*((c1*rand).*(PBest- offspring = vel + pop; % BOUNDARY CONTROL IS ALWAYS NECESSARY offspring=BoundaryControl(offspring,low,up); % Cost Value of offspring fitness_offspring=feval(fnc,offspring); % updating the value of PBest and GBest ind = fitness_offspring<PBest_value; PBest(ind,:)=offspring(ind,:); PBest_value(ind,:)=fitness_offspring(ind,:); [pop_best,ind1]=min(fitness_offspring); if pop_best<GBest_value for j=1:popsize GBest(j,:)=offspring(ind1,:); end GBest_value=pop_best; end % the new co-ordinates and the velocity pop = offspring; v = vel; assignin('base','globalminimizer',GBest(1,:)); assignin('base','globalminimum',GBest_value); fprintf('PSO_byBhanu|%5.0f %9.16f\n',i,GBest_value); end return -----> % $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ %%%%%%%%%% %%to GeneratePopulation.m %%%%%%%%%%% generate the population function pop=GeneratePopulation(popsize,dim,low,up) pop=ones(popsize,dim); for i=1:popsize for j=1:dim pop(i,j)=rand*(up(j)-low(j))+low(j); end end % for i=1:popsize % ch1=round(pop(i,26)); % ch2=round(pop(i,25)); % if (ch1-ch2)<3 % if ch2<22 % pop(i,26)=ch2+3+rand*(19-ch2); % end % if ch2>21 % pop(i,25)=21*rand; % pop(i,26)=round(pop(i,25))+3; % end % end % assignin('base','firstpop',pop); % end end %$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$ %%%%%%%% BoundaryControl.m %%%%%%% function pop=BoundaryControl(pop,low,up) %% Double boundary control strategy % Strategy can be changed accordingly [popsize,dim]=size(pop); for i=1:popsize for j=1:dim k=rand<rand; if pop(i,j)<low(j), if k, pop(i,j)=low(j); else pop(i,j)=rand*(up(j)-low(j))+low(j); end, end if pop(i,j)>up(j), if k, pop(i,j)=up(j); pop(i,j)=rand*(up(j)-low(j))+low(j); end, end end end return else