Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #999

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TulipaEnergyModel"
uuid = "5d7bd171-d18e-45a5-9111-f1f11ac5d04d"
authors = ["Abel Soares Siqueira <[email protected]>", "Diego A. Tejada-Arango <[email protected]>", "Germán Morales-España <[email protected]>", "Grigory Neustroev <[email protected]>", "Juha Kiviluoma <[email protected]>", "Lauren Clisby <[email protected]>", "Maaike Elgersma <[email protected]>", "Ni Wang <[email protected]>", "Suvayu Ali <[email protected]>", "Zhi Gao <[email protected]>"]
version = "0.10.3"
version = "0.10.4"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
60 changes: 33 additions & 27 deletions src/constraints/storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function add_storage_constraints!(
dataframes,
Ai,
accumulated_energy_capacity,
incoming_flow_lowest_storage_resolution_intra_rp,
outgoing_flow_lowest_storage_resolution_intra_rp,
incoming_flow_lowest_storage_resolution_intra_rp::Vector{JuMP.AffExpr},
outgoing_flow_lowest_storage_resolution_intra_rp::Vector{JuMP.AffExpr},
df_storage_intra_rp_balance_grouped,
df_storage_inter_rp_balance_grouped,
storage_level_intra_rp,
storage_level_inter_rp,
incoming_flow_storage_inter_rp_balance,
outgoing_flow_storage_inter_rp_balance,
incoming_flow_storage_inter_rp_balance::Vector{JuMP.AffExpr},
outgoing_flow_storage_inter_rp_balance::Vector{JuMP.AffExpr},
)

