基于蚁群算法的配电网重构计算程序

 

基于蚁群算法的配电网重构计算程序

使用蚁群算法(ACO)解决配电网重构问题。配电网重构旨在优化网络拓扑结构,降低网损、改善电压分布并提高系统可靠性。

%% 基于蚁群算法的配电网重构程序
% 系统:IEEE 33节点配电系统
% 目标:最小化网损、均衡负荷、提高电压质量
% 算法:蚁群优化(ACO) + 前推回代潮流计算

clc; clear; close all;
addpath(genpath('.')); % 添加工具箱路径

%% 主函数
function main()
    % 加载IEEE 33节点系统数据
    [bus, branch, gen] = load_IEEE33_Data();
    
    % 显示原始系统信息
    display_SystemInfo(bus, branch);
    
    % 初始潮流计算
    [V0, Ploss0] = powerFlow(bus, branch);
    fprintf('初始系统网损: %.4f kW\n', Ploss0);
    
    % 设置ACO参数
    acoParams = struct();
    acoParams.numAnts = 20;          % 蚂蚁数量
    acoParams.maxIter = 50;          % 最大迭代次数
    acoParams.alpha = 1.0;           % 信息素重要程度
    acoParams.beta = 2.0;            % 启发式信息重要程度
    acoParams.rho = 0.1;             % 信息素挥发率
    acoParams.Q = 100;               % 信息素强度
    acoParams.eliteFactor = 2;       % 精英蚂蚁因子
    
    % 运行蚁群算法
    [bestTopology, bestLoss, convergence] = run_ACO_Reconfiguration(bus, branch, acoParams);
    
    % 显示优化结果
    display_OptimizationResults(bus, branch, bestTopology, bestLoss, Ploss0);
    
    % 可视化结果
    visualize_Results(convergence, bus, branch, bestTopology, V0, bestLoss, Ploss0);
end

%% 加载IEEE 33节点系统数据
function [bus, branch, gen] = load_IEEE33_Data()
    % 节点数据 [节点编号, 类型, 有功负荷(kW), 无功负荷(kvar)]
    bus = [
        1   3   0       0;       % 平衡节点
        2   1   100     60;
        3   1   90      40;
        4   1   120     80;
        5   1   60      30;
        6   1   60      20;
        7   1   200     100;
        8   1   200     100;
        9   1   60      20;
        10  1   60      20;
        11  1   45      30;
        12  1   60      35;
        13  1   60      35;
        14  1   120     80;
        15  1   60      10;
        16  1   60      20;
        17  1   60      20;
        18  1   90      40;
        19  1   90      40;
        20  1   90      40;
        21  1   90      40;
        22  1   90      40;
        23  1   90      50;
        24  1   420     200;
        25  1   420     200;
        26  1   60      25;
        27  1   60      25;
        28  1   60      20;
        29  1   120     70;
        30  1   200     600;
        31  1   150     70;
        32  1   210     100;
        33  1   60      40;
    ];
    
    % 支路数据 [起始节点, 终止节点, 电阻(Ω), 电抗(Ω), 开关状态(1闭合/0断开)]
    branch = [
        1   2   0.0922  0.0470  1;
        2   3   0.4930  0.2511  1;
        3   4   0.3660  0.1864  1;
        4   5   0.3811  0.1941  1;
        5   6   0.8190  0.7070  1;
        6   7   0.1872  0.6188  1;
        7   8   0.7114  0.2351  1;
        8   9   1.0300  0.7400  1;
        9   10  1.0440  0.7400  1;
        10  11  0.1966  0.0650  1;
        11  12  0.3744  0.1238  1;
        12  13  1.4680  1.1550  1;
        13  14  0.5416  0.7129  1;
        14  15  0.5910  0.5260  1;
        15  16  0.7463  0.5450  1;
        16  17  1.2890  1.7210  1;
        17  18  0.7320  0.5740  1;
        2   19  0.1640  0.1565  1;
        19  20  1.5042  1.3554  1;
        20  21  0.4095  0.4784  1;
        21  22  0.7089  0.9373  1;
        3   23  0.4512  0.3083  1;
        23  24  0.8980  0.7091  1;
        24  25  0.8960  0.7011  1;
        6   26  0.2030  0.1034  1;
        26  27  0.2842  0.1447  1;
        27  28  1.0590  0.9337  1;
        28  29  0.8042  0.7006  1;
        29  30  0.5075  0.2585  1;
        30  31  0.9744  0.9630  1;
        31  32  0.3105  0.3619  1;
        32  33  0.3410  0.5302  1;
        
        % 联络开关 (初始状态为断开)
        7   20  0.5000  0.5000  0;
        8   14  0.5000  0.5000  0;
        11  21  0.5000  0.5000  0;
        17  32  0.5000  0.5000  0;
        24  28  0.5000  0.5000  0;
    ];
    
    % 发电机数据 [节点编号, 电压幅值(pu), 有功出力(kW)]
    gen = [
        1   1.00   0;
    ];
