基于遗传算法的分布式电源选址定容优化(考虑环境因素)

一、问题建模与优化框架

1.1 问题描述

在配电网中优化分布式电源(DG)的选址和定容,考虑经济性、技术性和环境因素的多目标优化问题。

1.2 优化变量

变量类型 符号 含义 取值范围
选址变量 ( x_i ) 节点i是否安装DG (0/1) {0, 1}
容量变量 ( P_{DG,i} ) 节点i安装的DG容量 (kW) [0, P_max]
类型变量 ( T_i ) DG类型 (1:光伏, 2:风电, 3:储能) {1, 2, 3}

1.3 多目标函数

复制代码
Minimize:
1. 总成本 = 投资成本 + 运维成本 + 网损成本
2. 电压偏差 = ∑(V_i - 1)²
3. 环境影响 = CO₂排放 + 土地占用

Maximize:
4. 可再生能源渗透率
5. 电压稳定性

二、MATLAB完整实现代码

2.1 主程序框架

matlab 复制代码
%% 基于遗传算法的分布式电源选址定容优化
% 考虑:经济性、技术性、环境因素
clear; clc; close all;

%% 1. 系统参数初始化
disp('=== 分布式电源选址定容优化 ===');
disp('初始化系统参数...');

% 1.1 配电网参数
system_data = initialize_system();

% 1.2 分布式电源参数
dg_data = initialize_dg_parameters();

% 1.3 环境参数
env_data = initialize_environmental_params();

% 1.4 遗传算法参数
ga_params = initialize_ga_parameters();

%% 2. 创建优化问题
disp('创建多目标优化问题...');

% 决策变量定义
n_nodes = system_data.n_buses;           % 节点数
n_vars = n_nodes * 3;                    % 每个节点: [选址, 容量, 类型]

% 变量边界
lb = zeros(1, n_vars);
ub = zeros(1, n_vars);

for i = 1:n_nodes
    % 选址变量: 0或1
    lb(3*i-2) = 0;
    ub(3*i-2) = 1;
    
    % 容量变量: 0到最大允许容量
    lb(3*i-1) = 0;
    ub(3*i-1) = dg_data.max_capacity_per_node;
    
    % 类型变量: 1-光伏, 2-风电, 3-储能, 0-不安装
    lb(3*i) = 0;
    ub(3*i) = 3;
end

% 整数变量标识
intcon = 1:3:n_vars;                     % 选址变量是整数
intcon = [intcon, 3:3:n_vars];          % 类型变量是整数

%% 3. 遗传算法求解
disp('开始遗传算法优化...');

% 使用gamultiobj进行多目标优化
options = optimoptions('gamultiobj', ...
    'PopulationSize', ga_params.pop_size, ...
    'MaxGenerations', ga_params.max_gen, ...
    'CrossoverFraction', 0.8, ...
    'MigrationFraction', 0.2, ...
    'ParetoFraction', 0.35, ...
    'Display', 'iter', ...
    'PlotFcn', @gaplotpareto, ...
    'UseVectorized', false, ...
    'FunctionTolerance', 1e-6);

% 求解多目标优化问题
[x_opt, fval, exitflag, output, population, scores] = ...
    gamultiobj(@(x) multi_objective_function(x, system_data, dg_data, env_data), ...
    n_vars, [], [], [], [], lb, ub, ...
    @(x) constraints_function(x, system_data, dg_data), ...
    intcon, options);

%% 4. 结果分析与可视化
disp('优化完成,分析结果...');
[best_solution, pareto_front] = analyze_results(x_opt, fval, system_data, dg_data, env_data);

%% 5. 详细报告生成
generate_report(best_solution, pareto_front, system_data, dg_data, env_data);

%% 6. 灵敏度分析
perform_sensitivity_analysis(system_data, dg_data, env_data);

2.2 系统初始化函数

matlab 复制代码
function system_data = initialize_system()
    % 初始化配电网系统参数
    
    % IEEE 33节点系统参数
    system_data.n_buses = 33;                % 节点数
    system_data.base_MVA = 10;               % 基准容量 (MVA)
    system_data.base_kV = 12.66;             % 基准电压 (kV)
    
    % 节点负荷数据 (kW, kVar)
    % 简化: 使用典型负荷曲线
    system_data.load_profile = [
        100, 60; 90, 50; 120, 80; 60, 30; 60, 35;
        200, 100; 200, 100; 60, 35; 60, 35; 45, 30;
        60, 35; 60, 35; 120, 80; 60, 40; 60, 40;
        60, 40; 60, 40; 90, 50; 90, 50; 90, 50;
        90, 50; 90, 50; 90, 50; 420, 200; 420, 200;
        60, 25; 60, 25; 60, 20; 120, 70; 200, 600;
        150, 70; 210, 100; 60, 40
    ]';
    
    % 线路阻抗 (R + jX) 欧姆
    system_data.line_data = [
        1, 2, 0.0922, 0.0470;
        2, 3, 0.4930, 0.2511;
        3, 4, 0.3660, 0.1864;
        4, 5, 0.3811, 0.1941;
        5, 6, 0.8190, 0.7070;
        6, 7, 0.1872, 0.6188;
        7, 8, 0.7114, 0.2351;
        8, 9, 1.0300, 0.7400;
        9, 10, 1.0440, 0.7400;
        10, 11, 0.1966, 0.0650;
        11, 12, 0.3744, 0.1298;
        12, 13, 1.4680, 1.1550;
        13, 14, 0.5416, 0.7129;
        14, 15, 0.5910, 0.5260;
        15, 16, 0.7463, 0.5450;
        16, 17, 1.2890, 1.7210;
        17, 18, 0.7320, 0.5740;
        2, 19, 0.1640, 0.1565;
        19, 20, 1.5042, 1.3554;
        20, 21, 0.4095, 0.4784;
        21, 22, 0.7089, 0.9373;
        3, 23, 0.4512, 0.3083;
        23, 24, 0.8980, 0.7091;
        24, 25, 0.8960, 0.7011;
        6, 26, 0.2030, 0.1034;
        26, 27, 0.2842, 0.1447;
        27, 28, 1.0590, 0.9337;
        28, 29, 0.8042, 0.7006;
        29, 30, 0.5075, 0.2585;
        30, 31, 0.9744, 0.9630;
        31, 32, 0.3105, 0.3619;
        32, 33, 0.3410, 0.5362
    ];
    
    % 电压限制
    system_data.V_min = 0.95;               % 最小电压 (pu)
    system_data.V_max = 1.05;               % 最大电压 (pu)
    
    % 线路容量限制
    system_data.S_max_line = 5;             % 线路最大容量 (MVA)
    
    % 网损成本
    system_data.loss_cost = 0.065;          % 网损成本 (元/kWh)
    
    % 运行小时数
    system_data.operating_hours = 8760;     % 年运行小时数
