嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
def __init__(self):
self.w = 0.5 # 惯性因子
self.c1 = 1 # 自我认知学习因子
self.c2 = 1 # 社会认知学习因子
self.gbest = 0 # 种群当前最好位置
self.N = 20 # 种群中粒子数量
self.POP = [] # 种群
self.iter_N = 100 # 迭代次数
# 适应度值计算函数
def fitness(self, x):
return x 16 * np.sin(5 * x) 10 * np.cos(4 * x)
# 找到全局最优解
def g_best(self, pop):
for bird in pop:
if bird.fitness > self.fitness(self.gbest):
self.gbest = bird.pos
# 初始化种群
def initPopulation(self, pop, N):
for i in range(N):
bird = particle()#初始化鸟
bird.pos = np.random.uniform(-10, 10)#均匀分布
bird.fitness = self.fitness(bird.pos)
bird.pbest = bird.fitness
pop.append(bird)
# 找到种群中的最优位置
self.g_best(pop)