Skip to content

Commit

Permalink
Refactor group constraints to improve performance (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada authored Sep 19, 2024
1 parent 0892a04 commit 550a1c6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/src/formulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ The following constraints aggregate variables of different assets depending on t

These constraints apply to assets in a group using the investment method $\mathcal{G}^{\text{ai}}$. They help impose an investment potential of a spatial area commonly shared by several assets that can be invested there.

> **Note**: These constraints are applied to the investments each year. The model does not yet have investment limits to a group's accumulated invested capacity.
##### Minimum Investment Limit of a Group

```math
Expand Down
6 changes: 4 additions & 2 deletions docs/src/how-to-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,10 @@ The mathematical formulation of the maximum and minimum investment limit for gro
- `invest_method = true`. This parameter enables the model to use the investment group constraints.
- `min_investment_limit` $\neq$ `missing` or `max_investment_limit` $\neq$ `missing`. This value represents the limits that will be imposed on the investment that belongs to the group.

> **Note:**
> A missing value in the parameters `min_investment_limit` and `max_investment_limit` means that there is no investment limit.
> **Notes:**
>
> 1. A missing value in the parameters `min_investment_limit` and `max_investment_limit` means that there is no investment limit.
> 2. These constraints are applied to the investments each year. The model does not yet have investment limits to a group's accumulated invested capacity.
#### Example

Expand Down
24 changes: 18 additions & 6 deletions src/constraints/group.jl
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
export add_group_constraints!

"""
add_group_constraints!(graph, Ai, assets_investment, groups)
add_group_constraints!(model, graph, ...)
Adds group constraints for assets that share a common limits or bounds
"""
function add_group_constraints!(model, graph, Y, Ai, assets_investment, groups)

# - Investment group constraints
# - Group constraints for investments at each year
assets_at_year_in_group = Dict(
group => (
(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 in Ai[y] if !ismissing(graph[a].group) && graph[a].group == group.name
(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(
model,
investment_group[group] group.max_investment_limit,
base_name = "investment_group_max_limit[$(group.name)]"
) for group in groups if !ismissing(group.max_investment_limit)
) 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(
model,
investment_group[group] group.min_investment_limit,
base_name = "investment_group_min_limit[$(group.name)]"
) for group in groups if !ismissing(group.min_investment_limit)
) for group in groups_with_min_investment_limit
]

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

end
18 changes: 10 additions & 8 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1215,14 +1215,16 @@ function create_model(
flows_investment,
)

@timeit to "add_group_constraints!" add_group_constraints!(
model,
graph,
Y,
Ai,
assets_investment,
groups,
)
if !isempty(groups)
@timeit to "add_group_constraints!" add_group_constraints!(
model,
graph,
Y,
Ai,
assets_investment,
groups,
)
end

if !isempty(dataframes[:units_on_and_outflows])
@timeit to "add_ramping_constraints!" add_ramping_constraints!(
Expand Down

0 comments on commit 550a1c6

Please sign in to comment.