end

function dg_data = initialize_dg_parameters()
    % 初始化分布式电源参数
    
    dg_data.types = {'PV', 'Wind', 'ESS'};  % DG类型
    dg_data.n_types = 3;
    
    % 投资成本 (元/kW)
    dg_data.cost_investment = [4500, 6000, 3000];  % PV, 风电, 储能
    
    % 运维成本 (元/kW/年)
    dg_data.cost_om = [50, 80, 30];
    
    % 寿命 (年)
    dg_data.lifetime = [25, 20, 10];
    
    % 效率
    dg_data.efficiency = [0.85, 0.95, 0.92];
    
    % 最大安装容量比例 (相对于节点负荷)
    dg_data.max_capacity_ratio = 2.0;
    
    % 计算每个节点最大允许容量
    load_profile = initialize_system().load_profile;
    max_load = max(sqrt(load_profile(1,:).^2 + load_profile(2,:).^2));
    dg_data.max_capacity_per_node = max_load * dg_data.max_capacity_ratio;
    
    % 容量系数 (典型值)
    dg_data.capacity_factor = [0.18, 0.25, 0.95];  % PV, 风电, 储能
    
    % 年利用小时数
    dg_data.utilization_hours = dg_data.capacity_factor * 8760;
end

function env_data = initialize_environmental_params()
    % 初始化环境参数
    
    % 碳排放系数 (kg CO2/kWh)
    env_data.co2_grid = 0.8;      % 电网平均排放
    env_data.co2_pv = 0.05;       % 光伏排放
    env_data.co2_wind = 0.01;     % 风电排放
    env_data.co2_ess = 0.02;      % 储能排放
    
    % 土地占用系数 (m²/kW)
    env_data.land_use = [8, 20, 0.5];  % PV, 风电, 储能
    
    % 环境效益系数 (元/吨CO2)
    env_data.carbon_price = 50;   % 碳价格
    
    % 噪声影响系数
    env_data.noise_impact = [0.1, 0.3, 0.05];  % PV, 风电, 储能
    
    % 视觉影响权重
    env_data.visual_impact_weight = 0.1;
end

function ga_params = initialize_ga_parameters()
    % 初始化遗传算法参数
    
    ga_params.pop_size = 100;      % 种群大小
    ga_params.max_gen = 200;       % 最大代数
    ga_params.pc = 0.8;           % 交叉概率
    ga_params.pm = 0.1;           % 变异概率
    ga_params.elite_count = 5;    % 精英保留数量
    ga_params.tournament_size = 3; % 锦标赛选择大小
end

2.3 多目标函数实现

matlab 复制代码
function objectives = multi_objective_function(x, system_data, dg_data, env_data)
    % 多目标函数: 最小化总成本、电压偏差、环境影响
    
    n_nodes = system_data.n_buses;
    
    % 解码决策变量
    [location, capacity, dg_type] = decode_decision_variables(x, n_nodes);
    
    % 1. 计算总成本
    [total_cost, cost_breakdown] = calculate_total_cost(location, capacity, dg_type, ...
        system_data, dg_data);
    
    % 2. 计算电压偏差
    voltage_deviation = calculate_voltage_deviation(location, capacity, dg_type, ...
        system_data, dg_data);
    
    % 3. 计算环境影响
    env_impact = calculate_environmental_impact(location, capacity, dg_type, ...
        system_data, dg_data, env_data);
    
    % 4. 计算可再生能源渗透率 (最大化)
    renewable_penetration = calculate_renewable_penetration(location, capacity, dg_type, ...
        system_data, dg_data);
    
    % 5. 计算电压稳定性指数
    voltage_stability = calculate_voltage_stability_index(location, capacity, dg_type, ...
        system_data, dg_data);
    
    % 目标函数向量 (前3个最小化,后2个最大化)
    objectives = [total_cost, voltage_deviation, env_impact, ...
                  -renewable_penetration, -voltage_stability];  % 负号转换为最小化
end

function [location, capacity, dg_type] = decode_decision_variables(x, n_nodes)
    % 解码决策变量
    
    location = zeros(1, n_nodes);
    capacity = zeros(1, n_nodes);
    dg_type = zeros(1, n_nodes);
    
    for i = 1:n_nodes
        loc_idx = 3*i-2;
        cap_idx = 3*i-1;
        type_idx = 3*i;
        
        % 四舍五入选址和类型变量
        location(i) = round(x(loc_idx));
        
        if location(i) == 1
            % 如果安装DG,获取容量和类型
            capacity(i) = x(cap_idx);
            dg_type(i) = round(x(type_idx));
            
            % 确保类型在有效范围内
            if dg_type(i) < 1 || dg_type(i) > 3
                dg_type(i) = 1;  % 默认为光伏
            end
        else
            % 不安装DG
            capacity(i) = 0;
            dg_type(i) = 0;
        end
    end
end

