最近,对算法的理解慢慢上路了,所以我写了一个稍微完整的PSO算法,就是不知道约束条件添加的位置是否是正确的,欢迎大家进行讨论交流,不胜感激!!!
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 09:59:15 2019
"""
import numpy as np
import random
import matplotlib.pyplot as plt
#----------------------PSO参数设置---------------------------------
class PSO():
def __init__(self,pN,dim,max_iter):
self.w = 0.8
self.c1 = 2
self.c2 = 2
self.r1= 0.6
self.r2=0.3
self.pN = pN #粒子数量
self.dim = dim #搜索维度
self.max_iter = max_iter #迭代次数
self.X = np.zeros((self.pN,self.dim)) #所有粒子的位置和速度
self.V = np.zeros((self.pN,self.dim))
self.pbest = np.zeros((self.pN,self.dim)) #个体经历的最佳位置和全局最佳位置
self.gbest = np.zeros((1,self.dim))
self.p_fit = np.zeros(self.pN) #每个个体的历史最佳适应值
self.fit = 1e10 #全局最佳适应值
self.h = np.zeros((self.pN,self.dim))
self.ps = [69.2201, 67.4473, 67.4473, 67.4473, 67.4473, 67.4473, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0]
self.po = [0, 0, 0, 0, 0, 0,272.1346, 251.9091, 251.9091, 251.9091, 251.9091, 251.9091, 0, 0, 0, 0, 0, 0]
self.cost = [0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906, 0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906,0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906]
self.run_pro_speed = [48.605, 31.0979, 31.0979, 31.0979, 31.0979, 31.0979, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.run_use_speed = [58.7868, 62.9171, 62.9171, 62.9171, 62.9171, 62.9171, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.exit_flow = [0, 0, 0, 0, 0, 0, 59.0325, 68.2907, 68.2907, 68.2907, 68.2907, 68.2907, 0, 0, 0, 0, 0, 0]
self.run_use_speed1 = [0, 0, 0, 0, 0, 0, 58.7868, 62.9171, 62.9171, 62.9171, 62.9171, 62.9171, 0, 0, 0, 0, 0, 0]
self.paper_pro_speed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95.4955, 116.6144, 116.6144, 116.6144, 116.6144, 116.6144]
self.paper_use_speed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95.098, 107.4384, 107.4384, 107.4384, 107.4384, 107.4384]
#---------------------目标函数Sphere函数-----------------------------
def function(self,x):
sum = a = b = c = 0
#length = len(x)
#x = x**2
for i in range(self.dim):
if i < 6:
a = x[i]*self.ps[i]*self.cost[i]
if i > 5 and i < 12:
b = x[i]*self.po[i]*self.cost[i]
if i > 11 & i < 18:
c = x[i]*0*self.cost[i]
sum += abs(a + b + c)
return sum
#---------------------初始化种群----------------------------------
def init_Population(self):
for i in range(self.pN):
self.X[i][0] = random.uniform(0.5,1)
self.X[i][1] = random.uniform(1,5)
self.X[i][2] = random.uniform(0,1.5)
self.X[i][3] = random.uniform(0.5,2)
self.X[i][4] = random.uniform(1,5)
self.X[i][5] = random.uniform(0.5,2)
self.X[i][6] = random.uniform(1,2)
self.X[i][7] = random.uniform(4,8)
self.X[i][8] = random.uniform(0,1.5)
self.X[i][9] = random.uniform(1,2)
self.X[i][10] = random.uniform(1,6)
self.X[i][11] = random.uniform(1,4)
self.X[i][12] = random.uniform(0,2)
self.X[i][13] = random.uniform(0,8)
self.X[i][14] = random.uniform(0,2)
self.X[i][15] = random.uniform(0,2)
self.X[i][16] = random.uniform(0,6)
self.X[i][17] = random.uniform(0,4)
for j in range(self.dim):
self.V[i][j] = random.uniform(0,0.01)
for i in range(self.pN):
self.pbest[i] = self.X[i]
tmp = self.function(self.X[i])
self.p_fit[i] = tmp
if(tmp < self.fit):
self.fit = tmp
self.gbest = self.X[i]
#----------------------更新粒子位置----------------------------------
def iterator(self):
fitness = []
for t in range(self.max_iter):
for i in range(self.pN): #更新gbest\pbest
temp = self.function(self.X[i])
if(temp0.95:
d = self.h[i][0] - 0.95
self.X[i][0] =abs( self.X[i][0] - d*100/self.run_pro_speed[0] )
self.h[i][0] = 0.95
if self.X[i][0] > 2:
self.X[i][0] = 2
if self.X[i][0] < 0:
self.X[i][0] = 0
if self.h[i][0]<0.20:
d = 0.20 - self.h[i][0]
self.X[i][6] =abs( self.X[i][6] - d*100/self.run_use_speed[0] )
self.h[i][0] = 0.20
if self.X[i][6] > 2:
self.X[i][6] = 2
if self.X[i][6] < 0:
self.X[i][6] = 0
self.h[i][1] = self.h[i][0]+(self.run_pro_speed[1]*self.X[i][1]-self.run_use_speed[1]*self.X[i][7])/100 #h8点
if self.h[i][1]>0.95:
d = self.h[i][1] - 0.95
self.X[i][1] =abs( self.X[i][1] - d*100/self.run_pro_speed[1] )
self.h[i][1] = 0.95
if self.X[i][1] > 8:
self.X[i][1]=8
if self.X[i][1] < 0:
self.X[i][1] = 0
if self.h[i][1]<0.20:
d = 0.20 - self.h[i][1]
self.X[i][7] =abs( self.X[i][7] - d*100/self.run_use_speed[1] )
self.h[i][1] = 0.20
if self.X[i][7] > 8:
self.X[i][7] = 8
if self.X[i][7] < 0:
self.X[i][7] = 0
self.h[i][2] = self.h[i][1]+(self.run_pro_speed[2]*self.X[i][2]-self.run_use_speed[2]*self.X[i][8])/100 #h10点
if self.h[i][2]>0.95:
d = self.h[i][2] - 0.95
self.X[i][2] =abs( self.X[i][2] - d*100/self.run_pro_speed[2] )
self.h[i][2] = 0.95
if self.X[i][2] > 2:
self.X[i][2]=2
if self.X[i][2] < 0:
self.X[i][2] = 0
if self.h[i][2]<0.20:
d = 0.20 - self.h[i][2]
self.X[i][8] =abs( self.X[i][8] - d*100/self.run_use_speed[1] )
self.h[i][2] = 0.20
if self.X[i][8] > 2:
self.X[i][8]=2
if self.X[i][8] < 0:
self.X[i][8] = 0
self.h[i][3] = self.h[i][2]+(self.run_pro_speed[3]*self.X[i][3]-self.run_use_speed[3]*self.X[i][9])/100 #h12点
if self.h[i][3]>0.95:
d = self.h[i][3] - 0.95
self.X[i][3] =abs( self.X[i][3] - d*100/self.run_pro_speed[3] )
self.h[i][3] = 0.95
if self.X[i][3] > 2:
self.X[i][3] = 2
if self.X[i][3] < 0:
self.X[i][3] = 0
if self.h[i][3]<0.20:
d = 0.20 - self.h[i][3]
self.X[i][9] =abs( self.X[i][9] - d*100/self.run_use_speed[3] )
self.h[i][3] = 0.20
if self.X[i][9] > 2:
self.X[i][9] = 2
if self.X[i][9] < 0:
self.X[i][9] = 0
self.h[i][4] = self.h[i][3]+(self.run_pro_speed[4]*self.X[i][4]-self.run_use_speed[4]*self.X[i][10])/100 #h18点
if self.h[i][4]>0.95:
d = self.h[i][4] - 0.95
self.X[i][4] =abs( self.X[i][4] - d*100/self.run_pro_speed[4] )
self.h[i][4] = 0.95
if self.X[i][4] > 6:
self.X[i][4] = 6
if self.X[i][4] < 0:
self.X[i][4] = 0
if self.h[i][4] < 0.20:
d = 0.20 - self.h[i][4]
self.X[i][10] =abs( self.X[i][10] - d*100/self.run_use_speed[4] )
self.h[i][4] = 0.20
if self.X[i][10] > 6:
self.X[i][10] = 6
if self.X[i][10] < 0:
self.X[i][10] = 0
self.h[i][5] = self.h[i][4]+(self.run_pro_speed[5]*self.X[i][5]-self.run_use_speed[5]*self.X[i][11])/100 #h22点
if self.h[i][5]>0.95:
d = self.h[i][5] - 0.95
self.X[i][5] =abs( self.X[i][5] - d*100/self.run_pro_speed[5] )
self.h[i][5] = 0.95
if self.X[i][5] > 4:
self.X[i][5]=4
if self.X[i][5] < 0:
self.X[i][5] = 0
if self.h[i][5]<0.20:
d = 0.20 - self.h[i][5]
self.X[i][11] =abs( self.X[i][11] - d*100/self.run_use_speed[5] )
self.h[i][5] = 0.20
if self.X[i][11] > 4:
self.X[i][11]=4
if self.X[i][11] < 0:
self.X[i][11] = 0
#约束条件2
self.h[i][6] = (self.exit_flow[6]*self.X[i][6]-self.run_use_speed1[6]*self.X[i][12])/170 #h24点
if self.h[i][6]>0.95:
d = self.h[i][6] - 0.95
self.X[i][6] =abs( self.X[i][6] - d*170/self.exit_flow[6] )
self.h[i][6] = 0.95
if self.X[i][6] > 2:
self.X[i][6] = 2
if self.X[i][6] < 0:
self.X[i][6] = 0
if self.h[i][6] < 0.20:
d = 0.20 - self.h[i][6]
self.X[i][12] = abs( self.X[i][12] - d*170/self.run_use_speed1[6] )
self.h[i][6] = 0.20
if self.X[i][12] > 2:
self.X[i][12] = 2
if self.X[i][12] < 0:
self.X[i][12] = 0
self.h[i][7] = self.h[i][6] + (self.exit_flow[7]*self.X[i][7]-self.run_use_speed1[7]*self.X[i][13])/170 #h8点
if self.h[i][7]>0.95:
d = self.h[i][7] - 0.95
self.X[i][7] =abs( self.X[i][7] - d*170/self.exit_flow[7] )
self.h[i][7] = 0.95
if self.X[i][7] > 8:
self.X[i][7] = 8
if self.X[i][7] < 0:
self.X[i][7] = 0
if self.h[i][7] < 0.20:
d = 0.20 - self.h[i][7]
self.X[i][13] = abs( self.X[i][13] - d*170/self.run_use_speed1[7] )
self.h[i][7] = 0.20
if self.X[i][13] > 8:
self.X[i][13] = 8
if self.X[i][13] < 0:
self.X[i][13] = 0
self.h[i][8] = self.h[i][7] + (self.exit_flow[8]*self.X[i][8]-self.run_use_speed1[8]*self.X[i][14])/170 #h10点
if self.h[i][8]>0.95:
d = self.h[i][8] - 0.95
self.X[i][8] =abs( self.X[i][8] - d*170/self.exit_flow[8] )
self.h[i][8] = 0.95
if self.X[i][8] > 2:
self.X[i][8] = 2
if self.X[i][8] < 0:
self.X[i][8] = 0
if self.h[i][8] < 0.20:
d = 0.20 - self.h[i][8]
self.X[i][14] = abs( self.X[i][14] - d*170/self.run_use_speed1[8] )
self.h[i][8] = 0.20
if self.X[i][14] > 2:
self.X[i][14] = 2
if self.X[i][14] < 0:
self.X[i][14] = 0
self.h[i][9] = self.h[i][8] + (self.exit_flow[9]*self.X[i][9]-self.run_use_speed1[9]*self.X[i][15])/170 #h12点
if self.h[i][9]>0.95:
d = self.h[i][9] - 0.95
self.X[i][9] =abs( self.X[i][9] - d*170/self.exit_flow[9] )
self.h[i][9] = 0.95
if self.X[i][9] > 2:
self.X[i][9] = 2
if self.X[i][9] < 0:
self.X[i][9] = 0
if self.h[i][9] < 0.20:
d = 0.20 - self.h[i][9]
self.X[i][15] = abs( self.X[i][15] - d*170/self.run_use_speed1[9] )
self.h[i][9] = 0.20
if self.X[i][15] > 2:
self.X[i][15] = 2
if self.X[i][15] < 0:
self.X[i][15] = 0
self.h[i][10] = self.h[i][9] + (self.exit_flow[10]*self.X[i][10]-self.run_use_speed1[10]*self.X[i][16])/170 #h18点
if self.h[i][10]>0.95:
d = self.h[i][10] - 0.95
self.X[i][10] =abs( self.X[i][10] - d*170/self.exit_flow[10] )
self.h[i][10] = 0.95
if self.X[i][10] > 6:
self.X[i][10] = 6
if self.X[i][10] < 0:
self.X[i][10] = 0
if self.h[i][10] < 0.20:
d = 0.20 - self.h[i][10]
self.X[i][16] = abs( self.X[i][16] - d*170/self.run_use_speed1[10] )
self.h[i][10] = 0.20
if self.X[i][16] > 6:
self.X[i][16] = 6
if self.X[i][16] < 0:
self.X[i][16] = 0
self.h[i][11] = self.h[i][10] + (self.exit_flow[11]*self.X[i][11]-self.run_use_speed1[11]*self.X[i][17])/170 #h22点
if self.h[i][11]>0.95:
d = self.h[i][11] - 0.95
self.X[i][11] =abs( self.X[i][11] - d*170/self.exit_flow[11] )
self.h[i][11] = 0.95
if self.X[i][11] > 6:
self.X[i][11] = 6
if self.X[i][11] < 0:
self.X[i][11] = 0
if self.h[i][11] < 0.20:
d = 0.20 - self.h[i][11]
self.X[i][17] = abs( self.X[i][17] - d*170/self.run_use_speed1[17] )
self.h[i][11] = 0.20
if self.X[i][17] > 6:
self.X[i][17] = 6
if self.X[i][17] < 0:
self.X[i][17] = 0
#约束条件3
self.h[i][12] = (self.paper_pro_speed[12]*self.X[i][12]-self.paper_use_speed[12]*2)/170 #h24点
if self.h[i][12]>0.95:
d = self.h[i][12] - 0.95
self.X[i][12] = abs(self.X[i][12] - d*170/self.paper_pro_speed[12])
self.h[i][12] = 0.95
if self.X[i][12] > 2:
self.X[i][12] = 2
if self.X[i][12] < 0:
self.X[i][12] = 0
if self.h[i][12] < 0.20:
d = 0.20 - self.h[i][12]
self.X[i][12] =abs( self.X[i][12] + d*170/self.paper_use_speed[12] )
self.h[i][12] = 0.20
if self.X[i][12] > 2:
self.X[i][12] = 2
if self.X[i][12] < 0:
self.X[i][12] = 0
self.h[i][13] = self.h[i][12] + (self.paper_pro_speed[13]*self.X[i][13]-self.paper_use_speed[13]*8)/170 #h8点
if self.h[i][13]>0.95:
d = self.h[i][13] - 0.95
self.X[i][13] = abs(self.X[i][13] - d*170/self.paper_pro_speed[13])
self.h[i][13] = 0.95
if self.X[i][13] > 8:
self.X[i][13] = 8
if self.X[i][13] < 0:
self.X[i][13] = 0
if self.h[i][13] < 0.20:
d = 0.20 - self.h[i][13]
self.X[i][13] =abs( self.X[i][13] + d*170/self.paper_use_speed[13] )
self.h[i][13] = 0.20
if self.X[i][13] > 8:
self.X[i][13] = 8
if self.X[i][13] < 0:
self.X[i][13] = 0
self.h[i][14] = self.h[i][13] + (self.paper_pro_speed[14]*self.X[i][14]-self.paper_use_speed[14]*2)/170 #h10点
if self.h[i][14]>0.95:
d = self.h[i][14] - 0.95
self.X[i][14] = abs(self.X[i][14] - d*170/self.paper_pro_speed[14])
self.h[i][14] = 0.95
if self.X[i][14] > 2:
self.X[i][14] = 2
if self.X[i][14] < 0:
self.X[i][14] = 0
if self.h[i][14] < 0.20:
d = 0.20 - self.h[i][14]
self.X[i][14] =abs( self.X[i][13] + d*170/self.paper_use_speed[14] )
self.h[i][14] = 0.20
if self.X[i][14] > 2:
self.X[i][14] = 2
if self.X[i][14] < 0:
self.X[i][14] = 0
self.h[i][15] = self.h[i][14] + (self.paper_pro_speed[15]*self.X[i][15]-self.paper_use_speed[15]*2)/170 #h12点
if self.h[i][15]>0.95:
d = self.h[i][15] - 0.95
self.X[i][15] = abs(self.X[i][15] - d*170/self.paper_pro_speed[15])
self.h[i][15] = 0.95
if self.X[i][15] > 2:
self.X[i][15] = 2
if self.X[i][15] < 0:
self.X[i][15] = 0
if self.h[i][15] < 0.20:
d = 0.20 - self.h[i][15]
self.X[i][15] =abs( self.X[i][15] + d*170/self.paper_use_speed[15] )
self.h[i][15] = 0.20
if self.X[i][15] > 2:
self.X[i][15] = 2
if self.X[i][15] < 0:
self.X[i][15] = 0
self.h[i][16] = self.h[i][15] + (self.paper_pro_speed[16]*self.X[i][16]-self.paper_use_speed[16]*6)/170 #h18点
if self.h[i][16]>0.95:
d = self.h[i][16] - 0.95
self.X[i][16] = abs(self.X[i][16] - d*170/self.paper_pro_speed[16])
self.h[i][16] = 0.95
if self.X[i][16] > 6:
self.X[i][16] = 6
if self.X[i][16] < 0:
self.X[i][16] = 0
if self.h[i][16] < 0.20:
d = 0.20 - self.h[i][16]
self.X[i][16] =abs( self.X[i][16] + d*170/self.paper_use_speed[16] )
self.h[i][16] = 0.20
if self.X[i][16] > 6:
self.X[i][16] = 6
if self.X[i][16] < 0:
self.X[i][16] = 0
self.h[i][17] = self.h[i][16] + (self.paper_pro_speed[17]*self.X[i][17]-self.paper_use_speed[17]*4)/170 #h22点
if self.h[i][17]>0.95:
d = self.h[i][17] - 0.95
self.X[i][17] = abs(self.X[i][17] - d*170/self.paper_pro_speed[17])
self.h[i][17] = 0.95
if self.X[i][17] > 4:
self.X[i][17] = 4
if self.X[i][17] < 0:
self.X[i][17] = 0
if self.h[i][17] < 0.20:
d = 0.20 - self.h[i][17]
self.X[i][17] =abs( self.X[i][17] + d*170/self.paper_use_speed[17] )
self.h[i][17] = 0.20
if self.X[i][16] > 4:
self.X[i][17] = 4
if self.X[i][17] < 0:
self.X[i][17] = 0
fitness.append(self.fit)
print(self.fit) #输出最优值
print(self.gbest)
print(self.h)
return fitness
#----------------------程序执行-----------------------
my_pso = PSO(pN=50,dim=18,max_iter=500)
my_pso.init_Population()
fitness = my_pso.iterator()
#-------------------画图--------------------
plt.figure(1)
plt.title("Figure1")
plt.xlabel("iterators", size=14)
plt.ylabel("fitness", size=14)
t = np.array([t for t in range(0,500)])
fitness = np.array(fitness)
plt.plot(t,fitness, color='b',linewidth=3)
plt.show()
以上就是我加入约束条件的源代码。
我需要讲明一点的是,约束条件加在哪里我尝试过很多次,和同学也聊过这个问题,最终我加在了更新粒子位置和速度之后。
但是!!!效果依然很让人头疼。。。我先给大家看我的结果图。
其中过程结果如下:
过程结果显示的意思是:最优的目标函数值对应的自变量取值。按照我原来的设计,自变量是不可能取负值的,而且我在粒子更新位置和速度那里添加了对粒子的约束,目前原因还是没找到。。。。请求各位大神指点。。。
无约束条件:
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 09:59:15 2019
"""
import numpy as np
import random
import matplotlib.pyplot as plt
#----------------------PSO参数设置---------------------------------
class PSO():
def __init__(self,pN,dim,max_iter):
self.w = 0.8
self.c1 = 2
self.c2 = 2
self.r1= 0.6
self.r2=0.3
self.pN = pN #粒子数量
self.dim = dim #搜索维度
self.max_iter = max_iter #迭代次数
self.X = np.zeros((self.pN,self.dim)) #所有粒子的位置和速度
self.V = np.zeros((self.pN,self.dim))
self.pbest = np.zeros((self.pN,self.dim)) #个体经历的最佳位置和全局最佳位置
self.gbest = np.zeros((1,self.dim))
self.p_fit = np.zeros(self.pN) #每个个体的历史最佳适应值
self.fit = 1e10 #全局最佳适应值
self.h = np.zeros((self.pN,self.dim))
self.ps = [69.2201, 67.4473, 67.4473, 67.4473, 67.4473, 67.4473, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0]
self.po = [0, 0, 0, 0, 0, 0,272.1346, 251.9091, 251.9091, 251.9091, 251.9091, 251.9091, 0, 0, 0, 0, 0, 0]
self.cost = [0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906, 0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906,0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906]
self.run_pro_speed = [48.605, 31.0979, 31.0979, 31.0979, 31.0979, 31.0979, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.run_use_speed = [58.7868, 62.9171, 62.9171, 62.9171, 62.9171, 62.9171, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.exit_flow = [0, 0, 0, 0, 0, 0, 59.0325, 68.2907, 68.2907, 68.2907, 68.2907, 68.2907, 0, 0, 0, 0, 0, 0]
self.run_use_speed1 = [0, 0, 0, 0, 0, 0, 58.7868, 62.9171, 62.9171, 62.9171, 62.9171, 62.9171, 0, 0, 0, 0, 0, 0]
self.paper_pro_speed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95.4955, 116.6144, 116.6144, 116.6144, 116.6144, 116.6144]
self.paper_use_speed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95.098, 107.4384, 107.4384, 107.4384, 107.4384, 107.4384]
#---------------------目标函数Sphere函数-----------------------------
def function(self,x):
sum = a = b = c = 0
#length = len(x)
#x = x**2
for i in range(self.dim):
if i < 6:
a = x[i]*self.ps[i]*self.cost[i]
if i > 5 and i < 12:
b = x[i]*self.po[i]*self.cost[i]
if i > 11 & i < 18:
c = x[i]*0*self.cost[i]
sum += abs(a + b + c)
return sum
#---------------------初始化种群----------------------------------
def init_Population(self):
for i in range(self.pN):
self.X[i][0] = random.uniform(0.5,1)
self.X[i][1] = random.uniform(1,5)
self.X[i][2] = random.uniform(0,1.5)
self.X[i][3] = random.uniform(0.5,2)
self.X[i][4] = random.uniform(1,5)
self.X[i][5] = random.uniform(0.5,2)
self.X[i][6] = random.uniform(1,2)
self.X[i][7] = random.uniform(4,8)
self.X[i][8] = random.uniform(0,1.5)
self.X[i][9] = random.uniform(1,2)
self.X[i][10] = random.uniform(1,6)
self.X[i][11] = random.uniform(1,4)
self.X[i][12] = random.uniform(0,2)
self.X[i][13] = random.uniform(0,8)
self.X[i][14] = random.uniform(0,2)
self.X[i][15] = random.uniform(0,2)
self.X[i][16] = random.uniform(0,6)
self.X[i][17] = random.uniform(0,4)
for j in range(self.dim):
self.V[i][j] = random.uniform(0,0.01)
for i in range(self.pN):
self.pbest[i] = self.X[i]
tmp = self.function(self.X[i])
self.p_fit[i] = tmp
if(tmp < self.fit):
self.fit = tmp
self.gbest = self.X[i]
#----------------------更新粒子位置----------------------------------
def iterator(self):
fitness = []
for t in range(self.max_iter):
for i in range(self.pN): #更新gbest\pbest
temp = self.function(self.X[i])
if(temp
走过路过,大家给点意见,小弟不胜感激!!!!