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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details.

# Function for adding variables, constraints, and objective function terms
# related to the binary commitment, startup and shutdown decisions of units
function _add_unit_commitment!(
    model::JuMP.Model,
    g::ThermalUnit,
    formulation::Formulation,
)
    if !all(g.must_run) && any(g.must_run)
        error("Partially must-run units are not currently supported")
    end
    if g.initial_power === nothing || g.initial_status === nothing
        error("Initial conditions for $(g.name) must be provided")
    end

    # Variables
    _add_startup_shutdown_vars!(model, g)
    _add_status_vars!(model, g, formulation.status_vars)

    # Constraints and objective function
    _add_min_uptime_downtime_eqs!(model, g)
    _add_startup_cost_eqs!(model, g, formulation.startup_costs)
    _add_status_eqs!(model, g, formulation.status_vars)
    _add_commitment_status_eqs!(model, g)
    return
end

# Function for adding variables, constraints, and objective function terms
# related to the continuous dispatch decisions of units
function _add_unit_dispatch!(
    model::JuMP.Model,
    g::ThermalUnit,
    formulation::Formulation,
    sc::UnitCommitmentScenario,
)

    # Variables
    _add_production_vars!(model, g, formulation.prod_vars, sc)
    _add_spinning_reserve_vars!(model, g, sc)
    _add_flexiramp_reserve_vars!(model, g, sc)

    # Constraints and objective function
    _add_net_injection_eqs!(model, g, sc)
    # [1]
    # _add_production_limit_eqs!(model, g, formulation.prod_vars, sc)
    # [1]
    _add_production_limit_eqs!(model, g, formulation.prod_vars, formulation.power_trajectories, sc)
    _add_production_piecewise_linear_eqs!(
        model,
        g,
        formulation.prod_vars,
        formulation.pwl_costs,
        formulation.status_vars,
        sc,
    )
    _add_ramp_eqs!(
        model,
        g,
        formulation.prod_vars,
        formulation.ramping,
        formulation.status_vars,
        sc,
    )
    _add_power_trajectory_eqs!(
        model,
        g, 
        formulation.prod_vars, 
        formulation.power_trajectories, 
        formulation.status_vars, 
        sc,
    )
    _add_startup_shutdown_limit_eqs!(model, g, sc)
    return
end

_is_initially_on(g::ThermalUnit)::Float64 = (g.initial_status > 0 ? 1.0 : 0.0)

function _add_spinning_reserve_vars!(
    model::JuMP.Model,
    g::ThermalUnit,
    sc::UnitCommitmentScenario,
)::Nothing
    reserve = _init(model, :reserve)
    reserve_shortfall = _init(model, :reserve_shortfall)
    for r in g.reserves
        r.type == "spinning" || continue
        for t in 1:model[:instance].time
            reserve[sc.name, r.name, g.name, t] =
                @variable(model, lower_bound = 0, base_name =  "reserve_$(sc.name)_$(r.name)_$(g.name)_$(t)")
            if (sc.name, r.name, t) ∉ keys(reserve_shortfall)
                reserve_shortfall[sc.name, r.name, t] =
                    @variable(model, lower_bound = 0, base_name =  "reserve_shortfall_$(sc.name)_$(r.name)_$(t)")
                if r.shortfall_penalty < 0
                    set_upper_bound(reserve_shortfall[sc.name, r.name, t], 0.0)
                end
            end
        end
    end
    return
end

function _add_flexiramp_reserve_vars!(
    model::JuMP.Model,
    g::ThermalUnit,
    sc::UnitCommitmentScenario,
)::Nothing
    upflexiramp = _init(model, :upflexiramp)
    upflexiramp_shortfall = _init(model, :upflexiramp_shortfall)
    mfg = _init(model, :mfg)
    dwflexiramp = _init(model, :dwflexiramp)
    dwflexiramp_shortfall = _init(model, :dwflexiramp_shortfall)
    for t in 1:model[:instance].time
        # maximum feasible generation, \bar{g_{its}} in Wang & Hobbs (2016)
        mfg[sc.name, g.name, t] = @variable(model, lower_bound = 0, base_name =  "mfg_$(sc.name)_$(g.name)_$(t)")
        for r in g.reserves
            r.type == "flexiramp" || continue
            upflexiramp[sc.name, r.name, g.name, t] = @variable(model) # up-flexiramp, ur_{it} in Wang & Hobbs (2016)
            dwflexiramp[sc.name, r.name, g.name, t] = @variable(model) # down-flexiramp, dr_{it} in Wang & Hobbs (2016)
            if (sc.name, r.name, t) ∉ keys(upflexiramp_shortfall)
                upflexiramp_shortfall[sc.name, r.name, t] =
                    @variable(model, lower_bound = 0, base_name =  "upflexiramp_shortfall_$(sc.name)_$(r.name)_$(t)")
                dwflexiramp_shortfall[sc.name, r.name, t] =
                    @variable(model, lower_bound = 0, base_name =  "dwflexiramp_shortfall_$(sc.name)_$(r.name)_$(t)")
                if r.shortfall_penalty < 0
                    set_upper_bound(
                        upflexiramp_shortfall[sc.name, r.name, t],
                        0.0,
                    )
                    set_upper_bound(
                        dwflexiramp_shortfall[sc.name, r.name, t],
                        0.0,
                    )
                end
            end
        end
    end
    return
