From a454aeef8560e8270281e686ec5627d85d247478 Mon Sep 17 00:00:00 2001 From: manuelma Date: Mon, 2 Dec 2024 17:53:09 +0100 Subject: [PATCH] Make sure we only update coefficients once --- src/data_structure/temporal_structure.jl | 49 +++++++++++++++++------- src/run_spineopt_basic.jl | 6 +-- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/data_structure/temporal_structure.jl b/src/data_structure/temporal_structure.jl index 4d3e4bf279..1673e3ff85 100644 --- a/src/data_structure/temporal_structure.jl +++ b/src/data_structure/temporal_structure.jl @@ -366,16 +366,25 @@ _to_time_slice(time_slices::Array{TimeSlice,1}, t::TimeSlice) = _to_time_slice(t Roll a `TimeSliceSet` in time by a period specified by `forward`. """ function _roll_time_slice_set!(t_set::TimeSliceSet, forward::Union{Period,CompoundPeriod}) - roll!.(t_set.time_slices, forward) - roll!.(values(t_set.gaps), forward) - roll!.(values(t_set.bridges), forward) - nothing + updates = collect(_roll_many!(t_set.time_slices, forward)) + append!(updates, _roll_many!(values(t_set.gaps), forward)) + append!(updates, _roll_many!(values(t_set.bridges), forward)) + updates end -function _refresh_time_slice_set!(t_set::TimeSliceSet) - refresh!.(t_set.time_slices) - refresh!.(values(t_set.gaps)) - refresh!.(values(t_set.bridges)) +function _roll_many!(t_iter, forward) + (upd for t in t_iter for upd in roll!(t, forward; return_updates=true)) +end + +function _time_slice_set_collect_updates(t_set::TimeSliceSet) + updates = collect(_collect_updates_many(t_set.time_slices)) + append!(_collect_updates_many(values(t_set.gaps))) + append!(_collect_updates_many(values(t_set.bridges))) + updates +end + +function _collect_updates_many(t_iter) + (upd for t in t_iter for upd in collect_updates(t)) end function generate_time_slice!(m::Model) @@ -486,9 +495,11 @@ function _do_roll_temporal_structure!(m::Model, rf, rev) !rev && any( x >= model_end(model=m.ext[:spineopt].instance) for x in (end_(current_window), start(current_window) + rf) ) && return false - roll!(current_window, rf) - _roll_time_slice_set!(temp_struct[:time_slice], rf) - _roll_time_slice_set!(temp_struct[:history_time_slice], rf) + updates = roll!(current_window, rf; return_updates=true) + append!(updates, _roll_time_slice_set!(temp_struct[:time_slice], rf)) + append!(updates, _roll_time_slice_set!(temp_struct[:history_time_slice], rf)) + unique!(updates) + _call_many(updates) true end @@ -505,12 +516,22 @@ function rewind_temporal_structure!(m::Model) _update_variable_names!(m) _update_constraint_names!(m) else - refresh!(temp_struct[:current_window]) - _refresh_time_slice_set!(temp_struct[:time_slice]) - _refresh_time_slice_set!(temp_struct[:history_time_slice]) + updates = collect_updates(temp_struct[:current_window]) + append!(updates, _time_slice_set_collect_updates(temp_struct[:time_slice])) + append!(updates, _time_slice_set_collect_updates(temp_struct[:history_time_slice])) + unique!(updates) + _call_many(updates) end end +function _call_many(updates) + _do_call.(updates) +end + +function _do_call(upd) + upd() +end + """ to_time_slice(m; t) diff --git a/src/run_spineopt_basic.jl b/src/run_spineopt_basic.jl index 279f2921b3..83cdf064c2 100644 --- a/src/run_spineopt_basic.jl +++ b/src/run_spineopt_basic.jl @@ -544,7 +544,7 @@ function _do_solve_model!( end end _save_window_state(m, k; write_as_roll, resume_file_path) - if @timelog log_level 2 "$model_name - Rolling temporal structure...\n" !roll_temporal_structure!(m, k) + if @timelog log_level 2 "Rolling $model_name temporal structure...\n" !roll_temporal_structure!(m, k) @timelog log_level 2 "$model_name ... Rolling complete\n" break end update_model!(m; log_level=log_level, update_names=update_names) @@ -1230,9 +1230,7 @@ function _dump_resume_data(m::Model, k, resume_file_path) end function _clear_results!(m) - for out in output() - by_entity = get!(m.ext[:spineopt].outputs, out.name, nothing) - by_entity === nothing && continue + for by_entity in values(m.ext[:spineopt].outputs) empty!(by_entity) end end