粒子群优化算法
来源:互联网 发布:蒂姆邓肯数据 编辑:程序博客网 时间:2024/04/29 05:54
- #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
0 0
- 粒子群优化算法
- 粒子群优化算法
- 粒子群优化算法
- 粒子群优化算法
- 粒子群优化算法
- 粒子群优化算法
- 粒子群优化算法
- 粒子群优化算法
- PSO粒子群优化算法
- PSO粒子群优化算法
- PSO粒子群优化算法
- 粒子群优化算法思考
- 粒子群优化算法简介
- pso粒子群优化算法
- 粒子群优化算法(PSO)
- 粒子群优化算法介绍
- PSO-粒子群优化算法
- 粒子群优化算法(PSO)
- The controlfile header block returned by the OS has a sequence number that is too old
- angualrJS支持的event
- 做注册的验证,在服务器端处理,客服端ajax异步提交数据
- Debug Assertion Failed! f:\sp\vctools\vc7libs\ship\atlmfc\src\mfc\dlgdata.cpp Line: 42
- java 获取路径工具类
- 粒子群优化算法
- 挖掘x-forwarded-for注入
- js页面描点跳转(非常实用精简)
- 关于MySQL desc关键字误用为自定义属性引发的错误
- 《Win32多线程程序设计》(8)---Interlocked Variables
- maven src/main/java 下install非.java文件
- 推荐15款响应式Web设计测试工具
- android死机问题
- hdu 2159 FATE