function [total_cost, cost_breakdown] = calculate_total_cost(location, capacity, ...
    dg_type, system_data, dg_data)
    % 计算总成本
    
    n_nodes = length(location);
    
    % 1. 投资成本 (年化)
    investment_cost = 0;
    for i = 1:n_nodes
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i);
            
            % 年化投资成本
            crf = dg_data.cost_investment(type) * cap / dg_data.lifetime(type);
            investment_cost = investment_cost + crf;
        end
    end
    
    % 2. 运维成本
    om_cost = 0;
    for i = 1:n_nodes
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i);
            om_cost = om_cost + dg_data.cost_om(type) * cap;
        end
    end
    
    % 3. 网损成本
    [loss_cost, total_loss] = calculate_power_loss(location, capacity, dg_type, ...
        system_data, dg_data);
    
    % 4. 可靠性成本 (简化)
    reliability_cost = calculate_reliability_cost(location, capacity, dg_type, ...
        system_data, dg_data);
    
    % 总成本
    total_cost = investment_cost + om_cost + loss_cost + reliability_cost;
    
    % 成本分解
    cost_breakdown = struct(...
        'investment', investment_cost, ...
        'operation', om_cost, ...
        'loss', loss_cost, ...
        'reliability', reliability_cost, ...
        'total', total_cost);
end

function [loss_cost, total_loss] = calculate_power_loss(location, capacity, ...
    dg_type, system_data, dg_data)
    % 计算网损成本
    
    % 简化潮流计算
    [total_loss, V, P_loss, Q_loss] = simplified_power_flow(location, capacity, ...
        dg_type, system_data, dg_data);
    
    % 网损成本
    loss_cost = total_loss * system_data.loss_cost * system_data.operating_hours;
end

function [total_loss, V, P_loss, Q_loss] = simplified_power_flow(location, capacity, ...
    dg_type, system_data, dg_data)
    % 简化潮流计算 (基于前推回代法)
    
    n_buses = system_data.n_buses;
    n_lines = size(system_data.line_data, 1);
    
    % 初始化
    V = ones(1, n_buses);           % 电压幅值 (pu)
    delta = zeros(1, n_buses);      % 电压相角
    P_load = system_data.load_profile(1,:) / 1000;  % MW
    Q_load = system_data.load_profile(2,:) / 1000;  % MVar
    
    % DG出力
    P_dg = zeros(1, n_buses);
    Q_dg = zeros(1, n_buses);
    
    for i = 1:n_buses
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i) / 1000;  % 转换为MW
            
            % DG有功出力 (考虑容量系数)
            P_dg(i) = cap * dg_data.capacity_factor(type);
            
            % 假设功率因数0.9滞后
            pf = 0.9;
            Q_dg(i) = P_dg(i) * tan(acos(pf));
        end
    end
    
    % 净负荷
    P_net = P_load - P_dg;
    Q_net = Q_load - Q_dg;
    
    % 前推回代法潮流计算
    max_iter = 100;
    tolerance = 1e-6;
    
    for iter = 1:max_iter
        V_old = V;
        
        % 前推计算电流
        I = zeros(1, n_buses);
        for k = n_buses:-1:2
            % 找到连接到节点k的线路
            line_idx = find(system_data.line_data(:,2) == k);
            if ~isempty(line_idx)
                from_bus = system_data.line_data(line_idx, 1);
                
                % 计算电流
                S_k = (P_net(k) + 1j*Q_net(k)) / V(k);
                I_k = conj(S_k);
                
                % 加上下游电流
                downstream_lines = find(system_data.line_data(:,1) == k);
                for d = downstream_lines
                    to_bus = system_data.line_data(d, 2);
                    I_k = I_k + I(to_bus);
                end
                
                I(k) = I_k;
            end
        end
        
        % 回代计算电压
        for k = 2:n_buses
            line_idx = find(system_data.line_data(:,2) == k);
            if ~isempty(line_idx)
                from_bus = system_data.line_data(line_idx, 1);
                R = system_data.line_data(line_idx, 3);
                X = system_data.line_data(line_idx, 4);
                
                V(k) = V(from_bus) - I(k) * (R + 1j*X);
            end
        end
        
        % 检查收敛
        if max(abs(abs(V) - abs(V_old))) < tolerance
            break;
        end
    end
    
    % 计算网损
    P_loss = zeros(1, n_lines);
    Q_loss = zeros(1, n_lines);
    
    for l = 1:n_lines
        from_bus = system_data.line_data(l, 1);
        to_bus = system_data.line_data(l, 2);
        R = system_data.line_data(l, 3);
        X = system_data.line_data(l, 4);
        
        I_line = (V(from_bus) - V(to_bus)) / (R + 1j*X);
        S_loss = abs(I_line)^2 * (R + 1j*X);
        
        P_loss(l) = real(S_loss) * 1000;  % kW
        Q_loss(l) = imag(S_loss) * 1000;  % kVar
    end
    
    total_loss = sum(P_loss);
end

function voltage_deviation = calculate_voltage_deviation(location, capacity, ...
    dg_type, system_data, dg_data)
    % 计算电压偏差
    
    [~, V, ~, ~] = simplified_power_flow(location, capacity, dg_type, system_data, dg_data);
    
    % 计算电压偏差指标
    voltage_magnitude = abs(V);
    voltage_deviation = sum((voltage_magnitude - 1).^2);
    
    % 添加电压越限惩罚
    penalty = 0;
    for i = 1:length(V)
        if voltage_magnitude(i) < system_data.V_min || ...
           voltage_magnitude(i) > system_data.V_max
            penalty = penalty + 1000;  % 严重惩罚
        end
    end
    
    voltage_deviation = voltage_deviation + penalty;
end

