本文主要是介绍PSO算法学习心得,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 算法基本思想
粒子群优化算法属于群智能(swarm intelligence)优化算法。群智能分两种,一种是粒群优化,一种是蚁群优化。
群智能概念:假设你和你的朋友们(individual)去寻宝(objective),每个人都有一个探测器(function)可以知道宝藏到探测器的距离。在找的过程中,每个人都可以把信息共享出去,每个人都能看到现在谁离宝藏最近。这样,你看谁离宝藏最近,就以某种速度(velocity)向谁靠近,使得个体发现宝藏的机会变大,也比单人找的要快。在群智能的计算研究中,群的个体组织包括蚂蚁、白蚁、蜜蜂、黄蜂、鱼群、鸟群等。研究人员发现,蚂蚁在鸟巢和事物之间的运输路线,不管一开始多随机,最后蚂蚁总能找到一条最短路径。
粒群优化概念:粒群优化(particle swarm optimization, PSO)算法是一种基于群体搜索的算法,它建立在模拟鸟群社会的基础上。粒群概念的最初含义是通过图形来模拟鸟群优美和不可预测的舞蹈动作,发现鸟群支配同步飞行和以最佳队形突然改变飞行方向并重新编队的能力。这个概念已经被包含在一个简单有效的优化算法中。
二 算法描述
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- #define C1 2
- #define C2 2
- #define VMAX 5.0
- #define MAX_ITERATIONS 100
- float rand01()
- {
- return (float) (rand()/(double)RAND_MAX);
- }
- struct particle{
- float current;
- float pbest;
- };
- float fitness(float x)
- {
- return x*x - 20*x + 100;
- }
- float gbest = 10000;
- struct particle p[5];
- float v[5] = {0};
- void init_particles()
- {
- int i;
- for(i = 0; i < 5; i++)
- {
- p[i].current = -2+i;
- p[i].pbest = p[i].current;
- }
- }
- void find_gbest()
- {
- int i;
- for(i = 0; i < 5; i++)
- {
- if(fitness(gbest) > fitness(p[i].current))
- gbest = p[i].current;
- }
- }
- void adjust_v()
- {
- int i ;
- for(i = 0; i < 5; i++)
- {
- v[i] = v[i] + C1*rand01()*(p[i].pbest - p[i].current) + C2*rand01()*(gbest - p[i].current);
- if(v[i] > VMAX)
- v[i] = VMAX;
- }
- }
- void pso()
- {
- int i,iter_num;
- iter_num = 1;
- while(iter_num < MAX_ITERATIONS)
- {
- /*for(i = 0; i < 5; i++)
- {
- cout <<"p"<<i<<":current "<<p[i].current<<" pbest "<<p[i].pbest<<endl;
- }
- cout <<"gbest:"<<gbest<<endl;
- cout <<endl;
- getchar();*/
- for(i = 0; i < 5; i++)
- {
- if(fitness(p[i].current) < fitness(p[i].pbest))
- p[i].pbest = p[i].current;
- }
- find_gbest();
- adjust_v();
- for(i = 0; i < 5; i++)
- p[i].current += v[i];
- iter_num ++;
- }
- }
- int main()
- {
- init_particles();
- pso();
- printf("After %d iterations,gbest is %f\n",MAX_ITERATIONS,gbest);
- return 0;
- }
- After 1 iterations
- p0:current -2 pbest -2
- p1:current -1 pbest -1
- p2:current 0 pbest 0
- p3:current 1 pbest 1
- p4:current 2 pbest 2
- gbest:10000
- After 2 iterations
- p0:current 1.15506 pbest -2
- p1:current 3.79064 pbest -1
- p2:current 0.790205 pbest 0
- p3:current 2.53646 pbest 1
- p4:current 2 pbest 2
- gbest:2
- After 3 iterations
- p0:current 6.15506 pbest 1.15506
- p1:current 8.58128 pbest 3.79064
- p2:current 5.79021 pbest 0.790205
- p3:current 5.87216 pbest 2.53646
- p4:current 4.17373 pbest 2
- gbest:3.79064
- After 4 iterations
- p0:current 11.1551 pbest 6.15506
- p1:current 13.3719 pbest 8.58128
- p2:current 10.7902 pbest 5.79021
- p3:current 9.79741 pbest 5.87216
- p4:current 8.27141 pbest 4.17373
- gbest:8.58128
- After 5 iterations
- p0:current 13.8766 pbest 11.1551
- p1:current 10.1764 pbest 8.58128
- p2:current 14.7492 pbest 10.7902
- p3:current 13.7227 pbest 9.79741
- p4:current 13.2714 pbest 8.27141
- gbest:9.79741
- After 6 iterations
- p0:current 8.03327 pbest 11.1551
- p1:current 6.98078 pbest 10.1764
- p2:current 13.2414 pbest 10.7902
- p3:current 4.78856 pbest 9.79741
- p4:current 11.6974 pbest 8.27141
- gbest:10.1764
- After 7 iterations
- p0:current 5.84287 pbest 11.1551
- p1:current 9.25245 pbest 10.1764
- p2:current 5.23059 pbest 10.7902
- p3:current -3.28694 pbest 9.79741
- p4:current 9.93147 pbest 11.6974
- gbest:10.1764
这篇关于PSO算法学习心得的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!