Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions switch_model/generators/core/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,23 @@ def define_components(mod):
mod.min_data_check('GENERATION_PROJECTS', 'gen_tech', 'gen_energy_source',
'gen_load_zone', 'gen_max_age', 'gen_is_variable')

"""Construct GENS_* indexed sets efficiently with a
'construction dictionary' pattern: on the first call, make a single
traversal through all generation projects to generate a complete index,
use that for subsequent lookups, and clean up at the last call."""
def GENS_IN_ZONE_init(m, z):
if not hasattr(m, 'GENS_IN_ZONE_dict'):
m.GENS_IN_ZONE_dict = {_z: [] for _z in m.LOAD_ZONES}
for g in m.GENERATION_PROJECTS:
m.GENS_IN_ZONE_dict[m.gen_load_zone[g]].append(g)
result = m.GENS_IN_ZONE_dict.pop(z)
if not m.GENS_IN_ZONE_dict:
del m.GENS_IN_ZONE_dict
return result
mod.GENS_IN_ZONE = Set(
mod.LOAD_ZONES,
initialize=lambda m, z: set(
g for g in m.GENERATION_PROJECTS if m.gen_load_zone[g] == z))
initialize=GENS_IN_ZONE_init
)
mod.VARIABLE_GENS = Set(
initialize=mod.GENERATION_PROJECTS,
filter=lambda m, g: m.gen_is_variable[g])
Expand All @@ -218,12 +231,19 @@ def define_components(mod):
mod.BASELOAD_GENS = Set(
initialize=mod.GENERATION_PROJECTS,
filter=lambda m, g: m.gen_is_baseload[g])
# TODO: use a construction dictionary or closure to create all the GENS_BY_...
# indexed sets more efficiently

def GENS_BY_TECHNOLOGY_init(m, t):
if not hasattr(m, 'GENS_BY_TECH_dict'):
m.GENS_BY_TECH_dict = {_t: [] for _t in m.GENERATION_TECHNOLOGIES}
for g in m.GENERATION_PROJECTS:
m.GENS_BY_TECH_dict[m.gen_tech[g]].append(g)
result = m.GENS_BY_TECH_dict.pop(t)
if not m.GENS_BY_TECH_dict:
del m.GENS_BY_TECH_dict
return result
mod.GENS_BY_TECHNOLOGY = Set(
mod.GENERATION_TECHNOLOGIES,
initialize=lambda m, t:
[g for g in m.GENERATION_PROJECTS if m.gen_tech[g] == t]
initialize=GENS_BY_TECHNOLOGY_init
)

mod.CAPACITY_LIMITED_GENS = Set(within=mod.GENERATION_PROJECTS)
Expand Down Expand Up @@ -263,15 +283,30 @@ def define_components(mod):
if g in m.MULTIFUEL_GENS
else [m.gen_energy_source[g]]))

def GENS_BY_ENERGY_SOURCE_init(m, e):
if not hasattr(m, 'GENS_BY_ENERGY_dict'):
m.GENS_BY_ENERGY_dict = {_e: [] for _e in m.ENERGY_SOURCES}
for g in m.GENERATION_PROJECTS:
if g in m.FUEL_BASED_GENS:
for f in m.FUELS_FOR_GEN[g]:
m.GENS_BY_ENERGY_dict[f].append(g)
else:
m.GENS_BY_ENERGY_dict[m.gen_energy_source[g]].append(g)
result = m.GENS_BY_ENERGY_dict.pop(e)
if not m.GENS_BY_ENERGY_dict:
del m.GENS_BY_ENERGY_dict
return result
mod.GENS_BY_ENERGY_SOURCE = Set(
mod.ENERGY_SOURCES,
initialize=GENS_BY_ENERGY_SOURCE_init
)
mod.GENS_BY_NON_FUEL_ENERGY_SOURCE = Set(
mod.NON_FUEL_ENERGY_SOURCES,
initialize=lambda m, s:
[g for g in m.NON_FUEL_BASED_GENS if m.gen_energy_source[g] == s]
initialize=lambda m, s: m.GENS_BY_ENERGY_SOURCE[s]
)
mod.GENS_BY_FUEL = Set(
mod.FUELS,
initialize=lambda m, f:
[g for g in m.FUEL_BASED_GENS if f in m.FUELS_FOR_GEN[g]]
initialize=lambda m, f: m.GENS_BY_ENERGY_SOURCE[f]
)

mod.PREDETERMINED_GEN_BLD_YRS = Set(
Expand Down
9 changes: 5 additions & 4 deletions switch_model/generators/core/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ def period_active_gen_rule(m, period):
mod.TPS_FOR_GEN = Set(
mod.GENERATION_PROJECTS,
within=mod.TIMEPOINTS,
rule=lambda m, g: (
initialize=lambda m, g: (
tp for p in m.PERIODS_FOR_GEN[g] for tp in m.TPS_IN_PERIOD[p]
)
)

def rule(m, gen, period):
def init(m, gen, period):
try:
d = m._TPS_FOR_GEN_IN_PERIOD_dict
except AttributeError:
Expand All @@ -181,8 +181,9 @@ def rule(m, gen, period):
if not d: # all gone, delete the attribute
del m._TPS_FOR_GEN_IN_PERIOD_dict
return result
mod.TPS_FOR_GEN_IN_PERIOD = Set(mod.GENERATION_PROJECTS, mod.PERIODS,
within=mod.TIMEPOINTS, rule=rule)
mod.TPS_FOR_GEN_IN_PERIOD = Set(
mod.GENERATION_PROJECTS, mod.PERIODS,
within=mod.TIMEPOINTS, initialize=init)

mod.GEN_TPS = Set(
dimen=2,
Expand Down