From f6851c4a793b4f440938fc46eea1aa538111f33c Mon Sep 17 00:00:00 2001 From: Truls Flatberg Date: Mon, 17 Nov 2025 09:35:14 +0100 Subject: [PATCH 1/4] Cache total_duration for OperationalScenarios and SimpleTimes structs --- src/op_scenarios/core_types.jl | 5 +++-- src/simple.jl | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/op_scenarios/core_types.jl b/src/op_scenarios/core_types.jl index 64f7573..7bf51c9 100644 --- a/src/op_scenarios/core_types.jl +++ b/src/op_scenarios/core_types.jl @@ -36,6 +36,7 @@ struct OperationalScenarios{T,OP<:TimeStructure{T}} <: TimeStructure{T} len::Int scenarios::Vector{OP} probability::Vector{Float64} + total_duration::T function OperationalScenarios( len::Integer, scenarios::Vector{OP}, @@ -59,7 +60,7 @@ struct OperationalScenarios{T,OP<:TimeStructure{T}} <: TimeStructure{T} "This can lead to unexpected behavior." ) end - return new{T,OP}(len, scenarios, convert(Vector{Float64}, probability)) + return new{T,OP}(len, scenarios, convert(Vector{Float64}, probability), maximum(_total_duration(osc) for osc in scenarios)) end end function OperationalScenarios(len::Integer, oper::TimeStructure{T}) where {T} @@ -73,7 +74,7 @@ function OperationalScenarios(oper::Vector{<:TimeStructure{T}}) where {T} end function _total_duration(oscs::OperationalScenarios) - return maximum(_total_duration(osc) for osc in oscs.scenarios) + return oscs.total_duration end function _multiple_adj(oscs::OperationalScenarios, scen) diff --git a/src/simple.jl b/src/simple.jl index 9db9b98..90088c9 100644 --- a/src/simple.jl +++ b/src/simple.jl @@ -19,6 +19,7 @@ varying = SimpleTimes([2, 2, 2, 4, 10]) # 5 periods of varying length struct SimpleTimes{T} <: TimeStructure{T} len::Int duration::Vector{T} + total_duration::T function SimpleTimes(len::Integer, duration::Vector{T}) where {T<:Duration} if len > length(duration) throw( @@ -27,7 +28,7 @@ struct SimpleTimes{T} <: TimeStructure{T} ), ) else - new{T}(len, duration) + new{T}(len, duration, sum(duration)) end end end @@ -36,7 +37,7 @@ function SimpleTimes(len::Integer, duration::Duration) end SimpleTimes(dur::Vector{T}) where {T<:Duration} = SimpleTimes(length(dur), dur) -_total_duration(st::SimpleTimes) = sum(st.duration) +_total_duration(st::SimpleTimes) = st.total_duration # Add basic functions of iterators Base.length(st::SimpleTimes) = st.len From 91ca5bee3ba680493ad51fac8193631a7dbf0d1f Mon Sep 17 00:00:00 2001 From: Truls Flatberg Date: Tue, 18 Nov 2025 09:49:14 +0100 Subject: [PATCH 2/4] Cache total_duration for CalendarTimes --- src/calendar.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/calendar.jl b/src/calendar.jl index c142148..d10c95b 100644 --- a/src/calendar.jl +++ b/src/calendar.jl @@ -21,6 +21,16 @@ struct CalendarTimes{T<:Union{Dates.DateTime,TimeZones.ZonedDateTime}} <: start_date::T length::Int period::Dates.Period + total_duration::Float64 + function CalendarTimes( + start_date::T, + length::Integer, + period::Dates.Period, + ) where {T<:Union{Dates.DateTime,TimeZones.ZonedDateTime}} + end_date = start_date + length * period + total_duration = Dates.value(Dates.Hour(end_date - start_date)) + new{T}(start_date, length, period, total_duration) + end end function CalendarTimes( @@ -61,7 +71,7 @@ function CalendarTimes( return CalendarTimes(first, length, period) end -_total_duration(ts::CalendarTimes) = sum(duration(t) for t in ts) +_total_duration(ts::CalendarTimes) = ts.total_duration # Add basic functions of iterators Base.length(ts::CalendarTimes) = ts.length From 612c8c852fd3a03f8566c1f9a18256ccb20ee858 Mon Sep 17 00:00:00 2001 From: Truls Flatberg Date: Tue, 18 Nov 2025 10:12:15 +0100 Subject: [PATCH 3/4] Update workflows and change to JuliaFormatter v2 --- .github/workflows/ci.yml | 2 +- .github/workflows/documentation.yml | 2 +- .github/workflows/format_check.yml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d239493..6f414bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,6 @@ jobs: with: depwarn: error - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v3 + - uses: codecov/codecov-action@v4 with: file: lcov.info \ No newline at end of file diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index 76aff3f..31c3146 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -9,7 +9,7 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@latest with: version: '1' diff --git a/.github/workflows/format_check.yml b/.github/workflows/format_check.yml index 50bc51a..3e0d84f 100644 --- a/.github/workflows/format_check.yml +++ b/.github/workflows/format_check.yml @@ -13,13 +13,13 @@ jobs: - uses: julia-actions/setup-julia@latest with: version: '1' - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Format check shell: julia --color=yes {0} run: | using Pkg # If you update the version, also update the style guide docs. - Pkg.add(PackageSpec(name="JuliaFormatter", version="1")) + Pkg.add(PackageSpec(name="JuliaFormatter", version="2")) using JuliaFormatter format("src", verbose=true) format("test", verbose=true) From d5de5261dddb59fe6f3bdf6aa0fe2e5f857d830f Mon Sep 17 00:00:00 2001 From: Truls Flatberg Date: Tue, 18 Nov 2025 10:12:27 +0100 Subject: [PATCH 4/4] Format fixes --- src/calendar.jl | 2 +- src/op_scenarios/core_types.jl | 7 ++++++- src/strat_scenarios/strat_scenarios.jl | 8 +++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/calendar.jl b/src/calendar.jl index d10c95b..14cf5c3 100644 --- a/src/calendar.jl +++ b/src/calendar.jl @@ -29,7 +29,7 @@ struct CalendarTimes{T<:Union{Dates.DateTime,TimeZones.ZonedDateTime}} <: ) where {T<:Union{Dates.DateTime,TimeZones.ZonedDateTime}} end_date = start_date + length * period total_duration = Dates.value(Dates.Hour(end_date - start_date)) - new{T}(start_date, length, period, total_duration) + return new{T}(start_date, length, period, total_duration) end end diff --git a/src/op_scenarios/core_types.jl b/src/op_scenarios/core_types.jl index 7bf51c9..be81f37 100644 --- a/src/op_scenarios/core_types.jl +++ b/src/op_scenarios/core_types.jl @@ -60,7 +60,12 @@ struct OperationalScenarios{T,OP<:TimeStructure{T}} <: TimeStructure{T} "This can lead to unexpected behavior." ) end - return new{T,OP}(len, scenarios, convert(Vector{Float64}, probability), maximum(_total_duration(osc) for osc in scenarios)) + return new{T,OP}( + len, + scenarios, + convert(Vector{Float64}, probability), + maximum(_total_duration(osc) for osc in scenarios), + ) end end function OperationalScenarios(len::Integer, oper::TimeStructure{T}) where {T} diff --git a/src/strat_scenarios/strat_scenarios.jl b/src/strat_scenarios/strat_scenarios.jl index a298424..4d36b2f 100644 --- a/src/strat_scenarios/strat_scenarios.jl +++ b/src/strat_scenarios/strat_scenarios.jl @@ -115,9 +115,11 @@ When the `TimeStructure` is a [`StrategicScenario`](@ref), `strat_periods` retur These are equivalent to a [`StrategicPeriod`](@ref) of a [`TwoLevel`](@ref) time structure. """ -strat_periods(ts::StrategicScenario) = StratTreeNodes( - TwoLevelTree(length(ts), first(ts), [n for n in ts.nodes], ts.op_per_strat), -) +function strat_periods(ts::StrategicScenario) + return StratTreeNodes( + TwoLevelTree(length(ts), first(ts), [n for n in ts.nodes], ts.op_per_strat), + ) +end """ struct StratScens{S,T,OP<:AbstractTreeNode{S,T}} <: AbstractStratScens{T}