File size: 3,115 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
"""
    set_power_trajectories(instance, curves; clear_unselected = true)

Return a deep-copied instance with startup/shutdown curves assigned to the
selected thermal units.

`curves` must be a dictionary like:
Dict(
    "g1" => (startup = [75.035, 150.07], shutdown = [150.07, 75.035]),
    "g3" => (startup = [128.76, 257.52], shutdown = [257.52, 128.76]),
)
"""
function set_power_trajectories(
    instance::UnitCommitmentInstance,
    curves::AbstractDict{<:AbstractString,<:Any};
    clear_unselected::Bool = true,
)::UnitCommitmentInstance
    modified = deepcopy(instance)
    set_power_trajectories!(modified, curves; clear_unselected = clear_unselected)
    return modified
end

function set_power_trajectories!(
    instance::UnitCommitmentInstance,
    curves::AbstractDict{<:AbstractString,<:Any};
    clear_unselected::Bool = true,
)::Nothing
    normalized = Dict{
        String,
        NamedTuple{(:startup, :shutdown),Tuple{Vector{Float64},Vector{Float64}}},
    }()

    for (name, spec) in curves
        startup, shutdown = _normalize_curve_spec(String(name), spec)
        normalized[String(name)] = (startup = startup, shutdown = shutdown)
    end

    selected = Set(keys(normalized))

    for sc in instance.scenarios
        missing = [name for name in selected if !haskey(sc.thermal_units_by_name, name)]
        isempty(missing) || error(
            "Unknown thermal units in scenario $(sc.name): $(join(sort!(missing), ", "))",
        )

        for (name, spec) in normalized
            g = sc.thermal_units_by_name[name]
            g.startup_curve = copy(spec.startup)
            g.shutdown_curve = copy(spec.shutdown)
        end

        if clear_unselected
            for g in sc.thermal_units
                if !(g.name in selected)
                    g.startup_curve = Float64[]
                    g.shutdown_curve = Float64[]
                end
            end
        end
    end

    return
end

function clear_power_trajectories!(instance::UnitCommitmentInstance)::Nothing
    for sc in instance.scenarios
        for g in sc.thermal_units
            g.startup_curve = Float64[]
            g.shutdown_curve = Float64[]
        end
    end
    return
end

function _normalize_curve_spec(
    name::String,
    spec,
)::Tuple{Vector{Float64},Vector{Float64}}
    spec isa NamedTuple || error(
        "Curve spec for unit $name must be a named tuple with `startup` and `shutdown`.",
    )
    (:startup in keys(spec) && :shutdown in keys(spec)) || error(
        "Curve spec for unit $name must contain `startup` and `shutdown`.",
    )

    startup = Float64.(collect(spec.startup))
    shutdown = Float64.(collect(spec.shutdown))

    isempty(startup) &&
        error("startup curve for unit $name cannot be empty")
    isempty(shutdown) &&
        error("shutdown curve for unit $name cannot be empty")

    all(isfinite, startup) || error(
        "startup curve for unit $name contains non-finite values",
    )
    all(isfinite, shutdown) || error(
        "shutdown curve for unit $name contains non-finite values",
    )

    return startup, shutdown
end