You are on page 1of 3

function evalValues = standardPSO(fhd, functionNumber, DIM, evalsPerDIM) evalValues = zeros(1,11); nextEval = 1; evalPoints = [0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]; numEvals = 0; % PSO parameters -- D.

Bratton and J. Kennedy, "Defining a standard for part icle swarm optimization," IEEE SIS, 2007, pp. 120 127. popsize = 50; w = 0.72984; c1 = 2.05 * w; c2 = 2.05 * w; % % Search space parameters range = 5; %BBOB2009 range = 100; %CEC2013 xmin = -range * ones(1,DIM); xmax = range * ones(1,DIM); vmin = -range * ones(1,DIM); vmax = range * ones(1,DIM); % Random initial positions x = 2 * range * rand(popsize,DIM) - range; % zero for initial velocity -- A. Engelbrecht, "Particle Swarm Optimization: Velocity Initialization," IEEE CEC, 2012, pp. 70-77. v = zeros(popsize,DIM); % initialize personal best positions as initial positions pbest = x; pbestCosts = feval(fhd, x', functionNumber); numEvals = numEvals + popsize; % update lbest [lbest] = update_lbest(pbestCosts, x, popsize); maxevals = DIM * evalsPerDIM; maxgenerations = floor(maxevals/popsize); for generation = 2 : maxgenerations % Update velocity v = w*v + c1*rand(popsize,DIM).*(pbest-x) + c2*rand(popsize,DIM).*(lbest -x); % Clamp veloctiy oneForViolation = v < repmat(vmin,popsize,1); v = (1-oneForViolation).*v + oneForViolation.*repmat(vmin,popsize,1); oneForViolation = v > repmat(vmax,popsize,1); v = (1-oneForViolation).*v + oneForViolation.*repmat(vmax,popsize,1); % Update position x = x + v; % Reflect-Z for particles out of bounds -- S. Helwig, J. Branke, and S. Mostaghim, "Experimental Analysis of Bound Handling Techniques in Particle Swarm Optimization," IEEE TEC: 17(2), 2013, pp. 259-271 % reflect lower bound relectionAmount = repmat(xmin,popsize,1) - x; oneForNeedReflection = relectionAmount > zeros(popsize,DIM); relectionAmount = (1-oneForNeedReflection).*zeros(popsize,DIM) + oneForN eedReflection.*relectionAmount;

else lbest(1. end % Function to update lbest function [lbest] = update_lbest(costX. gbestCost = min(pbestCosts).1) . popsize) %particle 1 is neighbours with particle n=popsize sm(1. numEvals = numEvals + popsize. 1:2).*zeros(popsize. end end evalValues(1. popsize). pbestCosts(index) = newCosts(index).1). :) = x(index-1. oneForNeedReflection = relectionAmount < zeros(popsize. end for i = 2:popsize-1 . 1)= costX(1. popsize). % then reflect x = x + relectionAmount.*v + oneForNeedReflection.:) = x(index. end end [lbest] = update_lbest(pbestCosts.x. % then reflect x = x + relectionAmount. nextEval) = gbestCost.*repmat(xmax. sm(1.*repmat(xmin. % set velocity for reflected particles to zero v = (1-oneForNeedReflection).*zeros(popsize. 2:3)= costX(1.1). if numEvals >= evalPoints(nextEval) * maxevals evalValues(1. for index = 1:popsize if newCosts(index) < pbestCosts(index) pbest(index. [cost. 11) = gbestCost. relectionAmount = (1-oneForNeedReflection). functionNumber).:). x. :). and gbest newCosts = feval(fhd.*v + oneForNeedReflection. :). % Update pbest.*x + oneForNeedReflection. :) = x(popsize.DIM) + oneForN eedReflection. nextEval = nextEval + 1. % set velocity for reflected particles to zero v = (1-oneForNeedReflection).popsize. index] = min(sm).*x + oneForNeedReflection.pops ize.DI M).*zeros(popsize.DI M). % reflect upper bound relectionAmount = repmat(xmax. lbest. x.% clampfirst x = (1-oneForNeedReflection).*relectionAmount.pops ize.DIM). x'. % clampfirst x = (1-oneForNeedReflection). if index==1 lbest(1.

end % particle n=popsize is neighbours with particle 1 sm(1. [cost. :) = x(1. index] = min(sm). :) = x(i+index-2. index] = min(sm). :). :). :). end end . if index==3 lbest(popsize. 3)= costX(1. sm(1. [cost. 1). 1:2)= costX(1. :) = x(popsize-2+index. popsize-1:popsize). lbest(i. i-1:i+1). else lbest(popsize. 1:3)= costX(1.sm(1.