PSO算法学习心得

2024-08-22 16:32
文章标签 算法 pso 学习心得

本文主要是介绍PSO算法学习心得,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一 算法基本思想

   粒子群优化算法属于群智能(swarm intelligence)优化算法。群智能分两种,一种是粒群优化,一种是蚁群优化。

       群智能概念:假设你和你的朋友们(individual)去寻宝(objective),每个人都有一个探测器(function)可以知道宝藏到探测器的距离。在找的过程中,每个人都可以把信息共享出去,每个人都能看到现在谁离宝藏最近。这样,你看谁离宝藏最近,就以某种速度(velocity)向谁靠近,使得个体发现宝藏的机会变大,也比单人找的要快。在群智能的计算研究中,群的个体组织包括蚂蚁、白蚁、蜜蜂、黄蜂、鱼群、鸟群等。研究人员发现,蚂蚁在鸟巢和事物之间的运输路线,不管一开始多随机,最后蚂蚁总能找到一条最短路径。

粒群优化概念:粒群优化(particle swarm optimization, PSO)算法是一种基于群体搜索的算法,它建立在模拟鸟群社会的基础上。粒群概念的最初含义是通过图形来模拟鸟群优美和不可预测的舞蹈动作,发现鸟群支配同步飞行和以最佳队形突然改变飞行方向并重新编队的能力。这个概念已经被包含在一个简单有效的优化算法中。

二 算法描述

       先通过一个形象的场景来描述一下:5只鸟觅食,每个鸟都知道自己与食物的距离,并将此信息与其他鸟共享。一开始,5只鸟分散在不同的地方,假设每只鸟每秒钟更新自己的速度和方向,如何更新呢?每只鸟记下自己离食物的最近位置,成为Pbest(i),从这里边选一个最好的Gbest,作为组里最好的。鸟更新自己的速度v表示为:

    v_new  = v_old + c1*rand()*(pbest-pcurrent) +c2*rand()*(gbest-pcurrent)

    c1,c2一般为2,rand()产生0~1的随机数。

以下伪码摘自百度百科

 程序的伪代码如下

  For each particle

  ____Initialize particle

  END

  Do

  ____For each particle

  ________Calculate fitness value

  ________If the fitness value is better than the best fitness value (pBest) in history

  ____________set current value as the new pBest

  ____End

  ____Choose the particle with the best fitness value of all the particles as the gBest

  ____For each particle

  ________Calculate particle velocity according equation (a)

  ________Update particle position according equation (b)

  ____End

  While maximum iterations or minimum error criteria is not attained

  在每一维粒子的速度都会被限制在一个最大速度Vmax,如果某一维更新后的速度超过用户设定的Vmax,那么这一维的速度就被限定为Vmax。

程序实例

计算f(x) = x*x - 20x + 100 的最小值及取最小值时的x

  
  1. #include <iostream>  
  2. #include <cmath>  
  3. #include <cstdlib>  
  4. using namespace std;  
  5.  
  6. #define C1 2  
  7. #define C2 2  
  8. #define VMAX 5.0  
  9. #define MAX_ITERATIONS 100  
  10. float rand01()  
  11. {  
  12.         return (float) (rand()/(double)RAND_MAX);  
  13. }  
  14. struct particle{  
  15.         float current;  
  16.         float pbest;  
  17. };  
  18.  
  19. float fitness(float x)  
  20. {  
  21.         return x*x - 20*x + 100;  
  22. }  
  23.  
  24. float gbest = 10000;  
  25. struct particle p[5];  
  26. float v[5] = {0};  
  27.  
  28. void init_particles()  
  29. {  
  30.         int i;  
  31.         for(i = 0; i < 5; i++)  
  32.         {  
  33.                 p[i].current = -2+i;  
  34.                 p[i].pbest = p[i].current;  
  35.         }  
  36. }  
  37. void find_gbest()  
  38. {  
  39.         int i;  
  40.         for(i = 0; i < 5; i++)  
  41.         {  
  42.                         if(fitness(gbest) > fitness(p[i].current))  
  43.                                 gbest = p[i].current;  
  44.         }  
  45. }  
  46. void adjust_v()  
  47. {  
  48.         int i ;  
  49.         for(i = 0; i < 5; i++)  
  50.         {  
  51.                 v[i] = v[i] + C1*rand01()*(p[i].pbest - p[i].current) + C2*rand01()*(gbest - p[i].current);  
  52.                 if(v[i] > VMAX)  
  53.                         v[i] = VMAX;  
  54.         }  
  55. }  
  56. void pso()  
  57. {  
  58.         int i,iter_num;  
  59.         iter_num = 1;  
  60.         while(iter_num < MAX_ITERATIONS)  
  61.         {  
  62.                 /*for(i = 0; i < 5; i++)  
  63.                 {  
  64.                         cout <<"p"<<i<<":current "<<p[i].current<<" pbest "<<p[i].pbest<<endl;  
  65.                 }  
  66.                 cout <<"gbest:"<<gbest<<endl;  
  67.                 cout <<endl;  
  68.                 getchar();*/ 
  69.                 for(i = 0; i < 5; i++)  
  70.                 {  
  71.                         if(fitness(p[i].current) < fitness(p[i].pbest))  
  72.                                 p[i].pbest = p[i].current;  
  73.                 }  
  74.                 find_gbest();  
  75.                 adjust_v();  
  76.                 for(i = 0; i < 5; i++)  
  77.                         p[i].current += v[i];  
  78.                 iter_num ++;  
  79.         }  
  80. }  
  81. int main()  
  82. {  
  83.  
  84.         init_particles();  
  85.         pso();  
  86.         printf("After %d iterations,gbest is %f\n",MAX_ITERATIONS,gbest);  
  87.         return 0;  
  88. }  

