bpso python代码pythonpython
时间: 2023-07-02 10:07:16 浏览: 140
毕业设计 基于Python的二进制粒子群灰狼优化算法源码+详细文档+全部数据资料 高分项目.zip
以下是使用Python实现的基本粒子群优化算法(PSO)和二元粒子群优化算法(BPSO)的示例代码:
PSO代码:
```python
import random
class Particle:
def __init__(self, x0):
self.position = [] # 粒子的位置
self.velocity = [] # 粒子的速度
self.best_position = [] # 粒子的历史最优位置
self.fitness = -1 # 粒子的适应度值
self.best_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 or self.best_fitness == -1:
self.best_position = self.position
self.best_fitness = self.fitness
def update_velocity(self, best_position):
w = 0.5 # 惯性权重
c1 = 1 # 学习因子1
c2 = 2 # 学习因子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_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_fitness_value = -1
best_position_value = []
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_fitness_value or best_fitness_value == -1:
best_fitness_value = swarm[j].fitness
best_position_value = list(swarm[j].position)
for j in range(0, num_particles):
swarm[j].update_velocity(best_position_value)
swarm[j].update_position(bounds)
print('最优解为:', best_position_value)
print('最优解的适应度值为:', best_fitness_value)
```
BPSO代码:
```python
import random
class Particle:
def __init__(self, x0):
self.position = [] # 粒子的位置
self.velocity = [] # 粒子的速度
self.best_position = [] # 粒子的历史最优位置
self.fitness = -1 # 粒子的适应度值
self.best_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 or self.best_fitness == -1:
self.best_position = self.position
self.best_fitness = self.fitness
def update_velocity(self, best_position):
w = 0.5 # 惯性权重
c1 = 1 # 学习因子1
c2 = 2 # 学习因子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_position[i] - self.position[i])
self.velocity[i] = w * self.velocity[i] + cognitive_velocity + social_velocity
# 将速度限制在[-1,1]之间
if self.velocity[i] > 1:
self.velocity[i] = 1
if self.velocity[i] < -1:
self.velocity[i] = -1
def update_position(self, bounds):
for i in range(0, num_dimensions):
# 计算sigmoid函数
sigm = 1 / (1 + pow(2.71828, -self.velocity[i]))
# 判断是否需要翻转位置
if random.random() < sigm:
self.position[i] = 1
else:
self.position[i] = 0
class BPSO:
def __init__(self, cost_function, x0, bounds, num_particles, max_iterations):
global num_dimensions
num_dimensions = len(x0)
best_fitness_value = -1
best_position_value = []
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_fitness_value or best_fitness_value == -1:
best_fitness_value = swarm[j].fitness
best_position_value = list(swarm[j].position)
for j in range(0, num_particles):
swarm[j].update_velocity(best_position_value)
swarm[j].update_position(bounds)
print('最优解为:', best_position_value)
print('最优解的适应度值为:', best_fitness_value)
```
请注意,在这两个示例代码中,变量`num_dimensions`是搜索空间的维度,变量`bounds`是一个元组列表,表示每个维度的搜索范围。`x0`是搜索空间中的初始位置,`cost_function`是要最小化的代价函数。`num_particles`是粒子群的大小,`max_iterations`是算法的最大迭代次数。
阅读全文