Skip to content

Commit

Permalink
Add rowtables to test AOG
Browse files Browse the repository at this point in the history
  • Loading branch information
trulsf committed Nov 22, 2024
1 parent 6791e6d commit e77bdba
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
83 changes: 83 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ function end_oper_time(t::TimePeriod, opscen::AbstractOperationalScenario)
return end_oper_time(t, opscen.operational)
end

function end_oper_time(t::TimePeriod, ts::RepresentativePeriods)
return end_oper_time(t, ts.rep_periods[_rper(t)])
end

function end_oper_time(t::TimePeriod, ts::TwoLevel)
return end_oper_time(t, ts.operational[_strat_per(t)])
end
Expand Down Expand Up @@ -162,3 +166,82 @@ end

function profilechart end
function profilechart! end

_scen(sc::AbstractOperationalScenario) = "sc-$(TimeStruct._opscen(sc))"
_repr(rp::AbstractRepresentativePeriod) = "rp-$(TimeStruct._rper(rp))"
_strat(sp::AbstractStrategicPeriod) = "sp-$(TimeStruct._strat_per(sp))"

function rowtable(profile::TimeProfile, periods::SimpleTimes; include_end = true)
rowtable = []
for t in periods
push!(rowtable, (t = start_oper_time(t, periods), value = profile[t]))
end
if include_end
last_period = last(periods)
push!(
rowtable,
(t = end_oper_time(last_period, periods), value = profile[last_period]),
)
end
return rowtable
end

function rowtable(profile::TimeProfile, periods::OperationalScenarios; include_end = true)
rowtable = []
for sc in opscenarios(periods)
for t in sc
push!(
rowtable,
(opscen = _scen(sc), t = start_oper_time(t, periods), value = profile[t]),
)
end
if include_end
last_period = last(sc)
push!(
rowtable,
(
opscen = _scen(sc),
t = end_oper_time(last_period, periods),
value = profile[last_period],
),
)
end
end
return rowtable
end

function rowtable(profile::TimeProfile, periods::TwoLevel; include_end = true)
rowtable = []
for sp in strat_periods(periods)
for rp in repr_periods(sp)
for sc in opscenarios(rp)
for t in sc
push!(
rowtable,
(
strat = _strat(sp),
repr = _repr(rp),
opscen = _scen(sc),
t = start_oper_time(t, periods),
value = profile[t],
),
)
end
if include_end
last_period = last(sc)
push!(
rowtable,
(
strat = _strat(sp),
repr = _repr(rp),
opscen = _scen(sc),
t = end_oper_time(last_period, periods),
value = profile[last_period],
),
)
end
end
end
end
return rowtable
end
52 changes: 51 additions & 1 deletion test/test_makie.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CairoMakie
using TimeStruct
using AlgebraOfGraphics

periods = SimpleTimes([1, 2, 4, 2, 3])
profile = OperationalProfile([2.0, 3.4, 3.5, 1.2, 0.6])
Expand All @@ -14,5 +15,54 @@ scens = OperationalScenarios(3, periods)
scen_prof = ScenarioProfile([profile, 0.8 * profile, 1.2 * profile])
profilechart(scens, scen_prof)

two_level = TwoLevel(3, scens)
repr = RepresentativePeriods(2, [0.7, 0.3], [scens, scens])

two_level = TwoLevel(3, repr)
plot(two_level, scen_prof)

# Testing with AlgebraOfGraphics and rowtables
set_aog_theme!()
axis = (width = 225, height = 225)

tab = TimeStruct.rowtable(profile, periods)

plt = data(tab) * mapping(:t, :value) * visual(Stairs, step = :post)
draw(plt; axis = axis)

sc_tab = TimeStruct.rowtable(scen_prof, scens)
plt = data(sc_tab) * mapping(:t, :value, color = :opscen) * visual(Stairs, step = :post)
draw(plt; axis = axis)

plt = data(sc_tab) * mapping(:t, :value, col = :opscen) * visual(Stairs, step = :post)
draw(plt; axis = axis)

two_tab = TimeStruct.rowtable(scen_prof, two_level)
plt =
data(two_tab) *
mapping(:t, :value, row = :repr, col = :strat, color = :opscen) *
visual(Stairs, step = :post)
draw(plt; axis = axis)

plt = data(two_tab) * mapping(:t, :value, layout = :strat) * visual(Stairs, step = :post)
draw(plt; axis = axis)

using JuMP, HiGHS

m = Model()
@variable(m, x[periods] >= 0)
@constraint(m, [t in periods], x[t] == profile[t])
@objective(m, Min, 0)

set_optimizer(m, HiGHS.Optimizer)
optimize!(m)

tab = Containers.rowtable(value, x)

set_aog_theme!()
axis = (width = 225, height = 225)

plt = data(tab) * mapping(:x1 => (t -> start_oper_time(t, periods)), :y)
draw(plt; axis = axis)

plt = data(tab) * mapping(:x1, :y)
draw(plt; axis = axis)

0 comments on commit e77bdba

Please sign in to comment.