python实现粒子群优化算法

粒子群优化算法(Particle Swarm Optimization, PSO)是一种基于群体智能的优化算法,它通过模拟鸟群觅食的行为来解决问题。在PSO中,每个优化问题的解被视作搜索空间中的一个粒子,所有粒子都有一个由被优化函数决定的适应度值(fitness value),并且每个粒子都跟随两个"最佳位置"来更新自己的位置:一是粒子本身迄今为止找到的最佳位置(个体极值 pbest),二是整个种群迄今为止找到的最佳位置(全局极值 gbest)。

以下是一个使用Python实现的简单粒子群优化算法的示例,该算法用于求解一维函数的最小值问题(例如,f(x) = x^2):

|---|----------------------------------------------------------------------------------------|
| | import numpy as np |
| | |
| | class Particle: |
| | def __init__(self, bounds, fitness_func): |
| | self.position = np.random.uniform(bounds[0], bounds[1], 1) |
| | self.velocity = np.zeros_like(self.position) |
| | self.pbest_position = self.position.copy() |
| | self.pbest_value = fitness_func(self.position) |
| | |
| | def update_velocity(self, global_best_position, w=0.5, c1=1.0, c2=2.0): |
| | r1, r2 = np.random.rand(), np.random.rand() |
| | cognitive_component = c1 * r1 * (self.pbest_position - self.position) |
| | social_component = c2 * r2 * (global_best_position - self.position) |
| | self.velocity = w * self.velocity + cognitive_component + social_component |
| | |
| | def update_position(self, bounds): |
| | self.position += self.velocity |
| | # 边界处理 |
| | self.position = np.clip(self.position, bounds[0], bounds[1]) |
| | |
| | def evaluate(self, fitness_func): |
| | current_value = fitness_func(self.position) |
| | if current_value < self.pbest_value: |
| | self.pbest_value = current_value |
| | self.pbest_position = self.position.copy() |
| | |
| | class PSO: |
| | def __init__(self, num_particles, bounds, fitness_func, max_iter=100): |
| | self.num_particles = num_particles |
| | self.bounds = bounds |
| | self.fitness_func = fitness_func |
| | self.max_iter = max_iter |
| | self.particles = [Particle(bounds, fitness_func) for _ in range(num_particles)] |
| | self.gbest_position = None |
| | self.gbest_value = float('inf') |
| | |
| | def optimize(self): |
| | for iteration in range(self.max_iter): |
| | for particle in self.particles: |
| | particle.evaluate(self.fitness_func) |
| | if particle.pbest_value < self.gbest_value: |
| | self.gbest_value = particle.pbest_value |
| | self.gbest_position = particle.pbest_position.copy() |
| | |
| | for particle in self.particles: |
| | particle.update_velocity(self.gbest_position) |
| | particle.update_position(self.bounds) |
| | |
| | # 输出进度(可选) |
| | if iteration % 10 == 0: |
| | print(f"Iteration {iteration}: Best Value = {self.gbest_value}") |
| | |
| | return self.gbest_position, self.gbest_value |
| | |
| | # 示例使用 |
| | def fitness_func(x): |
| | return x**2 |
| | |
| | bounds = [-10, 10] # 定义搜索范围 |
| | pso = PSO(num_particles=30, bounds=bounds, fitness_func=fitness_func, max_iter=100) |
| | best_position, best_value = pso.optimize() |
| | print(f"Best Position: {best_position}, Best Value: {best_value}") |

这段代码首先定义了Particle类来表示每个粒子,它包含了粒子的位置、速度、个体最佳位置和对应的值。PSO类用于管理整个粒子群,包括初始化粒子、执行优化循环、更新粒子速度和位置以及处理边界条件。在优化循环中,每个粒子都会更新其速度和位置,并检查是否需要更新其个体最佳位置和全局最佳位置。。

相关推荐
北_鱼2 分钟前
支持向量机(SVM):算法讲解与原理推导
算法·机器学习·支持向量机
鸟哥大大8 分钟前
【Python】pypinyin-汉字拼音转换工具
python·自然语言处理
jiugie16 分钟前
MongoDB学习
数据库·python·mongodb
十八朵郁金香35 分钟前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
阿尔法波39 分钟前
python与pycharm如何设置文件夹为源代码根目录
开发语言·python·pycharm
xing25161 小时前
pytest下allure
开发语言·python·pytest
眸笑丶1 小时前
使用 Python 调用 Ollama API 并调用 deepseek-r1:8b 模型
开发语言·python
dexianshen1 小时前
配置mysql8.0使用PXC实现高可用
python
中国loong1 小时前
pandas连接mysql数据库
python
enyp801 小时前
Qt QStackedWidget 总结
开发语言·qt