Skip to content

Commit 1a7682c

Browse files
authored
Create OPR_PRJS_IN_TMP set more efficiently (#1344)
1 parent 776131b commit 1a7682c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

gridpath/project/capacity/capacity.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Copyright 2016-2023 Blue Marble Analytics LLC.
1+
# Copyright 2016-2025 Blue Marble Analytics LLC.
2+
# Copyright 2026 Sylvan Energy Analytics LLC.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -179,7 +180,10 @@ def add_model_components(
179180
],
180181
)
181182

182-
m.OPR_PRJS_IN_TMP = Set(m.TMPS, initialize=op_gens_by_tmp)
183+
m.OPR_PRJS_IN_TMP = Set(
184+
m.TMPS,
185+
initialize=op_gens_by_tmp,
186+
)
183187

184188
# Expressions
185189
###########################################################################
@@ -281,18 +285,18 @@ def fuel_storage_capacity_rule(mod, prj, prd):
281285
###############################################################################
282286

283287

284-
# TODO: the creation of the OPR_PRJS_IN_TMPS is by far
285-
# the most time-consuming step in instantiating the problem; is there
286-
# any way to speed it up? It is perhaps inefficient to iterate over all
287-
# (g, t) for every timepoint, but how do we get around having to do that?
288-
# Also, this is a more general problem with all the indexed sets,
289-
# but the larger timepoints-based sets are more of a problem
290-
def op_gens_by_tmp(mod, tmp):
288+
def op_gens_by_tmp(mod):
291289
"""
292-
Figure out which generators are operational in each timepoins.
290+
Figure out which generators are operational in each timepoint.
293291
"""
294-
gens = list(g for (g, t) in mod.PRJ_OPR_TMPS if t == tmp)
295-
return gens
292+
tmp_to_projects = {}
293+
for g, tmp in mod.PRJ_OPR_TMPS:
294+
tmp_to_projects.setdefault(tmp, set()).add(g)
295+
296+
for tmp in tmp_to_projects:
297+
tmp_to_projects[tmp] = list(tmp_to_projects[tmp])
298+
299+
return tmp_to_projects
296300

297301

298302
def operational_periods_by_project(prj, project_operational_periods):

0 commit comments

Comments
 (0)