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 onc1 = 1.49445;C2 = 1.49445;maxgen = 1000; Sizepop = 100; Vmax = 1; Vmin = -1;popmax = 5; Popmin = -5;for i = 1:sizepop pop (i,:) = 5*rands); V (i,:) = Rands); Fitness (i) = Fun (Pop (i,:)); end[bestfitness Bestindex] = max (fitness); zbest = Pop (bestindex,:); Gbest = Pop; Fitnessgbest = fitness; Fitnesszbest = bestfitness; For i = 1:maxgen for j = 1:sizepop V (j,:) = V (j,:) + c1*rand* (gbest (J,:)-pop (J,:)) + c2*rand* (zbest-p OP (J,:)); V (J,find (V (j,:) >vmax) = Vmax; V (J,find (V (j,:) <vmin)) = Vmin; Pop (j,:) = Pop (j,:) + V (j,:); Pop (J,find (pop (J,:) >popmax)) = Popmax; Pop (J,find (pop (J,:) <popmin)) = Popmin; Fitness (j) = Fun (Pop (j,:)); End for j = 1:sizepop If Fitness (j) > Fitnessgbest (J) Gbest (j,:) = Pop (j,:); FitnEssgbest (j) = Fitness (j); End If Fitness (j) > Fitnesszbest zbest = Pop (j,:); Fitnesszbest = Fitness (j); End End YY (i) = fitnesszbest; End[fitnesszbest, Zbest]plot3 (zbest (1), Zbest (2), fitnesszbest, ' ro ', ' linewidth ', 1.5) title (' Particle swarm algorithm: A three-dimensional grid diagram of the target function drawn, Red circle for the most advantage-jason niu ') figureplot (yy) title (' PSO: Using particle swarm optimization algorithm to find the optimal individual fitness for the target function-jason niu ', ' fontsize ', ' xlabel ', ' the ' Evolutionary algebra ', ' FontSize ', Ylabel (' Fitness ', ' fontsize ', 12);
PSO: Using PSO algorithm to optimize two-element function to find optimal individual fitness-jason NIU