Skip to content

Commit

Permalink
Improve performance when creating expressions for timeframe constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada committed Feb 5, 2025
1 parent 22f4174 commit 48bfb7a
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions src/model-preparation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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(
Expand Down

0 comments on commit 48bfb7a

Please sign in to comment.