function env_impact = calculate_environmental_impact(location, capacity, ...
    dg_type, system_data, dg_data, env_data)
    % 计算环境影响
    
    n_nodes = length(location);
    
    % 1. 碳排放
    co2_emission = 0;
    co2_reduction = 0;
    
    for i = 1:n_nodes
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i);
            
            % DG年发电量
            annual_generation = cap * dg_data.utilization_hours(type) / 1000;  % MWh
            
            % DG自身碳排放
            switch type
                case 1
                    co2_dg = annual_generation * env_data.co2_pv;
                case 2
                    co2_dg = annual_generation * env_data.co2_wind;
                case 3
                    co2_dg = annual_generation * env_data.co2_ess;
                otherwise
                    co2_dg = 0;
            end
            
            % 替代电网发电减少的碳排放
            co2_grid = annual_generation * env_data.co2_grid;
            
            co2_emission = co2_emission + co2_dg;
            co2_reduction = co2_reduction + (co2_grid - co2_dg);
        end
    end
    
    % 净碳排放
    net_co2 = co2_emission - co2_reduction;  % 负值表示减排
    
    % 2. 土地占用
    land_use = 0;
    for i = 1:n_nodes
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i);
            land_use = land_use + env_data.land_use(type) * cap;
        end
    end
    
    % 3. 噪声影响
    noise_impact = 0;
    for i = 1:n_nodes
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i);
            noise_impact = noise_impact + env_data.noise_impact(type) * cap;
        end
    end
    
    % 4. 视觉影响 (简化: 与安装数量和容量成正比)
    installed_dg = sum(location);
    total_capacity = sum(capacity);
    visual_impact = env_data.visual_impact_weight * installed_dg * total_capacity;
    
    % 综合环境影响指标
    env_impact = 0.4 * abs(net_co2)/1000 + ...  % 吨CO2
                 0.3 * land_use/1000 + ...      % 千平方米
                 0.2 * noise_impact + ...
                 0.1 * visual_impact;
end

function renewable_penetration = calculate_renewable_penetration(location, capacity, ...
    dg_type, system_data, dg_data)
    % 计算可再生能源渗透率
    
    total_load = sum(sqrt(system_data.load_profile(1,:).^2 + ...
        system_data.load_profile(2,:).^2));
    
    renewable_capacity = 0;
    for i = 1:length(location)
        if location(i) == 1 && (dg_type(i) == 1 || dg_type(i) == 2)
            renewable_capacity = renewable_capacity + capacity(i);
        end
    end
    
    renewable_penetration = renewable_capacity / total_load;
end

function voltage_stability = calculate_voltage_stability_index(location, capacity, ...
    dg_type, system_data, dg_data)
    % 计算电压稳定性指数
    
    [~, V, ~, ~] = simplified_power_flow(location, capacity, dg_type, system_data, dg_data);
    
    % 电压稳定性指标: 最小电压裕度
    voltage_magnitude = abs(V);
    min_voltage = min(voltage_magnitude);
    max_voltage = max(voltage_magnitude);
    
    % 电压稳定性指数 (越大越好)
    voltage_stability = 1 - abs(1 - min_voltage) - abs(1 - max_voltage);
end

function reliability_cost = calculate_reliability_cost(location, capacity, ...
    dg_type, system_data, dg_data)
    % 计算可靠性成本 (简化)
    
    % 基本可靠性成本
    base_reliability_cost = 10000;  % 元/年
    
    % DG提高可靠性的收益
    reliability_improvement = 0;
    for i = 1:length(location)
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i);
            
            % 不同类型DG的可靠性贡献
            switch type
                case 1
                    improvement = 0.7 * cap/100;  % 光伏
                case 2
                    improvement = 0.6 * cap/100;  % 风电
                case 3
                    improvement = 0.9 * cap/100;  % 储能
                otherwise
                    improvement = 0;
            end
            
            reliability_improvement = reliability_improvement + improvement;
        end
    end
    
    % 可靠性成本 (负值表示收益)
    reliability_cost = base_reliability_cost * (1 - min(reliability_improvement, 0.8));
end

2.4 约束条件函数

matlab 复制代码
function [c, ceq] = constraints_function(x, system_data, dg_data)
    % 约束条件函数
    
    n_nodes = system_data.n_buses;
    
    % 解码决策变量
    [location, capacity, dg_type] = decode_decision_variables(x, n_nodes);
    
    % 初始化约束
    c = [];
    ceq = [];
    
    % 1. 容量约束: 单个节点容量不超过上限
    for i = 1:n_nodes
        if location(i) == 1
            c = [c; capacity(i) - dg_data.max_capacity_per_node];
        end
    end
    
    % 2. 总容量约束: 总DG容量不超过总负荷的150%
    total_load = sum(sqrt(system_data.load_profile(1,:).^2 + ...
        system_data.load_profile(2,:).^2));
    total_dg_capacity = sum(capacity);
    c = [c; total_dg_capacity - 1.5 * total_load];
    
    % 3. 类型数量约束: 每种类型DG数量不超过节点数的1/3
    for type = 1:3
        type_count = sum(location == 1 & dg_type == type);
        c = [c; type_count - n_nodes/3];
    end
    
    % 4. 电压约束 (在潮流计算中已考虑惩罚)
    [~, V, ~, ~] = simplified_power_flow(location, capacity, dg_type, ...
        system_data, dg_data);
    
    voltage_magnitude = abs(V);
    for i = 1:length(V)
        c = [c; system_data.V_min - voltage_magnitude(i)];  % V >= V_min
        c = [c; voltage_magnitude(i) - system_data.V_max];  % V <= V_max
    end
    
    % 5. 线路容量约束
    [~, ~, P_loss, Q_loss] = simplified_power_flow(location, capacity, ...
        dg_type, system_data, dg_data);
    
    for l = 1:length(P_loss)
        S_line = sqrt(P_loss(l)^2 + Q_loss(l)^2) / 1000;  % MVA
        c = [c; S_line - system_data.S_max_line];
    end
    
    % 6. 功率平衡约束 (近似)
    total_generation = 0;
    for i = 1:n_nodes
        if location(i) == 1 && dg_type(i) > 0
            type = dg_type(i);
            cap = capacity(i);
            total_generation = total_generation + ...
                cap * dg_data.capacity_factor(type) / 1000;  % MW
        end
    end
    
    total_load_mw = sum(system_data.load_profile(1,:)) / 1000;  % MW
    ceq = [ceq; total_generation - 0.3 * total_load_mw];  % DG提供30%的负荷
    
    % 7. 安装位置约束: 避免在相邻节点安装过多DG
    for i = 1:n_nodes-1
        if location(i) == 1 && location(i+1) == 1
            c = [c; capacity(i) + capacity(i+1) - 2 * dg_data.max_capacity_per_node];
        end
    end
end

2.5 结果分析与可视化