end

%% 显示系统基本信息
function display_SystemInfo(bus, branch)
    fprintf('===============================================\n');
    fprintf('        IEEE 33节点配电系统重构\n');
    fprintf('===============================================\n');
    fprintf('节点数量: %d\n', size(bus, 1));
    fprintf('支路数量: %d\n', size(branch, 1));
    fprintf('联络开关数量: %d\n', sum(branch(:,5) == 0));
    fprintf('常规开关数量: %d\n', sum(branch(:,5) == 1));
    fprintf('===============================================\n');
end

%% 潮流计算函数 (前推回代法)
function [V, Ploss] = powerFlow(bus, branch)
    % 参数设置
    tol = 1e-6;          % 收敛容差
    maxIter = 100;       % 最大迭代次数
    V = ones(size(bus,1), 1); % 电压幅值初始化为1.0 pu
    V(1) = 1.0;           % 平衡节点电压
    
    % 节点参数提取
    nBus = size(bus, 1);
    Pload = bus(:,3)/1000; % kW -> MW
    Qload = bus(:,4)/1000; % kvar -> Mvar
    
    % 支路参数提取
    from = branch(:,1);
    to = branch(:,2);
    R = branch(:,3);
    X = branch(:,4);
    status = branch(:,5); % 开关状态
    
    % 构建节点-支路关联矩阵
    [parent, children] = buildTreeStructure(branch);
    
    % 迭代计算
    for iter = 1:maxIter
        Vprev = V;
        
        % 前向计算 (计算支路电流)
        I = zeros(nBus, 1);
        for i = nBus:-1:2 % 从末端节点开始
            % 节点负荷电流
            I(i) = conj((Pload(i) - 1j*Qload(i)) / conj(V(i)));
            
            % 加上子节点注入的电流
            for j = 1:length(children{i})
                child = children{i}(j);
                I(i) = I(i) + I(child);
            end
            
            % 计算支路电流
            if any(from == i & to == parent{i}) % 找到父节点支路
                idx = find(from == i & to == parent{i}, 1);
                I_branch = I(i);
                S_branch = V(i) * conj(I_branch);
            end
        end
        
        % 回代计算 (计算节点电压)
        for i = 2:nBus
            p = parent{i};
            idx = find(from == p & to == i, 1);
            if isempty(idx) || status(idx) == 0
                continue; % 跳过断开的支路
            end
            
            Z = R(idx) + 1j*X(idx);
            V(i) = V(p) - Z * I(i);
        end
        
        % 检查收敛
        if max(abs(V - Vprev)) < tol
            break;
        end
    end
    
    % 计算网损
    Ploss = 0;
    for k = 1:size(branch, 1)
        if status(k) == 0
            continue; % 跳过断开的支路
        end
        
        fromNode = branch(k,1);
        toNode = branch(k,2);
        I_k = abs((V(fromNode) - V(toNode)) / (R(k) + 1j*X(k)));
        Ploss = Ploss + R(k) * I_k^2;
    end
    Ploss = Ploss * 1000; % MW -> kW
end