end

function _add_startup_shutdown_vars!(model::JuMP.Model, g::ThermalUnit)::Nothing
    startup = _init(model, :startup)
    for t in 1:model[:instance].time
        for s in 1:length(g.startup_categories)
            startup[g.name, t, s] = @variable(model, binary = true, base_name =  "startup_$(g.name)_$(t)_$(s)")
        end
    end
    return
end

function _add_startup_shutdown_limit_eqs!(
    model::JuMP.Model,
    g::ThermalUnit,
    sc::UnitCommitmentScenario,
)::Nothing
    eq_shutdown_limit = _init(model, :eq_shutdown_limit)
    eq_startup_limit = _init(model, :eq_startup_limit)
    is_on = model[:is_on]
    prod_above = model[:prod_above]
    reserve = _total_reserves(model, g, sc)
    switch_off = model[:switch_off]
    switch_on = model[:switch_on]
    T = model[:instance].time
    for t in 1:T
        # Startup limit
        eq_startup_limit[sc.name, g.name, t] = @constraint(
            model,
            prod_above[sc.name, g.name, t] + reserve[t] <=
            (g.max_power[t] - g.min_power[t]) * is_on[g.name, t] -
            max(0, g.max_power[t] - g.startup_limit) * switch_on[g.name, t]
        )
        # Shutdown limit
        if g.initial_power > g.shutdown_limit
            eq_shutdown_limit[sc.name, g.name, 0] =
                @constraint(model, switch_off[g.name, 1] <= 0)
        end
        if t < T
            eq_shutdown_limit[sc.name, g.name, t] = @constraint(
                model,
                prod_above[sc.name, g.name, t] <=
                (g.max_power[t] - g.min_power[t]) * is_on[g.name, t] -
                max(0, g.max_power[t] - g.shutdown_limit) *
                switch_off[g.name, t+1]
            )
        end
    end
    return
end

function _add_ramp_eqs!(
    model::JuMP.Model,
    g::ThermalUnit,
    formulation::RampingFormulation,
    sc::UnitCommitmentScenario,
)::Nothing
    prod_above = model[:prod_above]
    reserve = _total_reserves(model, g, sc)
    eq_ramp_up = _init(model, :eq_ramp_up)
    eq_ramp_down = _init(model, :eq_ramp_down)
    for t in 1:model[:instance].time
        # Ramp up limit 
        if t == 1
            if _is_initially_on(g) == 1
                eq_ramp_up[sc.name, g.name, t] = @constraint(
                    model,
                    prod_above[sc.name, g.name, t] + reserve[t] <=
                    (g.initial_power - g.min_power[t]) + g.ramp_up_limit
                )
            end
        else
            eq_ramp_up[sc.name, g.name, t] = @constraint(
                model,
                prod_above[sc.name, g.name, t] + reserve[t] <=
                prod_above[sc.name, g.name, t-1] + g.ramp_up_limit
            )
        end

        # Ramp down limit
        if t == 1
            if _is_initially_on(g) == 1
                eq_ramp_down[sc.name, g.name, t] = @constraint(
                    model,
                    prod_above[sc.name, g.name, t] >=
                    (g.initial_power - g.min_power[t]) - g.ramp_down_limit
                )
            end
        else
            eq_ramp_down[sc.name, g.name, t] = @constraint(
                model,
                prod_above[sc.name, g.name, t] >=
                prod_above[sc.name, g.name, t-1] - g.ramp_down_limit
            )
        end
    end
end

function _add_power_trajectory_eqs!(
    model::JuMP.Model,
    g::ThermalUnit,
    formulation_prod_vars::ProductionVarsFormulation,
    formulation_power_trajectories::Nothing,
    formulation_status_vars::StatusVarsFormulation,
    sc::UnitCommitmentScenario,
)::Nothing
    return
end

