Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/manual/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Similar to representative periods, each period has a [`multiple`](@ref) that is
sum(duration(t) * probability(t) * multiple(t) for t in scenarios)
```

!!! tip "Identification function"
You can always check if a given `TimeStructure` includes operational scenarios through the function [`has_opscenarios`](@ref).

## [Representative periods](@id man-oper-repr)

In some cases, a fine-scale representation of the operations of the infrastructure for the whole time horizon is not feasible. A possible strategy is then to select one or more representative periods and use them to evaluate operational cost and feasibility. The time structure [`RepresentativePeriods`](@ref) consists of an ordered sequence of representative periods that represents a longer period of time. Each representative period covers a specified share of the whole time period.
Expand Down Expand Up @@ -98,3 +101,6 @@ For each time period, the [`multiple`](@ref) function returns how many times the
```@repl rp
sum(duration(t) * multiple(t) for t in periods)
```

!!! tip "Identification function"
You can always check if a given `TimeStructure` includes representative periods through the function [`has_repr_periods`](@ref).
2 changes: 1 addition & 1 deletion docs/src/manual/discount.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This approach results in a slightly higher discount factor.

To help set up the objective function in a typical optimization problem, there is a utility function [`objective_weight`](@ref) that returns the weight to give a time period in the objective, considering both discount factor, probability, and possible multiplicity.

!!! note "`objective_weight` for `TwoLevelTree`
!!! note "`objective_weight` for `TwoLevelTree`"
You must be careful when you utilize the function [`objective_weight`](@ref) for a [`TimeStruct.StratNode`](@ref).
The function includes the multiplication with the branch probability.
This implies that you must divide the function by [`probability_branch`](@ref) if you have a variable that is indexed over strategic periods and already includes the branch probability.
Expand Down
7 changes: 7 additions & 0 deletions docs/src/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,10 @@ Discounter
discount
objective_weight
```

## [Identification methods](@id api-ident)

