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

Hello Mat

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

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

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 2019-8-22 22:45:25 | 显示全部楼层 |阅读模式
零基础学习Python--粒子群算法PSO函数寻优-3:

链接:https://pan.baidu.com/s/1yECy3SICh_8a1QZzmtu5ww 提取码:n4ip
将fun.py文件和mian函数写到一起:
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor

  4. This is a temporary script file.
  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 fun

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

  15. def minIndex(fitness, fitnesszbest):
  16.     for i in range(len(fitness)):
  17.         if(fitness[i] == fitnesszbest):
  18.             break;
  19.     return i;

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





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

使用道具 举报

0

主题

18

帖子

1

金钱

新手上路

Rank: 1

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 07:23 , Processed in 0.208661 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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