function _add_power_trajectory_eqs!(
    model::JuMP.Model,
    g::ThermalUnit,
    formulation_prod_vars::ProductionVarsFormulation,
    formulation_power_trajectories::PowerTrajectoriesFormulation,
    formulation_status_vars::StatusVarsFormulation,
    sc::UnitCommitmentScenario,
)::Nothing
    T = model[:instance].time
    prod_above = model[:prod_above]       
    is_on = model[:is_on]        
    switch_on = model[:switch_on]
    switch_off = model[:switch_off]

    eq_traj_lower_startup = _init(model, :eq_traj_lower_startup)
    eq_traj_lower_shutdown = _init(model, :eq_traj_lower_shutdown)
    eq_traj_upper_startup = _init(model, :eq_traj_upper_startup)
    eq_traj_upper_shutdown = _init(model, :eq_traj_upper_shutdown)
    eq_traj_ramp_up = _init(model, :eq_traj_ramp_up)
    eq_traj_ramp_down = _init(model, :eq_traj_ramp_down)

    UD = hasproperty(g, :startup_curve) ? length(g.startup_curve) : 0
    DD = hasproperty(g, :shutdown_curve) ? length(g.shutdown_curve) : 0
    P_U = hasproperty(g, :startup_curve) ? g.startup_curve : zeros(UD)
    P_D = hasproperty(g, :shutdown_curve) ? g.shutdown_curve : zeros(DD)

    for t in 1:T
        set_lower_bound(prod_above[sc.name, g.name, t], -g.min_power[t])
    end

    for t in 1:T
        # sum_{i=1}^{UD} y(k-i+1)
        sum_y_UD = @expression(model, sum(switch_on[g.name, t-i+1] for i in 1:UD if t-i+1 >= 1; init=0))
        # sum_{i=1}^{DD} z(k+i)
        sum_z_DD = @expression(model, sum(switch_off[g.name, t+i] for i in 1:DD if t+i <= T; init=0))
        
        # [v(k) - sum_z - sum_y]
        base_status_term = @expression(model, is_on[g.name, t] - sum_z_DD - sum_y_UD)

        # [1] & [2]
        start_power_above = @expression(model, sum((P_U[i] - g.min_power[t]) * switch_on[g.name, t-i+1] for i in 1:UD if t-i+1 >= 1; init=0))
        shut_power_above = @expression(model, sum((P_D[i] - g.min_power[t]) * switch_off[g.name, t+DD-i+1] for i in 1:DD if t+DD-i+1 >= 1 && t+DD-i+1 <= T; init=0))

        eq_traj_lower_startup[sc.name, g.name, t] = @constraint(
            model,
            prod_above[sc.name, g.name, t] >= 
            start_power_above - g.min_power[t] * sum_z_DD
        )
        eq_traj_lower_shutdown[sc.name, g.name, t] = @constraint(
            model,
            prod_above[sc.name, g.name, t] >= 
            shut_power_above - g.min_power[t] * sum_y_UD
        )

        # [3] 
        eq_traj_upper_startup[sc.name, g.name, t] = @constraint(
            model,
            prod_above[sc.name, g.name, t] <= 
            start_power_above + (g.max_power[t] - g.min_power[t]) * (is_on[g.name, t] - sum_y_UD)
        )

        # [4]
        eq_traj_upper_shutdown[sc.name, g.name, t] = @constraint(
            model,
            prod_above[sc.name, g.name, t] <= 
            shut_power_above + (g.max_power[t] - g.min_power[t]) * (is_on[g.name, t] - sum_z_DD)
        )

        # [5,6]
        if t == 1
            eq_traj_ramp_up[sc.name, g.name, t] = @constraint(
                model,
                prod_above[sc.name, g.name, t] - (g.initial_power - g.min_power[t]) <= 
                g.max_power[t] * sum_y_UD + g.ramp_up_limit * (is_on[g.name, t] - sum_y_UD)
            )
            
            # z(k+i-1)
            sum_z_DD_prev = @expression(model, sum(switch_off[g.name, t+i-1] for i in 1:DD if t+i-1 <= T && t+i-1 >= 1; init=0))
            eq_traj_ramp_down[sc.name, g.name, t] = @constraint(
                model,
                (g.initial_power - g.min_power[t]) - prod_above[sc.name, g.name, t] <= 
                g.max_power[t] * sum_z_DD_prev + g.ramp_down_limit * (_is_initially_on(g) - sum_z_DD_prev)
            )
        else
            eq_traj_ramp_up[sc.name, g.name, t] = @constraint(
                model,
                prod_above[sc.name, g.name, t] - prod_above[sc.name, g.name, t-1] <= 
                g.max_power[t] * sum_y_UD + g.ramp_up_limit * (is_on[g.name, t] - sum_y_UD)
            )
            
            sum_z_DD_prev = @expression(model, sum(switch_off[g.name, t+i-1] for i in 1:DD if t+i-1 <= T && t+i-1 >= 1; init=0))
            eq_traj_ramp_down[sc.name, g.name, t] = @constraint(
                model,
                prod_above[sc.name, g.name, t-1] - prod_above[sc.name, g.name, t] <= 
                g.max_power[t] * sum_z_DD_prev + g.ramp_down_limit * (is_on[g.name, t-1] - sum_z_DD_prev)
            )
        end
    end
    return
