Skip to content

Commit e8b5303

Browse files
committed
Start some tests
1 parent 07ae3e4 commit e8b5303

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

test/test-rolling-horizon.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
@testitem "Rolling Horizon ..." setup = [CommonSetup] tags = [:rolling_horizon, :unit] begin
2+
# Test that if the EnergyProblem is created
3+
end
4+
5+
@testitem "add_rolling_horizon_parameters created Parameters" setup = [CommonSetup] tags =
6+
[:rolling_horizon, :unit] begin
7+
connection = DBInterface.connect(DuckDB.DB)
8+
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Rolling Horizon"))
9+
energy_problem = EnergyProblem(connection)
10+
11+
# These Parameters are not there initially
12+
create_model!(energy_problem; rolling_horizon = false, rolling_horizon_window_length = 24)
13+
@test all(
14+
length(p.rolling_horizon_variables) == 0 for p in values(energy_problem.profiles.rep_period)
15+
)
16+
17+
# With rolling horizon
18+
# create_model!(energy_problem; rolling_horizon = true, rolling_horizon_window_length = 24)
19+
window_length = 24
20+
TEM.add_rolling_horizon_parameters!(
21+
energy_problem.db_connection,
22+
energy_problem.model,
23+
energy_problem.variables,
24+
energy_problem.profiles,
25+
window_length,
26+
)
27+
@test all(
28+
length(p.rolling_horizon_variables) == window_length for
29+
p in values(energy_problem.profiles.rep_period)
30+
)
31+
end
32+
33+
@testitem "Verify tables created by rolling horizon" setup = [CommonSetup] tags =
34+
[:rolling_horizon, :unit] begin
35+
connection = DBInterface.connect(DuckDB.DB)
36+
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Rolling Horizon"))
37+
38+
# Not hardcoding these as they might change when the input changes
39+
move_forward = 24 * 28 * 3
40+
maximum_window_length = move_forward * 2
41+
horizon_length = TEM.get_single_element_from_query_and_ensure_its_only_one(
42+
DuckDB.query(connection, "SELECT max(timestep) FROM profiles_rep_periods"),
43+
)
44+
energy_problem = run_rolling_horizon(connection, move_forward, maximum_window_length)
45+
46+
# Table rolling_horizon_window
47+
@test "rolling_horizon_window" in
48+
[row.table_name for row in DuckDB.query(connection, "FROM duckdb_tables()")]
49+
50+
number_windows = ceil(Int, horizon_length / move_forward)
51+
df_rolling_horizon_window = DataFrame(DuckDB.query(connection, "FROM rolling_horizon_window"))
52+
@test maximum(df_rolling_horizon_window.id) == number_windows
53+
@test sum(df_rolling_horizon_window.opt_window_length) == horizon_length
54+
# TODO: If would be great to test something about the solution
55+
end
56+
57+
@testitem "If the window is very large, the solution is the same as no-horizon" setup =
58+
[CommonSetup] tags = [:rolling_horizon, :unit] begin
59+
connection = DBInterface.connect(DuckDB.DB)
60+
_read_csv_folder(connection, joinpath(INPUT_FOLDER, "Rolling Horizon"))
61+
62+
# Not hardcoding these as they might change when the input changes
63+
horizon_length = TEM.get_single_element_from_query_and_ensure_its_only_one(
64+
DuckDB.query(connection, "SELECT max(timestep) FROM profiles_rep_periods"),
65+
)
66+
maximum_window_length = horizon_length
67+
energy_problem = TulipaEnergyModel.run_scenario(connection; show_log = false)
68+
expected_objective = energy_problem.objective_value
69+
70+
for move_forward in [div(horizon_length, k) for k in 5:-1:2]
71+
energy_problem =
72+
run_rolling_horizon(connection, move_forward, maximum_window_length; show_log = false)
73+
df_rolling_horizon_window =
74+
DataFrame(DuckDB.query(connection, "FROM rolling_horizon_window"))
75+
@test energy_problem.objective_value == expected_objective
76+
end
77+
end
78+
79+
# Test opt_window = full_window works/fails correctly
80+
# Test that rolling_solution_* tables exist and have the correct number of elements
81+
# Test infeasible rolling problems (what should happen)?

0 commit comments

Comments
 (0)