Hello Mat

 找回密码
 立即注册
查看: 16363|回复: 3

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

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2019-8-22 23:13:51 | 显示全部楼层 |阅读模式
零基础学习Python--粒子群算法PSO函数寻优-5
链接:https://pan.baidu.com/s/1EuV2HkC_5oR7G6G-S1bXeA 提取码:cwmo
链接:https://pan.baidu.com/s/1yECy3SICh_8a1QZzmtu5ww 提取码:n4ip

精简一下代码:
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Aug 22 23:02:07 2019

  4. @author: ysw
  5. """

  6. from PIL import Image
  7. from pylab import *
  8. import matplotlib.pyplot as plt  
  9. import numpy as np
  10. from random import *
  11. import sys
  12. #import fun
  13. sys.path.append(r'C:\Users\ysw\Desktop\Python(x,y)2.7.10\PSO')

  14. def funfitness(x1,x2):
  15.     y = (x1-0.5)**2+(x2-0.6)**2;
  16.     return y;

  17. # 采用 np.argmax替代
  18. #    def minIndex(fitness, fitnesszbest):
  19. #        for i in range(len(fitness)):
  20. #            if(fitness[i] == fitnesszbest):
  21. #                break;
  22. #        return i;

  23. if __name__=="__main__":
  24.     c1 = 1.49445;
  25.     c2 = 1.49445;
  26.     maxg=200;  
  27.     sizepop=20;
  28.     Vmax=1;
  29.     Vmin=-1;
  30.     popmax=5;
  31.     popmin=-5;
  32.     nVar = 2;
  33.     #print random.random()
  34.     # PSO algorithm parameters
  35.     pop = [[0 for x in range(nVar)] for y in range(sizepop)]
  36.     gbest = [[0 for x in range(nVar)] for y in range(sizepop)]
  37.     fitness = [0 for x in range(sizepop)]
  38.     fitnessgbest = [0 for x in range(sizepop)]
  39.     zbest = [0 for x in range(nVar)]
  40.     V = [[0 for x in range(nVar)] for y in range(sizepop)]
  41.    
  42.     # init pop
  43.     for i in range(sizepop):
  44. #        pop[i][0] = popmin + (popmax-popmin)*random();
  45. #        pop[i][1] = popmin + (popmax-popmin)*random();
  46.         pop[i] = popmin + (popmax-popmin)*np.random.random(nVar);
  47.         fitness[i] = funfitness(pop[i][0],pop[i][1]);
  48. #        gbest[i][0] = pop[i][0]
  49. #        gbest[i][1] = pop[i][1]
  50.         gbest[i] = pop[i]
  51.         fitnessgbest[i] = fitness[i]
  52.         print(fitness[i])
  53.     fitnesszbest = min(fitness);
  54. #    index = fun.minIndex(fitness, fitnesszbest);
  55.     index = np.argmin( fitness )
  56. #    zbest[0] = pop[index][0]
  57. #    zbest[1] = pop[index][1]
  58.     zbest = pop[index]
  59.     print(zbest)
  60.     #plot(range(sizepop), fitness,'r*-')
  61.     #plot( fitness,'r*-')
  62.     #show()
  63.    
  64.     fitness_iter = [0 for x in range(maxg)]
  65.     # main loop
  66.     for i in range(maxg):
  67.         for j in range(sizepop):
  68.             # update vel
  69.             r1 = random();
  70.             r2 = random();
  71. #            V[j][0] = V[j][0] + c1*r1*(gbest[j][0]-pop[j][0])+c2*r2*(zbest[0]-pop[j][0]);
  72. #            V[j][1] = V[j][1] + c1*r1*(gbest[j][1]-pop[j][1])+c2*r2*(zbest[1]-pop[j][1]);
  73.             V[j] = V[j] + c1*r1*(gbest[j]-pop[j])+c2*r2*(zbest-pop[j]);
  74.             if (V[j][0]>Vmax):
  75.                 V[j][0] = Vmax;
  76.             elif (V[j][0]<Vmin):
  77.                 V[j][0] = Vmin;
  78.             if (V[j][1]>Vmax):
  79.                 V[j][1] = Vmax;
  80.             elif (V[j][1]<Vmin):
  81.                 V[j][1] = Vmin;
  82.             # update position
  83. #            pop[j][0] = pop[j][0] + 0.5*V[j][0];
  84. #            pop[j][1] = pop[j][1] + 0.5*V[j][1];
  85.             pop[j] = pop[j] + 0.5*V[j]
  86.             
  87.             if (pop[j][0]>popmax):
  88.                 pop[j][0] = popmax;
  89.             elif (pop[j][0]<popmin):
  90.                 pop[j][0] = popmin;
  91.             if (pop[j][1]>popmax):
  92.                 pop[j][1] = popmax;
  93.             elif (pop[j][1]<popmin):
  94.                 pop[j][1] = popmin;
  95.             
  96.             fitness[j] = funfitness(pop[j][0],pop[j][1]);
  97.             if (fitness[j]<fitnessgbest[j]):
  98.                 fitnessgbest[j] = fitness[j]
  99. #                gbest[j][0] = pop[j][0];
  100. #                gbest[j][1] = pop[j][1];
  101.                 gbest[j] = pop[j];
  102.             if (fitness[j]<fitnesszbest):
  103.                 fitnesszbest= fitness[j];
  104. #                zbest[0] = pop[j][0];
  105. #                zbest[1] = pop[j][1];
  106.                 zbest = pop[j];
  107.         fitness_iter[i] = fitnesszbest;
  108.    
  109.     print("最优解:")
  110.     print(zbest)
  111.     plot( fitness_iter,'b-' )
  112.     plt.show()
复制代码







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

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
 楼主| 发表于 2019-8-22 23:18:50 | 显示全部楼层
简介瘦身代码:
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Aug 22 23:02:07 2019

  4. @author: ysw
  5. """

  6. from PIL import Image
  7. from pylab import *
  8. import matplotlib.pyplot as plt  
  9. import numpy as np
  10. from random import *
  11. import sys

  12. sys.path.append(r'C:\Users\ysw\Desktop\Python(x,y)2.7.10\PSO')

  13. def funfitness(x1,x2):
  14.     y = (x1-0.5)**2+(x2-0.6)**2;
  15.     return y;

  16. if __name__=="__main__":
  17.     c1 = 1.49445;
  18.     c2 = 1.49445;
  19.     maxg=200;  
  20.     sizepop=20;
  21.     Vmax=1;
  22.     Vmin=-1;
  23.     popmax=5;
  24.     popmin=-5;
  25.     nVar = 2;
  26.     #print random.random()
  27.     # PSO algorithm parameters
  28.     pop = [[0 for x in range(nVar)] for y in range(sizepop)]
  29.     gbest = [[0 for x in range(nVar)] for y in range(sizepop)]
  30.     fitness = [0 for x in range(sizepop)]
  31.     fitnessgbest = [0 for x in range(sizepop)]
  32.     zbest = [0 for x in range(nVar)]
  33.     V = [[0 for x in range(nVar)] for y in range(sizepop)]
  34.    
  35.     # init pop
  36.     for i in range(sizepop):
  37.         pop[i] = popmin + (popmax-popmin)*np.random.random(nVar);
  38.         fitness[i] = funfitness(pop[i][0],pop[i][1]);
  39.         gbest[i] = pop[i]
  40.         fitnessgbest[i] = fitness[i]
  41.     fitnesszbest = min(fitness);
  42.     index = np.argmin( fitness )
  43.     zbest = pop[index]


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

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

使用道具 举报

0

主题

18

帖子

1

金钱

新手上路

Rank: 1

积分
19
发表于 2020-4-29 17:53:51 | 显示全部楼层

很好 的资源 谢谢楼主
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 17:46 , Processed in 0.244418 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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