下面是每次迭代后的结果,可以看到,经过5次迭代,结果就很好了。

  
  1. After 1 iterations  
  2. p0:current -2 pbest -2  
  3. p1:current -1 pbest -1  
  4. p2:current 0 pbest 0  
  5. p3:current 1 pbest 1  
  6. p4:current 2 pbest 2  
  7. gbest:10000  
  8.  
  9.  
  10. After 2 iterations  
  11. p0:current 1.15506 pbest -2  
  12. p1:current 3.79064 pbest -1  
  13. p2:current 0.790205 pbest 0  
  14. p3:current 2.53646 pbest 1  
  15. p4:current 2 pbest 2  
  16. gbest:2  
  17.  
  18.  
  19. After 3 iterations  
  20. p0:current 6.15506 pbest 1.15506  
  21. p1:current 8.58128 pbest 3.79064  
  22. p2:current 5.79021 pbest 0.790205  
  23. p3:current 5.87216 pbest 2.53646  
  24. p4:current 4.17373 pbest 2  
  25. gbest:3.79064  
  26.  
  27.  
  28. After 4 iterations  
  29. p0:current 11.1551 pbest 6.15506  
  30. p1:current 13.3719 pbest 8.58128  
  31. p2:current 10.7902 pbest 5.79021  
  32. p3:current 9.79741 pbest 5.87216  
  33. p4:current 8.27141 pbest 4.17373  
  34. gbest:8.58128  
  35.  
  36.  
  37. After 5 iterations  
  38. p0:current 13.8766 pbest 11.1551  
  39. p1:current 10.1764 pbest 8.58128  
  40. p2:current 14.7492 pbest 10.7902  
  41. p3:current 13.7227 pbest 9.79741  
  42. p4:current 13.2714 pbest 8.27141  
  43. gbest:9.79741  
  44.  
  45.  
  46. After 6 iterations  
  47. p0:current 8.03327 pbest 11.1551  
  48. p1:current 6.98078 pbest 10.1764  
  49. p2:current 13.2414 pbest 10.7902  
  50. p3:current 4.78856 pbest 9.79741  
  51. p4:current 11.6974 pbest 8.27141  
  52. gbest:10.1764  
  53.  
  54.  
  55. After 7 iterations  
  56. p0:current 5.84287 pbest 11.1551  
  57. p1:current 9.25245 pbest 10.1764  
  58. p2:current 5.23059 pbest 10.7902  
  59. p3:current -3.28694 pbest 9.79741  
  60. p4:current 9.93147 pbest 11.6974  
  61. gbest:10.1764  

 

本文出自 “牛哥的博客” 博客,请务必保留此出处http://nxlhero.blog.51cto.com/962631/734212



这篇关于PSO算法学习心得的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1096800

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

秋招最新大模型算法面试,熬夜都要肝完它

💥大家在面试大模型LLM这个板块的时候,不知道面试完会不会复盘、总结,做笔记的习惯,这份大模型算法岗面试八股笔记也帮助不少人拿到过offer ✨对于面试大模型算法工程师会有一定的帮助,都附有完整答案,熬夜也要看完,祝大家一臂之力 这份《大模型算法工程师面试题》已经上传CSDN,还有完整版的大模型 AI 学习资料,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

dp算法练习题【8】

不同二叉搜索树 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n = 3输出:5 示例 2: 输入:n = 1输出:1 class Solution {public int numTrees(int n) {int[] dp = new int

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

最大公因数:欧几里得算法

简述         求两个数字 m和n 的最大公因数,假设r是m%n的余数,只要n不等于0,就一直执行 m=n,n=r 举例 以18和12为例 m n r18 % 12 = 612 % 6 = 06 0所以最大公因数为:6 代码实现 #include<iostream>using namespace std;/