粒子群优化算法python实现
时间: 2023-12-04 10:37:40 浏览: 73
粒子群优化算法(Particle Swarm Optimization,PSO)是一种基于群体智能的优化算法,它通过模拟鸟群捕食行为来寻找最优解。在PSO算法中,每个解被看作是一个粒子,每个粒子都有一个位置和速度,通过不断地更新位置和速度,粒子最终会聚集到最优解附近。
下面是一个简单的Python实现:
```python
import random
class Particle:
def __init__(self, dim, minx, maxx):
self.__position = [random.uniform(minx, maxx) for i in range(dim)]
self.__velocity = [random.uniform(-1, 1) for i in range(dim)]
self.__best_position = self.__position[:]
self.__fitness = float('inf')
self.__best_fitness = float('inf')
def set_position(self, position):
self.__position = position[:]
def get_position(self):
return self.__position
def set_velocity(self, velocity):
self.__velocity = velocity[:]
def get_velocity(self):
return self.__velocity
def set_best_position(self, position):
self.__best_position = position[:]
def get_best_position(self):
return self.__best_position
def set_fitness(self, fitness):
self.__fitness = fitness
def get_fitness(self):
return self.__fitness
def set_best_fitness(self, fitness):
self.__best_fitness = fitness
def get_best_fitness(self):
return self.__best_fitness
class PSO:
def __init__(self, dim, size, iter_num, minx, maxx):
self.__particles = [Particle(dim, minx, maxx) for i in range(size)]
self.__gbest_fitness = float('inf')
self.__gbest_position = [random.uniform(minx, maxx) for i in range(dim)]
self.__iter_num = iter_num
def set_particles(self, particles):
self.__particles = particles[:]
def get_particles(self):
return self.__particles
def set_gbest_fitness(self, fitness):
self.__gbest_fitness = fitness
def get_gbest_fitness(self):
return self.__gbest_fitness
def set_gbest_position(self, position):
self.__gbest_position = position[:]
def get_gbest_position(self):
return self.__gbest_position
def update(self):
for i in range(len(self.__particles)):
fitness = self.fitness(self.__particles[i].get_position())
if fitness < self.__particles[i].get_best_fitness():
self.__particles[i].set_best_fitness(fitness)
self.__particles[i].set_best_position(self.__particles[i].get_position())
if fitness < self.__gbest_fitness:
self.__gbest_fitness = fitness
self.__gbest_position = self.__particles[i].get_position()
w = 0.729
c1 = 1.49445
c2 = 1.49445
r1 = random.uniform(0, 1)
r2 = random.uniform(0, 1)
new_velocity = []
for j in range(len(self.__particles[i].get_velocity())):
new_velocity.append(w * self.__particles[i].get_velocity()[j] + c1 * r1 * (self.__particles[i].get_best_position()[j] - self.__particles[i].get_position()[j]) + c2 * r2 * (self.__gbest_position[j] - self.__particles[i].get_position()[j]))
self.__particles[i].set_velocity(new_velocity)
new_position = []
for j in range(len(self.__particles[i].get_position())):
new_position.append(self.__particles[i].get_position()[j] + new_velocity[j])
self.__particles[i].set_position(new_position)
def fitness(self, position):
# 这里是目标函数,需要根据实际问题进行修改
return sum([x ** 2 for x in position])
def run(self):
for i in range(self.__iter_num):
self.update()
print('iteration: %d, gbest_fitness: %f' % (i, self.__gbest_fitness))
pso = PSO(dim=2, size=20, iter_num=100, minx=-10, maxx=10)
pso.run()
```
这个实现中,Particle类表示一个粒子,包含了位置、速度、最优位置、适应度和最优适应度等属性。PSO类表示整个算法,包含了粒子群、全局最优位置和适应度等属性,以及更新和适应度计算等方法。在实现中,需要根据实际问题修改fitness方法来计算目标函数的值。
阅读全文