File size: 5,325 Bytes
e9336c7
35f2452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9336c7
35f2452
 
 
 
 
 
e9336c7
35f2452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9336c7
35f2452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9336c7
 
35f2452
 
 
 
 
 
 
 
 
e9336c7
35f2452
 
 
 
e9336c7
35f2452
 
 
 
 
 
 
 
 
 
 
 
e9336c7
35f2452
 
 
 
 
 
e9336c7
 
35f2452
 
 
e9336c7
 
35f2452
 
 
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
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