switch_model/generators/extensions/hydro_simple.py
def define_components(mod):
    """
    HYDRO_GENS is the set of dispatchable hydro projects. This is a subet
    of GENERATION_PROJECTS, and is determined by the inputs file hydro_timeseries.csv.
    Members of this set can be called either g, or hydro_g.

    HYDRO_TS is the set of hydro timeseries over which the average flow constraint is defined.
    These hydro timeseries are different from the timeseries used in the reset of SWITCH
    and are defined by the input file hydro_timepoints.csv. If hydro_timepoints.csv doesn't exist,
    the default is for the timeseries to be the same as the SWITCH timeseries from timeseries.csv.
    Members of this set can be abbreviated as hts.

    HYDRO_GEN_TS is the set of Hydro projects and hydro timeseries for which
    minimum and average flow are specified. Members of this set can be
    abbreviated as (project, hydro_timeseries) or (g, hts).

    HYDRO_GEN_TPS is the set of Hydro projects and available
    dispatch points. This is a filtered version of GEN_TPS that
    only includes hydro projects.

    tp_to_hts[tp in TIMEPOINTS] is a parameter that returns the hydro timeseries
    for a given timepoint. It is defined in hydro_timepoints.csv and if unspecified
    it defaults to be equal to tp_ts.

    hydro_min_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
    determines minimum flow levels, specified in units of MW dispatch.

    hydro_avg_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
    determines average flow levels, specified in units of MW dispatch.

    Enforce_Hydro_Min_Flow[(g, t) in HYDRO_GEN_TPS] is a
    constraint that enforces minimum flow levels for each timepoint.

    Enforce_Hydro_Avg_Flow[(g, hts) in HYDRO_GEN_TS] is a constraint
    that enforces average flow levels across each hydro timeseries.
    """
    mod.tp_to_hts = Param(
        mod.TIMEPOINTS,
        input_file='hydro_timepoints.csv',
        default=lambda m, tp: m.tp_ts[tp],
        doc="Mapping of timepoints to a hydro series.",
        within=Any
    )

    mod.HYDRO_TS = Set(
        dimen=1,
        ordered=False,
        initialize=lambda m: set(m.tp_to_hts[tp] for tp in m.TIMEPOINTS),
        doc="Set of hydro timeseries as defined in the mapping."
    )

    mod.TPS_IN_HTS = Set(
        mod.HYDRO_TS,
        within=mod.TIMEPOINTS,
        ordered=False,
        initialize=lambda m, hts: set(t for t in m.TIMEPOINTS if m.tp_to_hts[t] == hts),
        doc="Set of timepoints in each hydro timeseries"
    )

    mod.HYDRO_GEN_TS_RAW = Set(
        dimen=2,
        input_file='hydro_timeseries.csv',
        input_optional=True,
        validate=lambda m, g, hts: (g in m.GENERATION_PROJECTS) & (hts in m.HYDRO_TS), )

    mod.HYDRO_GENS = Set(
        dimen=1,
        ordered=False,
        initialize=lambda m: set(g for (g, hts) in m.HYDRO_GEN_TS_RAW),
        doc="Dispatchable hydro projects")

    mod.HYDRO_GEN_TPS = Set(
        initialize=mod.GEN_TPS,
        filter=lambda m, g, t: g in m.HYDRO_GENS)

    mod.HYDRO_GEN_TS = Set(
        dimen=2,
        initialize=lambda m: set((g, m.tp_to_hts[tp]) for (g, tp) in m.HYDRO_GEN_TPS))

    # Validate that a timeseries data is specified for every hydro generator /
    # timeseries that we need. Extra data points (ex: outside of planning
    # horizon or beyond a plant's lifetime) can safely be ignored to make it
    # easier to create input files.
    mod.have_minimal_hydro_params = BuildCheck(
        mod.HYDRO_GEN_TS,
        rule=lambda m, g, hts: (g, hts) in m.HYDRO_GEN_TS_RAW)

    # Todo: Add validation check that timeseries data are specified for every valid timepoint.

    mod.hydro_min_flow_mw = Param(
        mod.HYDRO_GEN_TS_RAW,
        within=NonNegativeReals,
        input_file='hydro_timeseries.csv',
        default=0.0)
    mod.Enforce_Hydro_Min_Flow = Constraint(
        mod.HYDRO_GEN_TPS,
        rule=lambda m, g, t: Constraint.Skip
        if m.hydro_min_flow_mw[g, m.tp_to_hts[t]] == 0
        else m.DispatchGen[g, t] >= m.hydro_min_flow_mw[g, m.tp_to_hts[t]])

    mod.hydro_avg_flow_mw = Param(
        mod.HYDRO_GEN_TS_RAW,
        within=NonNegativeReals,
        input_file='hydro_timeseries.csv')

    # We use a scaling factor to improve the numerical properties
    # of the model. The scaling factor was determined using trial
    # and error and this tool https://github.com/staadecker/lp-analyzer.
    # Learn more by reading the documentation on Numerical Issues.
    enforce_hydro_avg_flow_scaling_factor = 1e1
    mod.Enforce_Hydro_Avg_Flow = Constraint(
        mod.HYDRO_GEN_TS,
        rule=lambda m, g, hts:
        enforce_hydro_avg_flow_scaling_factor *
        # Compute the weighted average of the dispatch
        sum(m.DispatchGen[g, t] * m.tp_weight[t] for t in m.TPS_IN_HTS[hts])
        / sum(m.tp_weight[tp] for tp in m.TPS_IN_HTS[hts])
        <= m.hydro_avg_flow_mw[g, hts] * enforce_hydro_avg_flow_scaling_factor
    )

    mod.min_data_check('hydro_min_flow_mw', 'hydro_avg_flow_mw')

    HYDRO_GENS is the set of dispatchable hydro projects. This is a subet
    of GENERATION_PROJECTS, and is determined by the inputs file hydro_timeseries.csv.
    Members of this set can be called either g, or hydro_g.

    HYDRO_TS is the set of hydro timeseries over which the average flow constraint is defined.
    These hydro timeseries are different from the timeseries used in the reset of SWITCH
    and are defined by the input file hydro_timepoints.csv. If hydro_timepoints.csv doesn't exist,
    the default is for the timeseries to be the same as the SWITCH timeseries from timeseries.csv.
    Members of this set can be abbreviated as hts.

    HYDRO_GEN_TS is the set of Hydro projects and hydro timeseries for which
    minimum and average flow are specified. Members of this set can be
    abbreviated as (project, hydro_timeseries) or (g, hts).

    HYDRO_GEN_TPS is the set of Hydro projects and available
    dispatch points. This is a filtered version of GEN_TPS that
    only includes hydro projects.

    tp_to_hts[tp in TIMEPOINTS] is a parameter that returns the hydro timeseries
    for a given timepoint. It is defined in hydro_timepoints.csv and if unspecified
    it defaults to be equal to tp_ts.

    hydro_min_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
    determines minimum flow levels, specified in units of MW dispatch.

    hydro_avg_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
    determines average flow levels, specified in units of MW dispatch.

    Enforce_Hydro_Min_Flow[(g, t) in HYDRO_GEN_TPS] is a
    constraint that enforces minimum flow levels for each timepoint.

    Enforce_Hydro_Avg_Flow[(g, hts) in HYDRO_GEN_TS] is a constraint
    that enforces average flow levels across each hydro timeseries.
    
