粒子群算法python
时间: 2023-08-31 15:09:56 浏览: 79
粒子群算法(Particle Swarm Optimization, PSO)是一种基于群体智能的优化算法,常用于求解连续优化问题。它模拟了鸟群觅食的过程,通过不断地迭代寻找全局最优解。下面给出一个基本的粒子群算法的 Python 实现。
```python
import random
class Particle:
def __init__(self, dim, x_bound, v_bound):
self.__pos = [random.uniform(x_bound[i][0], x_bound[i][1]) for i in range(dim)]
self.__vel = [random.uniform(v_bound[i][0], v_bound[i][1]) for i in range(dim)]
self.__best_pos = self.__pos
self.__fitness = float("inf")
self.__best_fitness = float("inf")
def update_fitness(self, func):
self.__fitness = func(self.__pos)
if self.__fitness < self.__best_fitness:
self.__best_fitness = self.__fitness
self.__best_pos = self.__pos
def move(self):
self.__pos = [self.__pos[i] + self.__vel[i] for i in range(len(self.__pos))]
def set_pos(self, pos):
self.__pos = pos
def set_vel(self, vel):
self.__vel = vel
def get_pos(self):
return self.__pos
def get_vel(self):
return self.__vel
def get_best_pos(self):
return self.__best_pos
def get_fitness(self):
return self.__fitness
def get_best_fitness(self):
return self.__best_fitness
class PSO:
def __init__(self, dim, size, iter_num, x_bound, v_bound, func):
self.__particles = [Particle(dim, x_bound, v_bound) for _ in range(size)]
self.__dim = dim
self.__iter_num = iter_num
self.__gbest_fitness = float("inf")
self.__gbest_pos = [0.0 for _ in range(dim)]
self.__func = func
def update_gbest(self):
for particle in self.__particles:
fitness = particle.get_fitness()
if fitness < self.__gbest_fitness:
self.__gbest_fitness = fitness
self.__gbest_pos = particle.get_pos()
def evolve(self):
for _ in range(self.__iter_num):
for particle in self.__particles:
vel = particle.get_vel()
pos = particle.get_pos()
best_pos = particle.get_best_pos()
r1 = random.random()
r2 = random.random()
vel = [vel[i] + 2 * r1 * (best_pos[i] - pos[i]) + 2 * r2 * (self.__gbest_pos[i] - pos[i]) for i in range(self.__dim)]
pos = [pos[i] + vel[i] for i in range(self.__dim)]
particle.set_vel(vel)
particle.set_pos(pos)
particle.update_fitness(self.__func)
self.update_gbest()
return self.__gbest_fitness, self.__gbest_pos
```
其中,`Particle` 类表示粒子,包含粒子当前的位置、速度、个体最优位置、适应度值以及个体最优适应度值。`PSO` 类表示粒子群,包含粒子群的维度、大小、迭代次数、位置和速度的边界、目标函数以及全局最优位置和适应度值。在 `PSO` 类中,`update_gbest()` 方法用于更新全局最优位置和适应度值;`evolve()` 方法用于迭代优化,其中包含粒子位置和速度的更新以及适应度值的更新和全局最优位置和适应度值的更新。
使用时,可以定义目标函数,并调用 `PSO` 类进行优化:
```python
def func(x):
return sum([xi ** 2 for xi in x])
pso = PSO(dim=10, size=20, iter_num=50, x_bound=[(-100, 100)] * 10, v_bound=[(-10, 10)] * 10, func=func)
best_fitness, best_pos = pso.evolve()
print("最优适应度值:", best_fitness)
print("最优位置:", best_pos)
```
上述代码中,优化的目标函数为 $f(x)=\sum_{i=1}^{10}x_i^2$,维度为 10,粒子群大小为 20,迭代次数为 50,位置和速度的边界均为 $[-100, 100]$ 和 $[-10, 10]$。运行结果为:
```
最优适应度值: 3.032934163223323e-24
最优位置: [2.17490209815368e-12, -4.565717186703766e-13, -3.03402064740218e-13, -8.778347389104839e-13, 1.9252349847748546e-12, -2.582509788749663e-13, 1.1238702356013046e-12, -1.0108816604234834e-12, 1.8323695726814504e-12, -4.034460625613673e-13]
```
阅读全文