% 电力系统参数
num_generators = 5; % 发电机数量
load_demand = 1000; % 负荷需求(MW)
% 发电机参数
generator_capacity = [200, 300, 250, 150, 200]; % 发电机容量(MW)
generator_cost = [10, 12, 11, 9, 10]; % 发电机成本($/MWh)
% 粒子群算法参数
num_particles = 50; % 粒子数量
max_iter = 100; % 最大迭代次数
w = 0.5; % 惯性权重
c1 = 1; % 个体学习因子
c2 = 1; % 社会学习因子
% 初始化粒子群
positions = zeros(num_particles, num_generators);
velocities = zeros(num_particles, num_generators);
pbest_positions = positions;
pbest_costs = inf(1, num_particles);
gbest_position = zeros(1, num_generators);
gbest_cost = inf;
% 开始迭代
for iter = 1:max_iter
% 更新速度和位置
for i = 1:num_particles
% 更新速度
velocities(i, 😃 = w * velocities(i, 😃 …
+ c1 * rand() * (pbest_positions(i, 😃 - positions(i, 😃) …
+ c2 * rand() * (gbest_position - positions(i, 😃);
% 更新位置
positions(i, :) = positions(i, :) + velocities(i, :);
% 限制位置在发电机容量范围内
positions(i, :) = max(0, min(positions(i, :), generator_capacity));
end
% 计算适应度值并更新个体最优和全局最优
for i = 1:num_particles
% 计算发电成本
generator_output = positions(i, :);
total_cost = sum(generator_cost .* generator_output);
% 更新个体最优
if total_cost < pbest_costs(i)
pbest_costs(i) = total_cost;
pbest_positions(i, :) = positions(i, :);
end
% 更新全局最优
if total_cost < gbest_cost
gbest_cost = total_cost;
gbest_position = positions(i, :);
end
end
% 输出当前迭代的最低成本
disp(['Iteration: ', num2str(iter), ', Lowest Cost: ', num2str(gbest_cost)]);
end
% 输出最佳解
disp(‘Best Generator Outputs:’);
disp(gbest_position);
disp(‘Total Cost:’);
disp(gbest_cost);