| 
 | 
 
零基础学习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- from PIL import Image
 
 - from pylab import *
 
 - import string
 
 - import numpy
 
 - from numpy import random
 
 - import fun
 
 - # web('halcom.cn')
 
  
- # PSO algorithm parameters
 
 - c1 = 1.49445;
 
 - c2 = 1.49445;
 
 - maxg=50;  
 
 - sizepop=20; 
 
 - Vmax=1;
 
 - Vmin=-1;
 
 - popmax= numpy.array( [ [5], [5] ] );  
 
 - popmin = numpy.array( [ [-5], [-4] ] );  
 
 - nVar = 2;  
 
 - pop = [[0 for x in range(nVar)] for y in range(sizepop)]
 
 - gbest = [[0 for x in range(nVar)] for y in range(sizepop)]
 
 - fitness = [0 for x in range(sizepop)]
 
 - fitnessgbest = [0 for x in range(sizepop)]
 
 - zbest = [0 for x in range(nVar)]
 
 - V = [[0 for x in range(nVar)] for y in range(sizepop)]
 
  
- # init pop
 
 - for i in range(sizepop):
 
 -     pop[i] = popmin + (popmax-popmin)*random.random( [ nVar, 1] );
 
 -     V[i] = numpy.array( [ [0], [0] ] ); 
 
 -     fitness[i] = fun.funfitness2(pop[i]);
 
 -     gbest[i] = pop[i]
 
 -     fitnessgbest[i] = fitness[i]
 
 - fitnesszbest = min(fitness);
 
 - index = fun.minIndex(fitness, fitnesszbest);
 
 - zbest = pop[index]
 
  
- fitness_iter = [0 for x in range(maxg)]
 
 - # main loop
 
 - for i in range(maxg):
 
 -     for j in range(sizepop):
 
 -         # update vel
 
 -         r1 = random.random();
 
 -         r2 = random.random();
 
 -         V[j] = V[j] + c1*r1*(gbest[j]-pop[j])+c2*r2*(zbest-pop[j]);
 
 -         if (V[j][0]>Vmax):
 
 -             V[j][0] = Vmax;
 
 -         elif (V[j][0]<Vmin):
 
 -             V[j][0] = Vmin;
 
 -         if (V[j][1]>Vmax):
 
 -             V[j][1] = Vmax;
 
 -         elif (V[j][1]<Vmin):
 
 -             V[j][1] = Vmin;
 
  
-         # update position
 
 -         pop[j] = pop[j] + 0.5*V[j];
 
 -         if (pop[j][0]>popmax[0]):
 
 -             pop[j][0] = popmax[0];
 
 -         elif (pop[j][0]<popmin[0]):
 
 -             pop[j][0] = popmin[0];
 
 -         if (pop[j][1]>popmax[1]):
 
 -             pop[j][1] = popmax[1];
 
 -         elif (pop[j][1]<popmin[1]):
 
 -             pop[j][1] = popmin[1];
 
 -         
 
 -         # update fitness
 
 -         fitness[j] = fun.funfitness2(pop[j]);
 
  
-         # compare
 
 -         if (fitness[j]<fitnessgbest[j]):
 
 -             fitnessgbest[j] = fitness[j]
 
 -             gbest[j][0] = pop[j][0];
 
 -             gbest[j][1] = pop[j][1];
 
 -         if (fitness[j]<fitnesszbest):
 
 -             fitnesszbest= fitness[j];
 
 -             zbest = pop[j];
 
 -     fitness_iter[i] = fitnesszbest;
 
  
- print "best solution is:"
 
 - print zbest
 
  
- print "min objective value is:"
 
 - print fitnesszbest
 
  
- plot( fitness_iter,'b-' )
 
 - show()
 
  复制代码 子函数如下: 
- def funfitness(x1,x2):
 
 -     y = (x1-0.5)**2+(x2-0.6)**2;
 
 -     return y;
 
  
- def funfitness2(x):
 
 -     y = (x[0]-0.5)**2+(x[1]-0.6)**2;
 
 -     return y;
 
  
 
- def minIndex(fitness, fitnesszbest):
 
 -     for i in range(len(fitness)):
 
 -         if(fitness[i] == fitnesszbest):
 
 -             break;
 
 -     return i;
 
 
  复制代码 
 
 
 
 
 
 
 |   
 
 
 
 |