HYDRO_GENS is the set of dispatchable hydro projects. This is a subet
of GENERATION_PROJECTS, and is determined by the inputs file hydro_timeseries.csv.
Members of this set can be called either g, or hydro_g.

HYDRO_TS is the set of hydro timeseries over which the average flow constraint is defined.
These hydro timeseries are different from the timeseries used in the reset of SWITCH
and are defined by the input file hydro_timepoints.csv. If hydro_timepoints.csv doesn't exist,
the default is for the timeseries to be the same as the SWITCH timeseries from timeseries.csv.
Members of this set can be abbreviated as hts.

HYDRO_GEN_TS is the set of Hydro projects and hydro timeseries for which
minimum and average flow are specified. Members of this set can be
abbreviated as (project, hydro_timeseries) or (g, hts).

HYDRO_GEN_TPS is the set of Hydro projects and available
dispatch points. This is a filtered version of GEN_TPS that
only includes hydro projects.

tp_to_hts[tp in TIMEPOINTS] is a parameter that returns the hydro timeseries
for a given timepoint. It is defined in hydro_timepoints.csv and if unspecified
it defaults to be equal to tp_ts.

hydro_min_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
determines minimum flow levels, specified in units of MW dispatch.

hydro_avg_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
determines average flow levels, specified in units of MW dispatch.

