EridanusQ
init
e9336c7
raw
history blame
9.88 kB
# preprocessing.jl
# Part 1: 直接从JSON提取Pmax,按 90% 位置计算阈值,对前x%合格机组写入轨迹约束,并重排机组列表输出 JSON_v1
# Part 2: 读取 JSON_v1,对重排后的前10%和10%~20%机组修改 Minimum uptime (h),输出 JSON_v2
using JSON
using CodecZlib
# ─────────────────────────────────────────────────────────────────────────────
# Part 1: 生成 JSON_v1 (添加起停轨迹约束)
# 输入:
# json_path : 原始JSON路径
# top_pct : 添加曲线的比例,默认前10%
# output_path : 输出的 JSON_v1 路径
# 输出:
# JSON_v1 路径(字符串)
# ─────────────────────────────────────────────────────────────────────────────
function add_trajectory_curves(
json_path::String;
top_pct::Float64 = 0.10,
output_path::String = replace(json_path, ".json" => "-part1.json"),
)
# ── 读取原始JSON ─────────────────────────────────────────────────────────
json_data = open(json_path) do io
if endswith(json_path, ".gz")
decompressor = GzipDecompressorStream(io)
JSON.parse(decompressor)
else
JSON.parse(io)
end
end
generators = json_data["Generators"]
# 仅处理含 "Minimum uptime (h)" 字段的机组
thermal_names = filter(
u -> haskey(generators[u], "Minimum uptime (h)"),
collect(keys(generators))
)
n_total = length(thermal_names)
# ── 1. 提取每个机组的 Pmax 和 Pmin ───────────────────────────────────────
# 直接读取 "Production cost curve (MW)" 数组的第一个值(Pmin)和最后一个值(Pmax)
Pmax_dict = Dict{String, Float64}()
Pmin_dict = Dict{String, Float64}()
for u in thermal_names
curve_mw = generators[u]["Production cost curve (MW)"]
Pmin_dict[u] = Float64(curve_mw[1])
Pmax_dict[u] = Float64(curve_mw[end])
end
# ── 2. 计算下界阈值 (Threshold) ──────────────────────────────────────────
# 将全网 Pmax 降序排列,取 90% 位置的值
all_pmax_desc = sort(collect(values(Pmax_dict)), rev=true)
idx_90 = max(1, ceil(Int, n_total * 0.90))
pmax_90_val = all_pmax_desc[idx_90]
# 阈值 = max(10, 降序第90%位置的值)
threshold = max(10.0, pmax_90_val)
println("── Part 1: 筛选与约束添加 ──")
println(" 热机组总数: $n_total")
println(" Pmax 降序第 90% 位置 (第 $idx_90 名) 的值: $pmax_90_val")
println(" Pmax 筛选下界阈值 (Threshold): $threshold")
# ── 3. 主干排序:主键 Minimum uptime 降序,次键 Pmax 降序 ───────────────
sort!(
thermal_names,
by = u -> (
get(generators[u], "Minimum uptime (h)", 0.0), # 主键:降序
Pmax_dict[u] # 次键:降序
),
rev = true,
)
# ── 4. 顺延挑选并重组数组供 Part 2 使用 ──────────────────────
qualified_units = String[]
disqualified_units = String[]
for u in thermal_names
if Pmax_dict[u] >= threshold
push!(qualified_units, u)
else
push!(disqualified_units, u)
end
end
# 核心重组逻辑:合格的排在最前面(保持原相对顺序),淘汰的全部扔到末尾
# 这样 Part 2 按照索引 1~n_top 和 n_top+1~n_second 去读时,拿到的全都是合格机组
reordered_units = vcat(qualified_units, disqualified_units)
# ── 5. 确定名额并向合格的前 n_top 台机组写入轨迹曲线 ─────────────────────
n_top = max(1, ceil(Int, n_total * top_pct))
# 防止合格机组总数少于 n_top 的极端情况
actual_top_count = min(n_top, length(qualified_units))
println("\n── 选中并添加轨迹的机组名单 (前 10% 名额: $n_top) ──")
for u in reordered_units[1:actual_top_count]
uptime = get(generators[u], "Minimum uptime (h)", 0)
pmax = Pmax_dict[u]
pmin = Pmin_dict[u]
# 写入 Startup / Shutdown curve
generators[u]["Startup curve (MW)"] = [pmin / 2.0, pmin]
generators[u]["Shutdown curve (MW)"] = [pmin, pmin / 2.0]
println(" [写入轨迹] $(rpad(u,10)) Uptime=$uptime Pmax=$(round(pmax, digits=2)) Pmin=$(round(pmin, digits=2))")
end
println("\n── 被 Pmax 阈值淘汰的机组 (展示前几位) ──")
for u in disqualified_units[1:min(5, length(disqualified_units))]
uptime = get(generators[u], "Minimum uptime (h)", 0)
pmax = Pmax_dict[u]
println(" [不足下界] $(rpad(u,10)) Uptime=$uptime Pmax=$(round(pmax, digits=2)) < 阈值 $threshold")
end
# ── 6. 将重组后的结果存入元数据,交接给 Part 2 ───────────────────────────
json_data["_sorted_thermal_units"] = reordered_units
# ── 7. 写出 JSON_v1 ──────────────────────────────────────────────────────
open(output_path, "w") do f
JSON.print(f, json_data, 4)
end
println("\nPart 1 完成 → 输出保存至: $output_path")
return output_path
end
# ─────────────────────────────────────────────────────────────────────────────
# Part 2: 生成 JSON_v2 (修改 Minimum uptime)
# (注:此处代码根据需求完全保持原样,未做任何核心逻辑改动,无缝衔接 Part 1)
# ─────────────────────────────────────────────────────────────────────────────
function modify_min_uptime(
json_v1_path::String;
top_pct::Float64 = 0.10,
output_path::String = replace(json_v1_path, "-part1.json" => "-part2.json"),
)
# 读取 JSON_v1
json_data = JSON.parsefile(json_v1_path)
generators = json_data["Generators"]
# 读取 Part 1 保存的重排结果
haskey(json_data, "_sorted_thermal_units") ||
error("缺少 _sorted_thermal_units 元数据,请先运行 Part 1(add_trajectory_curves)")
sorted_units = json_data["_sorted_thermal_units"]
n_total = length(sorted_units)
# 计算两个区间的索引边界
n_top = max(1, ceil(Int, n_total * top_pct))
n_second = min(n_total, ceil(Int, n_total * top_pct * 2))
println("\n── Part 2: Uptime 修改 ──")
println(" 机组总数: $n_total")
println(" 前10%区间: 1 ~ $n_top")
println(" 10%~20%区间: $(n_top+1) ~ $n_second")
skipped_top = String[]
skipped_second = String[]
modified_top = String[]
modified_second= String[]
# ── 处理前 10% 机组 ──────────────────────────────────────────────────────
for u in sorted_units[1:n_top]
uptime = get(generators[u], "Minimum uptime (h)", 1)
if uptime <= 5
generators[u]["Minimum uptime (h)"] = uptime * 3
push!(modified_top, u)
println("[10% 区间 | ×3 修改] $u uptime: $uptime$(uptime*3)")
else
push!(skipped_top, u)
println(" [10% 区间 | 跳过] $u uptime=$uptime > 5,不修改")
end
end
# ── 处理 10% ~ 20% 机组 ──────────────────────────────────────────────────
if n_top < n_second
for u in sorted_units[n_top+1:n_second]
uptime = get(generators[u], "Minimum uptime (h)", 1)
if uptime <= 5
generators[u]["Minimum uptime (h)"] = uptime * 2
push!(modified_second, u)
println("[20% 区间 | ×2 修改] $u uptime: $uptime$(uptime*2)")
else
push!(skipped_second, u)
println(" [20% 区间 | 跳过] $u uptime=$uptime > 5,不修改")
end
end
end
# ── 删除元数据字段,不写入最终 JSON_v2 ──────────────────────────────────
delete!(json_data, "_sorted_thermal_units")
# ── 写出 JSON_v2 ────────────────────────────────────────────────────────
open(output_path, "w") do f
JSON.print(f, json_data, 4)
end
println("\nPart 2 完成 → 输出保存至: $output_path")
println(" 前10% 已修改(×3): $(length(modified_top)) 个")
println(" 前10% 已跳过(>5): $(length(skipped_top)) 个")
println(" 10~20% 已修改(×2): $(length(modified_second)) 个")
println(" 10~20% 已跳过(>5): $(length(skipped_second)) 个")
return output_path
end