%% 构建树状结构 (用于前推回代法)
function [parent, children] = buildTreeStructure(branch)
    nBranch = size(branch, 1);
    nodes = unique([branch(:,1); branch(:,2)]);
    nNodes = length(nodes);
    
    % 初始化
    parent = zeros(nNodes, 1);
    children = cell(nNodes, 1);
    
    % 找到根节点 (平衡节点)
    root = branch(find(branch(:,1)==1 | branch(:,2)==1, 1), 1);
    if isempty(root)
        root = 1;
    end
    parent(root) = 0; % 根节点没有父节点
    
    % 构建邻接表
    adjList = cell(nNodes, 1);
    for k = 1:nBranch
        if branch(k,5) == 0 % 跳过断开的支路
            continue;
        end
        i = branch(k,1);
        j = branch(k,2);
        adjList{i} = [adjList{i}, j];
        adjList{j} = [adjList{j}, i];
    end
    
    % BFS遍历构建树结构
    visited = false(nNodes, 1);
    queue = root;
    visited(root) = true;
    
    while ~isempty(queue)
        node = queue(1);
        queue(1) = [];
        
        neighbors = adjList{node};
        for i = 1:length(neighbors)
            nb = neighbors(i);
            if ~visited(nb)
                visited(nb) = true;
                parent(nb) = node;
                children{node} = [children{node}, nb];
                queue(end+1) = nb;
            end
        end
    end
end

%% 蚁群算法主函数
function [bestTopology, bestLoss, convergence] = run_ACO_Reconfiguration(bus, branch, params)
    % 初始化
    numAnts = params.numAnts;
    maxIter = params.maxIter;
    alpha = params.alpha;
    beta = params.beta;
    rho = params.rho;
    Q = params.Q;
    eliteFactor = params.eliteFactor;
    
    % 提取可操作开关 (联络开关)
    tieSwitches = find(branch(:,5) == 0);
    numTieSwitches = length(tieSwitches);
    
    % 初始化信息素矩阵
    tau = ones(numTieSwitches, 1) * 0.5; % 初始信息素
    
    % 初始化启发式信息 (网损的倒数)
    eta = ones(numTieSwitches, 1);
    
    % 存储最优解
    bestTopology = branch(:,5); % 初始拓扑
    [V, bestLoss] = powerFlow(bus, branch); % 初始网损
    convergence = zeros(maxIter, 1); % 收敛曲线
    
    % 主循环
    for iter = 1:maxIter
        antSolutions = zeros(numAnts, numTieSwitches); % 蚂蚁的解
        antLosses = inf(numAnts, 1); % 蚂蚁的网损
        
        % 每只蚂蚁构建解
        for k = 1:numAnts
            % 复制当前拓扑
            currentTopology = branch(:,5);
            
            % 随机选择要操作的开关数量 (1到3个)
            numOperations = randi([1, min(3, numTieSwitches)]);
            
            % 随机选择要操作的开关
            selectedSwitches = randperm(numTieSwitches, numOperations);
            
            % 应用开关操作
            for idx = 1:numOperations
                swIdx = selectedSwitches(idx);
                actualSwitch = tieSwitches(swIdx);
                currentTopology(actualSwitch) = 1 - currentTopology(actualSwitch); % 翻转开关状态
            end
            
            % 检查辐射状结构
            if isRadial(currentTopology, branch)
                % 计算网损
                modifiedBranch = branch;
                modifiedBranch(:,5) = currentTopology;
                [~, loss] = powerFlow(bus, modifiedBranch);
                antSolutions(k, :) = currentTopology(tieSwitches)';
                antLosses(k) = loss;
                
                % 更新个体最优
                if loss < bestLoss
                    bestLoss = loss;
                    bestTopology = currentTopology;
                end
            else
                % 非辐射状结构,给予惩罚
                antSolutions(k, :) = currentTopology(tieSwitches)';
                antLosses(k) = inf;
            end
        end
        
        % 更新信息素
        tau = (1 - rho) * tau; % 信息素挥发
        
        % 精英蚂蚁更新
        [minLoss, idx] = min(antLosses);
        if minLoss < inf
            eliteSolution = antSolutions(idx, :);
            for sw = 1:numTieSwitches
                if eliteSolution(sw) == 1 % 开关闭合
                    tau(sw) = tau(sw) + eliteFactor * Q / minLoss;
                end
            end
        end
        
        % 所有蚂蚁更新
        for k = 1:numAnts
            if antLosses(k) < inf
                solution = antSolutions(k, :);
                for sw = 1:numTieSwitches
                    if solution(sw) == 1 % 开关闭合
                        tau(sw) = tau(sw) + Q / antLosses(k);
                    end
                end
            end
        end
        
        % 记录收敛曲线
        convergence(iter) = bestLoss;
        fprintf('迭代 %d: 最小网损 = %.4f kW\n', iter, bestLoss);
    end
    
    % 返回最优拓扑
    bestTopology(tieSwitches) = bestTopology(tieSwitches);
