ReW_p

经典exe


在这里插入图片描述
v18是我们输入的;v5是那一串带点的;v0是我们输入的字符串长度;
看函数sub_4116c7:
在这里插入图片描述
每三个一组,取前六位、取后两位、取前两位,和0x30、f、3f进行与操作。这么熟悉的操作,想起了base64加密。再看看加密字符串:

在这里插入图片描述
能够确定,是base64加密
然后往下看
v1是加密后字符串的长度;
然后进入函数sub_411389:
在这里插入图片描述
依旧是加密,只不过表的最后两位给换了。
往下看函数sub_411023:
在这里插入图片描述
就是给加密后的字符串加点,加到第一位:a2的第一位是点,然后a2的第二位是a1的第一位,然后依次换位。
接下来的函数也是加点,只不过加到了第22位上
接下来的函数运用的是爆破思想:
个人理解:首先是可见字符串的ASCII码值,x代表ASCII码值,a代表数组1,b代表数组2,c代表数组3
如果a的第一位的x对应b的第一位,那么就让c的第一位赋值为x
脚本:

int main()
{
    char Str[100];
    int a[100];
    char a1[]=".W1BqthGbfGBqoXBmVZRQd.W5VoXNJcMR6XNBxoM5FoFDucMWyWNfBpXNAoF0.";
    int a2 = 2;
    int len = strlen(a1);
     for ( int i = 0;i<len ; i++ )
  {
      for(int j = 0;j < 128;j++){
            a[i] = j;
    if ( a[i] < 65 || a[i] > 90 )//如果a[i]的值小于65或者大于90进去继续
    {
      if ( a[i] >= 97 && a[i] <= 122 )//如果值在(97,122)之间正好是25小于26,那么ASCII值加上97,变成大写的26个字母其中之一
        a[i] = (a[i] + a2 - 97) % 26 + 97;
    }
    else
    {
      a[i] = (a[i] + a2 - 65) % 26 + 65;//如果值在(65,90)之间正好是25小于26,那么ASCII值加上65,变成小写的26个字母其中之一
    }
    if((char)a[i] == a1[i]){
        Str[i] = j;
    }
  }
  }
  for(int i = 0;i<len;i++){
  printf("%c",Str[i]);
  }
  return 0;
}

在这里插入图片描述

解出来的字符串去点然后进行两次的base64解密即可

经典base

在这里插入图片描述
查看伪代码,我们发现有一个base58加密(Base58 采用数字、大写字母、小写字母,去除歧义字符 0(零)、O(大写字母 O)、I(大写字母i)、l(小写字母L),总计58个字符作为编码的字母表也就是:123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz)
那我们直接用icyberchef:
在这里插入图片描述
从base58转换为字符串

经典re1

用x64dbg打开之后,运行,然后右键>搜索>当前区域>字符串,然后ctrl+f搜索flag找到
在这里插入图片描述

经典re2

用ida打开
在这里插入图片描述
首先我们输入v7,然后将v7的值赋给v10,然后进入while循环,过程是把v7的值每一个都与7异或,然后退出循环,如果v8和那个unk函数里的值一样就是yes
那么我们要做的就是找到unk函数里的值,然后再每个值异或7即可
然后进入unk函数
在这里插入图片描述
发现什么都没有,这需要动态调试了,unk的值应该是程序运行到某个地方然后才会跑出来值
关于ida的动调:首先找一个合适的调试器
在这里插入图片描述
然后文件目录下找到dbgsrv
在这里插入图片描述
我们用的是64位的
在这里插入图片描述
把端口、ip填进去
在这里插入图片描述
开始调试
在这里插入图片描述
在这里插入图片描述
找到这一串字符串,发现是他们进入到unk函数里
在ida里面,进入unk函数
在这里插入图片描述
可以发现mov了很多值进入函数里,我们用f5查看代码
在这里插入图片描述
在这里插入图片描述
但不造我为啥后面没有字符串。。。。。
然后写一个简简单单的脚本得到flag
在这里插入图片描述

squid

放到linux里面运行
在这里插入图片描述
拉入ida,shift+f12查看字符串,发现有很多py开头的
在这里插入图片描述
意味着是一个python文件被打包成了exe文件
用pyinstxtractor.py文件
通过readme.txt获得使用方法
在这里插入图片描述

在这里插入图片描述
成功解包
在这里插入图片描述
捣鼓了半天,根据cmd里面的提示发现,说是要用python3.6去解包,于是我把python版本改为3.6,然后解包,PYZ-00.pyz_extracted文件夹里才有东西了。
之后把struct里面的文件头给pyc补充上,然后放到在线python反编译得到
在这里插入图片描述
打开PCcharm运行得到
在这里插入图片描述

cheems

先查壳
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
ida里面也找不到字符串和函数
然后可以看到是upx
在这里插入图片描述
但放到010发现是cpx,怪了,手动改改
在这里插入图片描述
可以了
然后ida找字符串
在这里插入图片描述

