Skip to content

Commit 930fc4f

Browse files
authored
Added partition duration for time structures (#95)
* Support for the following time structures * OperationalScenarios * RepresentativePeriods * RepresentativePeriods{OperationalScenarios} * TwoLevel{SimpleTimes} * TwoLevel{RepresentativePeriods} * TwoLevel{OperationalScenarios} * TwoLevel{RepresentativePeriods{OperationalScenarios}} * Time profile support for indexing via partitions * Included new profile type * Existing profiles can support `PeriodPartition` types
1 parent 2f7703a commit 930fc4f

8 files changed

Lines changed: 707 additions & 5 deletions

File tree

docs/src/reference/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ withprev
5959
withnext
6060
chunk
6161
chunk_duration
62+
partition_duration
6263
```
6364

6465
## [Properties of time periods](@id api-prop_per)
@@ -78,6 +79,7 @@ end_oper_time
7879
```@docs
7980
FixedProfile
8081
OperationalProfile
82+
PartitionProfile
8183
RepresentativeProfile
8284
ScenarioProfile
8385
StrategicProfile

src/TimeStruct.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ include("utils.jl")
2727
include("discount.jl")
2828
include("profiles.jl")
2929

30+
include("partitions/strat_periods.jl")
31+
include("partitions/rep_periods.jl")
32+
include("partitions/opscenarios.jl")
33+
3034
export TimeStructure
3135
export SimpleTimes
3236
export CalendarTimes
@@ -40,6 +44,7 @@ export TreeNode
4044
export TimeProfile
4145
export FixedProfile
4246
export OperationalProfile
47+
export PartitionProfile
4348
export ScenarioProfile
4449
export StrategicProfile
4550
export StrategicStochasticProfile
@@ -49,7 +54,7 @@ export opscenarios
4954
export repr_periods
5055
export strat_periods, strategic_periods
5156
export regular_tree, strat_nodes, strategic_scenarios
52-
export withprev, withnext, chunk, chunk_duration
57+
export withprev, withnext, chunk, chunk_duration, partition_duration
5358
export isfirst, duration, duration_strat, multiple
5459
export probability, probability_branch, probability_scen
5560
export multiple_strat

src/partitions/opscenarios.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Add generic partition duration type
2+
abstract type AbstractOpScenPart{T} <: PeriodPartition{T} end
3+
4+
ScenarioIndexable(::Type{<:AbstractOpScenPart}) = HasScenarioIndex()
5+
_opscen(pd::AbstractOpScenPart) = pd.scen
6+
7+
# Add partition type with constructor for indexing when operational scenarios are present
8+
struct OpScenPart{N,T} <: AbstractOpScenPart{T}
9+
scen::Int
10+
part::Int
11+
chunk::NTuple{N,T}
12+
end
13+
PeriodPartition(itr::OperationalScenario, part, chunk) = OpScenPart(itr.scen, part, chunk)
14+
eltype(::Type{PartitionDurationIterator{I}}) where {I<:OperationalScenario} = OpScenPart
15+
16+
Base.show(io::IO, pd::OpScenPart) = print(io, "sc$(pd.scen)-part$(pd.part)")
17+
18+
# Add function for generation of partitions from higher level
19+
function partition_duration(ts::OperationalScenarios, dur)
20+
return collect(
21+
Iterators.flatten(partition_duration(osc, dur) for osc in opscenarios(ts)),
22+
)
23+
end

src/partitions/rep_periods.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Add generic partition duration type
2+
abstract type AbstractReprPart{T} <: PeriodPartition{T} end
3+
4+
RepresentativeIndexable(::Type{<:AbstractReprPart}) = HasReprIndex()
5+
_rper(pd::AbstractReprPart) = pd.rp
6+
7+
# Add partition type with constructor for indexing when representative periods are present
8+
struct ReprPart{N,T} <: AbstractReprPart{T}
9+
rp::Int
10+
part::Int
11+
chunk::NTuple{N,T}
12+
end
13+
PeriodPartition(itr::RepresentativePeriod, part, chunk) = ReprPart(itr.rp, part, chunk)
14+
eltype(::Type{PartitionDurationIterator{I}}) where {I<:RepresentativePeriod} = ReprPart
15+
16+
Base.show(io::IO, pd::ReprPart) = print(io, "rp$(pd.rp)-part$(pd.part)")
17+
18+
# Add partition type with constructor for indexing when representative periods and operational
19+
# scenarios are present
20+
struct ReprOpScenPart{N,T} <: AbstractReprPart{T}
21+
rp::Int
22+
scen::Int
23+
part::Int
24+
chunk::NTuple{N,T}
25+
end
26+
function PeriodPartition(itr::ReprOpScenario, part, chunk)
27+
return ReprOpScenPart(itr.rp, itr.scen, part, chunk)
28+
end
29+
eltype(::Type{PartitionDurationIterator{I}}) where {I<:ReprOpScenario} = ReprOpScenPart
30+
31+
Base.show(io::IO, pd::ReprOpScenPart) = print(io, "rp$(pd.rp)-sc$(pd.scen)-part$(pd.part)")
32+
ScenarioIndexable(::Type{<:ReprOpScenPart}) = HasScenarioIndex()
33+
_opscen(pd::ReprOpScenPart) = pd.scen
34+
35+
# Add function for generation of partitions from higher level
36+
function partition_duration(ts::RepresentativePeriods, dur)
37+
return collect(
38+
Iterators.flatten(partition_duration(rp, dur) for rp in repr_periods(ts)),
39+
)
40+
end
41+
function partition_duration(
42+
ts::RepresentativePeriod{T,OP},
43+
dur,
44+
) where {T,OP<:OperationalScenarios}
45+
return collect(
46+
Iterators.flatten(partition_duration(osc, dur) for osc in opscenarios(ts)),
47+
)
48+
end

src/partitions/strat_periods.jl

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Add generic partition duration type
2+
abstract type AbstractStratPart{T} <: PeriodPartition{T} end
3+
4+
StrategicIndexable(::Type{<:AbstractStratPart}) = HasStratIndex()
5+
_strat_per(pd::AbstractStratPart) = pd.sp
6+
7+
# Add partition type with constructor for indexing when strategic periods are present
8+
struct StratPart{N,T} <: AbstractStratPart{T}
9+
sp::Int
10+
part::Int
11+
chunk::NTuple{N,T}
12+
end
13+
PeriodPartition(itr::StrategicPeriod, part, chunk) = StratPart(itr.sp, part, chunk)
14+
eltype(::Type{PartitionDurationIterator{I}}) where {I<:StrategicPeriod} = StratPart
15+
16+
Base.show(io::IO, pd::StratPart) = print(io, "sp$(pd.sp)-part$(pd.part)")
17+
18+
# Add partition type with constructor for indexing when strategic and representative periods
19+
# are present
20+
struct StratReprPart{N,T} <: AbstractStratPart{T}
21+
sp::Int
22+
rp::Int
23+
part::Int
24+
chunk::NTuple{N,T}
25+
end
26+
function PeriodPartition(itr::StratReprPeriod, part, chunk)
27+
return StratReprPart(itr.sp, itr.rp, part, chunk)
28+
end
29+
eltype(::Type{PartitionDurationIterator{I}}) where {I<:StratReprPeriod} = StratReprPart
30+
31+
Base.show(io::IO, pd::StratReprPart) = print(io, "sp$(pd.sp)-rp$(pd.rp)-part$(pd.part)")
32+
RepresentativeIndexable(::Type{<:StratReprPart}) = HasReprIndex()
33+
_rper(pd::StratReprPart) = pd.rp
34+
35+
# Add partition type with constructor for indexing when strategic periods and operational
36+
# scenarios are present
37+
struct StratOpScenPart{N,T} <: AbstractStratPart{T}
38+
sp::Int
39+
scen::Int
40+
part::Int
41+
chunk::NTuple{N,T}
42+
end
43+
function PeriodPartition(itr::StratOpScenario, part, chunk)
44+
return StratOpScenPart(itr.sp, itr.scen, part, chunk)
45+
end
46+
eltype(::Type{PartitionDurationIterator{I}}) where {I<:StratOpScenario} = StratOpScenPart
47+
48+
function Base.show(io::IO, pd::StratOpScenPart)
49+
return print(io, "sp$(pd.sp)-sc$(pd.scen)-part$(pd.part)")
50+
end
51+
ScenarioIndexable(::Type{<:StratOpScenPart}) = HasScenarioIndex()
52+
_opscen(pd::StratOpScenPart) = pd.scen
53+
54+
# Add partition type with constructor for indexing when strategic periods, representative
55+
# periods and operational scenarios are present
56+
struct StratReprOpScenPart{N,T} <: AbstractStratPart{T}
57+
sp::Int
58+
rp::Int
59+
scen::Int
60+
part::Int
61+
chunk::NTuple{N,T}
62+
end
63+
function PeriodPartition(itr::StratReprOpScenario, part, chunk)
64+
return StratReprOpScenPart(itr.sp, itr.rp, itr.scen, part, chunk)
65+
end
66+
function eltype(::Type{PartitionDurationIterator{I}}) where {I<:StratReprOpScenario}
67+
return StratReprOpScenPart
68+
end
69+
70+
function Base.show(io::IO, pd::StratReprOpScenPart)
71+
return print(io, "sp$(pd.sp)-rp$(pd.rp)-sc$(pd.scen)-part$(pd.part)")
72+
end
73+
RepresentativeIndexable(::Type{<:StratReprOpScenPart}) = HasReprIndex()
74+
ScenarioIndexable(::Type{<:StratReprOpScenPart}) = HasScenarioIndex()
75+
_rper(pd::StratReprOpScenPart) = pd.rp
76+
_opscen(pd::StratReprOpScenPart) = pd.scen
77+
78+
# Add function for generation of partitions from higher level
79+
function partition_duration(ts::TwoLevel, dur)
80+
return collect(
81+
Iterators.flatten(partition_duration(sp, dur) for sp in strategic_periods(ts)),
82+
)
83+
end
84+
function partition_duration(
85+
ts::StrategicPeriod{S,T,OP},
86+
dur,
87+
) where {S,T,OP<:RepresentativePeriods}
88+
return collect(
89+
Iterators.flatten(partition_duration(rp, dur) for rp in repr_periods(ts)),
90+
)
91+
end
92+
function partition_duration(
93+
ts::StrategicPeriod{S,T,OP},
94+
dur,
95+
) where {S,T,OP<:OperationalScenarios}
96+
return collect(
97+
Iterators.flatten(partition_duration(osc, dur) for osc in opscenarios(ts)),
98+
)
99+
end
100+
function partition_duration(
101+
ts::StratReprPeriod{T,RepresentativePeriod{T,OP}},
102+
dur,
103+
) where {T,OP<:OperationalScenarios}
104+
return collect(
105+
Iterators.flatten(partition_duration(osc, dur) for osc in opscenarios(ts)),
106+
)
107+
end

0 commit comments

Comments
 (0)