end

%% 检查辐射状结构
function radial = isRadial(topology, branch)
    % 构建图
    G = graph();
    for k = 1:size(branch, 1)
        if topology(k) == 1 % 开关闭合
            G = addedge(G, branch(k,1), branch(k,2));
        end
    end
    
    % 检查连通性
    if ~isconnected(G)
        radial = false;
        return;
    end
    
    % 检查是否有环
    [~, cycles] = bfsearch(G, 1);
    numEdges = nnz(topology);
    numNodes = max([branch(:,1); branch(:,2)]);
    
    % 辐射状网络应满足: 边数 = 节点数 - 1
    if numEdges == numNodes - 1
        radial = true;
    else
        radial = false;
    end
end

%% 显示优化结果
function display_OptimizationResults(bus, branch, bestTopology, bestLoss, Ploss0)
    % 计算优化后的电压分布
    modifiedBranch = branch;
    modifiedBranch(:,5) = bestTopology;
    [V_opt, ~] = powerFlow(bus, modifiedBranch);
    
    % 计算电压偏差
    V_base = 12.66; % kV
    V_deviation = abs((V_opt(2:end) * V_base - 12.66) / 12.66) * 100; % 百分比
    
    fprintf('\n===============================================\n');
    fprintf('           优化结果\n');
    fprintf('===============================================\n');
    fprintf('初始网损: %.4f kW\n', Ploss0);
    fprintf('优化后网损: %.4f kW\n', bestLoss);
    fprintf('网损降低: %.2f%%\n', (Ploss0 - bestLoss)/Ploss0 * 100);
    fprintf('最大电压偏差: %.2f%%\n', max(V_deviation));
    fprintf('平均电压偏差: %.2f%%\n', mean(V_deviation));
    fprintf('===============================================\n');
    
    % 显示开关状态变化
    tieSwitches = find(branch(:,5) == 0);
    fprintf('\n联络开关状态变化:\n');
    for i = 1:length(tieSwitches)
        idx = tieSwitches(i);
        initialState = branch(idx,5);
        finalState = bestTopology(idx);
        fprintf('开关 %d-%d: %d -> %d\n', branch(idx,1), branch(idx,2), initialState, finalState);
    end
end

