""" convert_to_subhourly(instance, next_instance) Return a deep-copied instance with half the original time step and twice as many time periods. Call this before assigning startup/shutdown curves. """ function convert_to_subhourly( instance::UnitCommitmentInstance, next_instance::UnitCommitmentInstance, )::UnitCommitmentInstance modified = deepcopy(instance) convert_to_subhourly!(modified, next_instance) return modified end function convert_to_subhourly!( instance::UnitCommitmentInstance, next_instance::UnitCommitmentInstance, )::Nothing length(instance.scenarios) == length(next_instance.scenarios) || error("instance and next_instance must have the same number of scenarios") for (sc_current, sc_next) in zip(instance.scenarios, next_instance.scenarios) _validate_subhourly_inputs(sc_current, sc_next) _convert_scenario_to_subhourly!(sc_current, sc_next) end instance.time *= 2 return end function _validate_subhourly_inputs( sc_current::UnitCommitmentScenario, sc_next::UnitCommitmentScenario, )::Nothing sc_current.time == sc_next.time || error("current and next scenario must have the same time dimension") sc_current.time_step == sc_next.time_step || error("current and next scenario must have the same time_step") sc_current.time_step % 2 == 0 || error("time_step $(sc_current.time_step) must be even") length(sc_current.buses) == length(sc_next.buses) || error("bus counts do not match") length(sc_current.thermal_units) == length(sc_next.thermal_units) || error("thermal unit counts do not match") length(sc_current.lines) == length(sc_next.lines) || error("line counts do not match") length(sc_current.reserves) == length(sc_next.reserves) || error("reserve counts do not match") length(sc_current.price_sensitive_loads) == length(sc_next.price_sensitive_loads) || error("price-sensitive load counts do not match") length(sc_current.profiled_units) == length(sc_next.profiled_units) || error("profiled unit counts do not match") length(sc_current.storage_units) == length(sc_next.storage_units) || error("storage unit counts do not match") for g in sc_current.thermal_units isempty(g.startup_curve) && isempty(g.shutdown_curve) || error( "call convert_to_subhourly! before assigning startup/shutdown curves", ) end return end function _convert_scenario_to_subhourly!( sc_current::UnitCommitmentScenario, sc_next::UnitCommitmentScenario, )::Nothing original_time = sc_current.time for i in eachindex(sc_current.buses) sc_current.buses[i].load = interpolate_values( sc_current.buses[i].load, sc_next.buses[i].load[1], ) end for i in eachindex(sc_current.thermal_units) g = sc_current.thermal_units[i] g_next = sc_next.thermal_units[i] g.max_power = repeat_values(g.max_power) g.min_power = repeat_values(g.min_power) g.must_run = repeat_values(g.must_run) g.min_power_cost = repeat_values(g.min_power_cost) g.commitment_status = repeat_values(g.commitment_status) for j in eachindex(g.cost_segments) g.cost_segments[j].cost = interpolate_values( g.cost_segments[j].cost, g_next.cost_segments[j].cost[1], ) ./ 2.0 g.cost_segments[j].mw = interpolate_values( g.cost_segments[j].mw, g_next.cost_segments[j].mw[1], ) end g.ramp_up_limit /= 2.0 g.ramp_down_limit /= 2.0 g.min_uptime *= 2 g.min_downtime *= 2 for startup_cat in g.startup_categories startup_cat.delay *= 2 end if g.initial_status !== nothing g.initial_status *= 2 end end for i in eachindex(sc_current.lines) line = sc_current.lines[i] line.normal_flow_limit = repeat_values(line.normal_flow_limit) line.emergency_flow_limit = repeat_values(line.emergency_flow_limit) line.flow_limit_penalty = repeat_values(line.flow_limit_penalty) end for i in eachindex(sc_current.reserves) reserve = sc_current.reserves[i] reserve_next = sc_next.reserves[i] reserve.amount = interpolate_values(reserve.amount, reserve_next.amount[1]) end for i in eachindex(sc_current.price_sensitive_loads) ps = sc_current.price_sensitive_loads[i] ps_next = sc_next.price_sensitive_loads[i] ps.demand = interpolate_values(ps.demand, ps_next.demand[1]) ps.revenue = interpolate_values(ps.revenue, ps_next.revenue[1]) end for i in eachindex(sc_current.profiled_units) pu = sc_current.profiled_units[i] pu_next = sc_next.profiled_units[i] pu.min_power = interpolate_values(pu.min_power, pu_next.min_power[1]) pu.max_power = interpolate_values(pu.max_power, pu_next.max_power[1]) pu.cost = interpolate_values(pu.cost, pu_next.cost[1]) end for i in eachindex(sc_current.storage_units) su = sc_current.storage_units[i] su_next = sc_next.storage_units[i] su.min_level = interpolate_values(su.min_level, su_next.min_level[1]) su.max_level = interpolate_values(su.max_level, su_next.max_level[1]) su.simultaneous_charge_and_discharge = repeat_values(su.simultaneous_charge_and_discharge) su.charge_cost = interpolate_values(su.charge_cost, su_next.charge_cost[1]) su.discharge_cost = interpolate_values(su.discharge_cost, su_next.discharge_cost[1]) su.charge_efficiency = repeat_values(su.charge_efficiency) su.discharge_efficiency = repeat_values(su.discharge_efficiency) su.loss_factor = repeat_values(su.loss_factor) su.min_charge_rate = repeat_values(su.min_charge_rate) su.max_charge_rate = repeat_values(su.max_charge_rate) su.min_discharge_rate = repeat_values(su.min_discharge_rate) su.max_discharge_rate = repeat_values(su.max_discharge_rate) end sc_current.power_balance_penalty = repeat_values(sc_current.power_balance_penalty) sc_current.time = 2 * original_time sc_current.time_step รท= 2 return end function interpolate_values(values::Vector{T}, next_first::T) where {T<:Real} n = length(values) result = Vector{Float64}(undef, 2 * n) for i in 1:(n - 1) result[2 * i - 1] = values[i] result[2 * i] = (values[i] + values[i + 1]) / 2 end result[2 * n - 1] = values[n] result[2 * n] = (values[n] + next_first) / 2 return result end function repeat_values(values::Vector{T}) where {T} n = length(values) result = Vector{T}(undef, 2 * n) for i in 1:n result[2 * i - 1] = values[i] result[2 * i] = values[i] end return result end