Skip to content

Commit

Permalink
Update filter usage to be more efficient (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira committed Apr 30, 2024
1 parent 1e267eb commit 179f064
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/constraints/consumer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function add_consumer_constraints!(
)

# - Balance constraint (using the lowest temporal resolution)
df = filter(row -> row.asset Ac, dataframes[:highest_in_out]; view = true)
df = filter(:asset => (Ac), dataframes[:highest_in_out]; view = true)
model[:consumer_balance] = [
@constraint(
model,
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function add_conversion_constraints!(
)

# - Balance constraint (using the lowest temporal resolution)
df = filter(row -> row.asset Acv, dataframes[:lowest]; view = true)
df = filter(:asset => (Acv), dataframes[:lowest]; view = true)
model[:conversion_balance] = [
@constraint(
model,
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/hub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function add_hub_constraints!(
)

# - Balance constraint (using the lowest temporal resolution)
df = filter(row -> row.asset Ah, dataframes[:highest_in_out]; view = true)
df = filter(:asset => (Ah), dataframes[:highest_in_out]; view = true)
model[:hub_balance] = [
@constraint(
model,
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/transport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function add_transport_constraints!(model, graph, df_flows, flow, Ft, flows_inve
]

## Constraints that define bounds for a transport flow Ft
df = filter(row -> (row.from, row.to) Ft, df_flows)
df = filter([:from, :to] => (from, to) -> (from, to) Ft, df_flows; view = true)

# - Max transport flow limit
model[:max_transport_flow_limit] = [
Expand Down
17 changes: 12 additions & 5 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,22 @@ function add_expression_terms_inter_rp_constraints!(

# Incoming, outgoing flows, and profile aggregation
for row_inter in eachrow(df_inter)
sub_df_map = filter(row -> row.period in row_inter.periods_block, df_map)
sub_df_map = filter(:period => in(row_inter.periods_block), df_map; view = true)

for row_map in eachrow(sub_df_map)
sub_df_flows =
filter(row -> row.to == row_inter.asset && row.rp == row_map.rep_period, df_flows)
sub_df_flows = filter(
[:to, :rp] => (to, rp) -> to == row_inter.asset && rp == row_map.rep_period,
df_flows;
view = true,
)
row_inter.incoming_flow +=
LinearAlgebra.dot(sub_df_flows.flow, sub_df_flows.efficiency) * row_map.weight
sub_df_flows =
filter(row -> row.from == row_inter.asset && row.rp == row_map.rep_period, df_flows)
sub_df_flows = filter(
[:from, :rp] =>
(from, rp) -> from == row_inter.asset && rp == row_map.rep_period,
df_flows;
view = true,
)
row_inter.outgoing_flow +=
LinearAlgebra.dot(sub_df_flows.flow, sub_df_flows.efficiency) * row_map.weight
row_inter.inflows_profile_aggregation +=
Expand Down
17 changes: 10 additions & 7 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ function create_internal_structures(table_tree::TableTree)
for asset_profile_row in eachrow(table_tree.profiles.assets["rep-periods"]) # row = asset, profile_type, profile_name
gp = DataFrames.groupby( # 3. group by RP
filter(
row -> row.profile_name == asset_profile_row.profile_name, # 2. Filter profile_name
table_tree.profiles.data["rep-periods"][asset_profile_row.profile_type], # 1. Get the profile of given type
:profile_name => ==(asset_profile_row.profile_name), # 2. Filter profile_name
table_tree.profiles.data["rep-periods"][asset_profile_row.profile_type]; # 1. Get the profile of given type
view = true,
),
:rep_period,
)
Expand All @@ -256,10 +257,11 @@ function create_internal_structures(table_tree::TableTree)
for flow_profile_row in eachrow(table_tree.profiles.flows)
gp = DataFrames.groupby(
filter(
row -> row.profile_name == flow_profile_row.profile_name,
table_tree.profiles.data["rep-periods"][flow_profile_row.profile_type],
:profile_name => ==(flow_profile_row.profile_name),
table_tree.profiles.data["rep-periods"][flow_profile_row.profile_type];
view = true,
),
:rep_period,
:rep_period;
)
for ((rp,), df) in pairs(gp)
graph[flow_profile_row.from_asset, flow_profile_row.to_asset].rep_periods_profiles[(
Expand All @@ -271,8 +273,9 @@ function create_internal_structures(table_tree::TableTree)

for asset_profile_row in eachrow(table_tree.profiles.assets["timeframe"]) # row = asset, profile_type, profile_name
df = filter(
row -> row.profile_name == asset_profile_row.profile_name, # 2. Filter profile_name
table_tree.profiles.data["timeframe"][asset_profile_row.profile_type], # 1. Get the profile of given type
:profile_name => ==(asset_profile_row.profile_name), # 2. Filter profile_name
table_tree.profiles.data["timeframe"][asset_profile_row.profile_type]; # 1. Get the profile of given type
view = true,
)
graph[asset_profile_row.asset].timeframe_profiles[asset_profile_row.profile_type] = df.value
end
Expand Down

0 comments on commit 179f064

Please sign in to comment.