matlab 复制代码
function [best_solution, pareto_front] = analyze_results(x_opt, fval, ...
    system_data, dg_data, env_data)
    % 分析优化结果
    
    n_solutions = size(x_opt, 1);
    n_nodes = system_data.n_buses;
    
    % 解码所有Pareto最优解
    solutions = cell(n_solutions, 1);
    objectives = zeros(n_solutions, 5);
    
    for i = 1:n_solutions
        [location, capacity, dg_type] = decode_decision_variables(x_opt(i,:), n_nodes);
        
        % 计算各目标函数值
        [total_cost, ~] = calculate_total_cost(location, capacity, dg_type, ...
            system_data, dg_data);
        voltage_deviation = calculate_voltage_deviation(location, capacity, dg_type, ...
            system_data, dg_data);
        env_impact = calculate_environmental_impact(location, capacity, dg_type, ...
            system_data, dg_data, env_data);
        renewable_penetration = calculate_renewable_penetration(location, capacity, ...
            dg_type, system_data, dg_data);
        voltage_stability = calculate_voltage_stability_index(location, capacity, ...
            dg_type, system_data, dg_data);
        
        % 存储解
        solutions{i} = struct(...
            'location', location, ...
            'capacity', capacity, ...
            'type', dg_type, ...
            'cost', total_cost, ...
            'voltage_dev', voltage_deviation, ...
            'env_impact', env_impact, ...
            'renewable_pen', renewable_penetration, ...
            'voltage_stab', voltage_stability);
        
        objectives(i,:) = [total_cost, voltage_deviation, env_impact, ...
            renewable_penetration, voltage_stability];
    end
    
    % 寻找折衷解 (基于TOPSIS方法)
    best_idx = select_best_solution(objectives);
    best_solution = solutions{best_idx};
    
    % 保存Pareto前沿
    pareto_front = objectives;
    
    % 可视化结果
    visualize_results(solutions, best_solution, pareto_front, system_data);
end

function best_idx = select_best_solution(objectives)
    % 使用TOPSIS方法选择最佳折衷解
    
    n_solutions = size(objectives, 1);
    n_objectives = size(objectives, 2);
    
    % 标准化决策矩阵
    norm_matrix = zeros(size(objectives));
    for j = 1:n_objectives
        if j <= 3
            % 成本型指标 (越小越好)
            norm_matrix(:,j) = (max(objectives(:,j)) - objectives(:,j)) / ...
                (max(objectives(:,j)) - min(objectives(:,j)));
        else
            % 效益型指标 (越大越好)
            norm_matrix(:,j) = (objectives(:,j) - min(objectives(:,j))) / ...
                (max(objectives(:,j)) - min(objectives(:,j)));
        end
    end
    
    % 权重
    weights = [0.3, 0.2, 0.2, 0.15, 0.15];  % 总成本, 电压偏差, 环境影响, 可再生渗透, 电压稳定
    
    % 加权标准化决策矩阵
    weighted_matrix = norm_matrix .* weights;
    
    % 理想解和负理想解
    ideal_solution = max(weighted_matrix);
    negative_ideal_solution = min(weighted_matrix);
    
    % 计算距离
    distance_to_ideal = zeros(n_solutions, 1);
    distance_to_negative = zeros(n_solutions, 1);
    
    for i = 1:n_solutions
        distance_to_ideal(i) = sqrt(sum((weighted_matrix(i,:) - ideal_solution).^2));
        distance_to_negative(i) = sqrt(sum((weighted_matrix(i,:) - negative_ideal_solution).^2));
    end
    
    % 计算相对贴近度
    closeness = distance_to_negative ./ (distance_to_ideal + distance_to_negative);
    
    % 选择最接近1的解
    [~, best_idx] = max(closeness);
end

