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类用于管理整个粒子群,包括初始化粒子、执行优化循环、更新粒子速度和位置以及处理边界条件。在优化循环中,每个粒子都会更新其速度和位置,并检查是否需要更新其个体最佳位置和全局最佳位置。。

相关推荐
.格子衫.12 分钟前
真题卷001——算法备赛
算法
学地理的小胖砸20 分钟前
【Python 操作 MySQL 数据库】
数据库·python·mysql
安迪小宝21 分钟前
6 任务路由与负载均衡
运维·python·celery
XiaoyaoCarter22 分钟前
每日一道leetcode
c++·算法·leetcode·职场和发展·二分查找·深度优先·前缀树
Blossom.11823 分钟前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song31 分钟前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
galaxy_strive32 分钟前
qtc++ qdebug日志生成
开发语言·c++·qt
TNTLWT35 分钟前
Qt功能区:简介与安装
开发语言·qt
lisw0535 分钟前
Python高级进阶:Vim与Vi使用指南
python·vim·excel
Hygge-star40 分钟前
【数据结构】二分查找5.12
java·数据结构·程序人生·算法·学习方法