检查以下代码,给出修改后的完整代码:%% 基于阶梯碳交易机制与P2P能源共享的综合能源系统优化调度模型 clc clear %% ===================== 记载数据初始化 ===================== % 可再生能源预测出力 P_rew_e = [875.53, 846.02, 862.71, 870.27, 836.21, 841.49, 782.69, 698.20, ... 701.43, 621.10, 581.15, 596.79, 580.38, 594.44, 566.58, 574.68, ... 585.51, 552.64, 670.59, 783.63, 804.22, 850.56, 884.58, 897.08]; % kW % 用户负荷数据 % 用户1 - 住宅区 P_Load_e1 = [438.92, 449.06, 384.87, 383.56, 555.78, 829.08, 695.91, 634.75, ... 824.46, 534.34, 811.55, 886.41, 730.53, 887.85, 541.62, 696.93, ... 769.47, 699.58, 572.86, 540.62, 435.75, 396.30, 474.34, 437.77]; % 电负荷 kW P_Load_h1 = [281.52, 210.02, 303.03, 218.45, 263.47, 350.30, 375.96, 330.86, ... 337.32, 444.79, 436.02, 503.66, 312.27, 366.63, 462.30, 500.07, ... 438.91, 331.59, 454.63, 356.66, 321.48, 363.06, 246.40, 320.91]; % 热负荷 kW % 用户2 - 商业区 P_Load_e2 = [311.35, 496.96, 422.78, 490.75, 501.39, 743.94, 606.49, 663.09, ... 700.90, 848.33, 800.27, 776.04, 651.84, 983.69, 671.58, 618.50, ... 512.87, 509.70, 756.91, 687.65, 554.83, 461.36, 344.51, 451.31]; P_Load_h2 = [292.69, 279.79, 249.42, 335.28, 324.71, 311.21, 440.46, 505.70, ... 346.73, 328.94, 360.90, 296.29, 434.50, 444.01, 359.39, 373.67, ... 554.70, 503.06, 296.99, 336.52, 328.45, 356.39, 328.94, 293.09]; % 用户3 - 工业区 P_Load_e3 = [392.65, 394.79, 398.32, 350.81, 375.84, 690.77, 627.59, 806.97, ... 545.60, 717.08, 874.86, 782.36, 809.26, 819.57, 549.26, 802.08, ... 831.48, 612.44, 597.20, 564.87, 544.12, 579.74, 481.82, 412.59]; P_Load_h3 = [260.08, 247.82, 262.92, 282.56, 402.31, 310.23, 394.91, 441.18, ... 295.73, 348.94, 413.06, 447.17, 439.96, 355.54, 496.10, 402.27, ... 402.00, 557.99, 482.69, 437.40, 307.05, 243.88, 303.08, 306.68]; %% ===================== 基础参数初始化 ===================== % 时间参数 num_hours = 24; hours = 1:num_hours; % 电价参数 C_Pri_buy = [0.60*ones(1,8), 0.75*ones(1,4), 1.20*ones(1,3), ... 0.75*ones(1,4), 1.20*ones(1,4), 0.60*ones(1,1)]; % 购电价 C_Pri_sell = [0.30*ones(1,8), 0.40*ones(1,4), 0.60*ones(1,3), ... 0.40*ones(1,4), 0.60*ones(1,4), 0.30*ones(1,1)]; % 售电价 C_gas_price = 3.2; % 天然气价格 (元/Nm³) C_carbon_price = [0.2, 0.3, 0.45]; % 阶梯碳价 [元/kg] k_curtail_penalty = 0.5; % 弃风惩罚系数 % 设备参数 CHP_eff_e = 0.30; % CHP发电效率 CHP_eff_h = 0.45; % CHP产热效率 CHP_gas_heat = 9.7; % 天然气热 (kWh/Nm³) GB_eff = 0.90; % 燃气锅炉效率 P2G_eff = 0.95; % P2G转换效率 CCS_energy = 0.109; % CCS单位能耗 (kWh/kgCO2) CO2_factor = 0.390; % 单位天然气碳排放 (kgCO2/Nm³) % 置信区间计算 (正态分布双侧分位数) confidence_level = 0.8; margin = norminv((1+confidence_level)/2); % 80%置信水平 M = 1e6; % Big-M法大常数 %% ===================== 决策变量定义 ===================== % 设备变量 P_e_rew = sdpvar(1, num_hours); % 可再生能源实际出力 P_e_cur = sdpvar(1, num_hours); % 弃风量 P_e_CHP = sdpvar(1, num_hours); % CHP发电功率 P_h_CHP = sdpvar(1, num_hours); % CHP产热功率 P_h_GB = sdpvar(1, num_hours); % 燃气锅炉产热 P_e_P2G = sdpvar(1, num_hours); % P2G消耗功率 P_e_CCS = sdpvar(1, num_hours); % 碳捕集消耗功率 Gas_CHP = sdpvar(1, num_hours); % CHP耗气量 Gas_GB = sdpvar(1, num_hours); % GB耗气量 Gas_P2G = sdpvar(1, num_hours); % P2G产气量 CO2_CHP = sdpvar(1, num_hours); % CHP碳排放 CO2_CCS = sdpvar(1, num_hours); % CCS捕集量 % 电网交易变量 P_buy = sdpvar(1, num_hours); % 购电功率 P_sell = sdpvar(1, num_hours); % 售电功率 miu_ies = binvar(1, num_hours); % 交易状态 (0-售电/1-购电) % 用户变量 p_cut_1 = sdpvar(1, num_hours); % 用户1电负荷削减 h_cut_1 = sdpvar(1, num_hours); % 用户1热负荷削减 p_cut_2 = sdpvar(1, num_hours); % 用户2电负荷削减 h_cut_2 = sdpvar(1, num_hours); % 用户2热负荷削减 p_cut_3 = sdpvar(1, num_hours); % 用户3电负荷削减 h_cut_3 = sdpvar(1, num_hours); % 用户3热负荷削减 % P2P交易变量 (允许双向交易) p_tran_12 = sdpvar(1, num_hours); % 用户1<->2交易电量 p_tran_13 = sdpvar(1, num_hours); % 用户1<->3交易电量 p_tran_23 = sdpvar(1, num_hours); % 用户2<->3交易电量 % 碳交易变量 E_CO2 = sdpvar(1,1); % 实际碳排放 E_c = sdpvar(1,1); % 碳配额 E_1 = sdpvar(1,1); % 阶梯碳交易分段1 E_2 = sdpvar(1,1); % 阶梯碳交易分段2 E_3 = sdpvar(1,1); % 阶梯碳交易分段3 %% ===================== 约束条件 ===================== Cons = []; % === 设备物理约束 === % 可再生能源约束 Cons = [Cons, P_e_cur + P_e_rew == P_rew_e]; % 出力平衡 Cons = [Cons, 0 <= P_e_cur <= P_rew_e]; % 弃风限制 Cons = [Cons, 0 <= P_e_rew <= P_rew_e]; % 实际出力限制 % CHP机组约束 Cons = [Cons, P_e_CHP == CHP_eff_e * CHP_gas_heat * Gas_CHP]; % 电功率 Cons = [Cons, P_h_CHP == CHP_eff_h * CHP_gas_heat * Gas_CHP]; % 热功率 Cons = [Cons, 0 <= P_e_CHP <= 2000]; % 发电上限 Cons = [Cons, CO2_CHP == CO2_factor * Gas_CHP]; % 碳排放 Cons = [Cons, 0 <= Gas_CHP <= 2000/(CHP_eff_e*CHP_gas_heat)]; % 耗气量限制 % 燃气锅炉约束 (统一碳排放因子) Cons = [Cons, P_h_GB == GB_eff * CHP_gas_heat * Gas_GB]; % 热功率 Cons = [Cons, 0 <= P_h_GB <= 2000]; % 产热上限 Cons = [Cons, 0 <= Gas_GB <= 2000/(GB_eff*CHP_gas_heat)]; % 耗气量限制 % P2G约束 Cons = [Cons, Gas_P2G == P2G_eff * P_e_P2G / CHP_gas_heat]; % 产气量 Cons = [Cons, 0 <= P_e_P2G <= 500]; % 消耗功率上限 Cons = [Cons, 0 <= Gas_P2G]; % 产气量非负 % 碳捕集约束 Cons = [Cons, P_e_CCS == CCS_energy * CO2_CCS]; % 能耗计算 Cons = [Cons, 0 <= CO2_CCS <= 0.9 * CO2_CHP]; % 捕集量限制 Cons = [Cons, 0 <= P_e_CCS <= 500]; % 消耗功率上限 % === 系统平衡约束 === % 电力平衡 (考虑负荷削减) total_power_generation = P_e_CHP + P_e_rew; % 总发电量 total_power_consumption = (P_Load_e1 - p_cut_1) + (P_Load_e2 - p_cut_2) + (P_Load_e3 - p_cut_3) + ... P_e_P2G + P_e_CCS; % 总耗电量(含削减) uncertainty_margin = margin * 0.2 * P_rew_e; % 不确定性余量 Cons = [Cons, (P_buy - P_sell) + total_power_generation - total_power_consumption >= uncertainty_margin]; % 热力平衡 (考虑负荷削减) Cons = [Cons, P_h_CHP + P_h_GB == (P_Load_h1 - h_cut_1) + (P_Load_h2 - h_cut_2) + (P_Load_h3 - h_cut_3)]; % 天然气平衡 Cons = [Cons, Gas_CHP + Gas_GB <= Gas_P2G]; % 天然气消耗 <= P2G生产 % 购售电逻辑约束 Cons = [Cons, implies(miu_ies == 1, [P_sell == 0, P_buy >= 0])]; % 购电状态 Cons = [Cons, implies(miu_ies == 0, [P_buy == 0, P_sell >= 0])]; % 售电状态 Cons = [Cons, 0 <= P_buy <= 1000]; % 购电上限 Cons = [Cons, 0 <= P_sell <= 1000]; % 售电上限 % === 用户侧约束 === % 用户1负荷削减约束 Cons = [Cons, 0 <= p_cut_1 <= 0.15 * P_Load_e1]; % 最大削减15% Cons = [Cons, 0 <= h_cut_1 <= 0.15 * P_Load_h1]; % 用户2负荷削减约束 Cons = [Cons, 0 <= p_cut_2 <= 0.20 * P_Load_e2]; % 商业区可削减20% Cons = [Cons, 0 <= h_cut_2 <= 0.20 * P_Load_h2]; % 用户3负荷削减约束 Cons = [Cons, 0 <= p_cut_3 <= 0.25 * P_Load_e3]; % 工业区可削减25% Cons = [Cons, 0 <= h_cut_3 <= 0.25 * P_Load_h3]; % P2P交易约束 (允许双向交易) Cons = [Cons, p_tran_12 + p_tran_13 <= 0.2 * P_Load_e1]; % 用户1最大出售量 Cons = [Cons, -0.2 * P_Load_e2 <= p_tran_12 - p_tran_23 <= 0.2 * P_Load_e2]; % 用户2交易平衡 Cons = [Cons, -0.2 * P_Load_e3 <= -p_tran_13 - p_tran_23 <= 0.2 * P_Load_e3]; % 用户3交易平衡 Cons = [Cons, abs(p_tran_12) <= 200, abs(p_tran_13) <= 200, abs(p_tran_23) <= 200]; % 交易功率限制 % === 碳交易机制 === total_CO2_production = sum(CO2_CHP) + CO2_factor * sum(Gas_GB); % 统一碳排放因子 total_energy_output = sum(1.67*P_e_CHP + P_h_CHP + P_h_GB); % 总能量输出 Cons = [Cons, E_CO2 == total_CO2_production - sum(CO2_CCS)]; % 实际碳排放 Cons = [Cons, E_c == 0.047 * total_energy_output]; % 碳配额 Cons = [Cons, E_CO2 - E_c == E_1 + E_2 + E_3]; % 分段变量 Cons = [Cons, 0 <= E_1 <= 5000]; % 第一段 Cons = [Cons, 0 <= E_2 <= 5000]; % 第二段 Cons = [Cons, 0 <= E_3]; % 第三段 %% ===================== 目标函数 ===================== % 成本计算 fuel_cost = C_gas_price * sum(Gas_CHP + Gas_GB - Gas_P2G); % 燃料成本 energy_trade_cost = sum(C_Pri_buy .* P_buy) - sum(C_Pri_sell .* P_sell); % 电网交易净成本 curtail_cost = k_curtail_penalty * sum(P_e_cur); % 弃风惩罚 carbon_cost = C_carbon_price * [E_1; E_2; E_3]; % 阶梯碳成本 % 用户收益 (负荷削减收益) user_benefit = 0.8 * sum(C_Pri_sell .* (p_cut_1 + p_cut_2 + p_cut_3)) + ... 0.7 * sum(mean(C_Pri_sell) * (h_cut_1 + h_cut_2 + h_cut_3)); % P2P交易收益 (基于实际交易量) avg_price = mean([C_Pri_buy, C_Pri_sell]); % 平均电价 p2p_revenue = 0.5 * avg_price * sum(abs(p_tran_12) + abs(p_tran_13) + abs(p_tran_23)); % 系统净成本 (最小化) total_cost = fuel_cost + energy_trade_cost + curtail_cost + carbon_cost - user_benefit - p2p_revenue; % 目标函数 Obj = total_cost; %% ===================== 模型求解 ===================== % 求解器设置 options = sdpsettings('solver', 'cplex', 'verbose', 1, 'showprogress', 1,... 'cplex.timelimit', 600, 'cplex.mip.tolerances.mipgap', 1e-4); % 求解优化问题 disp('开始求解优化问题...'); tic; sol = optimize(Cons, Obj, options); solve_time = toc; % 检查求解状态 if sol.problem == 0 disp(['优化成功! 求解时间: ', num2str(solve_time), '秒']); disp(['系统总成本: ', num2str(value(Obj)), '元']); else error('求解失败! 原因: %s', sol.info); end %% ===================== 结果提取 ===================== % 设备出力 P_e_rew_opt = value(P_e_rew); P_e_cur_opt = value(P_e_cur); P_e_CHP_opt = value(P_e_CHP); P_h_CHP_opt = value(P_h_CHP); P_h_GB_opt = value(P_h_GB); P_e_P2G_opt = value(P_e_P2G); P_e_CCS_opt = value(P_e_CCS); Gas_CHP_opt = value(Gas_CHP); Gas_GB_opt = value(Gas_GB); Gas_P2G_opt = value(Gas_P2G); CO2_CHP_opt = value(CO2_CHP); CO2_CCS_opt = value(CO2_CCS); % 电网交易 P_buy_opt = value(P_buy); P_sell_opt = value(P_sell); miu_ies_opt = value(miu_ies); % 用户侧 p_cut_1_opt = value(p_cut_1); h_cut_1_opt = value(h_cut_1); p_cut_2_opt = value(p_cut_2); h_cut_2_opt = value(h_cut_2); p_cut_3_opt = value(p_cut_3); h_cut_3_opt = value(h_cut_3); % P2P交易 p_tran_12_opt = value(p_tran_12); p_tran_13_opt = value(p_tran_13); p_tran_23_opt = value(p_tran_23); % 碳排放 E_CO2_opt = value(E_CO2); E_c_opt = value(E_c); E_1_opt = value(E_1); E_2_opt = value(E_2); E_3_opt = value(E_3); % 成本分解 fuel_cost_val = value(fuel_cost); energy_trade_val = value(energy_trade_cost); curtail_cost_val = value(curtail_cost); carbon_cost_val = value(carbon_cost); user_benefit_val = value(user_benefit); p2p_revenue_val = value(p2p_revenue);
11-29
整理以下代码:%% 基于阶梯碳交易机制与P2P能源共享的综合能源系统优化调度模型 clc clear %% ===================== 数据初始化 ===================== % 可再生能源预测出力 P_rew_max = [875.53, 846.02, 862.71, 870.27, 836.21, 841.49, 782.69, 698.20, ... 701.43, 621.10, 581.15, 596.79, 580.38, 594.44, 566.58, 574.68, ... 585.51, 552.64, 670.59, 783.63, 804.22, 850.56, 884.58, 897.08]; % kW % 用户1负荷数据 - 住宅区 P_Load_e1 = [438.92, 449.06, 384.87, 383.56, 555.78, 829.08, 695.91, 634.75, ... 824.46, 534.34, 811.55, 886.41, 730.53, 887.85, 541.62, 696.93, ... 769.47, 699.58, 572.86, 540.62, 435.75, 396.30, 474.34, 437.77]; % 电负荷 kW P_Load_h1 = [281.52, 210.02, 303.03, 218.45, 263.47, 350.30, 375.96, 330.86, ... 337.32, 444.79, 436.02, 503.66, 312.27, 366.63, 462.30, 500.07, ... 438.91, 331.59, 454.63, 356.66, 321.48, 363.06, 246.40, 320.91]; % 热负荷 kW % 用户2负荷数据 - 商业区 P_Load_e2 = [311.35, 496.96, 422.78, 490.75, 501.39, 743.94, 606.49, 663.09, ... 700.90, 848.33, 800.27, 776.04, 651.84, 983.69, 671.58, 618.50, ... 512.87, 509.70, 756.91, 687.65, 554.83, 461.36, 344.51, 451.31]; P_Load_h2 = [292.69, 279.79, 249.42, 335.28, 324.71, 311.21, 440.46, 505.70, ... 346.73, 328.94, 360.90, 296.29, 434.50, 444.01, 359.39, 373.67, ... 554.70, 503.06, 296.99, 336.52, 328.45, 356.39, 328.94, 293.09]; % 用户3负荷数据 - 工业区 P_Load_e3 = [392.65, 394.79, 398.32, 350.81, 375.84, 690.77, 627.59, 806.97, ... 545.60, 717.08, 874.86, 782.36, 809.26, 819.57, 549.26, 802.08, ... 831.48, 612.44, 597.20, 564.87, 544.12, 579.74, 481.82, 412.59]; P_Load_h3 = [260.08, 247.82, 262.92, 282.56, 402.31, 310.23, 394.91, 441.18, ... 295.73, 348.94, 413.06, 447.17, 439.96, 355.54, 496.10, 402.27, ... 402.00, 557.99, 482.69, 437.40, 307.05, 243.88, 303.08, 306.68]; %% ===================== 基础参数初始化 ===================== % 时间参数 Num_hours = 24; % 市场电价参数 C_Pri_buy = [0.60*ones(1,8), 0.75*ones(1,4), 1.20*ones(1,3), 0.75*ones(1,4), 1.20*ones(1,4), 0.60*ones(1,1)]; % 购电价 C_Pri_sell = [0.30*ones(1,8), 0.40*ones(1,4), 0.60*ones(1,3), 0.40*ones(1,4), 0.60*ones(1,4), 0.30*ones(1,1)]; % 售电价 C_gas_price = 3.2; % 天然气价格 (元/Nm³) C_carbon_price = [0.27, 0.32, 0.45]; % 阶梯碳价 [元/kg] % 设备参数 k_chp_e = 0.34; % CHP发电效率 k_chp_h = 0.27; % CHP产热效率 k_gas_h = 9.7; % 天然气热 (kWh/Nm³) k_gb_h = 0.92; % 燃气锅炉效率 k_p2g_g = 0.95; % P2G转换效率 U_CCS_energy = 0.109; % CCS单位能耗 (kWh/kg_CO2) U_CO2_factor = 0.390; % 单位天然气碳排放 (kg_CO2/Nm³) % 置信区间计算 (正态分布双侧分位数) Z_cf_level = 0.8; Z_margin = norminv((1+Z_cf_level)/2); % 80%置信水平 M = 1000; % Big-M法大常数 (CPLEX兼容) % 热价参数 C_heat_price = 0.4 * ones(1,24); % 基础热价 (元/kWh) C_heat_price(9:12) = 0.6; % 高峰时段热价 (9:00-12:00) C_heat_price(18:21) = 0.6; % 高峰时段热价 (18:00-21:00) %% ===================== 决策变量定义 ===================== % 设备变量 P_wt_e = sdpvar(1, Num_hours); % 风电实际出力 P_waste_wind = sdpvar(1, Num_hours); % 弃风量 P_CHP_e = sdpvar(1, Num_hours); % CHP发电功率 P_CHP_h = sdpvar(1, Num_hours); % CHP产热功率 P_GB_h = sdpvar(1, Num_hours); % 燃气锅炉产热 P_e_P2G = sdpvar(1, Num_hours); % P2G消耗功率 P_e_CCS = sdpvar(1, Num_hours); % CCS消耗功率 V_Gas_CHP = sdpvar(1, Num_hours); % CHP耗气量 V_Gas_GB = sdpvar(1, Num_hours); % GB耗气量 V_P2G_Gas = sdpvar(1, Num_hours); % P2G产气量 V_CHP_CO2 = sdpvar(1, Num_hours); % CHP碳排放 V_CO2_CCS = sdpvar(1, Num_hours); % CCS捕集量 V_buy_g = sdpvar(1, Num_hours); % 外部购气量 % 电网交易变量 P_buy_e = sdpvar(1, Num_hours); % 购电功率 P_sell_e = sdpvar(1, Num_hours); % 售电功率 B_miu_ies = binvar(1, Num_hours); % 交易状态 (0-售电/1-购电) % 用户变量 PL_cut_1 = sdpvar(1, Num_hours); % 用户1电负荷削减 HL_cut_1 = sdpvar(1, Num_hours); % 用户1热负荷削减 PL_cut_2 = sdpvar(1, Num_hours); % 用户2电负荷削减 HL_cut_2 = sdpvar(1, Num_hours); % 用户2热负荷削减 PL_cut_3 = sdpvar(1, Num_hours); % 用户3电负荷削减 HL_cut_3 = sdpvar(1, Num_hours); % 用户3热负荷削减 % P2P交易变量 P_tran_12_pos = sdpvar(1, Num_hours); % 用户1→用户2交易量 P_tran_12_neg = sdpvar(1, Num_hours); % 用户2→用户1交易量 P_tran_13_pos = sdpvar(1, Num_hours); % 用户1→用户3交易量 P_tran_13_neg = sdpvar(1, Num_hours); % 用户3→用户1交易量 P_tran_23_pos = sdpvar(1, Num_hours); % 用户2→用户3交易量 P_tran_23_neg = sdpvar(1, Num_hours); % 用户3→用户2交易量 P_net_1 = sdpvar(1, Num_hours); % 用户1净交易量 P_net_2 = sdpvar(1, Num_hours); % 用户2净交易量 P_net_3 = sdpvar(1, Num_hours); % 用户3净交易量 % P2P交易方向二进制变量 B_tran_12 = binvar(1, Num_hours); % 1表示1→2, 0表示2→1 B_tran_13 = binvar(1, Num_hours); % 1表示1→3, 0表示3→1 B_tran_23 = binvar(1, Num_hours); % 1表示2→3, 0表示3→2 % 碳交易变量 E_CO2_t = sdpvar(1,1); % 实际碳排放 P_CO2_t = sdpvar(1,1); % 碳配额数量 E_carbon = sdpvar(1,1); % 碳排放超额量 T_E_1 = sdpvar(1,1); % 阶梯碳交易分段1 (0-5000kg) T_E_2 = sdpvar(1,1); % 阶梯碳交易分段2 (5000-10000kg) T_E_3 = sdpvar(1,1); % 阶梯碳交易分段3 (>10000kg) %% ===================== 约束条件 ===================== Cons = []; % 可再生能源约束 Cons = [Cons, P_waste_wind + P_wt_e == P_rew_max]; Cons = [Cons, 0 <= P_waste_wind <= P_rew_max]; Cons = [Cons, 0 <= P_wt_e <= P_rew_max]; % CHP机组约束 Cons = [Cons, P_CHP_e == k_chp_e * k_gas_h * V_Gas_CHP]; Cons = [Cons, P_CHP_h == k_chp_h * k_gas_h * V_Gas_CHP]; Cons = [Cons, 0 <= P_CHP_e <= 2000]; Cons = [Cons, V_CHP_CO2 == U_CO2_factor * V_Gas_CHP]; Cons = [Cons, 0 <= V_Gas_CHP <= 2000/(k_chp_e*k_gas_h)]; % 燃气锅炉约束 Cons = [Cons, P_GB_h == k_gb_h * k_gas_h * V_Gas_GB]; Cons = [Cons, 0 <= P_GB_h <= 2000]; Cons = [Cons, 0 <= V_Gas_GB <= 2000/(k_gb_h*k_gas_h)]; % P2G机组约束 Cons = [Cons, V_P2G_Gas == k_p2g_g * P_e_P2G / k_gas_h]; Cons = [Cons, 0 <= P_e_P2G <= 500]; Cons = [Cons, 0 <= V_P2G_Gas]; % CCS机组约束 Cons = [Cons, P_e_CCS == U_CCS_energy * V_CO2_CCS]; Cons = [Cons, 0 <= V_CO2_CCS <= 0.9 * V_CHP_CO2]; Cons = [Cons, 0 <= P_e_CCS <= 500]; % === CPLEX兼容的关键修改: 重构购售电约束 === % 显式边界约束 Cons = [Cons, 0 <= P_buy_e <= M]; Cons = [Cons, 0 <= P_sell_e <= M]; % Big-M法实现互斥 Cons = [Cons, P_buy_e <= M * B_miu_ies]; Cons = [Cons, P_sell_e <= M * (1 - B_miu_ies)]; % ====== 能源系统平衡约束 ====== % 电功率平衡 Total_generation = P_CHP_e + P_wt_e; Total_consumption = (P_Load_e1 - PL_cut_1) + ... % 用户1电负荷 (P_Load_e2 - PL_cut_2) + ... % 用户2电负荷 (P_Load_e3 - PL_cut_3) + ... % 用户3电负荷 P_e_P2G + P_e_CCS; % 设备耗电 % 不确定性裕度计算 Uncertainty_margin = Z_margin * 0.2 .* P_rew_max; Cons = [Cons, Total_generation + P_buy_e == Total_consumption + P_sell_e + Uncertainty_margin]; % 热功率平衡 Cons = [Cons, P_CHP_h + P_GB_h == (P_Load_h1 - HL_cut_1) + (P_Load_h2 - HL_cut_2) + (P_Load_h3 - HL_cut_3)]; % 天然气平衡 Cons = [Cons, V_Gas_CHP + V_Gas_GB == V_P2G_Gas + V_buy_g]; Cons = [Cons, V_buy_g >= 0]; % 非负约束 % ====== 用户侧约束 ====== % 负荷削减约束 Cons = [Cons, 0 <= PL_cut_1 <= 0.15 * P_Load_e1]; Cons = [Cons, 0 <= HL_cut_1 <= 0.15 * P_Load_h1]; Cons = [Cons, 0 <= PL_cut_2 <= 0.20 * P_Load_e2]; Cons = [Cons, 0 <= HL_cut_2 <= 0.20 * P_Load_h2]; Cons = [Cons, 0 <= PL_cut_3 <= 0.25 * P_Load_e3]; Cons = [Cons, 0 <= HL_cut_3 <= 0.25 * P_Load_h3]; % ====== P2P交易约束 ====== % 净交易量计算 Cons = [Cons, P_net_1 == (P_tran_12_pos - P_tran_12_neg) + (P_tran_13_pos - P_tran_13_neg)]; Cons = [Cons, P_net_2 == (P_tran_12_neg - P_tran_12_pos) + (P_tran_23_pos - P_tran_23_neg)]; Cons = [Cons, P_net_3 == (P_tran_13_neg - P_tran_13_pos) + (P_tran_23_neg - P_tran_23_pos)]; % P2P净交易守恒约束 Cons = [Cons, P_net_1 + P_net_2 + P_net_3 == 0]; % 用户净交易量约束 Cons = [Cons, -0.2 * P_Load_e1 <= P_net_1 <= 0.2 * P_Load_e1]; Cons = [Cons, -0.2 * P_Load_e2 <= P_net_2 <= 0.2 * P_Load_e2]; Cons = [Cons, -0.2 * P_Load_e3 <= P_net_3 <= 0.2 * P_Load_e3]; % 交易功率限制 Cons = [Cons, P_tran_12_pos <= 200 * B_tran_12]; Cons = [Cons, P_tran_12_neg <= 200 * (1 - B_tran_12)]; Cons = [Cons, P_tran_13_pos <= 200 * B_tran_13]; Cons = [Cons, P_tran_13_neg <= 200 * (1 - B_tran_13)]; Cons = [Cons, P_tran_23_pos <= 200 * B_tran_23]; Cons = [Cons, P_tran_23_neg <= 200 * (1 - B_tran_23)]; % ====== 碳交易机制 (线性模型) ====== % 总碳排放计算 Total_CO2_production = sum(V_CHP_CO2) + U_CO2_factor * sum(V_Gas_GB); % 碳配额计算 (根据行业标准0.047kg/kWh) % 使用CHP发电效率转换计算等效能源 electrical_equivalent = P_CHP_e / k_chp_e; % 将电能转换为一次能源 Total_energy_output = electrical_equivalent + P_CHP_h + P_GB_h; Cons = [Cons, E_CO2_t == Total_CO2_production - sum(V_CO2_CCS)]; Cons = [Cons, P_CO2_t == 0.047 * sum(Total_energy_output)]; % 超额碳排放计算 Cons = [Cons, E_carbon >= 0]; Cons = [Cons, E_carbon >= E_CO2_t - P_CO2_t]; % 阶梯碳交易约束 (线性分段) Cons = [Cons, E_carbon == T_E_1 + T_E_2 + T_E_3]; Cons = [Cons, 0 <= T_E_1 <= 5000]; % 第一段:0-5000kg Cons = [Cons, 0 <= T_E_2 <= 5000]; % 第二段:5000-10000kg Cons = [Cons, T_E_3 >= 0]; % 第三段:>10000kg %% ===================== 目标函数 ===================== % 成本计算 F1_cost = C_gas_price * sum(V_buy_g); F2_cost = sum(C_Pri_buy .* P_buy_e) - sum(C_Pri_sell .* P_sell_e); F3_cost = C_carbon_price(1)*T_E_1 + C_carbon_price(2)*T_E_2 + C_carbon_price(3)*T_E_3; % 负荷削减补偿成本 electric_comp = 0.8 * sum(C_Pri_sell .* (PL_cut_1 + PL_cut_2 + PL_cut_3)); heat_comp = 0.7 * (sum(C_heat_price .* HL_cut_1) + ... sum(C_heat_price .* HL_cut_2) + ... sum(C_heat_price .* HL_cut_3)); F4_cost = electric_comp + heat_comp; % P2P交易收益 p2p_volume = sum(P_tran_12_pos + P_tran_12_neg + ... P_tran_13_pos + P_tran_13_neg + ... P_tran_23_pos + P_tran_23_neg); avg_price = mean([C_Pri_buy, C_Pri_sell]); F5_revenue = 0.52 * 0.97 * avg_price * p2p_volume; % 系统净成本 total_cost = F1_cost + F2_cost + F3_cost + F4_cost - F5_revenue; %% ===================== 模型求解 ===================== options = sdpsettings('solver', 'cplex', 'verbose', 2); options.cplex.timelimit = 600; % 10分钟求解时间限制 options.cplex.mip.tolerances.mipgap = 0.01; % 1%最优间隙 disp('>>>开始求解优化问题...'); tic; sol = optimize(Cons, total_cost, options); solve_time = toc; if sol.problem == 0 disp(['>>>优化成功! 求解时间: ', num2str(solve_time), '秒']); % 计算并显示各子成本 Fuel_cost_val = value(F1_cost); Trade_cost_val = value(F2_cost); Carbon_cost_val = value(F3_cost); Compensation_cost_val = value(F4_cost); F_p2p_revenue_val = value(F5_revenue); total_cost_val = value(total_cost); % 详细成本输出 disp('======= 成本明细 ======='); disp(['1. 机组燃料成本: ', num2str(Fuel_cost_val), '元']); disp(['2. 电网交易成本: ', num2str(Trade_cost_val), '元 (购电: ', ... num2str(sum(value(C_Pri_buy .* P_buy_e))), '元, 售电收入: ', ... num2str(-sum(value(C_Pri_sell .* P_sell_e))), '元)']); disp(['3. 碳交易成本: ', num2str(Carbon_cost_val), '元']); disp([' - 第一阶梯: ', num2str(value(C_carbon_price(1)*T_E_1)), '元']); disp([' - 第二阶梯: ', num2str(value(C_carbon_price(2)*T_E_2)), '元']); disp([' - 第三阶梯: ', num2str(value(C_carbon_price(3)*T_E_3)), '元']); disp(['4. 负荷补偿成本: ', num2str(Compensation_cost_val), '元']); disp([' - 电负荷补偿: ', num2str(value(electric_comp)), '元']); disp([' - 热负荷补偿: ', num2str(value(heat_comp)), '元']); disp(['5. P2P交易收益: ', num2str(F_p2p_revenue_val), '元']); disp('------------------------'); disp(['>>>总运行成本: ', num2str(total_cost_val), '元']); % 额外分析指标 carbon_quota = value(P_CO2_t); actual_emission = value(E_CO2_t); disp(['>>>碳排放总量: ', num2str(actual_emission), 'kg, 碳配额: ', ... num2str(carbon_quota), 'kg, 超额: ', num2str(value(E_carbon)), 'kg']); % 验证互斥约束有效性 P_buy_val = value(P_buy_e); P_sell_val = value(P_sell_e); conflict_hours = sum((P_buy_val > 0.1) & (P_sell_val > 0.1)); disp(['>>>购售电互斥验证: 冲突时段数 = ', num2str(conflict_hours)]); tran12_pos = value(P_tran_12_pos); tran12_neg = value(P_tran_12_neg); conflict_tran = sum((tran12_pos > 0.1) & (tran12_neg > 0.1)); disp(['>>>P2P交易互斥验证: 用户1-2冲突时段数 = ', num2str(conflict_tran)]); % 绘制关键结果 figure; subplot(2,1,1) plot(1:24, value(P_buy_e), 'b-o', 'LineWidth', 1.5); hold on; plot(1:24, value(P_sell_e), 'r-s', 'LineWidth', 1.5); plot(1:24, value(P_wt_e), 'g-^', 'LineWidth', 1.5); legend('购电功率', '售电功率', '风电出力'); title('电力系统运行结果'); xlabel('小时'); ylabel('功率(kW)'); grid on; subplot(2,1,2) area(1:24, [value(P_CHP_h); value(P_GB_h)]'); legend('CHP产热', '燃气锅炉产热'); title('热力系统运行结果'); xlabel('小时'); ylabel('热功率(kW)'); grid on; else error('>>>求解失败! 原因: %s', sol.info); end %% 基于阶梯碳交易机制与P2P能源共享的综合能源系统优化调度模型 clc clear %% ===================== 数据初始化 ===================== % 可再生能源预测出力 P_rew_max = [875.53, 846.02, 862.71, 870.27, 836.21, 841.49, 782.69, 698.20, ... 701.43, 621.10, 581.15, 596.79, 580.38, 594.44, 566.58, 574.68, ... 585.51, 552.64, 670.59, 783.63, 804.22, 850.56, 884.58, 897.08]; % kW % 用户1负荷数据 - 住宅区 P_Load_e1 = [438.92, 449.06, 384.87, 383.56, 555.78, 829.08, 695.91, 634.75, ... 824.46, 534.34, 811.55, 886.41, 730.53, 887.85, 541.62, 696.93, ... 769.47, 699.58, 572.86, 540.62, 435.75, 396.30, 474.34, 437.77]; % 电负荷 kW P_Load_h1 = [281.52, 210.02, 303.03, 218.45, 263.47, 350.30, 375.96, 330.86, ... 337.32, 444.79, 436.02, 503.66, 312.27, 366.63, 462.30, 500.07, ... 438.91, 331.59, 454.63, 356.66, 321.48, 363.06, 246.40, 320.91]; % 热负荷 kW % 用户2负荷数据 - 商业区 P_Load_e2 = [311.35, 496.96, 422.78, 490.75, 501.39, 743.94, 606.49, 663.09, ... 700.90, 848.33, 800.27, 776.04, 651.84, 983.69, 671.58, 618.50, ... 512.87, 509.70, 756.91, 687.65, 554.83, 461.36, 344.51, 451.31]; P_Load_h2 = [292.69, 279.79, 249.42, 335.28, 324.71, 311.21, 440.46, 505.70, ... 346.73, 328.94, 360.90, 296.29, 434.50, 444.01, 359.39, 373.67, ... 554.70, 503.06, 296.99, 336.52, 328.45, 356.39, 328.94, 293.09]; % 用户3负荷数据 - 工业区 P_Load_e3 = [392.65, 394.79, 398.32, 350.81, 375.84, 690.77, 627.59, 806.97, ... 545.60, 717.08, 874.86, 782.36, 809.26, 819.57, 549.26, 802.08, ... 831.48, 612.44, 597.20, 564.87, 544.12, 579.74, 481.82, 412.59]; P_Load_h3 = [260.08, 247.82, 262.92, 282.56, 402.31, 310.23, 394.91, 441.18, ... 295.73, 348.94, 413.06, 447.17, 439.96, 355.54, 496.10, 402.27, ... 402.00, 557.99, 482.69, 437.40, 307.05, 243.88, 303.08, 306.68]; %% ===================== 基础参数初始化 ===================== % 时间参数 Num_hours = 24; % 市场电价参数 C_Pri_buy = [0.60*ones(1,8), 0.75*ones(1,4), 1.20*ones(1,3), 0.75*ones(1,4), 1.20*ones(1,4), 0.60*ones(1,1)]; % 购电价 C_Pri_sell = [0.30*ones(1,8), 0.40*ones(1,4), 0.60*ones(1,3), 0.40*ones(1,4), 0.60*ones(1,4), 0.30*ones(1,1)]; % 售电价 C_gas_price = 3.2; % 天然气价格 (元/Nm³) C_carbon_price = [0.27, 0.32, 0.45]; % 阶梯碳价 [元/kg] % 设备参数 k_chp_e = 0.34; % CHP发电效率 k_chp_h = 0.27; % CHP产热效率 k_gas_h = 9.7; % 天然气热 (kWh/Nm³) k_gb_h = 0.92; % 燃气锅炉效率 k_p2g_g = 0.95; % P2G转换效率 U_CCS_energy = 0.109; % CCS单位能耗 (kWh/kg_CO2) U_CO2_factor = 0.390; % 单位天然气碳排放 (kg_CO2/Nm³) % 置信区间计算 (正态分布双侧分位数) Z_cf_level = 0.8; Z_margin = norminv((1+Z_cf_level)/2); % 80%置信水平 M = 1e6; % Big-M法大常数 % 热价参数 C_heat_price = 0.4 * ones(1,24); % 基础热价 (元/kWh) C_heat_price(9:12) = 0.6; % 高峰时段热价 (9:00-12:00) C_heat_price(18:21) = 0.6; % 高峰时段热价 (18:00-21:00) %% ===================== 决策变量定义 ===================== % 设备变量 P_wt_e = sdpvar(1, Num_hours); % 风电实际出力 P_cur_e = sdpvar(1, Num_hours); % 弃风量 P_CHP_e = sdpvar(1, Num_hours); % CHP发电功率 P_CHP_h = sdpvar(1, Num_hours); % CHP产热功率 P_GB_h = sdpvar(1, Num_hours); % 燃气锅炉产热 P_e_P2G = sdpvar(1, Num_hours); % P2G消耗功率 P_e_CCS = sdpvar(1, Num_hours); % CCS消耗功率 V_Gas_CHP = sdpvar(1, Num_hours); % CHP耗气量 V_Gas_GB = sdpvar(1, Num_hours); % GB耗气量 V_P2G_Gas = sdpvar(1, Num_hours); % P2G产气量 V_CHP_CO2 = sdpvar(1, Num_hours); % CHP碳排放 V_CO2_CCS = sdpvar(1, Num_hours); % CCS捕集量 V_buy_g = sdpvar(1, Num_hours); % 外部购气量 % 电网交易变量 P_buy_e = sdpvar(1, Num_hours); % 购电功率 P_sell_e = sdpvar(1, Num_hours); % 售电功率 B_miu_ies = binvar(1, Num_hours); % 交易状态 (0-售电/1-购电) % 用户变量 PL_cut_1 = sdpvar(1, Num_hours); % 用户1电负荷削减 HL_cut_1 = sdpvar(1, Num_hours); % 用户1热负荷削减 PL_cut_2 = sdpvar(1, Num_hours); % 用户2电负荷削减 HL_cut_2 = sdpvar(1, Num_hours); % 用户2热负荷削减 PL_cut_3 = sdpvar(1, Num_hours); % 用户3电负荷削减 HL_cut_3 = sdpvar(1, Num_hours); % 用户3热负荷削减 % P2P交易变量 P_tran_12_pos = sdpvar(1, Num_hours); % 用户1→用户2交易量 P_tran_12_neg = sdpvar(1, Num_hours); % 用户2→用户1交易量 P_tran_13_pos = sdpvar(1, Num_hours); % 用户1→用户3交易量 P_tran_13_neg = sdpvar(1, Num_hours); % 用户3→用户1交易量 P_tran_23_pos = sdpvar(1, Num_hours); % 用户2→用户3交易量 P_tran_23_neg = sdpvar(1, Num_hours); % 用户3→用户2交易量 P_net_1 = sdpvar(1, Num_hours); % 用户1净交易量 P_net_2 = sdpvar(1, Num_hours); % 用户2净交易量 P_net_3 = sdpvar(1, Num_hours); % 用户3净交易量 % 碳交易变量 E_CO2_t = sdpvar(1,1); % 实际碳排放 P_CO2_t = sdpvar(1,1); % 碳配额数量 E_carbon = sdpvar(1,1); % 碳排放超额量 T_E_1 = sdpvar(1,1); % 阶梯碳交易分段1 (0-5000kg) T_E_2 = sdpvar(1,1); % 阶梯碳交易分段2 (5000-10000kg) T_E_3 = sdpvar(1,1); % 阶梯碳交易分段3 (>10000kg) %% ===================== 约束条件 ===================== Cons = []; % 可再生能源约束 Cons = [Cons, P_cur_e + P_wt_e == P_rew_max]; Cons = [Cons, 0 <= P_wt_e <= P_rew_max]; % CHP机组约束 Cons = [Cons, P_CHP_e == k_chp_e * k_gas_h * V_Gas_CHP]; Cons = [Cons, P_CHP_h == k_chp_h * k_gas_h * V_Gas_CHP]; Cons = [Cons, 0 <= P_CHP_e <= 2000]; Cons = [Cons, V_CHP_CO2 == U_CO2_factor * V_Gas_CHP]; Cons = [Cons, 0 <= V_Gas_CHP <= 2000/(k_chp_e*k_gas_h)]; % 燃气锅炉约束 Cons = [Cons, P_GB_h == k_gb_h * k_gas_h * V_Gas_GB]; Cons = [Cons, 0 <= P_GB_h <= 2000]; Cons = [Cons, 0 <= V_Gas_GB <= 2000/(k_gb_h*k_gas_h)]; % P2G机组约束 Cons = [Cons, V_P2G_Gas == k_p2g_g * P_e_P2G / k_gas_h]; Cons = [Cons, 0 <= P_e_P2G <= 500]; Cons = [Cons, 0 <= V_P2G_Gas]; % CCS机组约束 Cons = [Cons, P_e_CCS == U_CCS_energy * V_CO2_CCS]; Cons = [Cons, 0 <= V_CO2_CCS <= 0.9 * V_CHP_CO2]; Cons = [Cons, 0 <= P_e_CCS <= 500]; % ====== 能源系统平衡约束 ====== % 电功率平衡 Total_generation = P_CHP_e + P_wt_e; Total_consumption = (P_Load_e1 - PL_cut_1) + ... % 用户1电负荷 (P_Load_e2 - PL_cut_2) + ... % 用户2电负荷 (P_Load_e3 - PL_cut_3) + ... % 用户3电负荷 P_e_P2G + P_e_CCS; % 设备耗电 % 不确定性裕度计算 Uncertainty_margin = Z_margin * 0.2 .* P_rew_max; Cons = [Cons, Total_generation + P_buy_e == Total_consumption + P_sell_e + Uncertainty_margin]; % 热功率平衡 Cons = [Cons, P_CHP_h + P_GB_h == (P_Load_h1 - HL_cut_1) + (P_Load_h2 - HL_cut_2) + (P_Load_h3 - HL_cut_3)]; % 天然气平衡 Cons = [Cons, V_Gas_CHP + V_Gas_GB == V_P2G_Gas + V_buy_g]; Cons = [Cons, V_buy_g >= 0]; % 非负约束 % 购售电逻辑约束 Cons = [Cons, implies(B_miu_ies == 1, [P_sell_e == 0, P_buy_e >= 0])]; Cons = [Cons, implies(B_miu_ies == 0, [P_buy_e == 0, P_sell_e >= 0])]; Cons = [Cons, 0 <= P_buy_e <= 1000]; Cons = [Cons, 0 <= P_sell_e <= 1000]; % ====== 用户侧约束 ====== % 负荷削减约束 Cons = [Cons, 0 <= PL_cut_1 <= 0.15 * P_Load_e1]; Cons = [Cons, 0 <= HL_cut_1 <= 0.15 * P_Load_h1]; Cons = [Cons, 0 <= PL_cut_2 <= 0.20 * P_Load_e2]; Cons = [Cons, 0 <= HL_cut_2 <= 0.20 * P_Load_h2]; Cons = [Cons, 0 <= PL_cut_3 <= 0.25 * P_Load_e3]; Cons = [Cons, 0 <= HL_cut_3 <= 0.25 * P_Load_h3]; % ====== P2P交易约束 ====== % 净交易量计算 Cons = [Cons, P_net_1 == (P_tran_12_pos - P_tran_12_neg) + (P_tran_13_pos - P_tran_13_neg)]; Cons = [Cons, P_net_2 == (P_tran_12_neg - P_tran_12_pos) + (P_tran_23_pos - P_tran_23_neg)]; Cons = [Cons, P_net_3 == (P_tran_13_neg - P_tran_13_pos) + (P_tran_23_neg - P_tran_23_pos)]; % P2P净交易守恒约束 Cons = [Cons, P_net_1 + P_net_2 + P_net_3 == 0]; % 用户净交易量约束 Cons = [Cons, -0.2 * P_Load_e1 <= P_net_1 <= 0.2 * P_Load_e1]; Cons = [Cons, -0.2 * P_Load_e2 <= P_net_2 <= 0.2 * P_Load_e2]; Cons = [Cons, -0.2 * P_Load_e3 <= P_net_3 <= 0.2 * P_Load_e3]; % 交易功率限制 Cons = [Cons, 0 <= P_tran_12_pos <= 200, 0 <= P_tran_12_neg <= 200]; Cons = [Cons, 0 <= P_tran_13_pos <= 200, 0 <= P_tran_13_neg <= 200]; Cons = [Cons, 0 <= P_tran_23_pos <= 200, 0 <= P_tran_23_neg <= 200]; % ====== 碳交易机制 (线性模型) ====== % 总碳排放计算 Total_CO2_production = sum(V_CHP_CO2) + U_CO2_factor * sum(V_Gas_GB); Total_energy_output = sum(1.67*P_CHP_e + P_CHP_h + P_GB_h); % 碳配额计算 (根据行业标准0.047kg/kWh) Cons = [Cons, E_CO2_t == Total_CO2_production - sum(V_CO2_CCS)]; Cons = [Cons, P_CO2_t == 0.047 * Total_energy_output]; % 超额碳排放计算 Cons = [Cons, E_carbon >= 0]; Cons = [Cons, E_carbon >= E_CO2_t - P_CO2_t]; % 阶梯碳交易约束 (线性分段) Cons = [Cons, E_carbon == T_E_1 + T_E_2 + T_E_3]; Cons = [Cons, 0 <= T_E_1 <= 5000]; % 第一段:0-5000kg Cons = [Cons, 0 <= T_E_2 <= 5000]; % 第二段:5000-10000kg Cons = [Cons, T_E_3 >= 0]; % 第三段:>10000kg % 阶梯边界约束 Cons = [Cons, T_E_2 >= 5000 - (10000 - E_carbon)]; % 确保阶梯正确分段 Cons = [Cons, T_E_3 >= E_carbon - 10000]; % 第三段起始点 %% ===================== 目标函数 ===================== % 成本计算 F1_cost = C_gas_price * sum(V_buy_g); F2_cost = sum(C_Pri_buy .* P_buy_e) - sum(C_Pri_sell .* P_sell_e); F3_cost = C_carbon_price(1)*T_E_1 + C_carbon_price(2)*T_E_2 + C_carbon_price(3)*T_E_3; % 负荷削减补偿成本 electric_comp = 0.8 * sum(C_Pri_sell .* (PL_cut_1 + PL_cut_2 + PL_cut_3)); heat_comp = 0.7 * (sum(C_heat_price .* HL_cut_1) + ... sum(C_heat_price .* HL_cut_2) + ... sum(C_heat_price .* HL_cut_3)); F4_cost = electric_comp + heat_comp; % P2P交易收益 p2p_volume = sum(P_tran_12_pos + P_tran_12_neg + ... P_tran_13_pos + P_tran_13_neg + ... P_tran_23_pos + P_tran_23_neg); avg_price = mean([C_Pri_buy, C_Pri_sell]); F5_revenue = 0.52 * 0.97 * avg_price * p2p_volume; % 系统净成本 total_cost = F1_cost + F2_cost + F3_cost + F4_cost - F5_revenue; %% ===================== 模型求解 ===================== options = sdpsettings('solver', 'cplex', 'verbose', 1); disp('>>>开始求解优化问题...'); tic; sol = optimize(Cons, total_cost, options); solve_time = toc; if sol.problem == 0 disp(['>>>优化成功! 求解时间: ', num2str(solve_time), '秒']); % 计算并显示各子成本 Fuel_cost_val = value(F1_cost); Trade_cost_val = value(F2_cost); Carbon_cost_val = value(F3_cost); Compensation_cost_val = value(F4_cost); F_p2p_revenue_val = value(F5_revenue); total_cost_val = value(total_cost); % 详细成本输出 disp('======= 成本明细 ======='); disp(['1. 机组燃料成本: ', num2str(Fuel_cost_val), '元']); disp(['2. 电网交易成本: ', num2str(Trade_cost_val), '元 (购电: ', ... num2str(sum(value(C_Pri_buy .* P_buy_e))), '元, 售电收入: ', ... num2str(-sum(value(C_Pri_sell .* P_sell_e))), '元)']); disp(['3. 碳交易成本: ', num2str(Carbon_cost_val), '元']); disp([' - 第一阶梯: ', num2str(value(C_carbon_price(1)*T_E_1)), '元']); disp([' - 第二阶梯: ', num2str(value(C_carbon_price(2)*T_E_2)), '元']); disp([' - 第三阶梯: ', num2str(value(C_carbon_price(3)*T_E_3)), '元']); disp(['4. 负荷补偿成本: ', num2str(Compensation_cost_val), '元']); disp([' - 电负荷补偿: ', num2str(value(electric_comp)), '元']); disp([' - 热负荷补偿: ', num2str(value(heat_comp)), '元']); disp(['5. P2P交易收益: ', num2str(F_p2p_revenue_val), '元']); disp('------------------------'); disp(['>>>总运行成本: ', num2str(total_cost_val), '元']); % 额外分析指标 carbon_quota = value(P_CO2_t); actual_emission = value(E_CO2_t); disp(['>>>碳排放总量: ', num2str(actual_emission), 'kg, 碳配额: ', ... num2str(carbon_quota), 'kg, 超额: ', num2str(value(E_carbon)), 'kg']); else error('>>>求解失败! 原因: %s', sol.info); end %% ===================== 模型求解 ===================== options = sdpsettings('solver', 'cplex', 'verbose', 1, 'showprogress', 1,... 'cplex.timelimit', 600, 'cplex.mip.tolerances.mipgap', 1e-4); disp('>>>开始求解优化问题...'); tic; sol = optimize(Cons, total_cost, options); solve_time = toc; disp(['>>>优化成功! 求解时间: ', num2str(solve_time), '秒']); % 计算并显示各子成本 Fuel_cost_val = value(F1_cost); Trade_cost_val = value(F2_cost); Carbon_cost_val = value(F3_cost); Compensation_cost_val = value(F4_cost); F_p2p_revenue_val = value(F5_revenue); total_cost_val = value(total_cost); % 详细成本输出 disp('======= 成本明细 ======='); disp(['1. 机组燃料成本: ', num2str(Fuel_cost_val), '元']); disp(['2. 电网交易成本: ', num2str(Trade_cost_val), '元 (购电: ', ... num2str(sum(value(C_Pri_buy .* P_buy_e))), '元, 售电收入: ', ... num2str(-sum(value(C_Pri_sell .* P_sell_e))), '元)']); disp(['3. 碳交易成本: ', num2str(Carbon_cost_val), '元']); disp([' - 第一阶梯: ', num2str(value(C_carbon_price(1)*T_E_1)), '元']); disp([' - 第二阶梯: ', num2str(value(C_carbon_price(2)*T_E_2)), '元']); disp([' - 第三阶梯: ', num2str(value(C_carbon_price(3)*T_E_3)), '元']); disp(['4. 负荷补偿成本: ', num2str(Compensation_cost_val), '元']); disp([' - 电负荷补偿: ', num2str(value(electric_comp)), '元']); disp([' - 热负荷补偿: ', num2str(value(heat_comp)), '元']); disp(['5. P2P交易收益: -', num2str(F_p2p_revenue_val), '元']); disp('------------------------'); disp(['>>>综合能源系统总运行成本: ', num2str(total_cost_val), '元']); % 额外分析指标 carbon_quota = value(P_CO2_t); actual_emission = value(E_CO2_t); disp(['碳排放总量: ', num2str(actual_emission), 'kg, 碳配额: ', ... num2str(carbon_quota), 'kg, 超额: ', num2str(value(E_carbon)), 'kg']); %% 基于阶梯碳交易机制与P2P能源共享的综合能源系统优化调度模型 clc clear %% ===================== 数据初始化 ===================== % 可再生能源预测出力 P_rew_max = [875.53, 846.02, 862.71, 870.27, 836.21, 841.49, 782.69, 698.20, ... 701.43, 621.10, 581.15, 596.79, 580.38, 594.44, 566.58, 574.68, ... 585.51, 552.64, 670.59, 783.63, 804.22, 850.56, 884.58, 897.08]; % kW % 用户1负荷数据 - 住宅区 P_Load_e1 = [438.92, 449.06, 384.87, 383.56, 555.78, 829.08, 695.91, 634.75, ... 824.46, 534.34, 811.55, 886.41, 730.53, 887.85, 541.62, 696.93, ... 769.47, 699.58, 572.86, 540.62, 435.75, 396.30, 474.34, 437.77]; % 电负荷 kW P_Load_h1 = [281.52, 210.02, 303.03, 218.45, 263.47, 350.30, 375.96, 330.86, ... 337.32, 444.79, 436.02, 503.66, 312.27, 366.63, 462.30, 500.07, ... 438.91, 331.59, 454.63, 356.66, 321.48, 363.06, 246.40, 320.91]; % 热负荷 kW % 用户2负荷数据 - 商业区 P_Load_e2 = [311.35, 496.96, 422.78, 490.75, 501.39, 743.94, 606.49, 663.09, ... 700.90, 848.33, 800.27, 776.04, 651.84, 983.69, 671.58, 618.50, ... 512.87, 509.70, 756.91, 687.65, 554.83, 461.36, 344.51, 451.31]; P_Load_h2 = [292.69, 279.79, 249.42, 335.28, 324.71, 311.21, 440.46, 505.70, ... 346.73, 328.94, 360.90, 296.29, 434.50, 444.01, 359.39, 373.67, ... 554.70, 503.06, 296.99, 336.52, 328.45, 356.39, 328.94, 293.09]; % 用户3负荷数据 - 工业区 P_Load_e3 = [392.65, 394.79, 398.32, 350.81, 375.84, 690.77, 627.59, 806.97, ... 545.60, 717.08, 874.86, 782.36, 809.26, 819.57, 549.26, 802.08, ... 831.48, 612.44, 597.20, 564.87, 544.12, 579.74, 481.82, 412.59]; P_Load_h3 = [260.08, 247.82, 262.92, 282.56, 402.31, 310.23, 394.91, 441.18, ... 295.73, 348.94, 413.06, 447.17, 439.96, 355.54, 496.10, 402.27, ... 402.00, 557.99, 482.69, 437.40, 307.05, 243.88, 303.08, 306.68]; %% ===================== 基础参数初始化 ===================== % 时间参数 Num_hours = 24; % 市场电价参数 C_Pri_buy = [0.60*ones(1,8), 0.75*ones(1,4), 1.20*ones(1,3), 0.75*ones(1,4), 1.20*ones(1,4), 0.60*ones(1,1)]; % 购电价 C_Pri_sell = [0.30*ones(1,8), 0.40*ones(1,4), 0.60*ones(1,3), 0.40*ones(1,4), 0.60*ones(1,4), 0.30*ones(1,1)]; % 售电价 C_gas_price = 3.2; % 天然气价格 (元/Nm³) C_carbon_price = [0.2, 0.3, 0.45]; % 阶梯碳价 [元/kg] K_curtail_penalty = 0.5; % 弃风惩罚系数 % 设备参数 k_chp_e = 0.30; % CHP发电效率 k_chp_h = 0.45; % CHP产热效率 k_gas_h = 9.7; % 天然气热 (kWh/Nm³) k_gb_h = 0.90; % 燃气锅炉效率 k_p2g_g = 0.95; % P2G转换效率 U_CCS_energy = 0.109; % CCS单位能耗 (kWh/kg_CO2) U_CO2_factor = 0.390; % 单位天然气碳排放 (kg_CO2/Nm³) % 置信区间计算 (正态分布双侧分位数) Z_cf_level = 0.8; Z_margin = norminv((1+Z_cf_level)/2); % 80%置信水平 M = 1e6; % Big-M法大常数 % 热价参数 C_heat_price = 0.4 * ones(1,24); % 基础热价 (元/kWh) C_heat_price(9:12) = 0.6; % 高峰时段热价 (9:00-12:00) C_heat_price(18:21) = 0.6; % 高峰时段热价 (18:00-21:00) %% ===================== 决策变量定义 ===================== % 设备变量 P_wt_e = sdpvar(1, Num_hours); % 风电实际出力 P_cur_e = sdpvar(1, Num_hours); % 弃风量 P_CHP_e = sdpvar(1, Num_hours); % CHP发电功率 P_CHP_h = sdpvar(1, Num_hours); % CHP产热功率 P_GB_h = sdpvar(1, Num_hours); % 燃气锅炉产热 P_e_P2G = sdpvar(1, Num_hours); % P2G消耗功率 P_e_CCS = sdpvar(1, Num_hours); % CCS消耗功率 V_Gas_CHP = sdpvar(1, Num_hours); % CHP耗气量 V_Gas_GB = sdpvar(1, Num_hours); % GB耗气量 V_P2G_Gas = sdpvar(1, Num_hours); % P2G产气量 V_CHP_CO2 = sdpvar(1, Num_hours); % CHP碳排放 V_CO2_CCS = sdpvar(1, Num_hours); % CCS捕集量 V_buy_g = sdpvar(1, Num_hours); % 外部购气量 % 电网交易变量 P_buy_e = sdpvar(1, Num_hours); % 购电功率 P_sell_e = sdpvar(1, Num_hours); % 售电功率 B_miu_ies = binvar(1, Num_hours); % 交易状态 (0-售电/1-购电) % 用户变量 PL_cut_1 = sdpvar(1, Num_hours); % 用户1电负荷削减 HL_cut_1 = sdpvar(1, Num_hours); % 用户1热负荷削减 PL_cut_2 = sdpvar(1, Num_hours); % 用户2电负荷削减 HL_cut_2 = sdpvar(1, Num_hours); % 用户2热负荷削减 PL_cut_3 = sdpvar(1, Num_hours); % 用户3电负荷削减 HL_cut_3 = sdpvar(1, Num_hours); % 用户3热负荷削减 % P2P交易变量 P_tran_12_pos = sdpvar(1, Num_hours); % 用户1→用户2交易量 P_tran_12_neg = sdpvar(1, Num_hours); % 用户2→用户1交易量 P_tran_13_pos = sdpvar(1, Num_hours); % 用户1→用户3交易量 P_tran_13_neg = sdpvar(1, Num_hours); % 用户3→用户1交易量 P_tran_23_pos = sdpvar(1, Num_hours); % 用户2→用户3交易量 P_tran_23_neg = sdpvar(1, Num_hours); % 用户3→用户2交易量 P_net_1 = sdpvar(1, Num_hours); % 用户1净交易量 P_net_2 = sdpvar(1, Num_hours); % 用户2净交易量 P_net_3 = sdpvar(1, Num_hours); % 用户3净交易量 % 碳交易变量 E_CO2_t = sdpvar(1,1); % 实际碳排放 P_CO2_t = sdpvar(1,1); % 碳配额数量 E_carbon = sdpvar(1,1); % 碳排放超额量 T_E_1 = sdpvar(1,1); % 阶梯碳交易分段1 T_E_2 = sdpvar(1,1); % 阶梯碳交易分段2 T_E_3 = sdpvar(1,1); % 阶梯碳交易分段3 b_carbon = binvar(2,1); % 分段状态变量 %% ===================== 约束条件 ===================== Cons = []; % 可再生能源约束 Cons = [Cons, P_cur_e + P_wt_e == P_rew_max]; Cons = [Cons, 0 <= P_cur_e <= P_rew_max]; Cons = [Cons, 0 <= P_wt_e <= P_rew_max]; % CHP机组约束 Cons = [Cons, P_CHP_e == k_chp_e * k_gas_h * V_Gas_CHP]; Cons = [Cons, P_CHP_h == k_chp_h * k_gas_h * V_Gas_CHP]; Cons = [Cons, 0 <= P_CHP_e <= 2000]; Cons = [Cons, V_CHP_CO2 == U_CO2_factor * V_Gas_CHP]; Cons = [Cons, 0 <= V_Gas_CHP <= 2000/(k_chp_e*k_gas_h)]; % 燃气锅炉约束 Cons = [Cons, P_GB_h == k_gb_h * k_gas_h * V_Gas_GB]; Cons = [Cons, 0 <= P_GB_h <= 2000]; Cons = [Cons, 0 <= V_Gas_GB <= 2000/(k_gb_h*k_gas_h)]; % P2G机组约束 Cons = [Cons, V_P2G_Gas == k_p2g_g * P_e_P2G / k_gas_h]; Cons = [Cons, 0 <= P_e_P2G <= 500]; Cons = [Cons, 0 <= V_P2G_Gas]; % CCS机组约束 Cons = [Cons, P_e_CCS == U_CCS_energy * V_CO2_CCS]; Cons = [Cons, 0 <= V_CO2_CCS <= 0.9 * V_CHP_CO2]; Cons = [Cons, 0 <= P_e_CCS <= 500]; % ====== 能源系统平衡约束 ====== % 电功率平衡 Total_generation = P_CHP_e + P_wt_e; Total_consumption = (P_Load_e1 - PL_cut_1) + ... % 用户1原始净负荷 (P_Load_e2 - PL_cut_2) + ... % 用户2原始净负荷 (P_Load_e3 - PL_cut_3) + ... % 用户3原始净负荷 P_net_1 + P_net_2 + P_net_3 + ... % P2P交易净量 P_e_P2G + P_e_CCS; % 设备耗电 Uncertainty_margin = Z_margin * 0.2 * P_rew_max; Cons = [Cons, Total_generation + P_buy_e == Total_consumption + P_sell_e + Uncertainty_margin]; % 热力平衡 Cons = [Cons, P_CHP_h + P_GB_h == (P_Load_h1 - HL_cut_1) + (P_Load_h2 - HL_cut_2) + (P_Load_h3 - HL_cut_3)]; % 天然气平衡 Cons = [Cons, V_Gas_CHP + V_Gas_GB == V_P2G_Gas + V_buy_g]; Cons = [Cons, V_buy_g >= 0]; % 非负约束 % 购售电逻辑约束 Cons = [Cons, implies(B_miu_ies == 1, [P_sell_e == 0, P_buy_e >= 0])]; Cons = [Cons, implies(B_miu_ies == 0, [P_buy_e == 0, P_sell_e >= 0])]; Cons = [Cons, 0 <= P_buy_e <= 1000]; Cons = [Cons, 0 <= P_sell_e <= 1000]; % ====== 用户侧约束 ====== % 负荷削减约束 Cons = [Cons, 0 <= PL_cut_1 <= 0.15 * P_Load_e1]; Cons = [Cons, 0 <= HL_cut_1 <= 0.15 * P_Load_h1]; Cons = [Cons, 0 <= PL_cut_2 <= 0.20 * P_Load_e2]; Cons = [Cons, 0 <= HL_cut_2 <= 0.20 * P_Load_h2]; Cons = [Cons, 0 <= PL_cut_3 <= 0.25 * P_Load_e3]; Cons = [Cons, 0 <= HL_cut_3 <= 0.25 * P_Load_h3]; % ====== P2P交易约束 ====== % 净交易量计算 Cons = [Cons, P_net_1 == (P_tran_12_pos - P_tran_12_neg) + (P_tran_13_pos - P_tran_13_neg)]; Cons = [Cons, P_net_2 == (P_tran_12_neg - P_tran_12_pos) + (P_tran_23_pos - P_tran_23_neg)]; Cons = [Cons, P_net_3 == (P_tran_13_neg - P_tran_13_pos) + (P_tran_23_neg - P_tran_23_pos)]; % 用户净交易量约束 Cons = [Cons, -0.2 * P_Load_e1 <= P_net_1 <= 0.2 * P_Load_e1]; Cons = [Cons, -0.2 * P_Load_e2 <= P_net_2 <= 0.2 * P_Load_e2]; Cons = [Cons, -0.2 * P_Load_e3 <= P_net_3 <= 0.2 * P_Load_e3]; % 交易功率限制 Cons = [Cons, 0 <= P_tran_12_pos <= 200, 0 <= P_tran_12_neg <= 200]; Cons = [Cons, 0 <= P_tran_13_pos <= 200, 0 <= P_tran_13_neg <= 200]; Cons = [Cons, 0 <= P_tran_23_pos <= 200, 0 <= P_tran_23_neg <= 200]; % ====== 碳交易机制 ====== Total_CO2_production = sum(V_CHP_CO2) + U_CO2_factor * sum(V_Gas_GB); Total_energy_output = sum(1.67*P_CHP_e + P_CHP_h + P_GB_h); Cons = [Cons, E_CO2_t == Total_CO2_production - sum(V_CO2_CCS)]; Cons = [Cons, P_CO2_t == 0.047 * Total_energy_output]; % 超额碳排放计算 (使用辅助变量和约束实现) Cons = [Cons, E_carbon >= 0]; Cons = [Cons, E_carbon >= E_CO2_t - P_CO2_t]; Cons = [Cons, E_carbon <= E_CO2_t - P_CO2_t + M*(1-b_carbon(1))]; Cons = [Cons, E_carbon <= M*b_carbon(1)]; % 阶梯碳交易约束 Cons = [Cons, E_carbon == T_E_1 + T_E_2 + T_E_3]; Cons = [Cons, 0 <= T_E_1 <= 5000]; Cons = [Cons, 0 <= T_E_2 <= 5000]; Cons = [Cons, T_E_3 >= 0]; % 二元变量约束 Cons = [Cons, implies(b_carbon(1)==1, T_E_1 == 5000)]; Cons = [Cons, implies(b_carbon(1)==0, T_E_2 == 0)]; Cons = [Cons, implies(b_carbon(2)==1, T_E_2 == 5000)]; Cons = [Cons, b_carbon(1) >= b_carbon(2)]; % 顺序约束 %% ===================== 目标函数 ===================== % 成本计算 F1_cost = C_gas_price * sum(V_buy_g); F2_cost = sum(C_Pri_buy .* P_buy_e) - sum(C_Pri_sell .* P_sell_e); F3_cost = C_carbon_price * [T_E_1; T_E_2; T_E_3]; % 负荷削减补偿成本 electric_comp = 0.8 * sum(C_Pri_sell .* (PL_cut_1 + PL_cut_2 + PL_cut_3)); heat_comp = 0.7 * (sum(C_heat_price .* HL_cut_1) + ... sum(C_heat_price .* HL_cut_2) + ... sum(C_heat_price .* HL_cut_3)); F4_cost = electric_comp + heat_comp; % P2P交易收益 p2p_volume = sum(P_tran_12_pos + P_tran_12_neg + ... P_tran_13_pos + P_tran_13_neg + ... P_tran_23_pos + P_tran_23_neg); avg_price = mean([C_Pri_buy, C_Pri_sell]); F5_revenue = 0.5 * avg_price * p2p_volume; % 系统净成本 total_cost = F1_cost + F2_cost + F3_cost + F4_cost - F5_revenue; %% ===================== 模型求解 ===================== options = sdpsettings('solver', 'cplex', 'verbose', 1, 'showprogress', 1,... 'cplex.timelimit', 600, 'cplex.mip.tolerances.mipgap', 1e-4); disp('>>>开始求解优化问题...'); tic; sol = optimize(Cons, total_cost, options); solve_time = toc; if sol.problem == 0 disp(['>>>优化成功! 求解时间: ', num2str(solve_time), '秒']); % 计算并显示各子成本 Fuel_cost_val = value(F1_cost); Trade_cost_val = value(F2_cost); Carbon_cost_val = value(F3_cost); Compensation_cost_val = value(F4_cost); F_p2p_revenue_val = value(F5_revenue); total_cost_val = value(total_cost); % 详细成本输出 disp('======= 成本明细 ======='); disp(['1. 机组燃料成本: ', num2str(Fuel_cost_val), '元']); disp(['2. 电网交易成本: ', num2str(Trade_cost_val), '元 (购电: ', ... num2str(sum(value(C_Pri_buy .* P_buy_e))), '元, 售电收入: ', ... num2str(-sum(value(C_Pri_sell .* P_sell_e))), '元)']); disp(['3. 碳交易成本: ', num2str(Carbon_cost_val), '元']); disp([' - 第一阶梯: ', num2str(value(C_carbon_price(1)*T_E_1)), '元']); disp([' - 第二阶梯: ', num2str(value(C_carbon_price(2)*T_E_2)), '元']); disp([' - 第三阶梯: ', num2str(value(C_carbon_price(3)*T_E_3)), '元']); disp(['4. 负荷补偿成本: ', num2str(Compensation_cost_val), '元']); disp([' - 电负荷补偿: ', num2str(value(electric_comp)), '元']); disp([' - 热负荷补偿: ', num2str(value(heat_comp)), '元']); disp(['5. P2P交易收益: -', num2str(F_p2p_revenue_val), '元']); disp('------------------------'); disp(['>>>综合能源系统总运行成本: ', num2str(total_cost_val), '元']); % 额外分析指标 carbon_quota = value(P_CO2_t); actual_emission = value(E_CO2_t); disp(['碳排放总量: ', num2str(actual_emission), 'kg, 碳配额: ', ... num2str(carbon_quota), 'kg, 超额: ', num2str(value(E_carbon)), 'kg']); else error('>>>求解失败! 原因: %s', sol.info); end %% ===================== 模型求解 ===================== options = sdpsettings('solver', 'cplex', 'verbose', 1, 'showprogress', 1,... 'cplex.timelimit', 600, 'cplex.mip.tolerances.mipgap', 1e-4); disp('>>>开始求解优化问题...'); tic; sol = optimize(Cons, total_cost, options); solve_time = toc; if sol.problem == 0 disp(['>>>优化成功! 求解时间: ', num2str(solve_time), '秒']); disp(['系统总成本: ', num2str(value(total_cost)), '元']); else error('>>>求解失败! 原因: %s', sol.info); end %% 结果可视化 % 电功率平衡可视化 figure('Name', '电功率平衡', 'Position', [100, 100, 800, 600]); plot_data = [results.P_e_CHP; results.P_e_rew; -results.p_load{1}-results.p_load{2}-results.p_load{3}]'; bar(1:24, plot_data, 'stacked'); legend('CHP发电', '可再生能源', '用户负荷', 'Location', 'best'); title('电功率平衡'); xlabel('时间 (h)'); ylabel('功率 (kW)'); grid on; % 热功率平衡可视化 figure('Name', '热功率平衡', 'Position', [200, 200, 800, 600]); plot_data = [results.P_h_CHP; results.P_h_GB; -results.h_load{1}-results.h_load{2}-results.h_load{3}]'; bar(1:24, plot_data, 'stacked'); legend('CHP产热', '燃气锅炉', '用户热负荷', 'Location', 'best'); title('热功率平衡'); xlabel('时间 (h)'); ylabel('功率 (kW)'); grid on; % 用户电价可视化 figure('Name', '用户电价', 'Position', [300, 300, 1000, 800]); subplot(3,1,1); plot(1:24, results.pri_e_user{1}, 'b-o', 'LineWidth', 1.5); title('用户1电价'); ylabel('电价 (元/kWh)'); grid on; ylim([0.2, 1.2]); subplot(3,1,2); plot(1:24, results.pri_e_user{2}, 'r-s', 'LineWidth', 1.5); title('用户2电价'); ylabel('电价 (元/kWh)'); grid on; ylim([0.2, 1.2]); subplot(3,1,3); plot(1:24, results.pri_e_user{3}, 'g-^', 'LineWidth', 1.5); title('用户3电价'); xlabel('时间 (h)'); ylabel('电价 (元/kWh)'); grid on; ylim([0.2, 1.2]); % 碳排放分析 figure('Name', '碳排放分析', 'Position', [400, 400, 600, 400]); emissions = [results.E_c, results.E_CO2, results.E_1+results.E_2+results.E_3]; bar(emissions); set(gca, 'XTickLabel', {'碳排放配额', '实际碳排放', '碳交易量'}); ylabel('碳排放量 (kg)'); title('碳排放分析'); grid on; % 用户负荷削减分析 figure('Name', '负荷削减分析', 'Position', [500, 500, 1000, 800]); for i = 1:3 subplot(3,1,i); plot(1:24, results.p_cut{i}, 'b', 'LineWidth', 1.5); hold on; plot(1:24, results.h_cut{i}, 'r', 'LineWidth', 1.5); title(['用户', num2str(i), '负荷削减']); legend('电负荷削减', '热负荷削减', 'Location', 'best'); xlabel('时间 (h)'); ylabel('削减量 (kW)'); grid on; end
11-30
检查以下代码,指出需要修改的地方:%% 基于阶梯碳交易机制与P2P能源共享的综合能源系统优化调度模型 clc clear %% ===================== 数据初始化 ===================== % 可再生能源预测出力 P_rew_max = [875.53, 846.02, 862.71, 870.27, 836.21, 841.49, 782.69, 698.20, ... 701.43, 621.10, 581.15, 596.79, 580.38, 594.44, 566.58, 574.68, ... 585.51, 552.64, 670.59, 783.63, 804.22, 850.56, 884.58, 897.08]; % kW % 用户负荷数据 P_Load_e1 = [438.92, 449.06, 384.87, 383.56, 555.78, 829.08, 695.91, 634.75, ... 824.46, 534.34, 811.55, 886.41, 730.53, 887.85, 541.62, 696.93, ... 769.47, 699.58, 572.86, 540.62, 435.75, 396.30, 474.34, 437.77]; P_Load_h1 = [281.52, 210.02, 303.03, 218.45, 263.47, 350.30, 375.96, 330.86, ... 337.32, 444.79, 436.02, 503.66, 312.27, 366.63, 462.30, 500.07, ... 438.91, 331.59, 454.63, 356.66, 321.48, 363.06, 246.40, 320.91]; P_Load_e2 = [311.35, 496.96, 422.78, 490.75, 501.39, 743.94, 606.49, 663.09, ... 700.90, 848.33, 800.27, 776.04, 651.84, 983.69, 671.58, 618.50, ... 512.87, 509.70, 756.91, 687.65, 554.83, 461.36, 344.51, 451.31]; P_Load_h2 = [292.69, 279.79, 249.42, 335.28, 324.71, 311.21, 440.46, 505.70, ... 346.73, 328.94, 360.90, 296.29, 434.50, 444.01, 359.39, 373.67, ... 554.70, 503.06, 296.99, 336.52, 328.45, 356.39, 328.94, 293.09]; P_Load_e3 = [392.65, 394.79, 398.32, 350.81, 375.84, 690.77, 627.59, 806.97, ... 545.60, 717.08, 874.86, 782.36, 809.26, 819.57, 549.26, 802.08, ... 831.48, 612.44, 597.20, 564.87, 544.12, 579.74, 481.82, 412.59]; P_Load_h3 = [260.08, 247.82, 262.92, 282.56, 402.31, 310.23, 394.91, 441.18, ... 295.73, 348.94, 413.06, 447.17, 439.96, 355.54, 496.10, 402.27, ... 402.00, 557.99, 482.69, 437.40, 307.05, 243.88, 303.08, 306.68]; %% ===================== 基础参数初始化 ===================== Num_hours = 24; % 市场电价参数 C_Pri_buy = [0.60*ones(1,8), 0.75*ones(1,4), 1.20*ones(1,3), 0.75*ones(1,4), 1.20*ones(1,4), 0.60*ones(1,1)]; C_Pri_sell = [0.30*ones(1,8), 0.40*ones(1,4), 0.60*ones(1,3), 0.40*ones(1,4), 0.60*ones(1,4), 0.30*ones(1,1)]; C_gas_price = 3.2; % 元/Nm³ C_carbon_price = [0.27, 0.32, 0.45]; % 元/kg % 设备参数 k_chp_e = 0.34; % CHP发电效率 k_chp_h = 0.27; % CHP产热效率 k_gas_h = 9.7; % 天然气热 (kWh/Nm³) k_gb_h = 0.92; % 燃气锅炉效率 k_p2g_g = 0.95; % P2G转换效率 U_CCS_energy = 0.109; % CCS单位能耗 (kWh/kg_CO2) U_CO2_factor = 0.390; % 单位天然气碳排放 (kg_CO2/Nm³) % 置信区间计算 Z_cf_level = 0.8; Z_margin = norminv((1+Z_cf_level)/2); % 80%置信水平 % 热价阶梯参数 C_heat_price = 0.4 * ones(1,24); C_heat_price(9:12) = 0.6; % 高峰时段热价 C_heat_price(18:21) = 0.6; % 高峰时段热价 %% ===================== 决策变量定义 ===================== % 设备运行变量 P_wt_e = sdpvar(1, Num_hours); P_cur_e = sdpvar(1, Num_hours); P_CHP_e = sdpvar(1, Num_hours); P_CHP_h = sdpvar(1, Num_hours); P_GB_h = sdpvar(1, Num_hours); P_e_P2G = sdpvar(1, Num_hours); P_e_CCS = sdpvar(1, Num_hours); V_Gas_CHP = sdpvar(1, Num_hours); V_Gas_GB = sdpvar(1, Num_hours); V_P2G_Gas = sdpvar(1, Num_hours); V_CHP_CO2 = sdpvar(1, Num_hours); V_CO2_CCS = sdpvar(1, Num_hours); V_buy_gas = sdpvar(1, Num_hours); % 电网交易变量 P_buy_e = sdpvar(1, Num_hours); P_sell_e = sdpvar(1, Num_hours); B_miu_ies = binvar(1, Num_hours); % 交易状态 (0-售电/1-购电) % 用户变量 PL_cut_1 = sdpvar(1, Num_hours); HL_cut_1 = sdpvar(1, Num_hours); PL_cut_2 = sdpvar(1, Num_hours); HL_cut_2 = sdpvar(1, Num_hours); PL_cut_3 = sdpvar(1, Num_hours); HL_cut_3 = sdpvar(1, Num_hours); % P2P交易变量 P_tran_12_pos = sdpvar(1, Num_hours); % 用户1→用户2 P_tran_12_neg = sdpvar(1, Num_hours); % 用户2→用户1 P_tran_13_pos = sdpvar(1, Num_hours); % 用户1→用户3 P_tran_13_neg = sdpvar(1, Num_hours); % 用户3→用户1 P_tran_23_pos = sdpvar(1, Num_hours); % 用户2→用户3 P_tran_23_neg = sdpvar(1, Num_hours); % 用户3→用户2 P_net_1 = sdpvar(1, Num_hours); P_net_2 = sdpvar(1, Num_hours); P_net_3 = sdpvar(1, Num_hours); % 碳交易变量 E_CO2_t = sdpvar(1,1); % 实际碳排放 P_CO2_t = sdpvar(1,1); % 碳免费配额 E_carbon = sdpvar(1,1); % 超额碳排放 T_E_1 = sdpvar(1,1); % 阶梯碳交易分段1 T_E_2 = sdpvar(1,1); % 阶梯碳交易分段2 T_E_3 = sdpvar(1,1); % 阶梯碳交易分段3 %% ===================== 约束条件 ===================== Cons = []; % 可再生能源约束 Cons = [Cons, P_cur_e + P_wt_e == P_rew_max]; Cons = [Cons, 0 <= P_cur_e <= P_rew_max]; Cons = [Cons, 0 <= P_wt_e <= P_rew_max]; % CHP机组约束 Cons = [Cons, P_CHP_e == k_chp_e * k_gas_h * V_Gas_CHP]; Cons = [Cons, P_CHP_h == k_chp_h * k_gas_h * V_Gas_CHP]; Cons = [Cons, 0 <= P_CHP_e <= 2000]; Cons = [Cons, V_CHP_CO2 == U_CO2_factor * V_Gas_CHP]; Cons = [Cons, 0 <= V_Gas_CHP <= 2000/(k_chp_e*k_gas_h)]; % 燃气锅炉约束 Cons = [Cons, P_GB_h == k_gb_h * k_gas_h * V_Gas_GB]; Cons = [Cons, 0 <= P_GB_h <= 1800]; Cons = [Cons, 0 <= V_Gas_GB <= 1800/(k_gb_h*k_gas_h)]; % P2G机组约束 Cons = [Cons, V_P2G_Gas == k_p2g_g * P_e_P2G / k_gas_h]; Cons = [Cons, 0 <= P_e_P2G <= 500]; Cons = [Cons, 0 <= V_P2G_Gas]; % CCS机组约束 Cons = [Cons, P_e_CCS == U_CCS_energy * V_CO2_CCS]; Cons = [Cons, 0 <= V_CO2_CCS <= 0.9 * V_CHP_CO2]; Cons = [Cons, 0 <= P_e_CCS <= 500]; % ====== 能源系统平衡约束 ====== % 电功率平衡 Total_generation = P_CHP_e + P_wt_e; Total_consumption = (P_Load_e1 - PL_cut_1) + ... (P_Load_e2 - PL_cut_2) + ... (P_Load_e3 - PL_cut_3) + ... P_e_P2G + P_e_CCS; Uncertainty_margin = Z_margin * 0.2 .* P_rew_max; Cons = [Cons, Total_generation + P_buy_e == Total_consumption + P_sell_e + Uncertainty_margin]; % 热功率平衡 Cons = [Cons, P_CHP_h + P_GB_h == (P_Load_h1 - HL_cut_1) + (P_Load_h2 - HL_cut_2) + (P_Load_h3 - HL_cut_3)]; % 天然气平衡 Cons = [Cons, V_Gas_CHP + V_Gas_GB == V_P2G_Gas + V_buy_gas]; Cons = [Cons, V_buy_gas >= 0]; % 购售电逻辑约束 Cons = [Cons, implies(B_miu_ies == 1, [P_sell_e == 0, P_buy_e >= 0])]; Cons = [Cons, implies(B_miu_ies == 0, [P_buy_e == 0, P_sell_e >= 0])]; Cons = [Cons, 0 <= P_buy_e <= 1000]; Cons = [Cons, 0 <= P_sell_e <= 1000]; % ====== 用户侧约束 ====== Cons = [Cons, 0 <= PL_cut_1 <= 0.15 * P_Load_e1]; Cons = [Cons, 0 <= HL_cut_1 <= 0.15 * P_Load_h1]; Cons = [Cons, 0 <= PL_cut_2 <= 0.20 * P_Load_e2]; Cons = [Cons, 0 <= HL_cut_2 <= 0.20 * P_Load_h2]; Cons = [Cons, 0 <= PL_cut_3 <= 0.25 * P_Load_e3]; Cons = [Cons, 0 <= HL_cut_3 <= 0.25 * P_Load_h3]; % ====== P2P交易约束 ====== % 净交易量计算 Cons = [Cons, P_net_1 == (P_tran_12_pos - P_tran_12_neg) + (P_tran_13_pos - P_tran_13_neg)]; Cons = [Cons, P_net_2 == (P_tran_12_neg - P_tran_12_pos) + (P_tran_23_pos - P_tran_23_neg)]; Cons = [Cons, P_net_3 == (P_tran_13_neg - P_tran_13_pos) + (P_tran_23_neg - P_tran_23_pos)]; % P2P净交易守恒约束 Cons = [Cons, P_net_1 + P_net_2 + P_net_3 == 0]; % 用户净交易量约束 Cons = [Cons, -0.2 * P_Load_e1 <= P_net_1 <= 0.2 * P_Load_e1]; Cons = [Cons, -0.2 * P_Load_e2 <= P_net_2 <= 0.2 * P_Load_e2]; Cons = [Cons, -0.2 * P_Load_e3 <= P_net_3 <= 0.2 * P_Load_e3]; % 交易功率限制 Cons = [Cons, 0 <= P_tran_12_pos <= 200, 0 <= P_tran_12_neg <= 200]; Cons = [Cons, 0 <= P_tran_13_pos <= 200, 0 <= P_tran_13_neg <= 200]; Cons = [Cons, 0 <= P_tran_23_pos <= 200, 0 <= P_tran_23_neg <= 200]; % ====== 碳交易机制 (线性模型) ====== Total_CO2_production = sum(V_CHP_CO2) + U_CO2_factor * sum(V_Gas_GB); Total_energy_output = sum(1.67*P_CHP_e + P_CHP_h + P_GB_h); Cons = [Cons, E_CO2_t == Total_CO2_production - sum(V_CO2_CCS)]; Cons = [Cons, P_CO2_t == 0.047 * Total_energy_output]; Cons = [Cons, E_carbon >= E_CO2_t - P_CO2_t]; Cons = [Cons, E_carbon == T_E_1 + T_E_2 + T_E_3]; Cons = [Cons, 0 <= T_E_1 <= 5000]; % 第一段 Cons = [Cons, 0 <= T_E_2 <= 5000]; % 第二段 Cons = [Cons, T_E_3 >= 0]; % 第三段 %% ===================== 目标函数 ===================== % 成本计算 F1_cost = C_gas_price * sum(V_buy_gas); F2_cost = sum(C_Pri_buy .* P_buy_e) - sum(C_Pri_sell .* P_sell_e); F3_cost = C_carbon_price(1)*T_E_1 + C_carbon_price(2)*T_E_2 + C_carbon_price(3)*T_E_3; % 负荷削减补偿成本 electric_comp = 0.8 * sum(C_Pri_sell .* (PL_cut_1 + PL_cut_2 + PL_cut_3)); heat_comp = 0.7 * (sum(C_heat_price .* HL_cut_1) + ... sum(C_heat_price .* HL_cut_2) + ... sum(C_heat_price .* HL_cut_3)); F4_cost = electric_comp + heat_comp; % P2P交易收益 p2p_volume = sum(P_tran_12_pos + P_tran_12_neg + ... P_tran_13_pos + P_tran_13_neg + ... P_tran_23_pos + P_tran_23_neg); avg_price = mean([C_Pri_buy, C_Pri_sell]); F5_revenue = 0.52 * 0.97 * avg_price * p2p_volume; % 系统净成本 total_cost = F1_cost + F2_cost + F3_cost + F4_cost - F5_revenue; %% ===================== 模型求解 ===================== options = sdpsettings('solver', 'cplex', 'verbose', 1); disp('>>>开始求解优化问题...'); tic; sol = optimize(Cons, total_cost, options); solve_time = toc; if sol.problem == 0 disp(['>>>优化成功! 求解时间: ', num2str(solve_time), '秒']); % 计算并显示各子成本 Fuel_cost_val = value(F1_cost); Trade_cost_val = value(F2_cost); Carbon_cost_val = value(F3_cost); Compensation_cost_val = value(F4_cost); F_p2p_revenue_val = value(F5_revenue); total_cost_val = value(total_cost); % 详细成本输出 disp('======= 成本明细 ======='); disp(['1. 机组燃料成本: ', num2str(Fuel_cost_val), '元']); disp(['2. 电网交易成本: ', num2str(Trade_cost_val), '元 (购电: ', ... num2str(sum(value(C_Pri_buy .* P_buy_e))), '元, 售电收入: ', ... num2str(-sum(value(C_Pri_sell .* P_sell_e))), '元)']); disp(['3. 碳交易成本: ', num2str(Carbon_cost_val), '元']); disp([' - 第一阶梯: ', num2str(value(C_carbon_price(1)*T_E_1)), '元']); disp([' - 第二阶梯: ', num2str(value(C_carbon_price(2)*T_E_2)), '元']); disp([' - 第三阶梯: ', num2str(value(C_carbon_price(3)*T_E_3)), '元']); disp(['4. 负荷补偿成本: ', num2str(Compensation_cost_val), '元']); disp([' - 电负荷补偿: ', num2str(value(electric_comp)), '元']); disp([' - 热负荷补偿: ', num2str(value(heat_comp)), '元']); disp(['5. P2P交易收益: ', num2str(F_p2p_revenue_val), '元']); disp('------------------------'); disp(['>>>总运行成本: ', num2str(total_cost_val), '元']); % 额外分析指标 carbon_quota = value(P_CO2_t); actual_emission = value(E_CO2_t); disp(['>>>碳排放总量: ', num2str(actual_emission), 'kg, 碳配额: ', ... num2str(carbon_quota), 'kg, 超额: ', num2str(value(E_carbon)), 'kg']); else error('>>>求解失败! 原因: %s', sol.info); end
11-30
请你检查代码:%% 基于阶梯碳交易机制与P2P能源共享的综合能源系统优化调度模型 clc clear %% ===================== 数据初始化 ===================== % 可再生能源预测出力 P_rew_max = [875.53, 846.02, 862.71, 870.27, 836.21, 841.49, 782.69, 698.20, … 701.43, 621.10, 581.15, 596.79, 580.38, 594.44, 566.58, 574.68, … 585.51, 552.64, 670.59, 783.63, 804.22, 850.56, 884.58, 897.08]; % kW % 用户负荷数据 % 用户1 - 住宅区 P_Load_e1 = [438.92, 449.06, 384.87, 383.56, 555.78, 829.08, 695.91, 634.75, … 824.46, 534.34, 811.55, 886.41, 730.53, 887.85, 541.62, 696.93, … 769.47, 699.58, 572.86, 540.62, 435.75, 396.30, 474.34, 437.77]; % 电负荷 kW P_Load_h1 = [281.52, 210.02, 303.03, 218.45, 263.47, 350.30, 375.96, 330.86, … 337.32, 444.79, 436.02, 503.66, 312.27, 366.63, 462.30, 500.07, … 438.91, 331.59, 454.63, 356.66, 321.48, 363.06, 246.40, 320.91]; % 热负荷 kW % 用户2 - 商业区 P_Load_e2 = [311.35, 496.96, 422.78, 490.75, 501.39, 743.94, 606.49, 663.09, … 700.90, 848.33, 800.27, 776.04, 651.84, 983.69, 671.58, 618.50, … 512.87, 509.70, 756.91, 687.65, 554.83, 461.36, 344.51, 451.31]; P_Load_h2 = [292.69, 279.79, 249.42, 335.28, 324.71, 311.21, 440.46, 505.70, … 346.73, 328.94, 360.90, 296.29, 434.50, 444.01, 359.39, 373.67, … 554.70, 503.06, 296.99, 336.52, 328.45, 356.39, 328.94, 293.09]; % 用户3 - 工业区 P_Load_e3 = [392.65, 394.79, 398.32, 350.81, 375.84, 690.77, 627.59, 806.97, … 545.60, 717.08, 874.86, 782.36, 809.26, 819.57, 549.26, 802.08, … 831.48, 612.44, 597.20, 564.87, 544.12, 579.74, 481.82, 412.59]; P_Load_h3 = [260.08, 247.82, 262.92, 282.56, 402.31, 310.23, 394.91, 441.18, … 295.73, 348.94, 413.06, 447.17, 439.96, 355.54, 496.10, 402.27, … 402.00, 557.99, 482.69, 437.40, 307.05, 243.88, 303.08, 306.68]; %% ===================== 基础参数初始化 ===================== % 时间参数 num_hours = 24; hours = 1:num_hours; % 电价参数 C_Pri_buy = [0.60ones(1,8), 0.75ones(1,4), 1.20ones(1,3), 0.75ones(1,4), 1.20ones(1,4), 0.60ones(1,1)]; % 购电价 C_Pri_sell = [0.30ones(1,8), 0.40ones(1,4), 0.60ones(1,3), 0.40ones(1,4), 0.60ones(1,4), 0.30ones(1,1)]; % 售电价 C_gas_price = 3.2; % 天然气价格 (元/Nm³) C_carbon_price = [0.2, 0.3, 0.45]; % 阶梯碳价 [元/kg] K_curtail_penalty = 0.5; % 弃风惩罚系数 % 设备参数 k_chp_e = 0.30; % CHP发电效率 k_chp_h = 0.45; % CHP产热效率 k_gas_h = 9.7; % 天然气热 (kWh/Nm³) GB_eff_h = 0.90; % 燃气锅炉效率 P2G_eff_g = 0.95; % P2G转换效率 CCS_energy = 0.109; % CCS单位能耗 (kWh/kg_CO2) CO2_factor = 0.390; % 单位天然气碳排放 (kg_CO2/Nm³) % 置信区间计算 (正态分布双侧分位数) Z_cf_level = 0.8; Z_margin = norminv((1+Z_cf_level)/2); % 80%置信水平 M = 1e6; % Big-M法大常数 %% ===================== 决策变量定义 ===================== % 设备变量 P_wt_e = sdpvar(1, num_hours); % 风电实际出力 P_cur_e = sdpvar(1, num_hours); % 弃风量 P_CHP_e = sdpvar(1, num_hours); % CHP发电功率 P_CHP_h = sdpvar(1, num_hours); % CHP产热功率 P_GB_h = sdpvar(1, num_hours); % 燃气锅炉产热 P_e_P2G = sdpvar(1, num_hours); % P2G消耗功率 P_e_CCS = sdpvar(1, num_hours); % CCS消耗功率 V_Gas_CHP = sdpvar(1, num_hours); % CHP耗气量 V_Gas_GB = sdpvar(1, num_hours); % GB耗气量 V_P2G_Gas = sdpvar(1, num_hours); % P2G产气量 V_CHP_CO2 = sdpvar(1, num_hours); % CHP碳排放 V_CO2_CCS = sdpvar(1, num_hours); % CCS捕集量 % 电网交易变量 P_buy_e = sdpvar(1, num_hours); % 购电功率 P_sell_e = sdpvar(1, num_hours); % 售电功率 B_miu_ies = binvar(1, num_hours); % 交易状态 (0-售电/1-购电) % 用户变量 PL_cut_1 = sdpvar(1, num_hours); % 用户1电负荷削减 HL_cut_1 = sdpvar(1, num_hours); % 用户1热负荷削减 PL_cut_2 = sdpvar(1, num_hours); % 用户2电负荷削减 HL_cut_2 = sdpvar(1, num_hours); % 用户2热负荷削减 PL_cut_3 = sdpvar(1, num_hours); % 用户3电负荷削减 HL_cut_3 = sdpvar(1, num_hours); % 用户3热负荷削减 % P2P交易变量 (允许双向交易) P_tran_12 = sdpvar(1, num_hours); % 用户1<->2交易电量 P_tran_13 = sdpvar(1, num_hours); % 用户1<->3交易电量 P_tran_23 = sdpvar(1, num_hours); % 用户2<->3交易电量 % 碳交易变量 E_CO2 = sdpvar(1,1); % 实际碳排放 P_CO2 = sdpvar(1,1); % 碳配额数量 E_1 = sdpvar(1,1); % 阶梯碳交易分段1 E_2 = sdpvar(1,1); % 阶梯碳交易分段2 E_3 = sdpvar(1,1); % 阶梯碳交易分段3 %% ===================== 约束条件 ===================== Cons = []; % === 运行机组物理约束 === % 可再生能源约束 Cons = [Cons, P_cur_e + P_wt_e == P_rew_max]; % 出力平衡 Cons = [Cons, 0 <= P_cur_e <= P_rew_max]; % 弃风限制 Cons = [Cons, 0 <= P_wt_e <= P_rew_max]; % 实际出力限制 % CHP机组约束 (统一碳排放因子) Cons = [Cons, P_CHP_e == k_chp_e * k_gas_h * V_Gas_CHP]; % 电功率 Cons = [Cons, P_CHP_h == k_chp_h * k_gas_h * V_Gas_CHP]; % 热功率 Cons = [Cons, 0 <= P_CHP_e <= 2000]; % 发电上限 Cons = [Cons, V_CHP_CO2 == CO2_factor * V_Gas_CHP]; % 机组碳排放 Cons = [Cons, 0 <= V_Gas_CHP <= 2000/(k_chp_e*k_gas_h)]; % 耗气量限制 % 燃气锅炉约束 (统一碳排放因子) Cons = [Cons, P_GB_h == GB_eff_h * k_gas_h * V_Gas_GB]; % 热功率 Cons = [Cons, 0 <= P_GB_h <= 2000]; % 产热上限 Cons = [Cons, 0 <= V_Gas_GB <= 2000/(GB_eff_h*k_gas_h)]; % 耗气量限制 % P2G约束 Cons = [Cons, V_P2G_Gas == P2G_eff_g * P_e_P2G / k_gas_h]; % 产气量 Cons = [Cons, 0 <= P_e_P2G <= 500]; % 消耗功率上限 Cons = [Cons, 0 <= V_P2G_Gas]; % 产气量非负 % 碳捕集约束 Cons = [Cons, P_e_CCS == CCS_energy * V_CO2_CCS]; % 能耗计算 Cons = [Cons, 0 <= V_CO2_CCS <= 0.9 * V_CHP_CO2]; % 捕集量限制 Cons = [Cons, 0 <= P_e_CCS <= 500]; % 消耗功率上限 % === 系统平衡约束 === % 电功率平衡 total_generation = P_CHP_e + P_wt_e; % 总发电量 total_consumption = (P_Load_e1 - PL_cut_1) + (P_Load_e2 - PL_cut_2) + (P_Load_e3 - PL_cut_3) + P_e_P2G + P_e_CCS; % 总耗电量(含削减) uncertainty_margin = Z_margin * 0.2 * P_rew_max; % 不确定性余量 % 正确的电力平衡方程 Cons = [Cons, total_generation + P_buy_e == total_consumption + P_sell_e + uncertainty_margin]; % 热力平衡 (考虑负荷削减) Cons = [Cons, P_CHP_h + P_GB_h == (P_Load_h1 - HL_cut_1) + (P_Load_h2 - HL_cut_2) + (P_Load_h3 - HL_cut_3)]; % 天然气平衡 Cons = [Cons, V_Gas_CHP + V_Gas_GB <= V_P2G_Gas]; % 天然气消耗 <= P2G生产 % 购售电逻辑约束 Cons = [Cons, implies(B_miu_ies == 1, [P_sell_e == 0, P_buy_e >= 0])]; % 购电状态 Cons = [Cons, implies(B_miu_ies == 0, [P_buy_e == 0, P_sell_e >= 0])]; % 售电状态 Cons = [Cons, 0 <= P_buy_e <= 1000]; % 购电上限 Cons = [Cons, 0 <= P_sell_e <= 1000]; % 售电上限 % === 用户侧约束 === % 用户1负荷削减约束 Cons = [Cons, 0 <= PL_cut_1 <= 0.15 * P_Load_e1]; % 最大削减15% Cons = [Cons, 0 <= HL_cut_1 <= 0.15 * P_Load_h1]; % 用户2负荷削减约束 Cons = [Cons, 0 <= PL_cut_2 <= 0.20 * P_Load_e2]; % 商业区可削减20% Cons = [Cons, 0 <= HL_cut_2 <= 0.20 * P_Load_h2]; % 用户3负荷削减约束 Cons = [Cons, 0 <= PL_cut_3 <= 0.25 * P_Load_e3]; % 工业区可削减25% Cons = [Cons, 0 <= HL_cut_3 <= 0.25 * P_Load_h3]; % P2P交易约束修正 (允许双向交易) Cons = [Cons, -0.2 * P_Load_e1 <= P_tran_12 + P_tran_13 <= 0.2 * P_Load_e1]; % 用户1净交易量 Cons = [Cons, -0.2 * P_Load_e2 <= P_tran_12 - P_tran_23 <= 0.2 * P_Load_e2]; % 用户2净交易量 Cons = [Cons, -0.2 * P_Load_e3 <= -P_tran_13 - P_tran_23 <= 0.2 * P_Load_e3]; % 用户3净交易量 Cons = [Cons, abs(P_tran_12) <= 200, abs(P_tran_13) <= 200, abs(P_tran_23) <= 200]; % 交易功率限制 % === 碳交易机制 === total_CO2_production = sum(V_CHP_CO2) + CO2_factor * sum(V_Gas_GB); % 统一碳排放因子 total_energy_output = sum(1.67*P_CHP_e + P_CHP_h + P_GB_h); % 总能量输出 Cons = [Cons, E_CO2 == total_CO2_production - sum(V_CO2_CCS)]; % 实际碳排放 Cons = [Cons, P_CO2 == 0.047 * total_energy_output]; % 碳配额 Cons = [Cons, E_CO2 - P_CO2 == E_1 + E_2 + E_3]; % 分段变量 Cons = [Cons, 0 <= E_1 <= 5000]; % 第一段 Cons = [Cons, 0 <= E_2 <= 5000]; % 第二段 Cons = [Cons, 0 <= E_3]; % 第三段 %% ===================== 目标函数 ===================== % 成本计算 fuel_cost = C_gas_price * sum(V_Gas_CHP + V_Gas_GB - V_P2G_Gas); % 燃料成本 energy_trade_cost = sum(C_Pri_buy .* P_buy_e) - sum(C_Pri_sell .* P_sell_e); % 电网交易净成本 curtail_cost = K_curtail_penalty * sum(P_cur_e); % 弃风惩罚 carbon_cost = C_carbon_price * [E_1; E_2; E_3]; % 阶梯碳成本 % 用户收益 (负荷削减收益) user_benefit = 0.8 * sum(C_Pri_sell .* (PL_cut_1 + PL_cut_2 + PL_cut_3)) + … 0.7 * sum(mean(C_Pri_sell) * (HL_cut_1 + HL_cut_2 + HL_cut_3)); % P2P交易收益 (基于实际交易量) avg_price = mean([C_Pri_buy, C_Pri_sell]); % 平均电价 p2p_revenue = 0.5 * avg_price * sum(abs(P_tran_12) + abs(P_tran_13) + abs(P_tran_23)); % 系统净成本 (最小化) total_cost = fuel_cost + energy_trade_cost + curtail_cost + carbon_cost - user_benefit - p2p_revenue; % 目标函数 Obj = total_cost; %% ===================== 模型求解 ===================== % 求解器设置 options = sdpsettings(‘solver’, ‘cplex’, ‘verbose’, 1, ‘showprogress’, 1,… ‘cplex.timelimit’, 600, ‘cplex.mip.tolerances.mipgap’, 1e-4); % 求解优化问题 disp(‘开始求解优化问题…’); tic; sol = optimize(Cons, Obj, options); solve_time = toc; % 检查求解状态 if sol.problem == 0 disp(['优化成功! 求解时间: ', num2str(solve_time), ‘秒’]); disp(['系统总成本: ', num2str(value(Obj)), ‘元’]); else error(‘求解失败! 原因: %s’, sol.info); end
11-29
再次检查修改后的代码:%% 基于P2P能源共享机制的综合能源系统优化调度模型 clc clear %% ===================== 数据初始化 ===================== % 可再生能源预测出力 P_rew_max = [875.53, 846.02, 862.71, 870.27, 836.21, 841.49, 782.69, 698.20, ... 701.43, 621.10, 581.15, 596.79, 580.38, 594.44, 566.58, 574.68, ... 585.51, 552.64, 670.59, 783.63, 804.22, 850.56, 884.58, 897.08]; % kW % 用户1负荷数据 - 住宅区 P_Load_e1 = [438.92, 449.06, 384.87, 383.56, 555.78, 829.08, 695.91, 634.75, ... 824.46, 534.34, 811.55, 886.41, 730.53, 887.85, 541.62, 696.93, ... 769.47, 699.58, 572.86, 540.62, 435.75, 396.30, 474.34, 437.77]; % 电负荷 kW P_Load_h1 = [281.52, 210.02, 303.03, 218.45, 263.47, 350.30, 375.96, 330.86, ... 337.32, 444.79, 436.02, 503.66, 312.27, 366.63, 462.30, 500.07, ... 438.91, 331.59, 454.63, 356.66, 321.48, 363.06, 246.40, 320.91]; % 热负荷 kW % 用户2负荷数据 - 商业区 P_Load_e2 = [311.35, 496.96, 422.78, 490.75, 501.39, 743.94, 606.49, 663.09, ... 700.90, 848.33, 800.27, 776.04, 651.84, 983.69, 671.58, 618.50, ... 512.87, 509.70, 756.91, 687.65, 554.83, 461.36, 344.51, 451.31]; P_Load_h2 = [292.69, 279.79, 249.42, 335.28, 324.71, 311.21, 440.46, 505.70, ... 346.73, 328.94, 360.90, 296.29, 434.50, 444.01, 359.39, 373.67, ... 554.70, 503.06, 296.99, 336.52, 328.45, 356.39, 328.94, 293.09]; % 用户3负荷数据 - 工业区 P_Load_e3 = [392.65, 394.79, 398.32, 350.81, 375.84, 690.77, 627.59, 806.97, ... 545.60, 717.08, 874.86, 782.36, 809.26, 819.57, 549.26, 802.08, ... 831.48, 612.44, 597.20, 564.87, 544.12, 579.74, 481.82, 412.59]; P_Load_h3 = [260.08, 247.82, 262.92, 282.56, 402.31, 310.23, 394.91, 441.18, ... 295.73, 348.94, 413.06, 447.17, 439.96, 355.54, 496.10, 402.27, ... 402.00, 557.99, 482.69, 437.40, 307.05, 243.88, 303.08, 306.68]; %% ===================== 基础参数初始化 ===================== % 时间参数 Num_hours = 24; % 市场电价参数 C_Pri_buy = [0.60*ones(1,8), 0.75*ones(1,4), 1.20*ones(1,3), 0.75*ones(1,4), 1.20*ones(1,4), 0.60*ones(1,1)]; % 购电价 C_Pri_sell = [0.30*ones(1,8), 0.40*ones(1,4), 0.60*ones(1,3), 0.40*ones(1,4), 0.60*ones(1,4), 0.30*ones(1,1)]; % 售电价 C_gas_price = 3.2; % 天然气价格 (元/Nm³) % 设备参数 k_chp_e = 0.34; % CHP发电效率 k_chp_h = 0.27; % CHP产热效率 k_gas_h = 9.7; % 天然气热 (kWh/Nm³) k_gb_h = 0.92; % 燃气锅炉效率 k_p2g_g = 0.95; % P2G转换效率 U_CCS_energy = 0.109; % CCS单位能耗 (kWh/kg_CO2) % 热价参数 C_heat_price = 0.4 * ones(1,24); % 基础热价 (元/kWh) C_heat_price(9:12) = 0.6; % 高峰时段热价 (9:00-12:00) C_heat_price(18:21) = 0.6; % 高峰时段热价 (18:00-21:00) %% ===================== 决策变量定义 ===================== % 设备变量 P_wt_e = sdpvar(1, Num_hours); % 风电实际出力 P_CHP_e = sdpvar(1, Num_hours); % CHP发电功率 P_CHP_h = sdpvar(1, Num_hours); % CHP产热功率 P_GB_h = sdpvar(1, Num_hours); % 燃气锅炉产热 P_e_P2G = sdpvar(1, Num_hours); % P2G消耗功率 P_e_CCS = sdpvar(1, Num_hours); % CCS消耗功率 V_Gas_CHP = sdpvar(1, Num_hours); % CHP耗气量 V_Gas_GB = sdpvar(1, Num_hours); % GB耗气量 V_P2G_Gas = sdpvar(1, Num_hours); % P2G产气量 V_buy_gas = sdpvar(1, Num_hours); % 外部购气量 % 电网交易变量 P_buy_e = sdpvar(1, Num_hours); % 购电功率 P_sell_e = sdpvar(1, Num_hours); % 售电功率 B_miu_ies = binvar(1, Num_hours); % 交易状态 (0-售电/1-购电) % 用户变量 PL_cut_1 = sdpvar(1, Num_hours); % 用户1电负荷削减 HL_cut_1 = sdpvar(1, Num_hours); % 用户1热负荷削减 PL_cut_2 = sdpvar(1, Num_hours); % 用户2电负荷削减 HL_cut_2 = sdpvar(1, Num_hours); % 用户2热负荷削减 PL_cut_3 = sdpvar(1, Num_hours); % 用户3电负荷削减 HL_cut_3 = sdpvar(1, Num_hours); % 用户3热负荷削减 % P2P交易变量 P_tran_12_pos = sdpvar(1, Num_hours); % 用户1→用户2交易量 P_tran_12_neg = sdpvar(1, Num_hours); % 用户2→用户1交易量 P_tran_13_pos = sdpvar(1, Num_hours); % 用户1→用户3交易量 P_tran_13_neg = sdpvar(1, Num_hours); % 用户3→用户1交易量 P_tran_23_pos = sdpvar(1, Num_hours); % 用户2→用户3交易量 P_tran_23_neg = sdpvar(1, Num_hours); % 用户3→用户2交易量 P_net_1 = sdpvar(1, Num_hours); % 用户1净交易量 P_net_2 = sdpvar(1, Num_hours); % 用户2净交易量 P_net_3 = sdpvar(1, Num_hours); % 用户3净交易量 %% ===================== 约束条件 ===================== Cons = []; % 可再生能源约束 Cons = [Cons, 0 <= P_wt_e <= P_rew_max]; for h = 1:Num_hours if C_Pri_sell(h) > mean(C_Pri_sell) Cons = [Cons, P_sell_e(h) >= 0.1 * P_rew_max(h)]; end end % CHP机组约束 Cons = [Cons, P_CHP_e == k_chp_e * k_gas_h * V_Gas_CHP]; Cons = [Cons, P_CHP_h == k_chp_h * k_gas_h * V_Gas_CHP]; Cons = [Cons, 0 <= P_CHP_e <= 2000]; Cons = [Cons, 0 <= V_Gas_CHP <= 2000/(k_chp_e*k_gas_h)]; % 燃气锅炉约束 Cons = [Cons, P_GB_h == k_gb_h * k_gas_h * V_Gas_GB]; Cons = [Cons, 0 <= P_GB_h <= 1800]; Cons = [Cons, 0 <= V_Gas_GB <= 1800/(k_gb_h*k_gas_h)]; % P2G机组约束 Cons = [Cons, V_P2G_Gas == k_p2g_g * P_e_P2G / k_gas_h]; Cons = [Cons, 0 <= P_e_P2G <= 500]; Cons = [Cons, 0 <= V_P2G_Gas]; % CCS机组约束 Cons = [Cons, 0 <= P_e_CCS <= 300]; % ====== 能源系统平衡约束 ====== % 电功率平衡 Total_generation = P_CHP_e + P_wt_e; Total_consumption = (P_Load_e1 - PL_cut_1) + (P_Load_e2 - PL_cut_2) + (P_Load_e3 - PL_cut_3) + P_e_P2G + P_e_CCS; Cons = [Cons, Total_generation + P_buy_e == Total_consumption + P_sell_e ]; % 热功率平衡 Cons = [Cons, P_CHP_h + P_GB_h == (P_Load_h1 - HL_cut_1) + (P_Load_h2 - HL_cut_2) + (P_Load_h3 - HL_cut_3)]; % 天然气平衡 Cons = [Cons, V_Gas_CHP + V_Gas_GB == V_P2G_Gas + V_buy_gas]; Cons = [Cons, V_buy_gas >= 0]; % 非负约束 % 购售电逻辑约束 Cons = [Cons, implies(B_miu_ies == 1, [P_sell_e == 0, P_buy_e >= 0])]; Cons = [Cons, implies(B_miu_ies == 0, [P_buy_e == 0, P_sell_e >= 0])]; Cons = [Cons, 0 <= P_buy_e <= 1000]; Cons = [Cons, 0 <= P_sell_e <= 1500]; % ====== 用户侧约束 ====== % 负荷削减约束 Cons = [Cons, 0 <= PL_cut_1 <= 0.15 * P_Load_e1]; Cons = [Cons, 0 <= HL_cut_1 <= 0.15 * P_Load_h1]; Cons = [Cons, 0 <= PL_cut_2 <= 0.20 * P_Load_e2]; Cons = [Cons, 0 <= HL_cut_2 <= 0.20 * P_Load_h2]; Cons = [Cons, 0 <= PL_cut_3 <= 0.25 * P_Load_e3]; Cons = [Cons, 0 <= HL_cut_3 <= 0.25 * P_Load_h3]; % ====== P2P交易约束 ====== % 净交易量计算 Cons = [Cons, P_net_1 == (P_tran_12_pos - P_tran_12_neg) + (P_tran_13_pos - P_tran_13_neg)]; Cons = [Cons, P_net_2 == (P_tran_12_neg - P_tran_12_pos) + (P_tran_23_pos - P_tran_23_neg)]; Cons = [Cons, P_net_3 == (P_tran_13_neg - P_tran_13_pos) + (P_tran_23_neg - P_tran_23_pos)]; % P2P净交易守恒约束 Cons = [Cons, P_net_1 + P_net_2 + P_net_3 == 0]; % 用户净交易量约束 Cons = [Cons, -0.2 * P_Load_e1 <= P_net_1 <= 0.2 * P_Load_e1]; Cons = [Cons, -0.2 * P_Load_e2 <= P_net_2 <= 0.2 * P_Load_e2]; Cons = [Cons, -0.2 * P_Load_e3 <= P_net_3 <= 0.2 * P_Load_e3]; % 交易功率限制 Cons = [Cons, 0 <= P_tran_12_pos <= 200, 0 <= P_tran_12_neg <= 200]; Cons = [Cons, 0 <= P_tran_13_pos <= 200, 0 <= P_tran_13_neg <= 200]; Cons = [Cons, 0 <= P_tran_23_pos <= 200, 0 <= P_tran_23_neg <= 200]; %% ===================== 目标函数 ===================== % 成本计算 F1_cost = C_gas_price * sum(V_buy_gas); % 燃料成本 F2_cost = sum(C_Pri_buy .* P_buy_e) - sum(C_Pri_sell .* P_sell_e); % 电网交易成本 % 负荷削减补偿成本 electric_comp = 0.8 * sum(C_Pri_sell .* (PL_cut_1 + PL_cut_2 + PL_cut_3)); heat_comp = 0.7 * (sum(C_heat_price .* HL_cut_1) + sum(C_heat_price .* HL_cut_2) + sum(C_heat_price .* HL_cut_3)); F4_cost = electric_comp + heat_comp; % 负荷补偿成本 % P2P交易收益 p2p_volume = sum(P_tran_12_pos + P_tran_12_neg + P_tran_13_pos + P_tran_13_neg + P_tran_23_pos + P_tran_23_neg); avg_price = mean([C_Pri_buy, C_Pri_sell]); F5_revenue = 0.52 * 0.97 * avg_price * p2p_volume; % P2P收益 % 综合能源系统总运行成本 total_cost = F1_cost + F2_cost + F4_cost - F5_revenue; %% ===================== 模型求解 ===================== options = sdpsettings('solver', 'cplex', 'verbose', 1); disp('>>>开始求解优化问题...'); tic; sol = optimize(Cons, total_cost, options); solve_time = toc; if sol.problem == 0 disp(['>>>优化成功! 求解时间: ', num2str(solve_time), '秒']); % 计算并显示各子成本 Fuel_cost_val = value(F1_cost); Trade_cost_val = value(F2_cost); Compensation_cost_val = value(F4_cost); F_p2p_revenue_val = value(F5_revenue); total_cost_val = value(total_cost); % 详细成本输出 disp('============== 成本明细 =============='); disp(['1. 机组燃料成本: ', num2str(Fuel_cost_val), '元']); disp(['2. 电网交易成本: ', num2str(Trade_cost_val), '元']); disp([' - 购电的成本: ', num2str(sum(value(C_Pri_buy .* P_buy_e))), '元']); disp([' - 售电的收益: ', num2str(sum(value(C_Pri_sell .* P_sell_e))), '元']); disp(['3. 用户响应成本: ', num2str(Compensation_cost_val), '元']); disp([' - 电负荷补偿: ', num2str(value(electric_comp)), '元']); disp([' - 热负荷补偿: ', num2str(value(heat_comp)), '元']); disp(['4. P2P交易收益: ', num2str(F_p2p_revenue_val), '元']); disp('------------------------------------------------'); disp(['>>>总运行成本: ', num2str(total_cost_val), '元']); % 计算并输出负荷削减指标 total_e_cut = sum(value(PL_cut_1 + PL_cut_2 + PL_cut_3)); total_h_cut = sum(value(HL_cut_1 + HL_cut_2 + HL_cut_3)); disp(['>>>总电负荷削减: ', num2str(total_e_cut), 'kW (', ... num2str(100*total_e_cut/sum(P_Load_e1+P_Load_e2+P_Load_e3)), '%)']); disp(['>>>总热负荷削减: ', num2str(total_h_cut), 'kW (', ... num2str(100*total_h_cut/sum(P_Load_h1+P_Load_h2+P_Load_h3)), '%)']); else error('>>>求解失败! 原因: %s', sol.info); end
11-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值