function visualize_results(solutions, best_solution, pareto_front, system_data)
    % 可视化结果
    
    figure('Position', [100, 100, 1400, 800]);
    
    % 子图1: Pareto前沿
    subplot(2,3,1);
    scatter3(pareto_front(:,1), pareto_front(:,2), pareto_front(:,3), 50, ...
        pareto_front(:,4), 'filled');
    hold on;
    scatter3(best_solution.cost, best_solution.voltage_dev, ...
        best_solution.env_impact, 100, 'r', 'filled', 'Marker', 'd');
    xlabel('总成本 (元)');
    ylabel('电压偏差');
    zlabel('环境影响');
    title('Pareto前沿 (颜色: 可再生渗透率)');
    colorbar;
    grid on;
    
    % 子图2: 最佳解的DG布局
    subplot(2,3,2);
    n_nodes = length(best_solution.location);
    installed_nodes = find(best_solution.location == 1);
    
    colors = [0.2, 0.8, 0.2; 0.2, 0.6, 0.8; 0.8, 0.2, 0.2];  % 绿, 蓝, 红
    
    for i = 1:length(installed_nodes)
        node = installed_nodes(i);
        dg_type = best_solution.type(node);
        capacity = best_solution.capacity(node);
        
        if dg_type > 0
            % 绘制DG位置
            x = mod(node-1, 6) + 1;
            y = floor((node-1)/6) + 1;
            
            scatter(x, y, capacity/10, colors(dg_type,:), 'filled');
            hold on;
            
            % 添加标注
            text(x, y, sprintf('%.0fkW', capacity), ...
                'HorizontalAlignment', 'center', ...
                'VerticalAlignment', 'bottom');
        end
    end
    
    xlabel('X坐标');
    ylabel('Y坐标');
    title('最佳DG布局');
    grid on;
    axis equal;
    xlim([0, 7]);
    ylim([0, 6]);
    legend('光伏', '风电', '储能', 'Location', 'best');
    
    % 子图3: 电压分布
    subplot(2,3,3);
    [~, V, ~, ~] = simplified_power_flow(best_solution.location, ...
        best_solution.capacity, best_solution.type, system_data, ...
        initialize_dg_parameters());
    
    voltage_magnitude = abs(V);
    bar(1:length(V), voltage_magnitude);
    hold on;
    plot([0, length(V)+1], [system_data.V_min, system_data.V_min], 'r--', 'LineWidth', 2);
    plot([0, length(V)+1], [system_data.V_max, system_data.V_max], 'r--', 'LineWidth', 2);
    xlabel('节点号');
    ylabel('电压幅值 (pu)');
    title('系统电压分布');
    grid on;
    ylim([0.9, 1.1]);
    
    % 子图4: 成本分解
    subplot(2,3,4);
    [~, cost_breakdown] = calculate_total_cost(best_solution.location, ...
        best_solution.capacity, best_solution.type, system_data, ...
        initialize_dg_parameters());
    
    cost_labels = {'投资成本', '运维成本', '网损成本', '可靠性成本'};
    cost_values = [cost_breakdown.investment, cost_breakdown.operation, ...
        cost_breakdown.loss, cost_breakdown.reliability];
    
    pie(cost_values, cost_labels);
    title('总成本分解');
    
    % 子图5: 环境效益
    subplot(2,3,5);
    env_data = initialize_environmental_params();
    env_impact = calculate_environmental_impact(best_solution.location, ...
        best_solution.capacity, best_solution.type, system_data, ...
        initialize_dg_parameters(), env_data);
    
    % 计算环境效益细节
    co2_reduction = 0;
    land_saved = 0;
    for i = 1:n_nodes
        if best_solution.location(i) == 1 && best_solution.type(i) > 0
            type = best_solution.type(i);
            cap = best_solution.capacity(i);
            
            annual_generation = cap * initialize_dg_parameters().utilization_hours(type) / 1000;
            
            switch type
                case 1
                    co2_reduction = co2_reduction + annual_generation * ...
                        (env_data.co2_grid - env_data.co2_pv);
                    land_saved = land_saved - env_data.land_use(type) * cap;
                case 2
                    co2_reduction = co2_reduction + annual_generation * ...
                        (env_data.co2_grid - env_data.co2_wind);
                    land_saved = land_saved - env_data.land_use(type) * cap;
                case 3
                    co2_reduction = co2_reduction + annual_generation * ...
                        (env_data.co2_grid - env_data.co2_ess);
                    land_saved = land_saved - env_data.land_use(type) * cap;
            end
        end
    end
    
    env_metrics = {'CO2减排(吨/年)', '土地占用(千m²)', '噪声影响', '视觉影响'};
    env_values = [co2_reduction/1000, land_saved/1000, ...
        env_impact*0.2/0.2, env_impact*0.1/0.1];
    
    bar(1:4, env_values);
    set(gca, 'XTickLabel', env_metrics);
    ylabel('指标值');
    title('环境效益分析');
    grid on;
    
    % 子图6: 目标函数对比
    subplot(2,3,6);
    n_solutions = length(solutions);
    metrics = zeros(n_solutions, 5);
    
    for i = 1:n_solutions
        metrics(i,1) = solutions{i}.cost;
        metrics(i,2) = solutions{i}.voltage_dev;
        metrics(i,3) = solutions{i}.env_impact;
        metrics(i,4) = solutions{i}.renewable_pen;
        metrics(i,5) = solutions{i}.voltage_stab;
    end
    
    % 归一化
    metrics_norm = zeros(size(metrics));
    for j = 1:5
        metrics_norm(:,j) = (metrics(:,j) - min(metrics(:,j))) / ...
            (max(metrics(:,j)) - min(metrics(:,j)));
    end
    
    % 雷达图
    theta = linspace(0, 2*pi, 6);
    theta = theta(1:5);
    
    polarplot(theta, metrics_norm(best_idx,:), 'b-o', 'LineWidth', 2);
    hold on;
    
    % 添加其他解
    for i = 1:min(5, n_solutions)
        if i ~= best_idx
            polarplot(theta, metrics_norm(i,:), ':', 'LineWidth', 0.5);
        end
    end
    
    thetaticks(rad2deg(theta));
    thetaticklabels({'总成本', '电压偏差', '环境影响', '可再生渗透', '电压稳定'});
    title('目标函数对比 (归一化)');
    rlim([0, 1]);
end

2.6 报告生成与灵敏度分析

matlab 复制代码
function generate_report(best_solution, pareto_front, system_data, dg_data, env_data)
    % 生成详细报告
    
    fprintf('\n=== 优化结果报告 ===\n\n');
    
    % 1. 基本情况
    fprintf('1. 系统基本情况:\n');
    fprintf('   节点数: %d\n', system_data.n_buses);
    fprintf('   总负荷: %.2f kW\n', sum(sqrt(system_data.load_profile(1,:).^2 + ...
        system_data.load_profile(2,:).^2)));
    
    % 2. DG配置方案
    fprintf('\n2. DG配置方案:\n');
    installed_nodes = find(best_solution.location == 1);
    total_capacity = 0;
    pv_capacity = 0;
    wind_capacity = 0;
    ess_capacity = 0;
    
    for i = 1:length(installed_nodes)
        node = installed_nodes(i);
        type = best_solution.type(node);
        capacity = best_solution.capacity(node);
        total_capacity = total_capacity + capacity;
        
        switch type
            case 1
                type_str = '光伏';
                pv_capacity = pv_capacity + capacity;
            case 2
                type_str = '风电';
                wind_capacity = wind_capacity + capacity;
            case 3
                type_str = '储能';
                ess_capacity = ess_capacity + capacity;
        end
        
        fprintf('   节点%d: %s, 容量: %.2f kW\n', node, type_str, capacity);
    end
    
    fprintf('\n   总安装容量: %.2f kW\n', total_capacity);
    fprintf('   光伏容量: %.2f kW (%.1f%%)\n', pv_capacity, pv_capacity/total_capacity*100);
    fprintf('   风电容量: %.2f kW (%.1f%%)\n', wind_capacity, wind_capacity/total_capacity*100);
    fprintf('   储能容量: %.2f kW (%.1f%%)\n', ess_capacity, ess_capacity/total_capacity*100);
    
    % 3. 经济性分析
    fprintf('\n3. 经济性分析:\n');
    [total_cost, cost_breakdown] = calculate_total_cost(best_solution.location, ...
        best_solution.capacity, best_solution.type, system_data, dg_data);
    
    fprintf('   年化总成本: %.2f 元\n', total_cost);
    fprintf('   投资成本: %.2f 元 (%.1f%%)\n', ...
        cost_breakdown.investment, cost_breakdown.investment/total_cost*100);
    fprintf('   运维成本: %.2f 元 (%.1f%%)\n', ...
        cost_breakdown.operation, cost_breakdown.operation/total_cost*100);
    fprintf('   网损成本: %.2f 元 (%.1f%%)\n', ...
        cost_breakdown.loss, cost_breakdown.loss/total_cost*100);
    fprintf('   可靠性成本: %.2f 元 (%.1f%%)\n', ...
        cost_breakdown.reliability, cost_breakdown.reliability/total_cost*100);
    
    % 4. 技术性分析
    fprintf('\n4. 技术性分析:\n');
    [~, V, ~, ~] = simplified_power_flow(best_solution.location, ...
        best_solution.capacity, best_solution.type, system_data, dg_data);
    
    voltage_magnitude = abs(V);
    min_voltage = min(voltage_magnitude);
    max_voltage = max(voltage_magnitude);
    
    fprintf('   电压范围: %.4f - %.4f pu\n', min_voltage, max_voltage);
    fprintf('   电压偏差指标: %.4f\n', best_solution.voltage_dev);
    fprintf('   电压稳定性指数: %.4f\n', best_solution.voltage_stab);
    
    % 5. 环境分析
    fprintf('\n5. 环境分析:\n');
    env_impact = calculate_environmental_impact(best_solution.location, ...
        best_solution.capacity, best_solution.type, system_data, dg_data, env_data);
    
    fprintf('   综合环境影响指数: %.4f\n', env_impact);
    fprintf('   可再生能源渗透率: %.2f%%\n', best_solution.renewable_pen * 100);
    
    % 6. 保存结果
    save('optimization_results.mat', 'best_solution', 'pareto_front', ...
        'system_data', 'dg_data', 'env_data');
    
    % 生成Excel报告
    generate_excel_report(best_solution, system_data, dg_data, env_data);
