using JuMP using UnitCommitment const DEFAULT_INPUT_ROOT = "instances/matpower" const DEFAULT_OUTPUT_ROOT = "../UnitCommitment_Trajectory_Dataset" const VARIANTS = ("hourly_noline", "hourly_withline", "subhourly_noline", "subhourly_withline") function _build_noline_formulation() return UnitCommitment.Formulation( transmission = UnitCommitment.ShiftFactorsFormulation( precomputed_isf = zeros(0, 0), precomputed_lodf = zeros(0, 0), ), ) end function _write_mps(model::JuMP.Model, path::String) mkpath(dirname(path)) JuMP.write_to_file(model, path) return nothing end function _list_json_gz(case_dir::String) files = filter(f -> endswith(f, ".json.gz"), readdir(case_dir)) sort!(files) return files end function discover_matpower_cases(input_root::String = DEFAULT_INPUT_ROOT) isdir(input_root) || error("Input directory does not exist: $input_root") case_dirs = filter(d -> isdir(joinpath(input_root, d)), readdir(input_root)) sort!(case_dirs) return [ (case_name, joinpath(input_root, case_name)) for case_name in case_dirs if !isempty(_list_json_gz(joinpath(input_root, case_name))) ] end function _parse_case_filter() raw = strip(get(ENV, "UC_CASES", "")) isempty(raw) && return nothing return Set(strip.(split(raw, ","))) end function _is_truthy_env(name::String) value = lowercase(strip(get(ENV, name, ""))) return value in ("1", "true", "yes", "y") end function _selected_cases(input_root::String) cases = discover_matpower_cases(input_root) selected = _parse_case_filter() selected === nothing && return cases return filter(case -> case[1] in selected, cases) end function _generate_one_instance!( case_name::AbstractString, date_tag::AbstractString, src_path::AbstractString, output_root::AbstractString, noline_formulation, ) inst_hourly = UnitCommitment.read(src_path) inst_hourly_noline = deepcopy(inst_hourly) empty!(inst_hourly_noline.scenarios[1].lines) model_hourly_noline = UnitCommitment.build_model( instance = inst_hourly_noline, formulation = noline_formulation, variable_names = true, ) _write_mps( model_hourly_noline, joinpath(output_root, case_name, "hourly_noline", "$(case_name)_$(date_tag)_h_noline.mps"), ) model_hourly_withline = UnitCommitment.build_model( instance = inst_hourly, variable_names = true, ) _write_mps( model_hourly_withline, joinpath(output_root, case_name, "hourly_withline", "$(case_name)_$(date_tag)_h_withline.mps"), ) inst_sub = UnitCommitment.convert_to_subhourly(inst_hourly, inst_hourly) inst_sub_noline = deepcopy(inst_sub) empty!(inst_sub_noline.scenarios[1].lines) model_sub_noline = UnitCommitment.build_model( instance = inst_sub_noline, formulation = noline_formulation, variable_names = true, ) _write_mps( model_sub_noline, joinpath(output_root, case_name, "subhourly_noline", "$(case_name)_$(date_tag)_s_noline.mps"), ) model_sub_withline = UnitCommitment.build_model( instance = inst_sub, variable_names = true, ) _write_mps( model_sub_withline, joinpath(output_root, case_name, "subhourly_withline", "$(case_name)_$(date_tag)_s_withline.mps"), ) return nothing end function generate_dataset(; input_root::String = get(ENV, "UC_INPUT_ROOT", DEFAULT_INPUT_ROOT), output_root::String = get(ENV, "UC_OUTPUT_ROOT", DEFAULT_OUTPUT_ROOT), ) cases = _selected_cases(input_root) isempty(cases) && error("No cases selected under $input_root. Check UC_CASES or the input directory.") mkpath(output_root) noline_formulation = _build_noline_formulation() total_instances = sum(length(_list_json_gz(case_dir)) for (_, case_dir) in cases) println("Input root: $input_root") println("Output root: $output_root") println("Cases: $(length(cases))") println("Instances: $total_instances") println("Variants: $(length(VARIANTS))") println("MPS files: $(total_instances * length(VARIANTS))") if _is_truthy_env("UC_DRY_RUN") println("\nDry run only. Set UC_DRY_RUN=0 or remove it to generate MPS files.") for (case_name, case_dir) in cases println(" $case_name: $(length(_list_json_gz(case_dir))) instances") end return nothing end for (case_index, (case_name, case_dir)) in enumerate(cases) files = _list_json_gz(case_dir) println("\n[$case_index/$(length(cases))] $case_name ($(length(files)) instances)") for variant in VARIANTS mkpath(joinpath(output_root, case_name, variant)) end for (i, file_name) in enumerate(files) date_tag = split(file_name, ".")[1] src_path = joinpath(case_dir, file_name) _generate_one_instance!(case_name, date_tag, src_path, output_root, noline_formulation) GC.gc() println(" [$case_name] $i/$(length(files)) $date_tag") end end println("\nDone. output_root=$output_root") return nothing end if abspath(PROGRAM_FILE) == @__FILE__ generate_dataset() end