粒子群优化算法pso代码
时间: 2023-07-06 16:43:34 浏览: 108
以下是一个简单的粒子群优化算法(PSO)的代码示例:
```
import random
class Particle:
def __init__(self, x0):
self.position = []
self.velocity = []
self.best_position = []
self.fitness = -1
for i in range(0, num_dimensions):
self.velocity.append(random.uniform(-1, 1))
self.position.append(x0[i])
def evaluate(self, cost_function):
self.fitness = cost_function(self.position)
if self.fitness < self.best_fitness:
self.best_fitness = self.fitness
self.best_position = self.position
def update_velocity(self, best_global_position):
w = 0.5
c1 = 1
c2 = 2
for i in range(0, num_dimensions):
r1 = random.random()
r2 = random.random()
cognitive_velocity = c1 * r1 * (self.best_position[i] - self.position[i])
social_velocity = c2 * r2 * (best_global_position[i] - self.position[i])
self.velocity[i] = w * self.velocity[i] + cognitive_velocity + social_velocity
def update_position(self, bounds):
for i in range(0, num_dimensions):
self.position[i] = self.position[i] + self.velocity[i]
if self.position[i] > bounds[i][1]:
self.position[i] = bounds[i][1]
if self.position[i] < bounds[i][0]:
self.position[i] = bounds[i][0]
class PSO:
def __init__(self, cost_function, x0, bounds, num_particles, max_iterations):
global num_dimensions
num_dimensions = len(x0)
best_global_position = []
best_global_fitness = -1
swarm = []
for i in range(0, num_particles):
swarm.append(Particle(x0))
for i in range(0, max_iterations):
for j in range(0, num_particles):
swarm[j].evaluate(cost_function)
if swarm[j].fitness < best_global_fitness:
best_global_fitness = swarm[j].fitness
best_global_position = list(swarm[j].position)
for j in range(0, num_particles):
swarm[j].update_velocity(best_global_position)
swarm[j].update_position(bounds)
print('Best position:', best_global_position)
print('Best fitness:', best_global_fitness)
def cost_function(x):
return sum([i**2 for i in x])
bounds = [(-10, 10), (-10, 10), (-10, 10)]
PSO(cost_function, x0=[0, 0, 0], bounds=bounds, num_particles=15, max_iterations=30)
```
这个代码演示了如何使用 PSO 来最小化一个简单的函数。需要注意的是,这个示例只展示了基本的 PSO 实现,实际上,PSO 还有很多改进和扩展,例如变异粒子群优化算法(MPSO)、共生进化粒子群优化算法(CEPSO)等等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)