Hello Mat

 找回密码
 立即注册
查看: 18129|回复: 0

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

[复制链接]

1323

主题

1551

帖子

0

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
发表于 2017-3-7 13:19:15 | 显示全部楼层 |阅读模式
零基础学习Python--粒子群算法PSO函数寻优-2:(菜鸟编程solemnysw)
版权声明:本文为博主原创文章,未经博主允许不得转载。严格禁止其它商用行为!
百度网盘链接:
链接:http://pan.baidu.com/s/1hrYe6cW
链接:https://pan.baidu.com/s/1yECy3SICh_8a1QZzmtu5ww 提取码:n4ip

具体链接在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 numpy
  5. from numpy import random
  6. import fun
  7. # web('halcom.cn')

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

  24. # init pop
  25. for i in range(sizepop):
  26.     pop[i] = popmin + (popmax-popmin)*random.random( [ nVar, 1] );
  27.     V[i] = numpy.array( [ [0], [0] ] );
  28.     fitness[i] = fun.funfitness2(pop[i]);
  29.     gbest[i] = pop[i]
  30.     fitnessgbest[i] = fitness[i]
  31. fitnesszbest = min(fitness);
  32. index = fun.minIndex(fitness, fitnesszbest);
  33. zbest = pop[index]

  34. fitness_iter = [0 for x in range(maxg)]
  35. # main loop
  36. for i in range(maxg):
  37.     for j in range(sizepop):
  38.         # update vel
  39.         r1 = random.random();
  40.         r2 = random.random();
  41.         V[j] = V[j] + c1*r1*(gbest[j]-pop[j])+c2*r2*(zbest-pop[j]);
  42.         if (V[j][0]>Vmax):
  43.             V[j][0] = Vmax;
  44.         elif (V[j][0]<Vmin):
  45.             V[j][0] = Vmin;
  46.         if (V[j][1]>Vmax):
  47.             V[j][1] = Vmax;
  48.         elif (V[j][1]<Vmin):
  49.             V[j][1] = Vmin;

  50.         # update position
  51.         pop[j] = pop[j] + 0.5*V[j];
  52.         if (pop[j][0]>popmax[0]):
  53.             pop[j][0] = popmax[0];
  54.         elif (pop[j][0]<popmin[0]):
  55.             pop[j][0] = popmin[0];
  56.         if (pop[j][1]>popmax[1]):
  57.             pop[j][1] = popmax[1];
  58.         elif (pop[j][1]<popmin[1]):
  59.             pop[j][1] = popmin[1];
  60.         
  61.         # update fitness
  62.         fitness[j] = fun.funfitness2(pop[j]);

  63.         # compare
  64.         if (fitness[j]<fitnessgbest[j]):
  65.             fitnessgbest[j] = fitness[j]
  66.             gbest[j][0] = pop[j][0];
  67.             gbest[j][1] = pop[j][1];
  68.         if (fitness[j]<fitnesszbest):
  69.             fitnesszbest= fitness[j];
  70.             zbest = pop[j];
  71.     fitness_iter[i] = fitnesszbest;

  72. print "best solution is:"
  73. print zbest

  74. print "min objective value is:"
  75. print fitnesszbest

  76. plot( fitness_iter,'b-' )
  77. show()
复制代码
子函数如下:
  1. def funfitness(x1,x2):
  2.     y = (x1-0.5)**2+(x2-0.6)**2;
  3.     return y;

  4. def funfitness2(x):
  5.     y = (x[0]-0.5)**2+(x[1]-0.6)**2;
  6.     return y;


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







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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-22 22:33 , Processed in 0.217603 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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