PSO:利用PSO算法优化二元函数,寻找最优个体适应度—Jason niu

Posted 一个处女座的IT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PSO:利用PSO算法优化二元函数,寻找最优个体适应度—Jason niu相关的知识,希望对你有一定的参考价值。

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 on

c1 = 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(1,2); 
    V(i,:) = rands(1,2);    
    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 - pop(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(\'粒子群算法:绘制的目标函数三维网格图,红圈为最优点—Jason niu\')

figure
plot(yy)
title(\'PSO:利用粒子群算法实现对目标函数寻找最优个体适应度—Jason niu\',\'fontsize\',12);
xlabel(\'进化代数\',\'fontsize\',12);ylabel(\'适应度\',\'fontsize\',12);

 

以上是关于PSO:利用PSO算法优化二元函数,寻找最优个体适应度—Jason niu的主要内容,如果未能解决你的问题,请参考以下文章

粒子群优化算法(PSO)找最优解

粒子群优化算法(PSO)

优化算法粒子群优化算法(PSO)含Matlab源码 1073期

梯度下降法和粒子群优化算法的区别

粒子群算法 | 启发式优化算法

粒子群优化算法(PSO)