Skip to content

Commit f834d68

Browse files
committed
Fix internal naming of windows
1 parent e8b5303 commit f834d68

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/rolling-horizon.jl

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Specify a `log_file` name to export the log to a file.
116116
"""
117117
function run_rolling_horizon(
118118
connection,
119-
move_forward,
119+
maximum_move_forward,
120120
maximum_window_length;
121121
output_folder = "",
122122
optimizer = HiGHS.Optimizer,
@@ -130,7 +130,7 @@ function run_rolling_horizon(
130130
)
131131
# Validation that the input data must satisfy to run rolling horizon
132132
# TODO: Should this be in data_validation?
133-
@assert maximum_window_length >= move_forward
133+
@assert maximum_window_length >= maximum_move_forward
134134
for row in DuckDB.query(
135135
connection,
136136
"SELECT year, max(rep_period) as num_rep_periods
@@ -142,7 +142,7 @@ function run_rolling_horizon(
142142
horizon_length = get_single_element_from_query_and_ensure_its_only_one(
143143
DuckDB.query(connection, "SELECT max(timestep) FROM profiles_rep_periods"),
144144
)
145-
@assert move_forward < horizon_length
145+
@assert maximum_move_forward < horizon_length
146146
@assert maximum_window_length <= horizon_length
147147

148148
# Rolling horizon info table
@@ -152,8 +152,8 @@ function run_rolling_horizon(
152152
CREATE OR REPLACE TABLE rolling_horizon_window (
153153
id INTEGER,
154154
window_start INTEGER,
155+
maximum_move_forward INTEGER,
155156
opt_window_length INTEGER,
156-
full_window_length INTEGER,
157157
objective_solution FLOAT8,
158158
);
159159
""",
@@ -174,7 +174,7 @@ function run_rolling_horizon(
174174
direct_model,
175175
)
176176
@timeit to "solve_model!" solve_model!(full_energy_problem)
177-
@timeit to "save_solution!" save_solution!(full_energy_problem, compute_duals = false)
177+
# @timeit to "save_solution!" save_solution!(full_energy_problem, compute_duals = false)
178178

179179
# These are all the non-empty variable tables
180180
# TODO: We possibly don't need all of them
@@ -249,19 +249,21 @@ function run_rolling_horizon(
249249

250250
# Loop over the windows, solve, save, update, repeat
251251
solved = true
252-
for (rolling_horizon_id, window_start) in enumerate(1:move_forward:horizon_length)
253-
# Windows might be shorter than expected due to maximum
252+
for (rolling_horizon_id, window_start) in enumerate(1:maximum_move_forward:horizon_length)
253+
# Windows might be shorter than expected due to horizon end
254+
move_forward =
255+
min(window_start + maximum_move_forward - 1, horizon_length) - window_start + 1
254256
window_end = min(window_start + maximum_window_length - 1, horizon_length)
255-
opt_window_length = min(window_start + move_forward - 1, horizon_length) - window_start + 1
256-
full_window_length = window_end - window_start + 1
257+
opt_window_length = window_end - window_start + 1
258+
@info "DEBUG" window_end, move_forward, opt_window_length
257259

258260
# If this window is too large, we decrease the rep_periods_data
259-
if full_window_length < maximum_window_length
261+
if opt_window_length < maximum_window_length
260262
DuckDB.query(
261263
connection,
262-
"UPDATE rep_periods_data SET num_timesteps = $full_window_length",
264+
"UPDATE rep_periods_data SET num_timesteps = $opt_window_length",
263265
)
264-
DuckDB.query(connection, "UPDATE year_data SET length = $full_window_length")
266+
DuckDB.query(connection, "UPDATE year_data SET length = $opt_window_length")
265267
end
266268

267269
# Update Parameters in the model (even for the first time)
@@ -271,13 +273,15 @@ function run_rolling_horizon(
271273
window_start + opt_window_length - 1,
272274
)
273275
# TODO: Update other scalar parameters
274-
update_initial_storage_level!(
275-
energy_problem.variables[:param_initial_storage_level],
276-
connection,
277-
)
276+
if rolling_horizon_id > 1 # Don't try to update the first initial values
277+
update_initial_storage_level!(
278+
energy_problem.variables[:param_initial_storage_level],
279+
connection,
280+
)
281+
end
278282

279-
@info energy_problem.objective_value # DEBUG
280283
@timeit to "solve_model!" solve_model!(energy_problem)
284+
@info "objective" energy_problem.objective_value # DEBUG
281285

282286
# Save window to table rolling_horizon_window
283287
objective_solution = if isnothing(energy_problem.objective_value)
@@ -289,11 +293,11 @@ function run_rolling_horizon(
289293
connection,
290294
"""
291295
INSERT INTO rolling_horizon_window
292-
VALUES ($rolling_horizon_id, $window_start, $opt_window_length, $full_window_length, $objective_solution);
296+
VALUES ($rolling_horizon_id, $window_start, $move_forward, $opt_window_length, $objective_solution);
293297
""",
294298
)
295299

296-
@info energy_problem # DEBUG
300+
# @info energy_problem # DEBUG
297301

298302
if !energy_problem.solved
299303
solved = false
@@ -349,7 +353,7 @@ function run_rolling_horizon(
349353
ON $where_condition -- this condition should match
350354
AND full_$table_name.time_block_start = $(window_start - 1) + $table_name.time_block_start
351355
WHERE
352-
$table_name.time_block_end <= $full_window_length -- limiting the time_block_end by the actual full_window_length to ignore extra variables in the last window
356+
$table_name.time_block_end <= $opt_window_length -- limiting the time_block_end by the actual opt_window_length to ignore extra variables in the last window
353357
)
354358
INSERT INTO rolling_solution_$table_name
355359
SELECT *

0 commit comments

Comments
 (0)