微电网的能量调度确实像一场高难度的俄罗斯方块游戏——光伏板、风力发电机等分布式电源是不规则下落的方块,而负载需求则是底部等待填充的空隙。作为一名在电力系统摸爬滚打十年的工程师,我深刻体会到这个"游戏"的挑战性:既要保证方块(发电)与空隙(用电)的实时匹配,又要避免堆叠过高(储能溢出)或出现缺口(供电不足)。
传统调度方法像是玩俄罗斯方块时只能看到当前方块,而粒子群算法(PSO)则像是获得了"预览下一个方块"的能力。这个源于鸟群觅食行为的智能算法,通过群体协作寻找最优解的特性,特别适合解决微电网中多变量、非线性的调度问题。今天我就用MATLAB带大家实操这个"电力版俄罗斯方块"的自动平衡方案。
我们的微电网模型包含以下发电单元:
储能系统采用锂电池模型:
SOC(t+1) = SOC(t) + (P_ch·η_ch - P_dis/η_dis)·Δt/E_rated
其中η_ch/dis为充放电效率,E_rated是额定容量
我们需要最小化的总成本函数:
code复制min Σ [C_dg(P_dg) + C_pv(P_pv) + C_wind(P_wind)
+ λ·|SOC-SOC_ref| + μ·(P_load-P_supply)²]
其中:
matlab复制n_particles = 50; % 粒子数量
max_iter = 200; % 最大迭代次数
w = 0.729; % 惯性权重
c1 = 1.49445; % 个体学习因子
c2 = 1.49445; % 社会学习因子
% 决策变量维度 = 时间间隔数×(光伏+风电+柴油机+储能)
dim = 24*(3+1);
% 初始化粒子位置和速度
positions = rand(n_particles, dim);
velocities = zeros(n_particles, dim);
matlab复制function cost = fitness(particle)
% 解码粒子位置为各电源出力计划
[P_pv, P_wind, P_dg, P_bess] = decode(particle);
% 计算供电平衡
P_supply = P_pv + P_wind + P_dg + P_bess;
imbalance = sum((P_load - P_supply).^2);
% 计算总成本
cost = sum(a*P_dg.^2 + b*P_dg + c) ... % 柴油机成本
+ 0.1*sum(P_pv) + 0.15*sum(P_wind) ... % 新能源运维
+ 0.5*abs(SOC - 0.5) ... % SOC偏离惩罚
+ 10*imbalance; % 功率不平衡惩罚
end
matlab复制for iter = 1:max_iter
for i = 1:n_particles
% 更新个体最优
current_fitness = fitness(positions(i,:));
if current_fitness < pbest_values(i)
pbest_values(i) = current_fitness;
pbest_positions(i,:) = positions(i,:);
end
% 更新全局最优
if current_fitness < gbest_value
gbest_value = current_fitness;
gbest_position = positions(i,:);
end
% 更新速度和位置
r1 = rand(1,dim);
r2 = rand(1,dim);
velocities(i,:) = w*velocities(i,:) ...
+ c1*r1.*(pbest_positions(i,:)-positions(i,:)) ...
+ c2*r2.*(gbest_position-positions(i,:));
positions(i,:) = positions(i,:) + velocities(i,:);
% 边界约束处理
positions(i,:) = max(0, min(1, positions(i,:)));
end
% 动态调整惯性权重
w = w * 0.995;
end
种群规模选择:
学习因子配置:
惯性权重衰减:
matlab复制% 在适应度函数中加入惩罚项
if SOC < 0.2 || SOC > 0.9
cost = cost + 1e6*(max(0,0.2-SOC) + max(0,SOC-0.9));
end
matlab复制% 解码时进行修正
for t = 2:24
delta = P_dg(t) - P_dg(t-1);
if delta > ramp_up
P_dg(t) = P_dg(t-1) + ramp_up;
elseif delta < -ramp_down
P_dg(t) = P_dg(t-1) - ramp_down;
end
end
matlab复制% 前20%粒子用启发式规则初始化
for i = 1:n_particles/5
positions(i,:) = heuristic_initialization(load_profile);
end
matlab复制% 每代保留前10%的优秀粒子
[~, idx] = sort(pbest_values);
elite = positions(idx(1:n_particles/10),:);
matlab复制if mod(iter,20)==0 && std(pbest_values)<threshold
for i = 1:n_particles
positions(i,:) = positions(i,:) + 0.1*randn(1,dim);
end
end
运行算法后,我们得到以下关键指标:
matlab复制figure;
area([P_pv; P_wind; P_dg; P_bess]');
hold on;
plot(P_load, 'k', 'LineWidth', 2);
legend('光伏','风电','柴油机','储能','总负荷');
xlabel('时间 (h)'); ylabel('功率 (kW)');
title('24小时最优调度方案');
matlab复制figure;
semilogy(convergence_curve);
xlabel('迭代次数'); ylabel('最优成本');
grid on;
title('PSO收敛曲线');
多时间尺度调度:
考虑预测不确定性:
matlab复制% 在适应度函数中加入鲁棒项
robust_cost = 0.2*std(P_pv_forecast-P_pv_actual) ...
+ 0.3*std(P_wind_forecast-P_wind_actual);
cost = cost + robust_cost;
硬件在环测试:
实际部署注意事项: