整理以下代码:%% 基于阶梯碳交易机制与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