Finally, I found a good tutorial on the into gold of the refinery.
Get it sorted out here.
The finishing touches of a particle swarm algorithm.
Main.m
I. Empty environment clcclear%% Ii. Draw target function curve figure[x,y] = Meshgrid ( -5:0.1:5,-5:0.1:5), z = x.^2 + y.^2-10*cos (2*pi*x)-10*cos (2*pi*y) + 20;mesh (x, y, z) hold O n%% III. Parameter initialization C1 = 1.49445;C2 = 1.49445;maxgen = 1000; % Evolutionary number Sizepop = 100; % population size Vmax = 1; Vmin = -1;popmax = 5;popmin = -5;%% Iv. Produces the initial particle and velocity for i = 1:sizepop% randomly produces a population pop (i,:) = 5*rands (); % Initial population V (i,:) = Rands); % initialization speed% calculation of fitness Fitness (i) = Fun (Pop (i,:)); Percent fitness of the chromosome end%% v. individual extremum and group extremum [bestfitness Bestindex] = max (fitness); zbest = Pop (bestindex,:); % global best gbest = Pop; % individual best fitnessgbest = fitness; % of individual optimal fitness value fitnesszbest = bestfitness; % global best fit value percent of VI. Iterative optimization for i = 1:maxgen for j = 1:sizepop% Speed Update V (j,:) = V (j,:) + c1*rand* (gbest (J,:)-pop (J,:)) + c2* Rand* (Zbest-pop (J,:)); V (J,find (V (j,:) >vmax) = Vmax; V (J,find (V (j,:) <vmin)) = Vmin; % Population Update pop (j,:) = Pop (j,:) + V (j,:); Pop (J,find (pop (J,:) >popmax)) = Popmax; Pop (Pop (J,find (j,:)<popmin)) = Popmin; % Fitness Value Update Fitness (j) = Fun (Pop (j,:)); End for j = 1:sizepop% individual optimal update if fitness (j) > Fitnessgbest (J) Gbest (j,:) = Pop (j,:); Fitnessgbest (j) = Fitness (j); End% population optimal Update if fitness (j) > Fitnesszbest zbest = Pop (j,:); Fitnesszbest = Fitness (j); End End YY (i) = fitnesszbest; end%% vii. Output [Fitnesszbest, Zbest]plot3 (zbest (1), Zbest (2), Fitnesszbest, ' Bo ', ' linewidth ', 1.5) Figureplot (yy) title (' Optimal individual fitness ', ' fontsize ', ' n '); Xlabel (' Evolutionary algebra ', ' fontsize ', ') ylabel (' Fitness ', ' fontsize ', 12);
Fun.m
Function y = fun (x)% functions are used to calculate particle fitness values of%x input particles%y output particle fitness value y = x (1). ^2 + x (2). ^2-10*cos (2*pi*x (1))- 10*cos (2*pi*x (2)) + 20;
Matlab elementary particle swarm Algorithm realization (four)