%% 可视化结果
function visualize_Results(convergence, bus, branch, bestTopology, V0, bestLoss, Ploss0)
    % 创建图形窗口
    figure('Name', '配电网重构结果', 'Position', [100, 100, 1400, 900], 'Color', 'w');
    
    % 收敛曲线
    subplot(2,3,1);
    plot(1:length(convergence), convergence, 'b-o', 'LineWidth', 1.5, 'MarkerFaceColor', 'b');
    xlabel('迭代次数');
    ylabel('网损 (kW)');
    title('蚁群算法收敛曲线');
    grid on;
    
    % 网损对比
    subplot(2,3,2);
    bar([Ploss0, bestLoss], 'FaceColor', [0.2 0.6 0.8]);
    set(gca, 'XTickLabel', {'初始网损', '优化后网损'});
    ylabel('网损 (kW)');
    title('网损对比');
    grid on;
    text(1, Ploss0, sprintf('%.2f kW', Ploss0), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
    text(2, bestLoss, sprintf('%.2f kW', bestLoss), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
    
    % 电压分布对比
    modifiedBranch = branch;
    modifiedBranch(:,5) = bestTopology;
    [V_opt, ~] = powerFlow(bus, modifiedBranch);
    
    subplot(2,3,3);
    plot(1:length(V0), V0, 'b-o', 'LineWidth', 1.5, 'DisplayName', '初始电压');
    hold on;
    plot(1:length(V_opt), V_opt, 'r--s', 'LineWidth', 1.5, 'DisplayName', '优化后电压');
    xlabel('节点编号');
    ylabel('电压幅值 (pu)');
    title('电压分布对比');
    legend('Location', 'best');
    grid on;
    
    % 网络拓扑图 (初始)
    subplot(2,3,4);
    drawNetworkTopology(bus, branch);
    title('初始网络拓扑');
    
    % 网络拓扑图 (优化后)
    subplot(2,3,5);
    drawNetworkTopology(bus, modifiedBranch);
    title('优化后网络拓扑');
    
    % 网损降低百分比
    subplot(2,3,6);
    percentReduction = (Ploss0 - bestLoss) / Ploss0 * 100;
    pie([percentReduction, 100-percentReduction], {'网损降低', '剩余网损'});
    title(sprintf('网损降低: %.2f%%', percentReduction));
    annotation('textbox', [0.65, 0.15, 0.2, 0.1], 'String', sprintf('%.2f kW', Ploss0-bestLoss), ...
               'FitBoxToText', 'on', 'BackgroundColor', 'white', 'FontSize', 12);
    
    % 添加总标题
    sgtitle('基于蚁群算法的配电网重构', 'FontSize', 16, 'FontWeight', 'bold');
end

%% 绘制网络拓扑图
function drawNetworkTopology(bus, branch)
    % 提取节点坐标 (简化布局)
    nBus = size(bus, 1);
    pos = zeros(nBus, 2);
    
    % 简单布局算法
    pos(1,:) = [0, 0]; % 平衡节点在中心
    angleStep = 2*pi / (nBus-1);
    angle = 0;
    for i = 2:nBus
        angle = angle + angleStep;
        radius = 5 + rand() * 2; % 随机半径
        pos(i,:) = [radius * cos(angle), radius * sin(angle)];
    end
    
    % 绘制节点
    plot(pos(:,1), pos(:,2), 'o', 'MarkerSize', 8, 'MarkerFaceColor', [0.2 0.4 0.8], 'MarkerEdgeColor', 'k');
    hold on;
    
    % 绘制支路
    for k = 1:size(branch, 1)
        from = branch(k,1);
        to = branch(k,2);
        status = branch(k,5);
        
        if status == 1 % 闭合开关
            lineColor = [0.3 0.7 0.3];
            lineWidth = 2;
        else % 断开开关
            lineColor = [0.8 0.2 0.2];
            lineWidth = 1.5;
        end
        
        plot([pos(from,1), pos(to,1)], [pos(from,2), pos(to,2)], ...
             'Color', lineColor, 'LineWidth', lineWidth);
    end
    
    % 标记节点编号
    for i = 1:nBus
        text(pos(i,1)+0.2, pos(i,2)+0.2, num2str(i), 'FontSize', 10);
    end
    
    % 设置图形属性
    axis equal;
    axis off;
    box on;
end

%% 运行主函数
main();

程序功能说明

这个程序实现了基于蚁群算法(ACO)的配电网重构,主要功能包括:

1. 系统建模

2. 核心算法

3. 电力系统分析

4. 可视化功能

算法流程

  1. 初始化

    • 加载IEEE 33节点系统数据
    • 设置ACO参数
    • 初始化信息素矩阵
  2. 主循环

    • 每只蚂蚁随机操作1-3个联络开关
    • 检查辐射状结构
    • 计算网损作为适应度值
    • 更新个体最优解
  3. 信息素更新

    • 信息素挥发(比例rho)
    • 精英蚂蚁额外增加信息素
    • 所有蚂蚁根据解质量增加信息素
  4. 结果输出

    • 显示优化前后网损对比
    • 显示开关状态变化
    • 可视化电压分布和网络拓扑

使用说明

  1. 在MATLAB中运行程序

  2. 程序自动执行以下步骤:

    • 加载IEEE 33节点系统数据
    • 计算初始潮流和网损
    • 运行蚁群优化算法
    • 显示优化结果
    • 绘制各种分析图表
  3. 参数调整:

    • 修改acoParams结构体调整算法参数
    • 修改branch矩阵改变网络拓扑
    • 修改bus矩阵改变负荷分布

参考代码 基于蚁群算法的配网重构计算程序 www.youwenfan.com/contentcsr/100584.html

预期结果

程序运行后将显示:

  1. 初始系统网损约202 kW
  2. 优化后网损降低10-20%
  3. 电压分布更加均衡
  4. 网络拓扑结构变化(部分联络开关闭合,部分断开)
  5. 收敛曲线显示算法快速找到较优解

技术特点

  1. 完整实现:包含配电网建模、潮流计算、ACO算法和可视化
  2. 真实数据:基于标准IEEE 33节点系统
  3. 实用算法:辐射状检查确保网络有效性
  4. 全面分析:网损、电压、拓扑等多维度评估
  5. 直观可视化:多种图表展示优化过程和结果

专注于matlab/simulink,电子电路,编程