Skip to content

Commit

Permalink
use tables instead of groups
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada committed Dec 18, 2024
1 parent 1256d32 commit cb6b285
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 31 deletions.
34 changes: 34 additions & 0 deletions src/constraints/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function compute_constraints_indices(connection)
:min_energy_over_clustered_year,
:max_energy_over_clustered_year,
:transport_flow_limit,
:group_max_investment_limit,
:group_min_investment_limit,
)
)

Expand Down Expand Up @@ -363,5 +365,37 @@ function _create_constraints_tables(connection)
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_group_max_investment_limit AS
SELECT
nextval('id') AS index,
ga.name,
ga.milestone_year,
ga.max_investment_limit,
FROM group_asset AS ga
WHERE
ga.invest_method AND
ga.max_investment_limit IS NOT NULL
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_group_min_investment_limit AS
SELECT
nextval('id') AS index,
ga.name,
ga.milestone_year,
ga.min_investment_limit,
FROM group_asset AS ga
WHERE
ga.invest_method AND
ga.min_investment_limit IS NOT NULL
",
)

return
end
77 changes: 48 additions & 29 deletions src/constraints/group.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export add_group_constraints!
Adds group constraints for assets that share a common limits or bounds
"""
function add_group_constraints!(model, variables, graph, sets, groups)
function add_group_constraints!(model, variables, constraints, graph, sets, groups)
# unpack from sets
Ai = sets[:Ai]
Y = sets[:Y]
Expand All @@ -14,41 +14,60 @@ function add_group_constraints!(model, variables, graph, sets, groups)

# - Group constraints for investments at each year
assets_at_year_in_group = Dict(
group => (
group.name => (
(a, y) for y in Y for
a in Ai[y] if !ismissing(graph[a].group) && graph[a].group == group.name
) for group in groups
)
@expression(
model,
investment_group[group in groups],
if group.invest_method
sum(
graph[a].capacity * assets_investment[y, a] for y in Y for
(a, y) in assets_at_year_in_group[group]
)
end
)

groups_with_max_investment_limit =
(group for group in groups if !ismissing(group.max_investment_limit))
model[:investment_group_max_limit] = [
@constraint(
for table_name in [:group_max_investment_limit, :group_min_investment_limit]
cons = constraints[table_name]
attach_expression!(
cons,
:investment_group,
[
@expression(
model,
sum(
graph[a].capacity * assets_investment[y, a] for y in Y for
(a, y) in assets_at_year_in_group[row.name]
)
) for row in eachrow(cons.indices)
],
)
end

let table_name = :group_max_investment_limit, cons = constraints[table_name]
attach_constraint!(
model,
investment_group[group] group.max_investment_limit,
base_name = "investment_group_max_limit[$(group.name)]"
) for group in groups_with_max_investment_limit
]

groups_with_min_investment_limit =
(group for group in groups if !ismissing(group.min_investment_limit))
model[:investment_group_min_limit] = [
@constraint(
cons,
:investment_group_max_limit,
[
@constraint(
model,
investment_group row.max_investment_limit,
base_name = "investment_group_max_limit[$(row.name)]"
) for (row, investment_group) in
zip(eachrow(cons.indices), cons.expressions[:investment_group])
],
)
end

let table_name = :group_min_investment_limit, cons = constraints[table_name]
attach_constraint!(
model,
investment_group[group] group.min_investment_limit,
base_name = "investment_group_min_limit[$(group.name)]"
) for group in groups_with_min_investment_limit
]
cons,
:investment_group_min_limit,
[
@constraint(
model,
investment_group row.min_investment_limit,
base_name = "investment_group_min_limit[$(row.name)]"
) for (row, investment_group) in
zip(eachrow(cons.indices), cons.expressions[:investment_group])
],
)
end

# - TODO: More group constraints e.g., limits on the accumulated investments of a group

Expand Down
1 change: 1 addition & 0 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function create_model(
@timeit to "add_group_constraints!" add_group_constraints!(
model,
variables,
constraints,
graph,
sets,
groups,
Expand Down
2 changes: 1 addition & 1 deletion src/input-schemas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const schemas = (
groups = (
# Schema for the group-asset.csv file.
data = OrderedDict(
:group => "VARCHAR", # Name of the Group
:name => "VARCHAR", # Name of the Group
:milestone_year => "INTEGER",
:invest_method => "BOOLEAN", # true -> activate group constraints; false -> no group investment constraints
:min_investment_limit => "DOUBLE", # MW (Missing -> no limit)
Expand Down
2 changes: 1 addition & 1 deletion test/inputs/Norse/group-asset.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group_name,,{false; true},MW,MW
group,milestone_year,invest_method,min_investment_limit,max_investment_limit
name,milestone_year,invest_method,min_investment_limit,max_investment_limit
renewables,2030,true,,40000
ccgt,2030,true,10000,

0 comments on commit cb6b285

Please sign in to comment.