一句話談粒子群最佳化演算法

來源:互聯網
上載者:User
簡介:C語言實現基本PSO演算法
C語言實現標準PSO演算法1. 先看看對PSO的語言描述一句話概括粒子群(Particle Swarm Optimization,PSO)的核心思想:牛人往往都是成對出現的。所以,如果想讓自己變的更牛,那就得往牛人身邊靠,果斷抱大腿,PSO的最初的思路源於鳥類和魚類在覓食,它基於以下的假設:在覓食鳥類的周圍必定存在食物,因此我們最好在其周圍尋找。比如,電線杆上面有站著一群鳥,有一隻飛下來在路上吃稻子,則其他鳥類看到後便會飛到此鳥周圍覓食,都是一樣道理。粒子群可以說是最簡單的啟發學習法最佳化演算法。對它的理解只需要簡單的數學向量的加減數乘即可。它的核心就兩公式:但是貌似參數還不少,α和β是常數;∈1和∈2是屬於0到1之間的隨機數向量;⊙為點乘,即兩個向量的乘累加。g*為全域”最牛“解。x*為個人曆史”最牛“解。所以拿那兩個”最牛“解去減去x,即為”向牛人學習“,在數學上的表示為”將自身指向最優解的向量疊加到自己的身上“,是不是和“向牛人學習之後的知識融入到自身的知識體系中”非常類似?!至於v,是一個中間的變數,在數學上是可以消掉的。2. 看完描述再看看更接近代碼形式的描述:這裡如果要對PSO進行改進的話:最容易想到的就是對這一個更新公式進行改進,當然還有別的,因為從Google Scholar上一搜,對PSO的開山之作已經超過了3萬多次的引用。3. 看完接近代碼形式的描述再看看真代碼的實現: pso_simpledemo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
% The Particle Swarm Optimization
% (written by X S Yang, Cambridge University)
% Usage: pso(number_of_particles,Num_iterations)
% eg: best=pso_demo(20,10);
% where best=[xbest ybest zbest] %an n by 3 matrix
% xbest(i)/ybest(i) are the best at ith iteration
function [best]=pso_simpledemo(n,Num_iterations)
% n=number of particles
% Num_iterations=total number of iterations
if nargin<2, Num_iterations=10; end
if nargin<1, n=20; end
% Michaelewicz Function f*=-1.801 at [2.20319, 1.57049]
% Splitting two parts to avoid a long line for printing
str1='-sin(x)*(sin(x^2/3.14159))^20';
str2='-sin(y)*(sin(2*y^2/3.14159))^20';
funstr=strcat(str1,str2);
% Converting to an inline function and vectorization
f=vectorize(inline(funstr));
% range=[xmin xmax ymin ymax];
range=[0 4 0 4];

% ----------------------------------------------------
% Setting the parameters: alpha, beta
% Random amplitude of roaming particles alpha=[0,1]
% alpha=gamma^t=0.7^t;
% Speed of convergence (0->1)=(slow->fast)
beta=0.5;
% ----------------------------------------------------
% Grid values of the objective function
% These values are used for visualization only
Ngrid=100;
dx=(range(2)-range(1))/Ngrid;
dy=(range(4)-range(3))/Ngrid;
xgrid=range(1):dx:range(2);
ygrid=range(3):dy:range(4);
[x,y]=meshgrid(xgrid,ygrid);
z=f(x,y);
% Display the shape of the function to be optimized
figure(1);
surfc(x,y,z);
% ---------------------------------------------------
%best=zeros(Num_iterations,3); 
% initialize history
% ----- Start Particle Swarm Optimization -----------
% generating the initial locations of n particles
[xn,yn]=init_pso(n,range);
% Display the paths of particles in a figure
% with a contour of the objective function
figure(2);
% Start iterations
for i=1:Num_iterations,
    % Show the contour of the function
    contour(x,y,z,15); hold on;
    % Find the current best location (xo,yo)
    zn=f(xn,yn);
    zn_min=min(zn);
    xo=min(xn(zn==zn_min));
    yo=min(yn(zn==zn_min));
    zo=min(zn(zn==zn_min));
    % Trace the paths of all roaming particles
    % Display these roaming particles
    plot(xn,yn,'.',xo,yo,'*'); axis(range);
    % The accelerated PSO with alpha=gamma^t
    gamma=0.7; alpha=gamma.^i;
    % Move all the particles to new locations
    [xn,yn]=pso_move(xn,yn,xo,yo,alpha,beta,range);
    drawnow;
    % Use "hold on" to display paths of particles
    hold off;
    % History
    best(i,1)=xo; best(i,2)=yo; best(i,3)=zo;
end %%%%% end of iterations
% ----- All subfunctions are listed here -----
% Intial locations of n particles
function [xn,yn]=init_pso(n,range)
    xrange=range(2)-range(1);
    yrange=range(4)-range(3);
    xn=rand(1,n)*xrange+range(1);
    yn=rand(1,n)*yrange+range(3);
% Move all the particles toward (xo,yo)
function [xn,yn]=pso_move(xn,yn,xo,yo,a,b,range)
    nn=size(yn,2); %a=alpha, b=beta
    xn=xn.*(1-b)+xo.*b+a.*(rand(1,nn)-0.5);
    yn=yn.*(1-b)+yo.*b+a.*(rand(1,nn)-0.5);
    [xn,yn]=findrange(xn,yn,range);
% Make sure the particles are within the range
function [xn,yn]=findrange(xn,yn,range)
    nn=length(yn);
    for i=1:nn,
        if xn(i)<=range(1), xn(i)=range(1); end
        if xn(i)>=range(2), xn(i)=range(2); end
        if yn(i)<=range(3), yn(i)=range(3); end
        if yn(i)>=range(4), yn(i)=range(4); end
    end

實驗結果:參考資料:Introduction to Mathematical Optimization (Xin-She Yang)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.