Skip to content

Commit

Permalink
Remove unnecessary expression and move lower bound of flow variable f…
Browse files Browse the repository at this point in the history
…rom src/constraints/capacity.jl (#976)
  • Loading branch information
abelsiqueira authored Dec 18, 2024
1 parent b34be57 commit 0a1e15b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
26 changes: 2 additions & 24 deletions src/constraints/capacity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ Adds the capacity constraints for all asset types to the model
function add_capacity_constraints!(model, variables, constraints, graph, sets)
## unpack from sets
Acv = sets[:Acv]
Ai = sets[:Ai]
Ap = sets[:Ap]
As = sets[:As]
Asb = sets[:Asb]
V_all = sets[:V_all]
Y = sets[:Y]
accumulated_set_using_compact_method = sets[:accumulated_set_using_compact_method]
accumulated_set_using_compact_method_lookup = sets[:accumulated_set_using_compact_method_lookup]
accumulated_units_lookup = sets[:accumulated_units_lookup]
decommissionable_assets_using_compact_method =
sets[:decommissionable_assets_using_compact_method]
decommissionable_assets_using_simple_method = sets[:decommissionable_assets_using_simple_method]

## unpack from model
accumulated_initial_units = model[:accumulated_initial_units]
Expand Down Expand Up @@ -81,13 +77,6 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
)
end

# - Create accumulated investment limit for the use of binary storage method with investments
accumulated_investment_limit = @expression(
model,
accumulated_investment_limit[y in Y, a in Ai[y]Asb],
graph[a].investment_limit[y]
)

# - Create capacity limit for outgoing flows with binary is_charging for storage assets
let table_name = :capacity_outgoing_non_investable_storage_with_binary,
cons = constraints[table_name]
Expand Down Expand Up @@ -160,7 +149,7 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
) *
(
graph[row.asset].capacity * accumulated_initial_units[row.asset, row.year] +
accumulated_investment_limit[row.year, row.asset]
graph[row.asset].investment_limit[row.year]
) *
(1 - is_charging)
) for
Expand Down Expand Up @@ -265,7 +254,7 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
) *
(
graph[row.asset].capacity * accumulated_initial_units[row.asset, row.year] +
accumulated_investment_limit[row.year, row.asset]
graph[row.asset].investment_limit[row.year]
) *
is_charging
) for
Expand Down Expand Up @@ -369,15 +358,4 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
],
)
end

# - Lower limit for flows associated with assets
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
2 changes: 1 addition & 1 deletion src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function create_model(
JuMP.set_string_names_on_creation(model, enable_names)

## Variables
@timeit to "add_flow_variables!" add_flow_variables!(model, variables)
@timeit to "add_flow_variables!" add_flow_variables!(connection, model, variables)
@timeit to "add_investment_variables!" add_investment_variables!(model, graph, sets, variables)
@timeit to "add_unit_commitment_variables!" add_unit_commitment_variables!(
model,
Expand Down
31 changes: 29 additions & 2 deletions src/variables/flows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,43 @@ Adds flow variables to the optimization `model` based on data from the `variable
The flow variables are created using the `@variable` macro for each row in the `:flows` dataframe.
"""
function add_flow_variables!(model, variables)
function add_flow_variables!(connection, model, variables)
# Unpacking the variable indices
flows_indices = variables[:flow].indices
indices = _create_flow_table(connection)

lower_bound(row) =
if row.from_asset_type in ("producer", "conversion", "storage") ||
row.to_asset_type in ("conversion", "storage")
0.0
else
-Inf
end

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

return
end

function _create_flow_table(connection)
return DuckDB.query(
connection,
"SELECT
var_flow.*,
from_asset.type AS from_asset_type,
to_asset.type AS to_asset_type,
FROM var_flow
LEFT JOIN asset AS from_asset
ON var_flow.from = from_asset.asset
LEFT JOIN asset AS to_asset
ON var_flow.to = to_asset.asset
ORDER BY var_flow.index
",
)
end

0 comments on commit 0a1e15b

Please sign in to comment.