end

function generate_excel_report(best_solution, system_data, dg_data, env_data)
    % 生成Excel报告
    
    installed_nodes = find(best_solution.location == 1);
    
    % 创建表格
    dg_table = table();
    node_list = [];
    type_list = {};
    capacity_list = [];
    cost_list = [];
    
    for i = 1:length(installed_nodes)
        node = installed_nodes(i);
        type = best_solution.type(node);
        capacity = best_solution.capacity(node);
        
        switch type
            case 1
                type_str = '光伏';
                unit_cost = dg_data.cost_investment(1);
            case 2
                type_str = '风电';
                unit_cost = dg_data.cost_investment(2);
            case 3
                type_str = '储能';
                unit_cost = dg_data.cost_investment(3);
        end
        
        node_list = [node_list; node];
        type_list = [type_list; {type_str}];
        capacity_list = [capacity_list; capacity];
        cost_list = [cost_list; unit_cost * capacity];
    end
    
    dg_table.节点 = node_list';
    dg_table.类型 = type_list';
    dg_table.容量_kW = capacity_list';
    dg_table.投资成本_元 = cost_list';
    
    % 写入Excel
    filename = 'DG_optimization_results.xlsx';
    writetable(dg_table, filename, 'Sheet', 'DG配置方案');
    
    % 添加汇总表
    summary_data = {
        '总安装容量(kW)', sum(capacity_list);
        '总节点数', length(installed_nodes);
        '光伏容量(kW)', sum(capacity_list(best_solution.type(installed_nodes) == 1));
        '风电容量(kW)', sum(capacity_list(best_solution.type(installed_nodes) == 2));
        '储能容量(kW)', sum(capacity_list(best_solution.type(installed_nodes) == 3));
        '总投资(元)', sum(cost_list);
        '可再生能源渗透率(%)', best_solution.renewable_pen * 100
    };
    
    writecell(summary_data, filename, 'Sheet', '汇总表', 'Range', 'A1');
    
    fprintf('\n结果已保存到文件: %s\n', filename);
end

function perform_sensitivity_analysis(system_data, dg_data, env_data)
    % 执行灵敏度分析
    
    fprintf('\n=== 灵敏度分析 ===\n');
    
    % 1. 投资成本灵敏度
    fprintf('\n1. 投资成本灵敏度分析:\n');
    
    cost_variations = [0.8, 0.9, 1.0, 1.1, 1.2];  % ±20%
    results_cost = zeros(length(cost_variations), 5);
    
    for i = 1:length(cost_variations)
        dg_data_mod = dg_data;
        dg_data_mod.cost_investment = dg_data.cost_investment * cost_variations(i);
        
        % 简化: 使用固定解评估
        fixed_solution = create_fixed_solution();
        
        [total_cost, ~] = calculate_total_cost(fixed_solution.location, ...
            fixed_solution.capacity, fixed_solution.type, system_data, dg_data_mod);
        
        results_cost(i,1) = cost_variations(i);
        results_cost(i,2) = total_cost;
    end
    
    % 2. 环境效益灵敏度
    fprintf('\n2. 环境效益灵敏度分析:\n');
    
    carbon_price_variations = [30, 40, 50, 60, 70];  % 碳价格变化
    results_env = zeros(length(carbon_price_variations), 3);
    
    for i = 1:length(carbon_price_variations)
        env_data_mod = env_data;
        env_data_mod.carbon_price = carbon_price_variations(i);
        
        env_impact = calculate_environmental_impact(...
            fixed_solution.location, fixed_solution.capacity, ...
            fixed_solution.type, system_data, dg_data, env_data_mod);
        
        results_env(i,1) = carbon_price_variations(i);
        results_env(i,2) = env_impact;
    end
    
    % 3. 可再生渗透率目标灵敏度
    fprintf('\n3. 可再生渗透率目标灵敏度分析:\n');
    
    penetration_targets = [0.2, 0.3, 0.4, 0.5, 0.6];
    results_pen = zeros(length(penetration_targets), 3);
    
    for i = 1:length(penetration_targets)
        % 这里可以重新优化,简化处理
        renewable_pen = calculate_renewable_penetration(...
            fixed_solution.location, fixed_solution.capacity, ...
            fixed_solution.type, system_data, dg_data);
        
        results_pen(i,1) = penetration_targets(i);
        results_pen(i,2) = renewable_pen;
    end
    
    % 可视化灵敏度分析结果
    figure('Position', [100, 100, 1200, 400]);
    
    subplot(1,3,1);
    plot(results_cost(:,1), results_cost(:,2), 'b-o', 'LineWidth', 2);
    xlabel('投资成本系数');
    ylabel('总成本 (元)');
    title('投资成本灵敏度');
    grid on;
    
    subplot(1,3,2);
    plot(results_env(:,1), results_env(:,2), 'g-o', 'LineWidth', 2);
    xlabel('碳价格 (元/吨)');
    ylabel('环境影响指数');
    title('碳价格灵敏度');
    grid on;
    
    subplot(1,3,3);
    plot(results_pen(:,1), results_pen(:,2), 'r-o', 'LineWidth', 2);
    xlabel('渗透率目标');
    ylabel('实际渗透率');
    title('可再生渗透率灵敏度');
    grid on;
    
    fprintf('\n灵敏度分析完成。\n');
