Skip to content

Commit

Permalink
Make sure we only update coefficients once
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Dec 2, 2024
1 parent 0c8424b commit a454aee
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
49 changes: 35 additions & 14 deletions src/data_structure/temporal_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions src/run_spineopt_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a454aee

Please sign in to comment.