
基于改进粒子群算法的混合储能系统容量优化程序
最近在搞混合储能系统容量优化,发现传统粒子群算法容易陷入局部最优。刚好用Python搓了个改
进版,实测风电场景下电池+超级电容的组合配置效果拔群。这里分享几个关键代码段和实现思路,方便同
行直接抄作业(记得改参数)。
先看核心的粒子更新策略。传统PSO的惯性权重是固定值,咱们用非线性衰减让搜索更灵活:
```python
def update_inertia_weight(iter, max_iter):
# 指数衰减惯性权重,前期广域搜索,后期精细调整
return 0.9 * (0.5 ** (iter / (0.3 * max_iter)))
class Particle:
def __init__(self, dim):
self.position = np.random.uniform(0, 200, dim) # 储能单元容量维度
self.velocity = np.random.rand(dim) * 0.1 * 200
self.best_pos = self.position.copy()
def update_velocity(self, global_best, w, c1=1.5, c2=1.7):
# 引入非对称学习因子,增强全局搜索能力
r1, r2 = np.random.rand(2)
cognitive = c1 * r1 * (self.best_pos - self.position)
social = c2 * r2 * (global_best - self.position)
self.velocity = w*self.velocity + cognitive + social
```
适应度函数是门学问,得把电池循环寿命和超级电容的功率成本揉在一起算。这里用等效年成本作
为优化目标:
```python
def fitness(position):
battery_cap, sc_cap = position
# 电池成本模型(包含循环寿命折算)
battery_cost = 1200 * battery_cap + 0.2 * daily_cycles * 365 * battery_cap
# 超级电容成本模型(功率特性敏感)