```@docs
has_opscenarios
has_repr_periods
```
1 change: 1 addition & 0 deletions docs/src/reference/internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
TimeStruct.TimeStructurePeriod
TimeStruct.TimeStructInnerIter
TimeStruct.TimeStructOuterIter
TimeStruct.PeriodPartition
```

## [Strategic period types ([`TwoLevelTree`](@ref))](@id int-types-twoleveltree)
Expand Down
2 changes: 2 additions & 0 deletions src/TimeStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export start_time, end_time, remaining
export start_oper_time, end_oper_time
export n_strat_per, n_children, n_leaves, n_branches

export has_opscenarios, has_repr_periods

export Discounter
export discount
export objective_weight
Expand Down
1 change: 0 additions & 1 deletion src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ Supertype for individual partitions for operational time periods. Subtypes must
for all potential time structures to be able to identify the respective
[`TimeStructurePeriod`](@ref).
"""

abstract type PeriodPartition{T<:TimePeriod} end
function PeriodPartition(itr, part, chunk)
return throw(
Expand Down
49 changes: 49 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,52 @@ function Base.iterate(ts::TreeStructure, state = nothing)

return TreePeriod(ts, next[1]), next[2]
end

"""
has_opscenarios(ts::TimeStructure)
has_opscenarios(ts::TimeStructurePeriod)

Returns `true` if any operational structure within the `TimeStructure` includes
`OperationalScenarios`. Specific methods for `TimeStructurePeriod`s are also included

An [`AbstractOperationalScenario`](@ref) corresponds to an individual scenario, and hence,
cannot contain `OperationalScenarios`.
"""
has_opscenarios(ts::TwoLevelTree) = any(has_opscenarios, ts.nodes)
has_opscenarios(ts::TwoLevel) = any(has_opscenarios, ts.operational)
has_opscenarios(ts::RepresentativePeriods) = any(has_opscenarios, ts.rep_periods)
has_opscenarios(::OperationalScenarios) = true
has_opscenarios(::SimpleTimes) = false
has_opscenarios(::CalendarTimes) = false

has_opscenarios(ts::StrategicScenario) = any(has_opscenarios, ts.nodes)
has_opscenarios(ts::SingleStrategicScenario) = has_opscenarios(ts.ts)
has_opscenarios(ts::AbstractStrategicPeriod) = has_opscenarios(ts.operational)
has_opscenarios(ts::SingleStrategicPeriod) = has_opscenarios(ts.ts)
has_opscenarios(ts::AbstractRepresentativePeriod) = has_opscenarios(ts.operational)
has_opscenarios(ts::SingleReprPeriod) = has_opscenarios(ts.ts)
has_opscenarios(::AbstractOperationalScenario) = false

"""
has_repr_periods(ts::TimeStructure)
has_repr_periods(ts::TimeStructurePeriod)

Returns `true` if any operational structure within the `TimeStructure` includes
`RepresentativePeriods`. Specific methods for `TimeStructurePeriod`s are also included

An [`AbstractRepresentativePeriod`](@ref) corresponds to an individual representative period,
and hence, cannot contain `RepresentativePeriods`.
"""
has_repr_periods(ts::TwoLevelTree) = any(has_repr_periods, ts.nodes)
has_repr_periods(ts::TwoLevel) = any(has_repr_periods, ts.operational)
has_repr_periods(::RepresentativePeriods) = true
has_repr_periods(::OperationalScenarios) = false
has_repr_periods(::SimpleTimes) = false
has_repr_periods(::CalendarTimes) = false

has_repr_periods(ts::StrategicScenario) = any(has_repr_periods, ts.nodes)
has_repr_periods(ts::SingleStrategicScenario) = has_repr_periods(ts.ts)
has_repr_periods(ts::AbstractStrategicPeriod) = has_repr_periods(ts.operational)
has_repr_periods(ts::SingleStrategicPeriod) = has_repr_periods(ts.ts)
has_repr_periods(ts::AbstractRepresentativePeriod) = false
has_repr_periods(::AbstractOperationalScenario) = false
63 changes: 63 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2776,5 +2776,68 @@ end
@test hash(_NamedPeriod("a", 1)) == hash(_NamedPeriod("a", 1))
end

@testitem "Identification of operational structures" begin
using Dates

day = SimpleTimes(24, 1)
cal = CalendarTimes(DateTime(2024, 1, 1), 24, Hour(1))
osc = OperationalScenarios(2, day)
rps = RepresentativePeriods(2, 8760, [0.5, 0.5], [day, day])
rps_osc = RepresentativePeriods(2, 8760, [0.5, 0.5], [day, osc])

# Operational structures
for (ts, has_osc, has_rp) in [
(day, false, false),
(cal, false, false),
(osc, true, false),
(rps, false, true),
(rps_osc, true, true),
]
@test has_opscenarios(ts) == has_osc
@test has_repr_periods(ts) == has_rp
end

# Strategic structures, in which only the last strategic period/node includes the
# operational scenarios and representative periods
two_level = TwoLevel([day, rps_osc])
tree = TwoLevelTree(TreeNode(1, day, [TreeNode(1, day), TreeNode(1, rps_osc)]))
for (ts, sps) in [(two_level, strat_periods(two_level)), (tree, strat_nodes(tree))]
@test has_opscenarios(ts) && has_repr_periods(ts)
sp_1, sp_2 = first(sps), last(sps)
@test !has_opscenarios(sp_1) && !has_repr_periods(sp_1)
@test has_opscenarios(sp_2) && has_repr_periods(sp_2)

# Representative periods and operational scenarios of a strategic period
rp_1, rp_2 = repr_periods(sp_2)
@test !has_opscenarios(rp_1) && has_opscenarios(rp_2)
@test !has_repr_periods(rp_2)
sc = first(opscenarios(rp_2))
@test !has_opscenarios(sc) && !has_repr_periods(sc)
end
for ts in [TwoLevel(2, 1, day), TwoLevelTree(1, [2], day)]
@test !has_opscenarios(ts) && !has_repr_periods(ts)
end

# Strategic scenarios, in which only the second scenario of the tree includes the
# operational scenarios and representative periods
scen_1, scen_2 = strategic_scenarios(tree)
@test !has_opscenarios(scen_1) && !has_repr_periods(scen_1)
@test has_opscenarios(scen_2) && has_repr_periods(scen_2)
scen = first(strategic_scenarios(two_level))
@test has_opscenarios(scen) && has_repr_periods(scen)
scen = first(strategic_scenarios(day))
@test !has_opscenarios(scen) && !has_repr_periods(scen)

# Periods of operational structures and single periods
rp_1, rp_2 = repr_periods(rps_osc)
@test !has_opscenarios(rp_1) && has_opscenarios(rp_2)
sc = first(opscenarios(osc))
@test !has_opscenarios(sc) && !has_repr_periods(sc)
sp = first(strat_periods(osc))
@test has_opscenarios(sp) && !has_repr_periods(sp)
rp = first(repr_periods(osc))
@test has_opscenarios(rp) && !has_repr_periods(rp)
end

@run_package_tests
Aqua.test_all(TimeStruct; ambiguities = false, unbound_args = false)
Loading