From 696764c3d4f7c379a6f5a4a82d9ee64dd4c5725e Mon Sep 17 00:00:00 2001 From: Abel Soares Siqueira Date: Wed, 18 Dec 2024 10:57:09 +0100 Subject: [PATCH 1/2] Remove unnecessary accumulated_investment_limit expression --- src/constraints/capacity.jl | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/constraints/capacity.jl b/src/constraints/capacity.jl index 6bf5ca21..3ac82201 100644 --- a/src/constraints/capacity.jl +++ b/src/constraints/capacity.jl @@ -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] @@ -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] @@ -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 @@ -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 From 763bb82686e971269096aa85b5a51049236dd089 Mon Sep 17 00:00:00 2001 From: Abel Soares Siqueira Date: Wed, 18 Dec 2024 11:04:34 +0100 Subject: [PATCH 2/2] Move lower bound of flow variable from capacity.jl to variable creation --- src/constraints/capacity.jl | 11 ----------- src/create-model.jl | 2 +- src/variables/flows.jl | 31 +++++++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/constraints/capacity.jl b/src/constraints/capacity.jl index 3ac82201..4115cc5f 100644 --- a/src/constraints/capacity.jl +++ b/src/constraints/capacity.jl @@ -358,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 diff --git a/src/create-model.jl b/src/create-model.jl index 53e262a2..9f3c6068 100644 --- a/src/create-model.jl +++ b/src/create-model.jl @@ -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, diff --git a/src/variables/flows.jl b/src/variables/flows.jl index a2d524f9..030e3bd1 100644 --- a/src/variables/flows.jl +++ b/src/variables/flows.jl @@ -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