Enforce_Hydro_Min_Flow[(g, t) in HYDRO_GEN_TPS] is a
constraint that enforces minimum flow levels for each timepoint.

Enforce_Hydro_Avg_Flow[(g, hts) in HYDRO_GEN_TS] is a constraint
that enforces average flow levels across each hydro timeseries.
HYDRO_GENS is the set of dispatchable hydro projects . This is a subet of GENERATION_PROJECTS and is determined by the inputs file hydro_timeseries . csv . Members of this set can be called either g or hydro_g . HYDRO_TS is the set of hydro timeseries over which the average flow constraint is defined . These hydro timeseries are different from the timeseries used in the reset of SWITCH and are defined by the input file hydro_timepoints . csv . If hydro_timepoints . csv doesn ' t exist the default is for the timeseries to be the same as the SWITCH timeseries from timeseries . csv . Members of this set can be abbreviated as hts . HYDRO_GEN_TS is the set of Hydro projects and hydro timeseries for which minimum and average flow are specified . Members of this set can be abbreviated as ( project hydro_timeseries ) or ( g hts ) . HYDRO_GEN_TPS is the set of Hydro projects and available dispatch points . This is a filtered version of GEN_TPS that only includes hydro projects . tp_to_hts [ tp in TIMEPOINTS ] is a parameter that returns the hydro timeseries for a given timepoint . It is defined in hydro_timepoints . csv and if unspecified it defaults to be equal to tp_ts . hydro_min_flow_mw [ ( g hts ) in HYDRO_GEN_TS ] is a parameter that determines minimum flow levels specified in units of MW dispatch . hydro_avg_flow_mw [ ( g hts ) in HYDRO_GEN_TS ] is a parameter that determines average flow levels specified in units of MW dispatch . Enforce_Hydro_Min_Flow [ ( g t ) in HYDRO_GEN_TPS ] is a constraint that enforces minimum flow levels for each timepoint . Enforce_Hydro_Avg_Flow [ ( g hts ) in HYDRO_GEN_TS ] is a constraint that enforces average flow levels across each hydro timeseries .
def define_components(mod):
    mod.tp_to_hts = Param(
        mod.TIMEPOINTS,
        input_file='hydro_timepoints.csv',
        default=lambda m, tp: m.tp_ts[tp],
        doc="Mapping of timepoints to a hydro series.",
        within=Any
    )
    mod.HYDRO_TS = Set(
        dimen=1,
        ordered=False,
        initialize=lambda m: set(m.tp_to_hts[tp] for tp in m.TIMEPOINTS),
        doc="Set of hydro timeseries as defined in the mapping."
    )
    mod.TPS_IN_HTS = Set(
        mod.HYDRO_TS,
        within=mod.TIMEPOINTS,
        ordered=False,
        initialize=lambda m, hts: set(t for t in m.TIMEPOINTS if m.tp_to_hts[t] == hts),
        doc="Set of timepoints in each hydro timeseries"
    )
    mod.HYDRO_GEN_TS_RAW = Set(
        dimen=2,
        input_file='hydro_timeseries.csv',
        input_optional=True,
        validate=lambda m, g, hts: (g in m.GENERATION_PROJECTS) & (hts in m.HYDRO_TS), )
    mod.HYDRO_GENS = Set(
        dimen=1,
        ordered=False,
        initialize=lambda m: set(g for (g, hts) in m.HYDRO_GEN_TS_RAW),
        doc="Dispatchable hydro projects")
    mod.HYDRO_GEN_TPS = Set(
        initialize=mod.GEN_TPS,
        filter=lambda m, g, t: g in m.HYDRO_GENS)
    mod.HYDRO_GEN_TS = Set(
        dimen=2,
        initialize=lambda m: set((g, m.tp_to_hts[tp]) for (g, tp) in m.HYDRO_GEN_TPS))
    mod.have_minimal_hydro_params = BuildCheck(
        mod.HYDRO_GEN_TS,
        rule=lambda m, g, hts: (g, hts) in m.HYDRO_GEN_TS_RAW)
    mod.hydro_min_flow_mw = Param(
        mod.HYDRO_GEN_TS_RAW,
        within=NonNegativeReals,
        input_file='hydro_timeseries.csv',
        default=0.0)
    mod.Enforce_Hydro_Min_Flow = Constraint(
        mod.HYDRO_GEN_TPS,
        rule=lambda m, g, t: Constraint.Skip
        if m.hydro_min_flow_mw[g, m.tp_to_hts[t]] == 0
        else m.DispatchGen[g, t] >= m.hydro_min_flow_mw[g, m.tp_to_hts[t]])
    mod.hydro_avg_flow_mw = Param(
        mod.HYDRO_GEN_TS_RAW,
        within=NonNegativeReals,
        input_file='hydro_timeseries.csv')
    enforce_hydro_avg_flow_scaling_factor = 1e1
    mod.Enforce_Hydro_Avg_Flow = Constraint(
        mod.HYDRO_GEN_TS,
        rule=lambda m, g, hts:
        enforce_hydro_avg_flow_scaling_factor *
        sum(m.DispatchGen[g, t] * m.tp_weight[t] for t in m.TPS_IN_HTS[hts])
        / sum(m.tp_weight[tp] for tp in m.TPS_IN_HTS[hts])
        <= m.hydro_avg_flow_mw[g, hts] * enforce_hydro_avg_flow_scaling_factor
    )
    mod.min_data_check('hydro_min_flow_mw', 'hydro_avg_flow_mw')