end

function _add_min_uptime_downtime_eqs!(
    model::JuMP.Model,
    g::ThermalUnit,
)::Nothing
    is_on = model[:is_on]
    switch_off = model[:switch_off]
    switch_on = model[:switch_on]
    eq_min_uptime = _init(model, :eq_min_uptime)
    eq_min_downtime = _init(model, :eq_min_downtime)
    T = model[:instance].time
    for t in 1:T
        # Minimum up-time
        eq_min_uptime[g.name, t] = @constraint(
            model,
            sum(switch_on[g.name, i] for i in (t-g.min_uptime+1):t if i >= 1) <= is_on[g.name, t]
        )
        # Minimum down-time
        eq_min_downtime[g.name, t] = @constraint(
            model,
            sum(
                switch_off[g.name, i] for i in (t-g.min_downtime+1):t if i >= 1
            ) <= 1 - is_on[g.name, t]
        )
        # Minimum up/down-time for initial periods
        if t == 1
            if g.initial_status > 0
                eq_min_uptime[g.name, 0] = @constraint(
                    model,
                    sum(
                        switch_off[g.name, i] for
                        i in 1:(g.min_uptime-g.initial_status) if i <= T
                    ) == 0
                )
            else
                eq_min_downtime[g.name, 0] = @constraint(
                    model,
                    sum(
                        switch_on[g.name, i] for
                        i in 1:(g.min_downtime+g.initial_status) if i <= T
                    ) == 0
                )
            end
        end
    end
end

function _add_commitment_status_eqs!(model::JuMP.Model, g::ThermalUnit)::Nothing
    is_on = model[:is_on]
    T = model[:instance].time
    eq_commitment_status = _init(model, :eq_commitment_status)
    for t in 1:T
        if g.commitment_status[t] !== nothing
            eq_commitment_status[g.name, t] = @constraint(
                model,
                is_on[g.name, t] == (g.commitment_status[t] ? 1.0 : 0.0)
            )
        end
    end
    return
end

function _add_net_injection_eqs!(
    model::JuMP.Model,
    g::ThermalUnit,
    sc::UnitCommitmentScenario,
)::Nothing
    expr_net_injection = model[:expr_net_injection]
    for t in 1:model[:instance].time
        # Add to net injection expression
        add_to_expression!(
            expr_net_injection[sc.name, g.bus.name, t],
            model[:prod_above][sc.name, g.name, t],
            1.0,
        )
        add_to_expression!(
            expr_net_injection[sc.name, g.bus.name, t],
            model[:is_on][g.name, t],
            g.min_power[t],
        )
    end
end

function _total_reserves(model, g, sc)::Vector
    T = model[:instance].time
    reserve = [0.0 for _ in 1:T]
    spinning_reserves = [r for r in g.reserves if r.type == "spinning"]
    if !isempty(spinning_reserves)
        reserve += [
            sum(
                model[:reserve][sc.name, r.name, g.name, t] for
                r in spinning_reserves
            ) for t in 1:model[:instance].time
        ]
    end
    return reserve
end

function _actual_power(
    model::JuMP.Model,
    g::ThermalUnit,
    sc::UnitCommitmentScenario,
    t::Int,
)::AffExpr
    prod_above = model[:prod_above]
    is_on = model[:is_on]
    return prod_above[sc.name, g.name, t] + g.min_power[t] * is_on[g.name, t]
end

function _in_startup_process(
    model::JuMP.Model,
    g::ThermalUnit,
    t::Int,
)::AffExpr
    switch_on = model[:switch_on]
    UD = length(g.startup_curve)
    T = model[:instance].time
    return sum(
        switch_on[g.name, t - i + 1]
        for i in 1:UD if (t - i + 1) >= 1;
        init = AffExpr(0.0),
    )
end

function _in_shutdown_process(
    model::JuMP.Model,
    g::ThermalUnit,
    t::Int,
)::AffExpr
    switch_off = model[:switch_off]
    DD = length(g.shutdown_curve)
    T = model[:instance].time
    return sum(
        switch_off[g.name, t + i]
        for i in 1:DD if (t + i) <= T;
        init = AffExpr(0.0),
    )
end