From 48bfb7a3554f6e03efe36cb1b39c90fb5dce704f Mon Sep 17 00:00:00 2001 From: datejada Date: Wed, 5 Feb 2025 12:07:56 +0100 Subject: [PATCH] Improve performance when creating expressions for timeframe constraints --- src/model-preparation.jl | 56 +++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/model-preparation.jl b/src/model-preparation.jl index 2df0b2d5..543c9143 100644 --- a/src/model-preparation.jl +++ b/src/model-preparation.jl @@ -379,9 +379,11 @@ function add_expression_terms_over_clustered_year_constraints!( # Incoming, outgoing flows, and profile aggregation for row_cons in eachrow(cons.indices) - sub_df_map = filter( - :period => p -> row_cons.period_block_start <= p <= row_cons.period_block_end, - df_map; + sub_df_map = DataFrames.subset( + df_map, + :period => DataFrames.ByRow( + p -> row_cons.period_block_start <= p <= row_cons.period_block_end, + ); view = true, ) @@ -392,43 +394,51 @@ function add_expression_terms_over_clustered_year_constraints!( # continue # end - sub_df_flows = filter( - [:from, :year, :rep_period] => - (from, y, rp) -> - (from, y, rp) == (row_cons.asset, row_map.year, row_map.rep_period), - flow.indices; + sub_df_flows = DataFrames.subset( + flow.indices, + :from => from -> from .== row_cons.asset, + :year => year -> year .== row_map.year, + :rep_period => rep_period -> rep_period .== row_map.rep_period; view = true, ) - sub_df_flows.duration = sub_df_flows.time_block_end - sub_df_flows.time_block_start .+ 1 + + duration = sub_df_flows.time_block_end - sub_df_flows.time_block_start .+ 1 if is_storage_level - cons.expressions[:outgoing][row_cons.index] += + JuMP.add_to_expression!( + cons.expressions[:outgoing][row_cons.index], LinearAlgebra.dot( flow.container[sub_df_flows.index], - sub_df_flows.duration ./ sub_df_flows.efficiency, - ) * row_map.weight + duration ./ sub_df_flows.efficiency, + ) * row_map.weight, + ) else - cons.expressions[:outgoing][row_cons.index] += - LinearAlgebra.dot(flow.container[sub_df_flows.index], sub_df_flows.duration) * - row_map.weight + JuMP.add_to_expression!( + cons.expressions[:outgoing][row_cons.index], + LinearAlgebra.dot(flow.container[sub_df_flows.index], duration) * + row_map.weight, + ) end if is_storage_level # TODO: There is some repetition here or am I missing something? - sub_df_flows = filter( - [:to, :year, :rep_period] => - (to, y, rp) -> - (to, y, rp) == (row_cons.asset, row_map.year, row_map.rep_period), - flow.indices; + sub_df_flows = DataFrames.subset( + flow.indices, + :to => to -> to .== row_cons.asset, + :year => year -> year .== row_map.year, + :rep_period => rep_period -> rep_period .== row_map.rep_period; view = true, ) + sub_df_flows.duration = sub_df_flows.time_block_end - sub_df_flows.time_block_start .+ 1 - cons.expressions[:incoming][row_cons.index] += + JuMP.add_to_expression!( + cons.expressions[:incoming][row_cons.index], LinearAlgebra.dot( flow.container[sub_df_flows.index], - sub_df_flows.duration .* sub_df_flows.efficiency, - ) * row_map.weight + duration .* sub_df_flows.efficiency, + ) * row_map.weight, + ) cons.expressions[:inflows_profile_aggregation][row_cons.index] += profile_aggregation(