Datasets:
File size: 11,883 Bytes
43c68a3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | using UnitCommitment
using HiGHS
import JuMP
CASES =[
("testdata/case2383wp/2017-07-27.json.gz", "case2383wp"),
# ("testdata/case2736sp/2017-07-21.json.gz", "case2736sp")
]
function build_and_solve(instance, use_traj)
formulation = use_traj ?
UnitCommitment.Formulation(
power_trajectories = UnitCommitment.ArrCon2004.PowerTrajectories()
) :
UnitCommitment.Formulation()
model = UnitCommitment.build_model(
instance = instance,
optimizer = HiGHS.Optimizer,
formulation = formulation,
)
JuMP.set_silent(model)
JuMP.optimize!(model)
return model
end
function get_actual_power(model, g, t, T, prev_p)
uname = g.name
UD = length(g.startup_curve)
DD = length(g.shutdown_curve)
startup_curve = g.startup_curve
shutdown_curve = g.shutdown_curve
Pmin = g.min_power[t]
Pmax = g.max_power[t]
RU = g.ramp_up_limit
v = round(JuMP.value(model[:is_on][uname, t]))
y = round(JuMP.value(model[:switch_on][uname, t]))
pa = JuMP.value(model[:prod_above]["s1", uname, t])
in_su = UD > 0 && any(
round(JuMP.value(model[:switch_on][uname, t-i+1])) == 1.0
for i in 1:UD if t-i+1 >= 1)
in_sd = DD > 0 && any(
round(JuMP.value(model[:switch_off][uname, t+i])) == 1.0
for i in 1:DD if t+i <= T)
p = 0.0
if in_su
for i in 1:UD
if t-i+1 >= 1 && round(JuMP.value(model[:switch_on][uname, t-i+1])) == 1.0
p = startup_curve[i]; break
end
end
elseif in_sd
for i in 1:DD
if t+i <= T && round(JuMP.value(model[:switch_off][uname, t+i])) == 1.0
p = shutdown_curve[DD-i+1]; break
end
end
elseif v > 0.5
p = pa + Pmin
end
status = if v == 0.0; "Offline"
elseif y == 1.0; "Startup_t1"
elseif in_su; "Startup_traj"
elseif in_sd; "Shutdown_traj"
else "Normal"
end
# 计算上下界 (ub, lb)
sum_y = sum(round(JuMP.value(model[:switch_on][uname, t-i+1])) for i in 1:UD if t-i+1 >= 1; init=0.0)
sum_z = DD > 0 ? sum(round(JuMP.value(model[:switch_off][uname, t+i])) for i in 1:DD if t+i <= T; init=0.0) : 0.0
ramp_ub = if t == 1
g.initial_power + RU
elseif sum_y > 0
Pmax
else
prev_p + RU
end
if v == 0.0 || (UD == 0 && DD == 0)
ub_val = ""
lb_val = ""
else
su_sum = sum(startup_curve[i] * round(JuMP.value(model[:switch_on][uname, t-i+1]))
for i in 1:UD if t-i+1 >= 1; init=0.0)
sd_sum = DD > 0 ? sum(shutdown_curve[i] * round(JuMP.value(model[:switch_off][uname, t+DD-i+1]))
for i in 1:DD if t+DD-i+1 >= 1 && t+DD-i+1 <= T; init=0.0) : 0.0
ub_val = min(su_sum + Pmax*(v-sum_y), sd_sum + Pmax*(v-sum_z), ramp_ub)
lb_val = max(Pmin*(v-sum_z-sum_y)+su_sum, Pmin*(v-sum_z-sum_y)+sd_sum)
end
return p, status, ub_val, lb_val
end
function export_combined_csv(
model_base, model_v1, model_v2,
instance_orig, instance_v1, instance_v2,
run_name, out_dir
)
sc_orig = instance_orig.scenarios[1]
sc_v1 = instance_v1.scenarios[1]
sc_v2 = instance_v2.scenarios[1]
T = instance_orig.time
unit_map_v1 = Dict(g.name => g for g in sc_v1.thermal_units)
unit_map_v2 = Dict(g.name => g for g in sc_v2.thermal_units)
rows = String[]
push!(rows,
"run_name,case,unit,t," *
"p_base,p_v1,p_v2," *
"ub_v1,lb_v1,ub_v2,lb_v2," *
"status_v1,status_v2," *
"pmin,pmax,startup_limit,shutdown_limit,UD,DD," *
"has_startup_v1,has_shutdown_v1,has_startup_v2,has_shutdown_v2," *
"min_uptime_orig,min_uptime_v2,has_curve"
)
case_name = split(run_name, "-")[1]
for g_orig in sc_orig.thermal_units
uname = g_orig.name
g_v1 = unit_map_v1[uname]
g_v2 = unit_map_v2[uname]
has_curve = !isempty(g_v1.startup_curve)
UD = length(g_v1.startup_curve)
DD = length(g_v1.shutdown_curve)
startup_limit = g_v1.startup_limit
shutdown_limit = g_v1.shutdown_limit
min_uptime_orig = g_orig.min_uptime
min_uptime_v2 = g_v2.min_uptime
# 判断整个周期内是否有真实启动/停机行为
su_v1 = has_curve && any(round(JuMP.value(model_v1[:switch_on][uname, t])) == 1.0 for t in 1:T)
sd_v1 = has_curve && any(round(JuMP.value(model_v1[:switch_off][uname, t])) == 1.0 for t in 1:T)
su_v2 = has_curve && any(round(JuMP.value(model_v2[:switch_on][uname, t])) == 1.0 for t in 1:T)
sd_v2 = has_curve && any(round(JuMP.value(model_v2[:switch_off][uname, t])) == 1.0 for t in 1:T)
prev_p_v1 = 0.0
prev_p_v2 = 0.0
for t in 1:T
Pmin = g_orig.min_power[t]
Pmax = g_orig.max_power[t]
# base model
pa0 = JuMP.value(model_base[:prod_above]["s1", uname, t])
v0 = round(JuMP.value(model_base[:is_on][uname, t]))
p_base = pa0 + Pmin * v0
# v1 model
p_v1, status_v1, ub_v1, lb_v1 = get_actual_power(model_v1, g_v1, t, T, prev_p_v1)
prev_p_v1 = p_v1
# v2 model
p_v2, status_v2, ub_v2, lb_v2 = get_actual_power(model_v2, g_v2, t, T, prev_p_v2)
prev_p_v2 = p_v2
push!(rows,
"$run_name,$case_name,$uname,$t," *
"$(round(p_base,digits=4)),$(round(p_v1,digits=4)),$(round(p_v2,digits=4))," *
"$ub_v1,$lb_v1,$ub_v2,$lb_v2," *
"$status_v1,$status_v2," *
"$(round(Pmin,digits=4)),$(round(Pmax,digits=4))," *
"$(round(startup_limit,digits=4)),$(round(shutdown_limit,digits=4))," *
"$UD,$DD,$su_v1,$sd_v1,$su_v2,$sd_v2," *
"$min_uptime_orig,$min_uptime_v2,$has_curve"
)
end
end
fname = joinpath(out_dir, "$(run_name)_combined.csv")
open(fname, "w") do f
for row in rows
println(f, row)
end
end
println(" Saved: $fname")
# ---------- 物理合理性验证统计输出 ----------
total_curves = 0
v1_su_count, v1_sd_count = 0, 0
v2_su_count, v2_sd_count = 0, 0
for g_v1 in sc_v1.thermal_units
uname = g_v1.name
if !isempty(g_v1.startup_curve)
total_curves += 1
if any(round(JuMP.value(model_v1[:switch_on][uname, t])) == 1.0 for t in 1:T) v1_su_count += 1 end
if any(round(JuMP.value(model_v1[:switch_off][uname, t])) == 1.0 for t in 1:T) v1_sd_count += 1 end
if any(round(JuMP.value(model_v2[:switch_on][uname, t])) == 1.0 for t in 1:T) v2_su_count += 1 end
if any(round(JuMP.value(model_v2[:switch_off][uname, t])) == 1.0 for t in 1:T) v2_sd_count += 1 end
end
end
println("\n [物理合理性验证] 启停轨迹激活统计:")
println(" 配置了曲线的机组总数: $total_curves")
println(" v1 发生实际启动: $(v1_su_count)/$total_curves | 发生实际停机: $(v1_sd_count)/$total_curves")
println(" v2 发生实际启动: $(v2_su_count)/$total_curves | 发生实际停机: $(v2_sd_count)/$total_curves")
if total_curves > 0 && v1_su_count == 0 && v2_su_count == 0
println(" 警告: 即使添加了曲线,也没有任何相关机组发生真实启动/停机。")
end
end
function export_summary_csv(results, run_name, case_name, out_dir)
fname = joinpath(out_dir, "$(run_name)_summary.csv")
open(fname, "w") do f
println(f, "case,model,objective,lower_bound,mip_gap_actual,solve_time,diff_pct")
for (tag, obj, lb, gap, stime, diff) in results
println(f, "$case_name,$tag,$(round(obj,digits=4)),$(round(lb,digits=4)),$(round(gap,digits=6)),$(round(stime,digits=4)),$(round(diff,digits=6))")
end
end
println(" Saved: $fname")
end
println("="^60)
# 定义主输出文件夹
master_dir = "test"
mkpath(master_dir)
println(">> 开始测试")
for (json_path, case_name) in CASES
# 提取日期,例如:2017-01-01(现在是从 .json 文件中提取)
date_str = split(basename(json_path), ".")[1]
# 拼接统一前缀名称:case2383wp-2017-01-01
run_name = "$(case_name)-$(date_str)"
# 输出存放在 master_dir 下
out_dir = joinpath(master_dir, run_name)
mkpath(out_dir)
println("\n[Run: $run_name]")
# 读取原始instance → 求解base model
instance_orig = UnitCommitment.read(json_path)
println(" [1/4] 求解 base model...")
model_base = build_and_solve(instance_orig, false)
obj_base = JuMP.objective_value(model_base)
lb_base = JuMP.objective_bound(model_base)
time_base = JuMP.solve_time(model_base)
gap_base = abs(obj_base - lb_base) / max(1e-10, abs(obj_base))
println(" base obj: $(round(obj_base, digits=2)) | gap: $(round(gap_base*100, digits=4))% | solver_time: $(round(time_base, digits=2))s")
println("\n[2/4] Part 1 - 添加 startup/shutdown curve...")
json_v1_path = UnitCommitment.add_trajectory_curves_to_source_data(
json_path;
top_pct = 0.10,
output_path = joinpath(out_dir, "$(run_name)-part1.json"),
)
instance_v1 = UnitCommitment.read(json_v1_path)
println(" 求解 traj_v1 model...")
model_v1 = build_and_solve(instance_v1, true)
obj_v1 = JuMP.objective_value(model_v1)
lb_v1 = JuMP.objective_bound(model_v1)
time_v1 = JuMP.solve_time(model_v1)
gap_v1 = abs(obj_v1 - lb_v1) / max(1e-10, abs(obj_v1))
diff_v1 = (obj_v1 - obj_base) / obj_base * 100
println(" v1 obj: $(round(obj_v1, digits=2)) diff=$(round(diff_v1, digits=4))% | gap: $(round(gap_v1*100, digits=4))% | solver_time: $(round(time_v1, digits=2))s")
println("\n [3/4] Part 2 - 修改 Minimum uptime...")
json_v2_path = UnitCommitment.modify_min_uptime_in_source_data(
json_v1_path;
output_path = joinpath(out_dir, "$(run_name)-part2.json"),
)
instance_v2 = UnitCommitment.read(json_v2_path)
println(" 求解 traj_v2 model...")
model_v2 = build_and_solve(instance_v2, true)
obj_v2 = JuMP.objective_value(model_v2)
lb_v2 = JuMP.objective_bound(model_v2)
time_v2 = JuMP.solve_time(model_v2)
gap_v2 = abs(obj_v2 - lb_v2) / max(1e-10, abs(obj_v2))
diff_v2 = (obj_v2 - obj_base) / obj_base * 100
println(" v2 obj: $(round(obj_v2, digits=2)) diff=$(round(diff_v2, digits=4))% | gap: $(round(gap_v2*100, digits=4))% | solver_time: $(round(time_v2, digits=2))s")
println("\n ── 目标值汇总 " * "─"^30)
println(" base : obj=$(round(obj_base, digits=2)), gap=$(round(gap_base*100, digits=4))%, solver_time=$(round(time_base, digits=2))s")
println(" v1 : obj=$(round(obj_v1, digits=2)), diff=$(round(diff_v1, digits=4))%, gap=$(round(gap_v1*100, digits=4))%, solver_time=$(round(time_v1, digits=2))s")
println(" v2 : obj=$(round(obj_v2, digits=2)), diff=$(round(diff_v2, digits=4))%, gap=$(round(gap_v2*100, digits=4))%, solver_time=$(round(time_v2, digits=2))s")
println("\n[4/4] 导出 CSV...")
export_combined_csv(
model_base, model_v1, model_v2,
instance_orig, instance_v1, instance_v2,
run_name, out_dir
)
results_to_export =[
("base", obj_base, lb_base, gap_base, time_base, 0.0),
("v1", obj_v1, lb_v1, gap_v1, time_v1, diff_v1),
("v2", obj_v2, lb_v2, gap_v2, time_v2, diff_v2)
]
export_summary_csv(results_to_export, run_name, case_name, out_dir)
end
println("\n" * "="^60)
println("测试完成") |