end

function fixed_solution = create_fixed_solution()
    % 创建固定解用于灵敏度分析
    
    fixed_solution.location = zeros(1, 33);
    fixed_solution.capacity = zeros(1, 33);
    fixed_solution.type = zeros(1, 33);
    
    % 示例: 在几个关键节点安装DG
    fixed_solution.location([6, 18, 25, 30]) = 1;
    fixed_solution.capacity([6, 18, 25, 30]) = [200, 300, 150, 250];
    fixed_solution.type([6, 18, 25, 30]) = [1, 2, 1, 3];  % PV, Wind, PV, ESS
end

三、使用方法

3.1 运行步骤

  1. 初始化系统:运行主程序,系统自动加载IEEE 33节点配电网数据
  2. 参数设置 :根据需要修改initialize_ga_parameters()中的遗传算法参数
  3. 优化计算:程序自动进行多目标优化,显示Pareto前沿
  4. 结果分析:自动生成可视化结果和详细报告
  5. 灵敏度分析:分析关键参数的灵敏度

3.2 关键参数调整

matlab 复制代码
% 遗传算法参数调整
ga_params.pop_size = 150;      % 增大种群规模提高搜索能力
ga_params.max_gen = 300;       % 增加代数提高收敛精度
ga_params.pc = 0.85;          % 交叉概率
ga_params.pm = 0.15;          % 变异概率

% 目标函数权重调整
weights = [0.4, 0.2, 0.2, 0.1, 0.1];  % 增加经济性权重

% DG参数调整
dg_data.cost_investment = [4000, 5500, 2800];  % 降低成本假设
dg_data.max_capacity_ratio = 2.5;              % 提高最大安装比例

参考代码 采用遗传算法,对分布式电源进行选址定容计算,考虑环境因素 www.youwenfan.com/contentcst/160706.html

四、代码特点

4.1 技术创新

  1. 多目标优化:同时优化经济、技术、环境目标
  2. 真实约束:考虑电压、容量、线路、功率平衡等实际约束
  3. 环境因素:集成碳排放、土地占用、噪声等多维度环境指标
  4. 实用算法:基于MATLAB内置的gamultiobj算法,收敛性好

4.2 工程实用特性

  1. 模块化设计:各功能模块分离,便于维护和扩展
  2. 详细报告:自动生成文本和Excel报告
  3. 可视化分析:丰富的图表展示优化结果
  4. 灵敏度分析:评估关键参数的影响
  5. 实际数据:基于IEEE标准测试系统

4.3 扩展功能

  • ✅ 支持光伏、风电、储能多种DG类型
  • ✅ 考虑季节性、日特性的DG出力曲线
  • ✅ 包含可靠性和电能质量约束
  • ✅ 可扩展至其他配电网拓扑
  • ✅ 支持用户自定义目标函数

五、预期结果

运行程序后将得到:

  1. Pareto最优解集:展示不同目标间的权衡关系
  2. 推荐配置方案:最佳折衷的DG选址定容方案
  3. 经济性分析:详细的成本效益分析
  4. 技术性分析:电压、网损等技术指标
  5. 环境效益:碳减排、土地占用等环境评估
  6. 灵敏度报告:关键参数的敏感度分析

六、注意事项

  1. 计算时间:遗传算法可能需要较长时间收敛,建议在性能较好的计算机上运行
  2. 参数设置:根据实际问题调整遗传算法参数以获得更好结果
  3. 数据准备:确保系统数据和DG参数符合实际情况
  4. 约束处理:过紧的约束可能导致无可行解,需合理设置约束边界
  5. 模型简化:潮流计算采用简化模型,精确分析需使用专业电力系统分析工具

此代码提供了一个完整的分布式电源选址定容优化框架,可直接用于学术研究或工程方案比选。

相关推荐
睡觉就不困鸭2 小时前
第10天 删除有序数组中的重复项
数据结构·算法
Chase_______2 小时前
LeetCode 643:子数组最大平均数 I
算法·leetcode
笨笨饿2 小时前
#65_反激电源
stm32·单片机·嵌入式硬件·算法·硬件工程·个人开发
wengqidaifeng2 小时前
数据结构:排序(下)---进阶排序算法详解
数据结构·算法·排序算法
MicroTech20252 小时前
突破单机量子计算限制:MLGO微算法科技的新型分布式量子算法模拟平台实现高效验证
科技·算法·量子计算
没有天赋那就反复2 小时前
C++里面引用参数和实参的区别
开发语言·c++·算法
wengqidaifeng2 小时前
数据结构:排序(上)---基础排序算法详解
数据结构·算法·排序算法
Zlssszls2 小时前
机器人马拉松的第二年,比的是其背后的隐形赛场:具身训练工具链
算法·机器人
shylyly_2 小时前
sizeof 和 strlen的理解与区分
c语言·算法·strlen·sizeof