按每1MW去优化程序。

This commit is contained in:
dmy
2025-12-27 17:30:40 +08:00
parent a80830e750
commit 7ce065f2df
13 changed files with 719 additions and 3831 deletions

View File

@@ -9,6 +9,7 @@
"""
import numpy as np
import math
from typing import List, Dict, Tuple, Optional
from dataclasses import dataclass
@@ -548,80 +549,76 @@ def optimize_storage_capacity(
print(f"基准容量 {best_capacity:.2f} MWh 的弃电量: {best_curtailed:.2f} MWh")
# 使用黄金分割搜索法寻找最优容量
# 黄金分割点
golden_ratio = (3 - 5**0.5) / 2 # 约0.382
# 对每1MW的容量进行计算寻找最优容量
print(f"\n开始对每1MW容量进行精确搜索...")
# 初始化两个测试点
a = search_lower
b = search_upper
# 计算搜索范围,确保为整数
start_capacity = int(math.ceil(search_lower))
end_capacity = int(math.floor(search_upper))
c = b - golden_ratio * (b - a)
d = a + golden_ratio * (b - a)
print(f"搜索范围: {start_capacity} MWh 到 {end_capacity} MWh")
max_refinement_iterations = 50
for iter_num in range(max_refinement_iterations):
# 计算点 c 的弃电量
# 遍历每个可能的容量值步长为1MW
for capacity in range(start_capacity, end_capacity + 1):
# 计算当前容量的弃电量
if is_yearly_data:
result_c = find_periodic_steady_state(
result = find_periodic_steady_state(
solar_output, wind_output, thermal_output, load_demand,
params, c
params, capacity
)
else:
result_c = calculate_energy_balance(
solar_output, wind_output, thermal_output, load_demand, params, c
result = calculate_energy_balance(
solar_output, wind_output, thermal_output, load_demand, params, capacity
)
curtailed_c = sum(result_c['curtailed_wind']) + sum(result_c['curtailed_solar'])
curtailed = sum(result['curtailed_wind']) + sum(result['curtailed_solar'])
# 计算点 d 的弃电量
if is_yearly_data:
result_d = find_periodic_steady_state(
solar_output, wind_output, thermal_output, load_demand,
params, d
)
# 检查约束条件
constraint_results = check_constraints(solar_output, wind_output, thermal_output, result, params)
# 检查是否满足约束条件
total_grid_feed_in = sum(result['grid_feed_in'])
if total_grid_feed_in > 0:
grid_constraint_satisfied = constraint_results['total_grid_feed_in_ratio'] <= params.max_grid_ratio
else:
result_d = calculate_energy_balance(
solar_output, wind_output, thermal_output, load_demand, params, d
grid_constraint_satisfied = True
# 新逻辑:弃光受最大弃光比例限制,弃风不受限制
solar_constraint_satisfied = constraint_results['total_curtailment_solar_ratio'] <= params.max_curtailment_solar
wind_constraint_satisfied = True # 弃风不受限制
grid_quota_zero = abs(params.max_grid_ratio) < 1e-10
if grid_quota_zero:
constraints_satisfied = grid_constraint_satisfied and solar_constraint_satisfied
else:
constraints_satisfied = (
solar_constraint_satisfied and # 弃光受限制
wind_constraint_satisfied and # 弃风不受限制
grid_constraint_satisfied # 上网电量受限制
)
curtailed_d = sum(result_d['curtailed_wind']) + sum(result_d['curtailed_solar'])
# 检查储能日平衡(周期结束时储能状态应接近初始值)
storage_initial = result['storage_profile'][0]
storage_final = result['storage_profile'][-1]
daily_balance = abs(storage_final - storage_initial) < tolerance
# 比较并更新最优解
if curtailed_c < best_curtailed:
best_curtailed = curtailed_c
best_capacity = c
best_result = {**result_c, **check_constraints(solar_output, wind_output, thermal_output, result_c, params)}
print(f" 发现更优容量: {best_capacity:.2f} MWh, 弃电量: {best_curtailed:.2f} MWh")
# 只有满足约束条件的容量才考虑
if constraints_satisfied and daily_balance:
# 更新最优解
if curtailed < best_curtailed:
best_curtailed = curtailed
best_capacity = capacity
best_result = {**result, **constraint_results}
print(f" 发现更优容量: {best_capacity} MWh, 弃电量: {best_curtailed:.2f} MWh")
if curtailed_d < best_curtailed:
best_curtailed = curtailed_d
best_capacity = d
best_result = {**result_d, **check_constraints(solar_output, wind_output, thermal_output, result_d, params)}
print(f" 发现更优容量: {best_capacity:.2f} MWh, 弃电量: {best_curtailed:.2f} MWh")
# 缩小搜索区间
if curtailed_c < curtailed_d:
b = d
d = c
c = b - golden_ratio * (b - a)
else:
a = c
c = d
d = a + golden_ratio * (b - a)
# 检查收敛
if b - a < tolerance:
print(f"搜索收敛,区间宽度: {b - a:.4f} MWh")
break
# 限制最大迭代次数
if iter_num % 10 == 0 and iter_num > 0:
print(f" 已完成 {iter_num} 次迭代,当前搜索区间: [{a:.2f}, {b:.2f}] MWh")
# 每处理10个容量值输出一次进度
if (capacity - start_capacity + 1) % 10 == 0:
print(f" 已检查 {capacity - start_capacity + 1}/{end_capacity - start_capacity + 1} 个容量值")
print(f"精确搜索完成,共检查 {end_capacity - start_capacity + 1} 个容量值")
# 将储能容量向上取整为整数
import math
rounded_capacity = math.ceil(best_capacity)
# 确保不超过容量上限
if params.max_storage_capacity is not None: