Skip to content

Commit

Permalink
Rename and split some constraints to reflect Miro (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira authored Dec 12, 2024
1 parent 3c78c1c commit f96f8c5
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 162 deletions.
74 changes: 32 additions & 42 deletions src/constraints/capacity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
flow = variables[:flow].container

## unpack from constraints
incoming_flow_highest_in_resolution = constraints[:highest_in].expressions[:incoming]
outgoing_flow_highest_out_resolution = constraints[:highest_out].expressions[:outgoing]
cons_incoming = constraints[:capacity_incoming]
cons_outgoing = constraints[:capacity_outgoing]
incoming_flow = cons_incoming.expressions[:incoming]
outgoing_flow = cons_outgoing.expressions[:outgoing]

## Expressions used by capacity constraints
# - Create capacity limit for outgoing flows
Expand Down Expand Up @@ -77,7 +79,7 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
graph[row.asset].capacity *
accumulated_units[accumulated_units_lookup[(row.asset, row.year)]]
)
end for row in eachrow(constraints[:highest_out].indices)
end for row in eachrow(cons_outgoing.indices)
]

# - Create accumulated investment limit for the use of binary storage method with investments
Expand Down Expand Up @@ -123,10 +125,8 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
(graph[row.asset].capacity * accumulated_initial_units[row.asset, row.year]) *
(1 - is_charging)
)
end for (row, is_charging) in zip(
eachrow(constraints[:highest_out].indices),
constraints[:highest_out].expressions[:is_charging],
)
end for (row, is_charging) in
zip(eachrow(cons_outgoing.indices), cons_outgoing.expressions[:is_charging])
]

assets_profile_times_capacity_out_with_binary_part2 =
Expand All @@ -149,10 +149,8 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
)
)
)
end for (row, is_charging) in zip(
eachrow(constraints[:highest_out].indices),
constraints[:highest_out].expressions[:is_charging],
)
end for (row, is_charging) in
zip(eachrow(cons_outgoing.indices), cons_outgoing.expressions[:is_charging])
]

# - Create capacity limit for incoming flows
Expand All @@ -171,7 +169,7 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
) *
graph[row.asset].capacity *
accumulated_units[accumulated_units_lookup[(row.asset, row.year)]]
) for row in eachrow(constraints[:highest_in].indices)
) for row in eachrow(cons_incoming.indices)
]

# - Create capacity limit for incoming flows with binary is_charging for storage assets
Expand Down Expand Up @@ -210,10 +208,8 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
(graph[row.asset].capacity * accumulated_initial_units[row.asset, row.year]) *
is_charging
)
end for (row, is_charging) in zip(
eachrow(constraints[:highest_in].indices),
constraints[:highest_in].expressions[:is_charging],
)
end for (row, is_charging) in
zip(eachrow(cons_incoming.indices), cons_incoming.expressions[:is_charging])
]

assets_profile_times_capacity_in_with_binary_part2 =
Expand All @@ -236,40 +232,36 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
)
)
)
end for (row, is_charging) in zip(
eachrow(constraints[:highest_in].indices),
constraints[:highest_in].expressions[:is_charging],
)
end for (row, is_charging) in
zip(eachrow(cons_incoming.indices), cons_incoming.expressions[:is_charging])
]

## Capacity limit constraints (using the highest resolution)
# - Maximum output flows limit
attach_constraint!(
model,
constraints[:highest_out],
cons_outgoing,
:max_output_flows_limit,
[
@constraint(
model,
outgoing_flow_highest_out_resolution[row.index]
assets_profile_times_capacity_out[row.index],
outgoing_flow[row.index] assets_profile_times_capacity_out[row.index],
base_name = "max_output_flows_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(constraints[:highest_out].indices)
) for row in eachrow(cons_outgoing.indices)
],
)

# - Maximum input flows limit
attach_constraint!(
model,
constraints[:highest_in],
cons_incoming,
:max_input_flows_limit,
[
@constraint(
model,
incoming_flow_highest_in_resolution[row.index]
assets_profile_times_capacity_in[row.index],
incoming_flow[row.index] assets_profile_times_capacity_in[row.index],
base_name = "max_input_flows_limit[$(row.asset),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(constraints[:highest_in].indices)
) for row in eachrow(cons_incoming.indices)
],
)

Expand All @@ -278,43 +270,41 @@ function add_capacity_constraints!(model, variables, constraints, graph, sets)
model[:max_output_flows_limit_with_binary_part1] = [
@constraint(
model,
outgoing_flow_highest_out_resolution[row.index]
outgoing_flow[row.index]
assets_profile_times_capacity_out_with_binary_part1[row.index],
base_name = "max_output_flows_limit_with_binary_part1[$(row.asset),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(constraints[:highest_out].indices) if
row.asset Asb && outgoing_flow_highest_out_resolution[row.index] != 0
) for row in eachrow(cons_outgoing.indices) if
row.asset Asb && outgoing_flow[row.index] != 0
]

model[:max_output_flows_limit_with_binary_part2] = [
@constraint(
model,
outgoing_flow_highest_out_resolution[row.index]
outgoing_flow[row.index]
assets_profile_times_capacity_out_with_binary_part2[row.index],
base_name = "max_output_flows_limit_with_binary_part2[$(row.asset),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(constraints[:highest_out].indices) if row.asset Ai[row.year] &&
row.asset Asb &&
outgoing_flow_highest_out_resolution[row.index] != 0
) for row in eachrow(cons_outgoing.indices) if
row.asset Ai[row.year] && row.asset Asb && outgoing_flow[row.index] != 0
]

# - Maximum input flows limit with is_charging binary for storage assets
model[:max_input_flows_limit_with_binary_part1] = [
@constraint(
model,
incoming_flow_highest_in_resolution[row.index]
incoming_flow[row.index]
assets_profile_times_capacity_in_with_binary_part1[row.index],
base_name = "max_input_flows_limit_with_binary_part1[$(row.asset),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(constraints[:highest_in].indices) if
row.asset Asb && incoming_flow_highest_in_resolution[row.index] != 0
) for row in eachrow(cons_incoming.indices) if
row.asset Asb && incoming_flow[row.index] != 0
]
model[:max_input_flows_limit_with_binary_part2] = [
@constraint(
model,
incoming_flow_highest_in_resolution[row.index]
incoming_flow[row.index]
assets_profile_times_capacity_in_with_binary_part2[row.index],
base_name = "max_input_flows_limit_with_binary_part2[$(row.asset),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(constraints[:highest_in].indices) if row.asset Ai[row.year] &&
row.asset Asb &&
incoming_flow_highest_in_resolution[row.index] != 0
) for row in eachrow(cons_incoming.indices) if
row.asset Ai[row.year] && row.asset Asb && incoming_flow[row.index] != 0
]

