EridanusQ
init
35f2452
module Modify
using JSON
using CodecZlib
export add_trajectory_curves_to_source_data, modify_min_uptime_in_source_data
function read_json_maybe_gz(path::String)
open(path) do io
if endswith(path, ".gz")
decompressor = GzipDecompressorStream(io)
return JSON.parse(decompressor)
end
return JSON.parse(io)
end
end
function write_json_pretty(path::String, json_data)
open(path, "w") do f
JSON.print(f, json_data, 4)
end
end
normalize_top_ratio(top_pct::Float64) = clamp(top_pct <= 1.0 ? top_pct : (top_pct / 100.0), 0.0, 1.0)
function get_eligible_thermal_units(generators)::Vector{String}
return filter(
u -> haskey(generators[u], "Minimum uptime (h)") &&
haskey(generators[u], "Production cost curve (MW)"),
collect(keys(generators)),
)
end
function build_unit_metric_dicts(generators, unit_names::Vector{String})
uptime_dict = Dict{String, Float64}()
pmax_dict = Dict{String, Float64}()
pmin_dict = Dict{String, Float64}()
for u in unit_names
curve_mw = generators[u]["Production cost curve (MW)"]
uptime_dict[u] = Float64(get(generators[u], "Minimum uptime (h)", 0.0))
pmin_dict[u] = Float64(curve_mw[1])
pmax_dict[u] = Float64(curve_mw[end])
end
return uptime_dict, pmax_dict, pmin_dict
end
function sort_by_uptime_then_pmax(unit_names::Vector{String}, uptime_dict, pmax_dict)
return sort(copy(unit_names), by = u -> (uptime_dict[u], pmax_dict[u]), rev = true)
end
function sort_by_pmax_then_uptime(unit_names::Vector{String}, uptime_dict, pmax_dict)
return sort(copy(unit_names), by = u -> (pmax_dict[u], uptime_dict[u]), rev = true)
end
function top_set(sorted_units::Vector{String}, ratio::Float64)
n_total = length(sorted_units)
n_total == 0 && return Set{String}()
n_top = max(1, ceil(Int, n_total * ratio))
return Set(sorted_units[1:n_top])
end
function build_four_categories(unit_order::Vector{String}, uptime_sorted::Vector{String}, pmax_sorted::Vector{String})
top_uptime_10 = top_set(uptime_sorted, 0.10)
top_uptime_20 = top_set(uptime_sorted, 0.20)
top_uptime_50 = top_set(uptime_sorted, 0.50)
top_pmax_10 = top_set(pmax_sorted, 0.10)
top_pmax_20 = top_set(pmax_sorted, 0.20)
top_pmax_50 = top_set(pmax_sorted, 0.50)
cat1_set = intersect(top_uptime_10, top_pmax_10)
cat2_set = setdiff(intersect(top_uptime_20, top_pmax_20), cat1_set)
cat3_set = setdiff(intersect(top_uptime_50, top_pmax_50), union(cat1_set, cat2_set))
cat4_set = setdiff(Set(unit_order), union(cat1_set, cat2_set, cat3_set))
category1 = [u for u in unit_order if u in cat1_set]
category2 = [u for u in unit_order if u in cat2_set]
category3 = [u for u in unit_order if u in cat3_set]
category4 = [u for u in unit_order if u in cat4_set]
return category1, category2, category3, category4
end
function add_trajectory_curves_to_source_data(
json_path::String;
top_pct::Float64 = 10.0,
output_path::String = replace(json_path, ".json" => "-part1.json"),
)
json_data = read_json_maybe_gz(json_path)
generators = json_data["Generators"]
thermal_names = get_eligible_thermal_units(generators)
n_total = length(thermal_names)
n_total == 0 && begin
println("── Part 1: 未发现可处理机组,直接写出原始数据 ──")
write_json_pretty(output_path, json_data)
return output_path
end
uptime_dict, pmax_dict, pmin_dict = build_unit_metric_dicts(generators, thermal_names)
top_ratio = normalize_top_ratio(top_pct)
top_count = min(n_total, max(1, ceil(Int, n_total * top_ratio)))
threshold = 10.0
println("── Part 1: 筛选与约束添加 ──")
println(" 热机组总数: $n_total")
println(" Top X% 参数: $top_pct (Top 数量: $top_count)")
println(" Pmax 下界阈值 (Threshold): $threshold")
uptime_sorted = sort_by_uptime_then_pmax(thermal_names, uptime_dict, pmax_dict)
pmax_sorted = sort_by_pmax_then_uptime(thermal_names, uptime_dict, pmax_dict)
top_uptime_set = Set(uptime_sorted[1:top_count])
top_pmax_set = Set(pmax_sorted[1:top_count])
qualified_units = String[]
disqualified_units = String[]
for u in uptime_sorted
if (u in top_uptime_set) && (u in top_pmax_set) && (pmax_dict[u] > threshold)
push!(qualified_units, u)
else
push!(disqualified_units, u)
end
end
reordered_units = vcat(qualified_units, disqualified_units)
println("\n── 选中并添加轨迹的机组名单 (交集且 Pmax > $threshold) ──")
for u in qualified_units
uptime = uptime_dict[u]
pmax = pmax_dict[u]
pmin = pmin_dict[u]
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── 未满足筛选条件的机组 (展示前几位) ──")
for u in disqualified_units[1:min(5, length(disqualified_units))]
uptime = uptime_dict[u]
pmax = pmax_dict[u]
in_top_uptime = u in top_uptime_set
in_top_pmax = u in top_pmax_set
println(" [未入选] $(rpad(u,10)) Uptime=$uptime Pmax=$(round(pmax, digits=2)) TopUptime=$in_top_uptime TopPmax=$in_top_pmax")
end
json_data["_sorted_thermal_units"] = reordered_units
write_json_pretty(output_path, json_data)
println("\nPart 1 完成 → 输出保存至: $output_path")
return output_path
end
function modify_min_uptime_in_source_data(
json_v1_path::String;
output_path::String = replace(json_v1_path, "-part1.json" => "-part2.json"),
)
json_data = read_json_maybe_gz(json_v1_path)
generators = json_data["Generators"]
sorted_units = get_eligible_thermal_units(generators)
n_total = length(sorted_units)
n_total == 0 && begin
println("── Part 2: 未发现可处理机组,直接写出原始数据 ──")
if haskey(json_data, "_sorted_thermal_units")
delete!(json_data, "_sorted_thermal_units")
end
write_json_pretty(output_path, json_data)
return output_path
end
uptime_dict, pmax_dict, _ = build_unit_metric_dicts(generators, sorted_units)
uptime_sorted = sort_by_uptime_then_pmax(sorted_units, uptime_dict, pmax_dict)
pmax_sorted = sort_by_pmax_then_uptime(sorted_units, uptime_dict, pmax_dict)
category1, category2, category3, category4 =
build_four_categories(uptime_sorted, uptime_sorted, pmax_sorted)
println("\n── Part 2: Uptime / Downtime 分类修改 ──")
println(" 机组总数: $n_total")
println(" Category 1 (top10%∩top10%): $(length(category1))")
println(" Category 2 (top20%∩top20% \\ cat1): $(length(category2))")
println(" Category 3 (top50%∩top50% \\ cat1,2): $(length(category3))")
println(" Category 4 (remaining): $(length(category4))")
modified_cat1 = String[]
modified_cat2 = String[]
modified_cat3 = String[]
function apply_uptime_downtime_multipliers!(u::String, up_mult::Int, down_mult::Int, tag::String)
old_up = get(generators[u], "Minimum uptime (h)", nothing)
old_down = get(generators[u], "Minimum downtime (h)", nothing)
if old_up !== nothing
generators[u]["Minimum uptime (h)"] = old_up * up_mult
end
if old_down !== nothing
generators[u]["Minimum downtime (h)"] = old_down * down_mult
end
println("[$tag] $u uptime: $old_up → $(get(generators[u], "Minimum uptime (h)", old_up)) downtime: $old_down → $(get(generators[u], "Minimum downtime (h)", old_down))")
end
for u in category1
apply_uptime_downtime_multipliers!(u, 4, 3, "Category 1")
push!(modified_cat1, u)
end
for u in category2
apply_uptime_downtime_multipliers!(u, 3, 2, "Category 2")
push!(modified_cat2, u)
end
for u in category3
apply_uptime_downtime_multipliers!(u, 2, 2, "Category 3")
push!(modified_cat3, u)
end
if haskey(json_data, "_sorted_thermal_units")
delete!(json_data, "_sorted_thermal_units")
end
write_json_pretty(output_path, json_data)
println("\nPart 2 完成 → 输出保存至: $output_path")
println(" Category 1 已修改 (uptime×4, downtime×3): $(length(modified_cat1)) 个")
println(" Category 2 已修改 (uptime×3, downtime×2): $(length(modified_cat2)) 个")
println(" Category 3 已修改 (uptime×2, downtime×2): $(length(modified_cat3)) 个")
println(" Category 4 未修改: $(length(category4)) 个")
return output_path
end
end