请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 19485|回复: 5

零基础学习Python--粒子群算法PSO函数寻优-1

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 2017-3-6 21:36:05 | 显示全部楼层 |阅读模式
零基础学习Python--粒子群算法PSO函数寻优:(菜鸟编程solemnysw)
版权声明:本文为博主原创文章,未经博主允许不得转载。严格禁止其它商用行为!
百度网盘链接:
链接:http://pan.baidu.com/s/1c19Qjjq
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1主函数:
  1. from PIL import Image
  2. from pylab import *
  3. import string
  4. import matplotlib.pyplot as plt  
  5. import numpy as np
  6. import random
  7. import fun

  8. #print random.random()
  9. # PSO algorithm parameters
  10. c1 = 1.49445;
  11. c2 = 1.49445;
  12. maxg=200;  
  13. sizepop=20;
  14. Vmax=1;
  15. Vmin=-1;
  16. popmax=5;
  17. popmin=-5;
  18. nVar = 2;
  19. pop = [[0 for x in range(nVar)] for y in range(sizepop)]
  20. gbest = [[0 for x in range(nVar)] for y in range(sizepop)]
  21. fitness = [0 for x in range(sizepop)]
  22. fitnessgbest = [0 for x in range(sizepop)]
  23. zbest = [0 for x in range(nVar)]
  24. V = [[0 for x in range(nVar)] for y in range(sizepop)]

  25. # init pop
  26. for i in range(sizepop):
  27.     pop[i][0] = popmin + (popmax-popmin)*random.random();
  28.     pop[i][1] = popmin + (popmax-popmin)*random.random();
  29.     fitness[i] = fun.funfitness(pop[i][0],pop[i][1]);
  30.     gbest[i][0] = pop[i][0]
  31.     gbest[i][1] = pop[i][1]
  32.     fitnessgbest[i] = fitness[i]
  33.     print fitness[i]
  34. fitnesszbest = min(fitness);
  35. index = fun.minIndex(fitness, fitnesszbest);
  36. zbest[0] = pop[index][0]
  37. zbest[1] = pop[index][1]
  38. print zbest
  39. #plot(range(sizepop), fitness,'r*-')
  40. #plot( fitness,'r*-')
  41. #show()

  42. fitness_iter = [0 for x in range(maxg)]
  43. # main loop
  44. for i in range(maxg):
  45.     for j in range(sizepop):
  46.         # update vel
  47.         r1 = random.random();
  48.         r2 = random.random();
  49.         V[j][0] = V[j][0] + c1*r1*(gbest[j][0]-pop[j][0])+c2*r2*(zbest[0]-pop[j][0]);
  50.         V[j][1] = V[j][1] + c1*r1*(gbest[j][1]-pop[j][1])+c2*r2*(zbest[1]-pop[j][1]);
  51.         if (V[j][0]>Vmax):
  52.             V[j][0] = Vmax;
  53.         elif (V[j][0]<Vmin):
  54.             V[j][0] = Vmin;
  55.         if (V[j][1]>Vmax):
  56.             V[j][1] = Vmax;
  57.         elif (V[j][1]<Vmin):
  58.             V[j][1] = Vmin;
  59.         # update position
  60.         pop[j][0] = pop[j][0] + 0.5*V[j][0];
  61.         pop[j][1] = pop[j][1] + 0.5*V[j][1];
  62.         if (pop[j][0]>popmax):
  63.             pop[j][0] = popmax;
  64.         elif (pop[j][0]<popmin):
  65.             pop[j][0] = popmin;
  66.         if (pop[j][1]>popmax):
  67.             pop[j][1] = popmax;
  68.         elif (pop[j][1]<popmin):
  69.             pop[j][1] = popmin;
  70.         
  71.         fitness[j] = fun.funfitness(pop[j][0],pop[j][1]);
  72.         if (fitness[j]<fitnessgbest[j]):
  73.             fitnessgbest[j] = fitness[j]
  74.             gbest[j][0] = pop[j][0];
  75.             gbest[j][1] = pop[j][1];
  76.         if (fitness[j]<fitnesszbest):
  77.             fitnesszbest= fitness[j];
  78.             zbest[0] = pop[j][0];
  79.             zbest[1] = pop[j][1];
  80.     fitness_iter[i] = fitnesszbest;

  81. print "最优解:"
  82. print zbest
  83. plot( fitness_iter,'b-' )
  84. show()
复制代码
相应的fun子函数如下:
  1. def funfitness(x1,x2):
  2.     y = (x1-0.5)**2+(x2-0.6)**2;
  3.     return y;

  4. def minIndex(fitness, fitnesszbest):
  5.     for i in range(len(fitness)):
  6.         if(fitness[i] == fitnesszbest):
  7.             break;
  8.     return i;
复制代码








算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

0

主题

13

帖子

36

金钱

新手上路

Rank: 1

积分
49
发表于 2020-2-18 13:00:47 | 显示全部楼层
谢谢楼主无私的分享
回复 支持 反对

使用道具 举报

0

主题

3

帖子

1

金钱

新手上路

Rank: 1

积分
25
发表于 2020-2-20 01:29:29 | 显示全部楼层
谢谢分享。
回复

使用道具 举报

0

主题

9

帖子

1

金钱

新手上路

Rank: 1

积分
10
发表于 2020-4-11 14:14:27 | 显示全部楼层
感谢大佬,谢谢分享。
回复 支持 反对

使用道具 举报

0

主题

18

帖子

1

金钱

新手上路

Rank: 1

积分
19
发表于 2020-4-28 15:57:22 | 显示全部楼层
谢谢楼主无私的分享
回复 支持 反对

使用道具 举报

0

主题

16

帖子

1

金钱

新手上路

Rank: 1

积分
8
发表于 2020-8-21 23:09:07 | 显示全部楼层

谢谢楼主无私的分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-28 23:10 , Processed in 0.194928 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表