# - Lower limit for flows associated with assets
Expand Down
11 changes: 5 additions & 6 deletions src/constraints/consumer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ add_consumer_constraints!(model,
Adds the consumer asset constraints to the model.
"""

function add_consumer_constraints!(model, constraints, graph, sets)
Ac = sets[:Ac]
incoming_flow_highest_in_out_resolution = constraints[:highest_in_out].expressions[:incoming]
outgoing_flow_highest_in_out_resolution = constraints[:highest_in_out].expressions[:outgoing]
function add_consumer_constraints!(model, constraints, graph)
cons = constraints[:balance_consumer]
incoming_flow_highest_in_out_resolution = cons.expressions[:incoming]
outgoing_flow_highest_in_out_resolution = cons.expressions[:outgoing]

# - Balance constraint (using the lowest temporal resolution)
df = filter(:asset => (Ac), constraints[:highest_in_out].indices; view = true)
model[:consumer_balance] = [
@constraint(
model,
Expand All @@ -35,6 +34,6 @@ function add_consumer_constraints!(model, constraints, graph, sets)
) * graph[row.asset].peak_demand[row.year] in
graph[row.asset].consumer_balance_sense,
base_name = "consumer_balance[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(df)
) for row in eachrow(cons.indices)
]
end
11 changes: 5 additions & 6 deletions src/constraints/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ add_conversion_constraints!(model,
Adds the conversion asset constraints to the model.
"""

function add_conversion_constraints!(model, constraints, sets)
Acv = sets[:Acv]
function add_conversion_constraints!(model, constraints)
# - Balance constraint (using the lowest temporal resolution)
df = filter(:asset => (Acv), constraints[:lowest].indices; view = true)
incoming = constraints[:lowest].expressions[:incoming]
outgoing = constraints[:lowest].expressions[:outgoing]
cons = constraints[:balance_conversion]
incoming = cons.expressions[:incoming]
outgoing = cons.expressions[:outgoing]
model[:conversion_balance] = [
@constraint(
model,
incoming[row.index] == outgoing[row.index],
base_name = "conversion_balance[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(df)
) for row in eachrow(cons.indices)
]
end
82 changes: 62 additions & 20 deletions src/constraints/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ function compute_constraints_indices(connection)

constraints = Dict{Symbol,TulipaConstraint}(
key => TulipaConstraint(connection, "cons_$key") for key in (
:lowest,
:highest_in_out,
:highest_in,
:highest_out,
:units_on_and_outflows,
:storage_level_intra_rp,
:storage_level_inter_rp,
:min_energy_inter_rp,
:max_energy_inter_rp,
:balance_conversion,
:balance_consumer,
:balance_hub,
:capacity_incoming,
:capacity_outgoing,
:ramping_with_unit_commitment,
:ramping_without_unit_commitment,
:balance_storage_rep_period,
:balance_storage_over_clustered_year,
:min_energy_over_clustered_year,
:max_energy_over_clustered_year,
)
)

Expand All @@ -25,7 +27,7 @@ function _create_constraints_tables(connection)
DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_lowest AS
CREATE OR REPLACE TABLE cons_balance_conversion AS
SELECT
nextval('id') AS index,
asset.asset,
Expand All @@ -37,7 +39,7 @@ function _create_constraints_tables(connection)
LEFT JOIN asset
ON t_low.asset = asset.asset
WHERE
asset.type in ('conversion', 'producer')
asset.type in ('conversion')
ORDER BY
asset.asset,
t_low.year,
Expand All @@ -49,7 +51,7 @@ function _create_constraints_tables(connection)
DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_highest_in_out AS
CREATE OR REPLACE TABLE cons_balance_consumer AS
SELECT
nextval('id') AS index,
t_high.*
Expand All @@ -62,13 +64,34 @@ function _create_constraints_tables(connection)
AND t_high.year = asset_both.commission_year
WHERE
asset_both.active = true
AND asset.type in ('hub', 'consumer')",
AND asset.type = 'consumer';
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_balance_hub AS
SELECT
nextval('id') AS index,
t_high.*
FROM t_highest_all_flows AS t_high
LEFT JOIN asset
ON t_high.asset = asset.asset
LEFT JOIN asset_both
ON t_high.asset = asset_both.asset
AND t_high.year = asset_both.milestone_year
AND t_high.year = asset_both.commission_year
WHERE
asset_both.active = true
AND asset.type = 'hub';
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_highest_in AS
CREATE OR REPLACE TABLE cons_capacity_incoming AS
SELECT
nextval('id') AS index,
t_high.*
Expand All @@ -87,7 +110,7 @@ function _create_constraints_tables(connection)
DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_highest_out AS
CREATE OR REPLACE TABLE cons_capacity_outgoing AS
SELECT
nextval('id') AS index,
t_high.*
Expand All @@ -106,7 +129,7 @@ function _create_constraints_tables(connection)
DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_units_on_and_outflows AS
CREATE OR REPLACE TABLE cons_ramping_with_unit_commitment AS
SELECT
nextval('id') AS index,
t_high.*
Expand All @@ -126,22 +149,41 @@ function _create_constraints_tables(connection)

DuckDB.query(
connection,
"CREATE OR REPLACE TABLE cons_storage_level_intra_rp AS
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_ramping_without_unit_commitment AS
SELECT
nextval('id') AS index,
t_high.*
FROM t_highest_out_flows AS t_high
LEFT JOIN asset
ON t_high.asset = asset.asset
LEFT JOIN asset_both
ON t_high.asset = asset_both.asset
AND t_high.year = asset_both.milestone_year
AND t_high.year = asset_both.commission_year
WHERE
asset_both.active = true
AND asset.type in ('producer', 'storage', 'conversion')",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TABLE cons_balance_storage_rep_period AS
SELECT * FROM var_storage_level_intra_rp
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TABLE cons_storage_level_inter_rp AS
"CREATE OR REPLACE TABLE cons_balance_storage_over_clustered_year AS
SELECT * FROM var_storage_level_inter_rp
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_min_energy_inter_rp AS
CREATE OR REPLACE TABLE cons_min_energy_over_clustered_year AS
SELECT
nextval('id') AS index,
attr.asset,
Expand All @@ -162,7 +204,7 @@ function _create_constraints_tables(connection)
DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_max_energy_inter_rp AS
CREATE OR REPLACE TABLE cons_max_energy_over_clustered_year AS
SELECT
nextval('id') AS index,
attr.asset,
Expand Down
Loading

0 comments on commit f96f8c5

Please sign in to comment.