Skip to content

Commit b20b272

Browse files
authored
Non regular TwoLevelTree and StrategicScenarioss (#90)
* Added fix for non-regular `TwoLevelTree` with `StrategicScenarios` * Fixed problem with naming a variable repr in testset * Removed unbound testing in Aqua
1 parent 0d75602 commit b20b272

4 files changed

Lines changed: 62 additions & 28 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TimeStruct"
22
uuid = "f9ed5ce0-9f41-4eaa-96da-f38ab8df101c"
33
authors = ["Lars Hellemo <Lars.Hellemo@sintef.no>, Truls.Flatberg <Truls.Flatberg@sintef.no>"]
4-
version = "0.9.8"
4+
version = "0.9.9"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/strat_scenarios/core_types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ n_children(n::StratNode, ts::TwoLevelTree) = count(c -> _parent(c) == n, strat_n
100100
"""
101101
n_leaves(ts::TwoLevelTree)
102102
103-
Returns the number of children of a [`TwoLevelTree`](@ref).
103+
Returns the number of leaves of a [`TwoLevelTree`](@ref).
104104
"""
105105
n_leaves(ts::TwoLevelTree) = count(n -> n_children(n, ts) == 0, strat_nodes(ts))
106106

src/strat_scenarios/strat_scenarios.jl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ decomposition algorithm.
8686
struct StrategicScenario{S,T,N,OP<:AbstractTreeNode{S,T}} <: AbstractStrategicScenario{T}
8787
scen::Int64
8888
probability::Float64
89-
nodes::NTuple{N,<:OP}
89+
nodes::NTuple{N,OP}
9090
op_per_strat::Float64
9191
end
92-
9392
Base.show(io::IO, scen::StrategicScenario) = print(io, "scen$(scen.scen)")
9493

9594
# Add basic functions of iterators
@@ -162,20 +161,16 @@ iterator `StratScens`.
162161
strategic_scenarios(ts::TwoLevelTree) = StratScens(ts)
163162

164163
# Provide a constructor to simplify the design
165-
function StrategicScenario(
166-
scs::StratScens{S,T,OP},
167-
scen::Int,
168-
) where {S,T,OP<:TimeStructure{T}}
164+
function StrategicScenario(scs::StratScens{S,T,OP}, scen::Int) where {S,T,OP}
169165
node = get_leaf(scs.ts, scen)
170166
prob = probability_branch(node)
171-
n_strat_per = _strat_per(node)
172-
nodes = Vector{OP}(undef, n_strat_per)
173-
for sp in n_strat_per:-1:1
167+
N = _strat_per(node)
168+
nodes = Vector{OP}(undef, N)
169+
for sp in N:-1:1
174170
nodes[sp] = node
175171
node = _parent(node)
176172
end
177-
178-
return StrategicScenario(scen, prob, Tuple(nodes), scs.ts.op_per_strat)
173+
return StrategicScenario{S,T,N,OP}(scen, prob, Tuple(nodes), scs.ts.op_per_strat)
179174
end
180175

181176
# Add basic functions of iterators

test/runtests.jl

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,12 @@ end
665665
test_invariants(TwoLevel(5, 10, SimpleTimes(10, 1)))
666666
test_invariants(TwoLevel(5, 30, opscen))
667667

668-
repr = RepresentativePeriods(2, 20, [0.2, 0.8], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
669-
two_level = TwoLevel(100, [repr, repr, repr]; op_per_strat = 1.0)
668+
rpers = RepresentativePeriods(2, 20, [0.2, 0.8], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
669+
two_level = TwoLevel(100, [rpers, rpers, rpers]; op_per_strat = 1.0)
670670
test_invariants(two_level)
671671

672-
repr = RepresentativePeriods(2, 20, [0.2, 0.8], [opscen, opscen])
673-
two_level = TwoLevel(100, [repr, repr]; op_per_strat = 1.0)
672+
rpers = RepresentativePeriods(2, 20, [0.2, 0.8], [opscen, opscen])
673+
two_level = TwoLevel(100, [rpers, rpers]; op_per_strat = 1.0)
674674
test_invariants(two_level)
675675
end
676676

@@ -1275,6 +1275,45 @@ end
12751275
@test repr(sc) == "scen$(k)"
12761276
@test eltype(typeof(sc)) == eltype(typeof(regtree))
12771277
end
1278+
1279+
# Test that non regular structures are working
1280+
day = SimpleTimes(24, 1)
1281+
week = SimpleTimes(168, 1)
1282+
scen = OperationalScenarios(2, [day, week], [7, 1]/8)
1283+
rpers = RepresentativePeriods(4, 8760, scen)
1284+
1285+
ts = TwoLevelTree(
1286+
TreeNode(
1287+
5,
1288+
rpers,
1289+
[0.7, 0.05, 0.1, 0.15],
1290+
[
1291+
TreeNode(5, rpers, TreeNode(5, rpers, TreeNode(5, week))),
1292+
TreeNode(2, week, TreeNode(8, week, TreeNode(5, week))),
1293+
TreeNode(
1294+
4,
1295+
scen,
1296+
[
1297+
TreeNode(6, week, TreeNode(5, week)),
1298+
TreeNode(6, week, 2, TreeNode(5, week)),
1299+
],
1300+
),
1301+
TreeNode(5, rpers, 2, TreeNode(8, rpers, TreeNode(5, week))),
1302+
],
1303+
),
1304+
;
1305+
op_per_strat = 8760.0,
1306+
)
1307+
1308+
# Test that the strategic periods are correct
1309+
sps = strat_periods(ts)
1310+
scens = strategic_scenarios(ts)
1311+
sps_scens = strat_periods(scens)
1312+
sps_scen_1 = strat_periods(first(scens))
1313+
@test sps == sps_scens
1314+
@test all(sps1 === sps2 for (sps1, sps2) in zip(sps_scen_1, collect(sps)[1:3]))
1315+
@test last(sps_scen_1) == collect(sps)[4]
1316+
@test isa(sps_scen_1, TS.ScenTreeNodes)
12781317
end
12791318

12801319
@testitem "Strategic scenarios with TwoLevel" begin
@@ -1336,8 +1375,8 @@ end
13361375
vals = collect(profile[sp] for sp in strat_periods(ts))
13371376
@test vals == [1, 2, 3]
13381377

1339-
repr = RepresentativePeriods(2, 5, [0.6, 0.4], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
1340-
ts = TwoLevel(3, 5, repr)
1378+
rpers = RepresentativePeriods(2, 5, [0.6, 0.4], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
1379+
ts = TwoLevel(3, 5, rpers)
13411380

13421381
vals = collect(profile[sp] for sp in strat_periods(ts))
13431382
@test vals == [1, 2, 3]
@@ -1355,8 +1394,8 @@ end
13551394
vals = collect(profile[rp] for rp in repr_periods(ts))
13561395
@test vals == [1, 1, 1]
13571396

1358-
repr = RepresentativePeriods(2, 5, [0.6, 0.4], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
1359-
ts = TwoLevel(3, 5, repr)
1397+
rpers = RepresentativePeriods(2, 5, [0.6, 0.4], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
1398+
ts = TwoLevel(3, 5, rpers)
13601399

13611400
vals = collect(profile[rp] for rp in repr_periods(ts))
13621401
@test vals == [1, 2, 1, 2, 1, 2]
@@ -1382,8 +1421,8 @@ end
13821421
@test vals == [1, 1, 1]
13831422

13841423
oscen = OperationalScenarios([SimpleTimes(5, 1), SimpleTimes(5, 1)])
1385-
repr = RepresentativePeriods(2, 5, [0.6, 0.4], [oscen, oscen])
1386-
ts = TwoLevel(3, 5, repr)
1424+
rpers = RepresentativePeriods(2, 5, [0.6, 0.4], [oscen, oscen])
1425+
ts = TwoLevel(3, 5, rpers)
13871426

13881427
vals = collect(profile[sc] for sc in opscenarios(ts))
13891428
@test vals == [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
@@ -1473,8 +1512,8 @@ end
14731512

14741513
# ScenarioProfile
14751514
oscen = OperationalScenarios([SimpleTimes(5, 1), SimpleTimes(5, 1)])
1476-
repr = RepresentativePeriods(2, 5, [0.6, 0.4], [oscen, oscen])
1477-
ts = TwoLevel(3, 5, repr)
1515+
rpers = RepresentativePeriods(2, 5, [0.6, 0.4], [oscen, oscen])
1516+
ts = TwoLevel(3, 5, rpers)
14781517
profile = +ScenarioProfile([1, 2, 3])
14791518
vals = collect(profile[sc] for sc in opscenarios(ts))
14801519
@test vals == [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
@@ -1484,8 +1523,8 @@ end
14841523
@test vals == [-1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2]
14851524

14861525
# RepresentativeProfile
1487-
repr = RepresentativePeriods(2, 5, [0.6, 0.4], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
1488-
ts = TwoLevel(3, 5, repr)
1526+
rpers = RepresentativePeriods(2, 5, [0.6, 0.4], [SimpleTimes(5, 1), SimpleTimes(5, 1)])
1527+
ts = TwoLevel(3, 5, rpers)
14891528

14901529
profile = +RepresentativeProfile([1, 2, 3])
14911530
vals = collect(profile[rp] for rp in repr_periods(ts))
@@ -1761,4 +1800,4 @@ end
17611800
end
17621801

17631802
@run_package_tests
1764-
Aqua.test_all(TimeStruct; ambiguities = false)
1803+
Aqua.test_all(TimeStruct; ambiguities = false, unbound_args = false)

0 commit comments

Comments
 (0)