| |
| |
| |
|
|
| using JSON |
| using CodecZlib |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function add_trajectory_curves( |
| json_path::String; |
| top_pct::Float64 = 0.10, |
| output_path::String = replace(json_path, ".json" => "-part1.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"] |
|
|
| |
| thermal_names = filter( |
| u -> haskey(generators[u], "Minimum uptime (h)"), |
| collect(keys(generators)) |
| ) |
| n_total = length(thermal_names) |
|
|
| |
| |
| 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 |
|
|
| |
| |
| 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] |
| |
| |
| 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") |
|
|
| |
| sort!( |
| thermal_names, |
| by = u -> ( |
| get(generators[u], "Minimum uptime (h)", 0.0), |
| Pmax_dict[u] |
| ), |
| rev = true, |
| ) |
|
|
| |
| 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 |
|
|
| |
| |
| reordered_units = vcat(qualified_units, disqualified_units) |
|
|
| |
| n_top = max(1, ceil(Int, n_total * top_pct)) |
| |
| |
| 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] |
|
|
| |
| 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 |
|
|
| |
| json_data["_sorted_thermal_units"] = reordered_units |
|
|
| |
| open(output_path, "w") do f |
| JSON.print(f, json_data, 4) |
| end |
| println("\nPart 1 完成 → 输出保存至: $output_path") |
|
|
| return output_path |
| end |
|
|
|
|
| |
| |
| |
| |
| 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_data = JSON.parsefile(json_v1_path) |
| generators = json_data["Generators"] |
|
|
| |
| 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[] |
|
|
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| delete!(json_data, "_sorted_thermal_units") |
|
|
| |
| 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 |