import numpy as np
import matplotlib.pyplot as plt
class PSO(object):
def __init__(self, population_size, max_steps):
self.w = 0.6
# 惯性权重
self.c1 = self.c2 = 2
self.population_size = population_size
# 粒子群数量
self.dim = 2
# 搜索空间的维度
self.max_steps = max_steps
# 迭代次数
self.x_bound = [-10, 10]
# 解空间范围
self.x = np.random.uniform(self.x_bound[0], self.x_bound[1],
(self.population_size, self.dim))
# 初始化粒子群位置
self.v = np.random.rand(self.population_size, self.dim)
# 初始化粒子群速度
fitness = self.calculate_fitness(self.x)
self.p = self.x
# 个体的最佳位置
self.pg = self.x[np.argmin(fitness)]
# 全局最佳位置
self.individual_best_fitness = fitness
# 个体的最优适应度
self.global_best_fitness = np.max(fitness)
# 全局最佳适应度
def calculate_fitness(self, x):
return np.sum(np.square(x), axis=1)
def evolve(self):
fig = plt.figure()
for step in range(self.max_steps):
r1 = np.random.rand(self.population_size, self.dim)
r2 = np.random.rand(self.population_size, self.dim)
# 更新速度和权重
self.v = self.w*self.v+self.c1*r1*(self.p-self.x)+self.c2*r2*(self.pg-self.x)
self.x = self.v + self.x
评论0
最新资源