## INTRA-TEMPORAL CONSTRAINTS (within a representative period)
Expand All @@ -29,7 +29,7 @@ function add_storage_constraints!(
# This assumes an ordering of the time blocks, that is guaranteed inside
# construct_dataframes
# The storage_inflows have been moved here
model[Symbol("storage_intra_rp_balance_$(a)_$(y)_$(rp)")] = [
model[Symbol("storage_intra_rp_balance_$(a)_$(y)_$(rp)")] = JuMP.ConstraintRef[
@constraint(
model,
storage_level_intra_rp[row.index] ==
Expand Down Expand Up @@ -116,28 +116,34 @@ function add_storage_constraints!(
# This assumes an ordering of the time blocks, that is guaranteed inside
# construct_dataframes
# The storage_inflows have been moved here
model[Symbol("storage_inter_rp_balance_$(a)_$(y)")] = [
@constraint(
model,
storage_level_inter_rp[row.index] ==
(
if k > 1
storage_level_inter_rp[row.index-1] # This assumes contiguous index
else
(
if ismissing(graph[a].initial_storage_level[row.year])
storage_level_inter_rp[last(sub_df.index)]
else
graph[a].initial_storage_level[row.year]
end
)
end
) +
row.inflows_profile_aggregation +
incoming_flow_storage_inter_rp_balance[row.index] -
outgoing_flow_storage_inter_rp_balance[row.index],
base_name = "storage_inter_rp_balance[$a,$(row.year),$(row.periods_block)]"
) for (k, row) in enumerate(eachrow(sub_df))
model[Symbol("storage_inter_rp_balance_$(a)_$(y)")] = JuMP.ConstraintRef[
if k == 1 && !ismissing(graph[a].initial_storage_level[row.year])
@constraint(
model,
storage_level_inter_rp[row.index] ==
graph[a].initial_storage_level[row.year] +
row.inflows_profile_aggregation +
incoming_flow_storage_inter_rp_balance[row.index] -
outgoing_flow_storage_inter_rp_balance[row.index],
base_name = "storage_inter_rp_balance[$a,$(row.year),$(row.periods_block)]"
)

else
previous_level::JuMP.VariableRef = if k > 1
storage_level_inter_rp[row.index-1]
else
storage_level_inter_rp[last(sub_df.index)]
end
@constraint(
model,
storage_level_inter_rp[row.index] ==
previous_level +
row.inflows_profile_aggregation +
incoming_flow_storage_inter_rp_balance[row.index] -
outgoing_flow_storage_inter_rp_balance[row.index],
base_name = "storage_inter_rp_balance[$a,$(row.year),$(row.periods_block)]"
)
end for (k, row) in enumerate(eachrow(sub_df))
]
end

Expand Down
81 changes: 47 additions & 34 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ function construct_dataframes(graph, representative_periods, constraints_partiti
RP = Dict(year => 1:length(representative_periods[year]) for year in years)

# Create subsets of assets
Ap = filter_graph(graph, A, "producer", :type)
Ap = filter_graph(graph, A, "producer", :type)
Acv = filter_graph(graph, A, "conversion", :type)
Auc = Dict(year => (Ap ∪ Acv) ∩ filter_graph(graph, A, true, :unit_commitment, year) for year in years)
Auc = Dict(
year => (Ap ∪ Acv) ∩ filter_graph(graph, A, true, :unit_commitment, year) for year in years
)

# Output object
dataframes = Dict{Symbol,DataFrame}()
Expand Down Expand Up @@ -432,18 +434,25 @@ function add_expression_terms_inter_rp_constraints!(
representative_periods;
is_storage_level = false,
)
df_inter[!, :outgoing_flow] .= JuMP.AffExpr(0.0)
df_inter[!, :outgoing_flow] = Vector{JuMP.AffExpr}(undef, size(df_inter, 1))

if is_storage_level
df_inter[!, :incoming_flow] .= JuMP.AffExpr(0.0)
df_inter[!, :inflows_profile_aggregation] .= JuMP.AffExpr(0.0)
df_inter[!, :incoming_flow] = Vector{JuMP.AffExpr}(undef, size(df_inter, 1))
df_inter[!, :inflows_profile_aggregation] = Vector{JuMP.AffExpr}(undef, size(df_inter, 1))
end

# TODO: The interaction between year and timeframe is not clear yet, so this is probably wrong
# At this moment, that relation is ignored (we don't even look at df_inter.year)

# Incoming, outgoing flows, and profile aggregation
for row_inter in eachrow(df_inter)
row_inter.outgoing_flow = JuMP.AffExpr(0.0)

if is_storage_level
row_inter.incoming_flow = JuMP.AffExpr(0.0)
row_inter.inflows_profile_aggregation = JuMP.AffExpr(0.0)
end

sub_df_map = DataFrames.subset(
df_map,
:period => DataFrames.ByRow(in(row_inter.periods_block));
Expand All @@ -459,38 +468,39 @@ function add_expression_terms_inter_rp_constraints!(

sub_df_flows = DataFrames.subset(
df_flows,
:from => DataFrames.ByRow(==(row_inter.asset)),
:year => DataFrames.ByRow(==(row_map.year)),
:rep_period => DataFrames.ByRow(==(row_map.rep_period));
:from => from -> from .== row_inter.asset,
:year => year -> year .== row_map.year,
:rep_period => rep_period -> rep_period .== row_map.rep_period;
view = true,
)
sub_df_flows.duration = length.(sub_df_flows.timesteps_block)
duration = length.(sub_df_flows.timesteps_block)
if is_storage_level
row_inter.outgoing_flow +=
LinearAlgebra.dot(
sub_df_flows.flow,
sub_df_flows.duration ./ sub_df_flows.efficiency,
) * row_map.weight
JuMP.add_to_expression!(
row_inter.outgoing_flow,
LinearAlgebra.dot(sub_df_flows.flow, duration ./ sub_df_flows.efficiency) *
row_map.weight,
)
else
row_inter.outgoing_flow +=
LinearAlgebra.dot(sub_df_flows.flow, sub_df_flows.duration) * row_map.weight
JuMP.add_to_expression!(
row_inter.outgoing_flow,
LinearAlgebra.dot(sub_df_flows.flow, duration) * row_map.weight,
)
end

if is_storage_level
sub_df_flows = DataFrames.subset(
df_flows,
:to => DataFrames.ByRow(==(row_inter.asset)),
:year => DataFrames.ByRow(==(row_map.year)),
:rep_period => DataFrames.ByRow(==(row_map.rep_period));
:to => to -> to .== row_inter.asset,
:year => year -> year .== row_map.year,
:rep_period => rep_period -> rep_period .== row_map.rep_period;
view = true,
)
sub_df_flows.duration = length.(sub_df_flows.timesteps_block)
row_inter.incoming_flow +=
LinearAlgebra.dot(
sub_df_flows.flow,
sub_df_flows.duration .* sub_df_flows.efficiency,
) * row_map.weight

duration = length.(sub_df_flows.timesteps_block)
JuMP.add_to_expression!(
row_inter.incoming_flow,
LinearAlgebra.dot(sub_df_flows.flow, duration .* sub_df_flows.efficiency) *
row_map.weight,
)
row_inter.inflows_profile_aggregation +=
profile_aggregation(
sum,
Expand Down Expand Up @@ -589,7 +599,7 @@ function create_model!(energy_problem; kwargs...)
end

"""
model = create_model(graph, representative_periods, dataframes, timeframe, groups; write_lp_file = false)
model = create_model(graph, representative_periods, dataframes, timeframe, groups; write_lp_file = false, enable_names = true)

Create the energy model given the `graph`, `representative_periods`, dictionary of `dataframes` (created by [`construct_dataframes`](@ref)), timeframe, and groups.
"""
Expand All @@ -602,6 +612,7 @@ function create_model(
groups,
model_parameters;
write_lp_file = false,
enable_names = true,
)

## Helper functions
Expand All @@ -622,14 +633,14 @@ function create_model(

## Sets unpacking
@timeit to "unpacking and creating sets" begin
A = MetaGraphsNext.labels(graph) |> collect
F = MetaGraphsNext.edge_labels(graph) |> collect
Ac = filter_graph(graph, A, "consumer", :type)
Ap = filter_graph(graph, A, "producer", :type)
As = filter_graph(graph, A, "storage", :type)
Ah = filter_graph(graph, A, "hub", :type)
A = MetaGraphsNext.labels(graph) |> collect
F = MetaGraphsNext.edge_labels(graph) |> collect
Ac = filter_graph(graph, A, "consumer", :type)
Ap = filter_graph(graph, A, "producer", :type)
As = filter_graph(graph, A, "storage", :type)
Ah = filter_graph(graph, A, "hub", :type)
Acv = filter_graph(graph, A, "conversion", :type)
Ft = filter_graph(graph, F, true, :is_transport)
Ft = filter_graph(graph, F, true, :is_transport)

# Create subsets of assets by investable
Ai = Dict(y => filter_graph(graph, A, true, :investable, y) for y in Y)
Expand Down Expand Up @@ -758,6 +769,8 @@ function create_model(
## Model
model = JuMP.Model()

JuMP.set_string_names_on_creation(model, enable_names)

## Variables
@timeit to "create variables" begin
### Flow variables
Expand Down
3 changes: 2 additions & 1 deletion src/run-scenario.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ function run_scenario(
write_lp_file = false,
log_file = "",
show_log = true,
enable_names = false,
)
energy_problem = @timeit to "create EnergyProblem from connection" EnergyProblem(
connection;
model_parameters_file,
)

@timeit to "create_model!" create_model!(energy_problem; write_lp_file)
@timeit to "create_model!" create_model!(energy_problem; write_lp_file, enable_names)

@timeit to "solve and store solution" solve_model!(energy_problem, optimizer; parameters)

Expand Down
Loading