Skip to content

Commit

Permalink
Create flow variables using the indices table (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada authored Oct 23, 2024
1 parent c372d65 commit f432fa4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
22 changes: 13 additions & 9 deletions src/constraints/capacity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ function add_capacity_constraints!(
model,
graph,
dataframes,
df_flows,
flow,
sets,
variables,
accumulated_initial_units,
accumulated_investment_units_using_simple_method,
accumulated_units,
Expand All @@ -35,6 +34,10 @@ function add_capacity_constraints!(
sets[:decommissionable_assets_using_compact_method]
decommissionable_assets_using_simple_method = sets[:decommissionable_assets_using_simple_method]

## unpack from variables
flows_indices = variables[:flow].indices
flow = variables[:flow].container

## Expressions used by capacity constraints
# - Create capacity limit for outgoing flows
assets_profile_times_capacity_out =
Expand Down Expand Up @@ -296,12 +299,13 @@ function add_capacity_constraints!(
]

# - Lower limit for flows associated with assets
assets_with_non_negative_outgoing_flows = Ap Acv As
assets_with_non_negative_incoming_flows = Acv As
for row in eachrow(df_flows)
if row.from in assets_with_non_negative_outgoing_flows ||
row.to in assets_with_non_negative_incoming_flows
JuMP.set_lower_bound(flow[row.index], 0.0)
end
assets_with_non_negative_flows_indices = DataFrames.subset(
flows_indices,
[:from, :to] => DataFrames.ByRow(
(from, to) -> from in Ap || from in Acv || from in As || to in Acv || to in As,
),
)
for row in eachrow(assets_with_non_negative_flows_indices)
JuMP.set_lower_bound(flow[row.index], 0.0)
end
end
22 changes: 14 additions & 8 deletions src/constraints/transport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ Adds the transport flow constraints to the model.
function add_transport_constraints!(
model,
graph,
df_flows,
flow,
sets,
variables,
accumulated_flows_export_units,
accumulated_flows_import_units,
)
## unpack from sets
Ft = sets[:Ft]

## unpack from variables
flows_indices = variables[:flow].indices
flow = variables[:flow].container

## Expressions used by transport flow constraints
# Filter df_flows to flows only for transport assets
df = filter([:from, :to] => (from, to) -> (from, to) Ft, df_flows; view = true)
# Filter flows_indices to flows only for transport assets
transport_flows_indices =
filter([:from, :to] => (from, to) -> (from, to) Ft, flows_indices; view = true)

# - Create upper limit of transport flow
upper_bound_transport_flow = [
Expand All @@ -35,7 +41,7 @@ function add_transport_constraints!(
) *
graph[row.from, row.to].capacity *
accumulated_flows_export_units[row.year, (row.from, row.to)]
) for row in eachrow(df)
) for row in eachrow(transport_flows_indices)
]

# - Create lower limit of transport flow
Expand All @@ -53,7 +59,7 @@ function add_transport_constraints!(
) *
graph[row.from, row.to].capacity *
accumulated_flows_import_units[row.year, (row.from, row.to)]
) for row in eachrow(df)
) for row in eachrow(transport_flows_indices)
]

## Constraints that define bounds for a transport flow Ft
Expand All @@ -64,7 +70,7 @@ function add_transport_constraints!(
model,
flow[row.index] upper_bound_transport_flow[idx],
base_name = "max_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.timesteps_block)]"
) for (idx, row) in enumerate(eachrow(df))
) for (idx, row) in enumerate(eachrow(transport_flows_indices))
]

# - Min transport flow limit
Expand All @@ -73,6 +79,6 @@ function add_transport_constraints!(
model,
flow[row.index] -lower_bound_transport_flow[idx],
base_name = "min_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.timesteps_block)]"
) for (idx, row) in enumerate(eachrow(df))
) for (idx, row) in enumerate(eachrow(transport_flows_indices))
]
end
12 changes: 6 additions & 6 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function create_model(
model = JuMP.Model()

## Variables
@timeit to "add_flow_variables!" add_flow_variables!(model, dataframes)
@timeit to "add_flow_variables!" add_flow_variables!(model, variables)
@timeit to "add_investment_variables!" add_investment_variables!(model, graph, sets)
@timeit to "add_unit_commitment_variables!" add_unit_commitment_variables!(
model,
Expand All @@ -104,12 +104,14 @@ function create_model(
assets_decommission_energy_simple_method = model[:assets_decommission_energy_simple_method]
assets_investment = model[:assets_investment]
assets_investment_energy = model[:assets_investment_energy]
flow = model[:flow]
flows_decommission_using_simple_method = model[:flows_decommission_using_simple_method]
flows_investment = model[:flows_investment]
storage_level_inter_rp = model[:storage_level_inter_rp]
storage_level_intra_rp = model[:storage_level_intra_rp]

# TODO: This should disapear after the changes on add_expressions_to_dataframe! and storing the solution
model[:flow] = df_flows.flow = variables[:flow].container

## Add expressions to dataframes
# TODO: What will improve this? Variables (#884)?, Constraints?
(
Expand Down Expand Up @@ -158,9 +160,8 @@ function create_model(
model,
graph,
dataframes,
df_flows,
flow,
sets,
variables,
accumulated_initial_units,
accumulated_investment_units_using_simple_method,
accumulated_units,
Expand Down Expand Up @@ -214,9 +215,8 @@ function create_model(
@timeit to "add_transport_constraints!" add_transport_constraints!(
model,
graph,
df_flows,
flow,
sets,
variables,
accumulated_flows_export_units,
accumulated_flows_import_units,
)
Expand Down
8 changes: 4 additions & 4 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ end
"""
Structure to hold the JuMP variables for the TulipaEnergyModel
"""
struct TulipaVariable
mutable struct TulipaVariable
indices::DataFrame
variable::Vector{JuMP.VariableRef}
container::Vector{JuMP.VariableRef}

function TulipaVariable(indices, variable)
return new(indices, variable)
function TulipaVariable(indices, container)
return new(indices, container)
end
end

Expand Down
22 changes: 11 additions & 11 deletions src/variables/flows.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
export add_flow_variables!

"""
add_flow_variables!(model, dataframes)
add_flow_variables!(model, variables)
Adds flow variables to the optimization `model` based on data from the `dataframes`.
Adds flow variables to the optimization `model` based on data from the `variables`.
The flow variables are created using the `@variable` macro for each row in the `:flows` dataframe.
"""
function add_flow_variables!(model, dataframes)
df_flows = dataframes[:flows]
function add_flow_variables!(model, variables)
# Unpacking the variable indices
flows_indices = variables[:flow].indices

model[:flow] =
df_flows.flow = [
@variable(
model,
base_name = "flow[($(row.from), $(row.to)), $(row.year), $(row.rep_period), $(row.timesteps_block)]"
) for row in eachrow(df_flows)
]
variables[:flow].container = [
@variable(
model,
base_name = "flow[($(row.from), $(row.to)), $(row.year), $(row.rep_period), $(row.timesteps_block)]"
) for row in eachrow(flows_indices)
]

return
end

0 comments on commit f432fa4

Please sign in to comment.