From f373a0726240c59dc1269b1af1ce4da29d245f9c Mon Sep 17 00:00:00 2001 From: Julian Straus Date: Fri, 16 Oct 2026 13:58:24 +0200 Subject: [PATCH 1/2] Included identification methods for - OperationalScenarios and - RepresentativePeriods --- docs/src/reference/api.md | 7 +++++ src/TimeStruct.jl | 2 ++ src/utils.jl | 49 ++++++++++++++++++++++++++++++ test/runtests.jl | 63 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) diff --git a/docs/src/reference/api.md b/docs/src/reference/api.md index c8527ec..0141db9 100644 --- a/docs/src/reference/api.md +++ b/docs/src/reference/api.md @@ -100,3 +100,10 @@ Discounter discount objective_weight ``` + +## [Identification methods](@id api-ident) + +```@docs +has_opscenarios +has_repr_periods +``` diff --git a/src/TimeStruct.jl b/src/TimeStruct.jl index 2e8e18b..7fb586c 100644 --- a/src/TimeStruct.jl +++ b/src/TimeStruct.jl @@ -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 diff --git a/src/utils.jl b/src/utils.jl index 4620ef1..9faa968 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index bf14240..9812592 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) From fb2f7cb673e53acd0d8fd53e5516383dd6fe23b8 Mon Sep 17 00:00:00 2001 From: Julian Straus Date: Fri, 16 Oct 2026 13:59:02 +0200 Subject: [PATCH 2/2] Updated documentation --- docs/src/manual/basic.md | 6 ++++++ docs/src/manual/discount.md | 2 +- docs/src/reference/internal.md | 1 + src/structures.jl | 1 - 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/src/manual/basic.md b/docs/src/manual/basic.md index bbe12bb..a894fa7 100644 --- a/docs/src/manual/basic.md +++ b/docs/src/manual/basic.md @@ -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. @@ -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). diff --git a/docs/src/manual/discount.md b/docs/src/manual/discount.md index fe2d4cb..d2f3e6a 100644 --- a/docs/src/manual/discount.md +++ b/docs/src/manual/discount.md @@ -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. diff --git a/docs/src/reference/internal.md b/docs/src/reference/internal.md index 733903b..cafef80 100644 --- a/docs/src/reference/internal.md +++ b/docs/src/reference/internal.md @@ -6,6 +6,7 @@ TimeStruct.TimeStructurePeriod TimeStruct.TimeStructInnerIter TimeStruct.TimeStructOuterIter +TimeStruct.PeriodPartition ``` ## [Strategic period types ([`TwoLevelTree`](@ref))](@id int-types-twoleveltree) diff --git a/src/structures.jl b/src/structures.jl index 77e0bb9..e18df2d 100644 --- a/src/structures.jl +++ b/src/structures.jl @@ -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(