|
零基础学习Python--粒子群算法PSO函数寻优-5
链接:https://pan.baidu.com/s/1EuV2HkC_5oR7G6G-S1bXeA 提取码:cwmo
链接:https://pan.baidu.com/s/1yECy3SICh_8a1QZzmtu5ww 提取码:n4ip
精简一下代码:
- # -*- coding: utf-8 -*-
- """
- Created on Thu Aug 22 23:02:07 2019
- @author: ysw
- """
- from PIL import Image
- from pylab import *
- import matplotlib.pyplot as plt
- import numpy as np
- from random import *
- import sys
- #import fun
- sys.path.append(r'C:\Users\ysw\Desktop\Python(x,y)2.7.10\PSO')
- def funfitness(x1,x2):
- y = (x1-0.5)**2+(x2-0.6)**2;
- return y;
- # 采用 np.argmax替代
- # def minIndex(fitness, fitnesszbest):
- # for i in range(len(fitness)):
- # if(fitness[i] == fitnesszbest):
- # break;
- # return i;
- if __name__=="__main__":
- c1 = 1.49445;
- c2 = 1.49445;
- maxg=200;
- sizepop=20;
- Vmax=1;
- Vmin=-1;
- popmax=5;
- popmin=-5;
- nVar = 2;
- #print random.random()
- # PSO algorithm parameters
- 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][0] = popmin + (popmax-popmin)*random();
- # pop[i][1] = popmin + (popmax-popmin)*random();
- pop[i] = popmin + (popmax-popmin)*np.random.random(nVar);
- fitness[i] = funfitness(pop[i][0],pop[i][1]);
- # gbest[i][0] = pop[i][0]
- # gbest[i][1] = pop[i][1]
- gbest[i] = pop[i]
- fitnessgbest[i] = fitness[i]
- print(fitness[i])
- fitnesszbest = min(fitness);
- # index = fun.minIndex(fitness, fitnesszbest);
- index = np.argmin( fitness )
- # zbest[0] = pop[index][0]
- # zbest[1] = pop[index][1]
- zbest = pop[index]
- print(zbest)
- #plot(range(sizepop), fitness,'r*-')
- #plot( fitness,'r*-')
- #show()
-
- fitness_iter = [0 for x in range(maxg)]
- # main loop
- for i in range(maxg):
- for j in range(sizepop):
- # update vel
- r1 = random();
- r2 = random();
- # V[j][0] = V[j][0] + c1*r1*(gbest[j][0]-pop[j][0])+c2*r2*(zbest[0]-pop[j][0]);
- # V[j][1] = V[j][1] + c1*r1*(gbest[j][1]-pop[j][1])+c2*r2*(zbest[1]-pop[j][1]);
- 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][0] = pop[j][0] + 0.5*V[j][0];
- # pop[j][1] = pop[j][1] + 0.5*V[j][1];
- pop[j] = pop[j] + 0.5*V[j]
-
- if (pop[j][0]>popmax):
- pop[j][0] = popmax;
- elif (pop[j][0]<popmin):
- pop[j][0] = popmin;
- if (pop[j][1]>popmax):
- pop[j][1] = popmax;
- elif (pop[j][1]<popmin):
- pop[j][1] = popmin;
-
- fitness[j] = funfitness(pop[j][0],pop[j][1]);
- if (fitness[j]<fitnessgbest[j]):
- fitnessgbest[j] = fitness[j]
- # gbest[j][0] = pop[j][0];
- # gbest[j][1] = pop[j][1];
- gbest[j] = pop[j];
- if (fitness[j]<fitnesszbest):
- fitnesszbest= fitness[j];
- # zbest[0] = pop[j][0];
- # zbest[1] = pop[j][1];
- zbest = pop[j];
- fitness_iter[i] = fitnesszbest;
-
- print("最优解:")
- print(zbest)
- plot( fitness_iter,'b-' )
- plt.show()
复制代码
|
|