def define_components ( mod ) : mod . tp_to_hts = Param ( mod . TIMEPOINTS , input_file = 'hydro_timepoints.csv' , default = lambda m , tp : m . tp_ts [ tp ] , doc = "Mapping of timepoints to a hydro series." , within = Any ) mod . HYDRO_TS = Set ( dimen = 1 , ordered = False , initialize = lambda m : set ( m . tp_to_hts [ tp ] for tp in m . TIMEPOINTS ) , doc = "Set of hydro timeseries as defined in the mapping." ) mod . TPS_IN_HTS = Set ( mod . HYDRO_TS , within = mod . TIMEPOINTS , ordered = False , initialize = lambda m , hts : set ( t for t in m . TIMEPOINTS if m . tp_to_hts [ t ] == hts ) , doc = "Set of timepoints in each hydro timeseries" ) mod . HYDRO_GEN_TS_RAW = Set ( dimen = 2 , input_file = 'hydro_timeseries.csv' , input_optional = True , validate = lambda m , g , hts : ( g in m . GENERATION_PROJECTS ) & ( hts in m . HYDRO_TS ) , ) mod . HYDRO_GENS = Set ( dimen = 1 , ordered = False , initialize = lambda m : set ( g for ( g , hts ) in m . HYDRO_GEN_TS_RAW ) , doc = "Dispatchable hydro projects" ) mod . HYDRO_GEN_TPS = Set ( initialize = mod . GEN_TPS , filter = lambda m , g , t : g in m . HYDRO_GENS ) mod . HYDRO_GEN_TS = Set ( dimen = 2 , initialize = lambda m : set ( ( g , m . tp_to_hts [ tp ] ) for ( g , tp ) in m . HYDRO_GEN_TPS ) ) mod . have_minimal_hydro_params = BuildCheck ( mod . HYDRO_GEN_TS , rule = lambda m , g , hts : ( g , hts ) in m . HYDRO_GEN_TS_RAW ) mod . hydro_min_flow_mw = Param ( mod . HYDRO_GEN_TS_RAW , within = NonNegativeReals , input_file = 'hydro_timeseries.csv' , default = 0.0 ) mod . Enforce_Hydro_Min_Flow = Constraint ( mod . HYDRO_GEN_TPS , rule = lambda m , g , t : Constraint . Skip if m . hydro_min_flow_mw [ g , m . tp_to_hts [ t ] ] == 0 else m . DispatchGen [ g , t ] >= m . hydro_min_flow_mw [ g , m . tp_to_hts [ t ] ] ) mod . hydro_avg_flow_mw = Param ( mod . HYDRO_GEN_TS_RAW , within = NonNegativeReals , input_file = 'hydro_timeseries.csv' ) enforce_hydro_avg_flow_scaling_factor = 1e1 mod . Enforce_Hydro_Avg_Flow = Constraint ( mod . HYDRO_GEN_TS , rule = lambda m , g , hts : enforce_hydro_avg_flow_scaling_factor * sum ( m . DispatchGen [ g , t ] * m . tp_weight [ t ] for t in m . TPS_IN_HTS [ hts ] ) / sum ( m . tp_weight [ tp ] for tp in m . TPS_IN_HTS [ hts ] ) <= m . hydro_avg_flow_mw [ g , hts ] * enforce_hydro_avg_flow_scaling_factor ) mod . min_data_check ( 'hydro_min_flow_mw' , 'hydro_avg_flow_mw' )
HYDRO_GENS is the set of dispatchable hydro projects.
HYDRO_GENS is the set of dispatchable hydro projects .
"""
    HYDRO_GENS is the set of dispatchable hydro projects. This is a subet
    of GENERATION_PROJECTS, and is determined by the inputs file hydro_timeseries.csv.
    Members of this set can be called either g, or hydro_g.

    HYDRO_TS is the set of hydro timeseries over which the average flow constraint is defined.
    These hydro timeseries are different from the timeseries used in the reset of SWITCH
    and are defined by the input file hydro_timepoints.csv. If hydro_timepoints.csv doesn't exist,
    the default is for the timeseries to be the same as the SWITCH timeseries from timeseries.csv.
    Members of this set can be abbreviated as hts.

    HYDRO_GEN_TS is the set of Hydro projects and hydro timeseries for which
    minimum and average flow are specified. Members of this set can be
    abbreviated as (project, hydro_timeseries) or (g, hts).

    HYDRO_GEN_TPS is the set of Hydro projects and available
    dispatch points. This is a filtered version of GEN_TPS that
    only includes hydro projects.

    tp_to_hts[tp in TIMEPOINTS] is a parameter that returns the hydro timeseries
    for a given timepoint. It is defined in hydro_timepoints.csv and if unspecified
    it defaults to be equal to tp_ts.

    hydro_min_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
    determines minimum flow levels, specified in units of MW dispatch.

    hydro_avg_flow_mw[(g, hts) in HYDRO_GEN_TS] is a parameter that
    determines average flow levels, specified in units of MW dispatch.

    Enforce_Hydro_Min_Flow[(g, t) in HYDRO_GEN_TPS] is a
    constraint that enforces minimum flow levels for each timepoint.

    Enforce_Hydro_Avg_Flow[(g, hts) in HYDRO_GEN_TS] is a constraint
    that enforces average flow levels across each hydro timeseries.
    """ # Validate that a timeseries data is specified for every hydro generator / # timeseries that we need. Extra data points (ex: outside of planning # horizon or beyond a plant's lifetime) can safely be ignored to make it # easier to create input files. # Todo: Add validation check that timeseries data are specified for every valid timepoint. # We use a scaling factor to improve the numerical properties # of the model. The scaling factor was determined using trial # and error and this tool https://github.com/staadecker/lp-analyzer. # Learn more by reading the documentation on Numerical Issues. # Compute the weighted average of the dispatch
mod