switch_model/utilities/__init__.py
def check_mandatory_components(model, *mandatory_model_components):
    """
    Checks whether mandatory elements of a Pyomo model are populated,
    and returns a clear error message if they don't exist.

    Typically, this method is not used directly. Instead, the
    min_data_check() method will set up a BuildCheck that uses this
    function.

    If an argument is a set, it must have non-zero length.

    If an argument is an indexed parameter, it must have a value for
    every index in the indexed set. Do not use this for indexed params
    that have default values. If the set indexing a param is not
    mandatory and is empty, then the indexed parameter may be empty as
    well.

    If an argument is a simple parameter, it must have a value.

    This does not work with indexed sets.

    EXAMPLE:
    >>> from pyomo.environ import *
    >>> import switch_model.utilities as utilities
    >>> mod = ConcreteModel()
    >>> mod.set_A = Set(initialize=[1,2])
    >>> mod.paramA_full = Param(mod.set_A, initialize={1:'a',2:'b'}, within=Any)
    >>> mod.paramA_empty = Param(mod.set_A)
    >>> mod.set_B = Set()
    >>> mod.paramB_empty = Param(mod.set_B)
    >>> mod.paramC = Param(initialize=1)
    >>> mod.paramD = Param()
    >>> utilities.check_mandatory_components(mod, 'set_A', 'paramA_full')
    True
    >>> utilities.check_mandatory_components(mod, 'paramB_empty')
    True
    >>> utilities.check_mandatory_components(mod, 'paramC')
    True
    >>> utilities.check_mandatory_components(\
        mod, 'set_A', 'paramA_empty') # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: Values are not provided for every element of the mandatory parameter 'paramA_empty'. Missing data for 2 values, including: [1, 2]
    >>> utilities.check_mandatory_components(mod, 'set_A', 'set_B')
    Traceback (most recent call last):
        ...
    ValueError: No data is defined for the mandatory set 'set_B'.

    # Demonstration of incorporating this function into Pyomo's BuildCheck()
    >>> mod.min_dat_pass = BuildCheck(\
            rule=lambda m: utilities.check_mandatory_components(\
                m, 'set_A', 'paramA_full','paramB_empty', 'paramC'))
    """

    for component_name in mandatory_model_components:
        obj = getattr(model, component_name)
        o_class = type(obj).__name__
        if o_class == 'ScalarSet' or o_class == 'OrderedScalarSet':
            if len(obj) == 0:
                raise ValueError(
                    "No data is defined for the mandatory set '{}'.".
                    format(component_name))
        elif o_class == 'IndexedParam':
            if len(obj) != len(obj._index):
                missing_index_elements = [v for v in set(obj._index) - set( obj.sparse_keys())]
                raise ValueError(
                    "Values are not provided for every element of the "
                    "mandatory parameter '{}'. "
                    "Missing data for {} values, including: {}"
                    .format(component_name, len(missing_index_elements), missing_index_elements[:10])
                )
        elif o_class == 'IndexedSet':
            if len(obj) != len(obj._index):
                raise ValueError(
                    ("Sets are not defined for every index of " +
                     "the mandatory indexed set '{}'").format(component_name))
        elif o_class == 'ScalarParam':
            if obj.value is None:
                raise ValueError(
                    "Value not provided for mandatory parameter '{}'".
                    format(component_name))
        else:
            raise ValueError(
                "Error! Object type {} not recognized for model element '{}'.".
                format(o_class, component_name))
    return True

    Checks whether mandatory elements of a Pyomo model are populated,
    and returns a clear error message if they don't exist.

    Typically, this method is not used directly. Instead, the
    min_data_check() method will set up a BuildCheck that uses this
    function.

    If an argument is a set, it must have non-zero length.

    If an argument is an indexed parameter, it must have a value for
    every index in the indexed set. Do not use this for indexed params
    that have default values. If the set indexing a param is not
    mandatory and is empty, then the indexed parameter may be empty as
    well.

    If an argument is a simple parameter, it must have a value.

    This does not work with indexed sets.

    EXAMPLE:
    >>> from pyomo.environ import *
    >>> import switch_model.utilities as utilities
    >>> mod = ConcreteModel()
    >>> mod.set_A = Set(initialize=[1,2])
    >>> mod.paramA_full = Param(mod.set_A, initialize={1:'a',2:'b'}, within=Any)
    >>> mod.paramA_empty = Param(mod.set_A)
    >>> mod.set_B = Set()
    >>> mod.paramB_empty = Param(mod.set_B)
    >>> mod.paramC = Param(initialize=1)
    >>> mod.paramD = Param()
    >>> utilities.check_mandatory_components(mod, 'set_A', 'paramA_full')
    True
    >>> utilities.check_mandatory_components(mod, 'paramB_empty')
    True
    >>> utilities.check_mandatory_components(mod, 'paramC')
    True
    >>> utilities.check_mandatory_components(\
        mod, 'set_A', 'paramA_empty') # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: Values are not provided for every element of the mandatory parameter 'paramA_empty'. Missing data for 2 values, including: [1, 2]
    >>> utilities.check_mandatory_components(mod, 'set_A', 'set_B')
    Traceback (most recent call last):
        ...
    ValueError: No data is defined for the mandatory set 'set_B'.

    # Demonstration of incorporating this function into Pyomo's BuildCheck()
    >>> mod.min_dat_pass = BuildCheck(\
            rule=lambda m: utilities.check_mandatory_components(\
                m, 'set_A', 'paramA_full','paramB_empty', 'paramC'))
    
Checks whether mandatory elements of a Pyomo model are populated,
and returns a clear error message if they don't exist.

Typically, this method is not used directly. Instead, the
min_data_check() method will set up a BuildCheck that uses this
function.

If an argument is a set, it must have non-zero length.

If an argument is an indexed parameter, it must have a value for
every index in the indexed set. Do not use this for indexed params
that have default values. If the set indexing a param is not
mandatory and is empty, then the indexed parameter may be empty as
well.

If an argument is a simple parameter, it must have a value.

This does not work with indexed sets.




Checks whether mandatory elements of a Pyomo model are populated and returns a clear error message if they don ' t exist . Typically this method is not used directly . Instead the min_data_check () method will set up a BuildCheck that uses this function . If an argument is a set it must have non - zero length . If an argument is an indexed parameter it must have a value for every index in the indexed set . Do not use this for indexed params that have default values . If the set indexing a param is not mandatory and is empty then the indexed parameter may be empty as well . If an argument is a simple parameter it must have a value . This does not work with indexed sets .
def check_mandatory_components(model, *mandatory_model_components):
    for component_name in mandatory_model_components:
        obj = getattr(model, component_name)
        o_class = type(obj).__name__
        if o_class == 'ScalarSet' or o_class == 'OrderedScalarSet':
            if len(obj) == 0:
                raise ValueError(
                    "No data is defined for the mandatory set '{}'.".
                    format(component_name))
        elif o_class == 'IndexedParam':
            if len(obj) != len(obj._index):
                missing_index_elements = [v for v in set(obj._index) - set( obj.sparse_keys())]
                raise ValueError(
                    "Values are not provided for every element of the "
                    "mandatory parameter '{}'. "
                    "Missing data for {} values, including: {}"
                    .format(component_name, len(missing_index_elements), missing_index_elements[:10])
                )
        elif o_class == 'IndexedSet':
            if len(obj) != len(obj._index):
                raise ValueError(
                    ("Sets are not defined for every index of " +
                     "the mandatory indexed set '{}'").format(component_name))
        elif o_class == 'ScalarParam':
            if obj.value is None:
                raise ValueError(
                    "Value not provided for mandatory parameter '{}'".
                    format(component_name))
        else:
            raise ValueError(
                "Error! Object type {} not recognized for model element '{}'.".
                format(o_class, component_name))
    return True
def check_mandatory_components ( model , * mandatory_model_components ) : for component_name in mandatory_model_components : obj = getattr ( model , component_name ) o_class = type ( obj ) . __name__ if o_class == 'ScalarSet' or o_class == 'OrderedScalarSet' : if len ( obj ) == 0 : raise ValueError ( "No data is defined for the mandatory set '{}'." . format ( component_name ) ) elif o_class == 'IndexedParam' : if len ( obj ) != len ( obj . _index ) : missing_index_elements = [ v for v in set ( obj . _index ) - set ( obj . sparse_keys ( ) ) ] raise ValueError ( "Values are not provided for every element of the " "mandatory parameter '{}'. " "Missing data for {} values, including: {}" . format ( component_name , len ( missing_index_elements ) , missing_index_elements [ : 10 ] ) ) elif o_class == 'IndexedSet' : if len ( obj ) != len ( obj . _index ) : raise ValueError ( ( "Sets are not defined for every index of " + "the mandatory indexed set '{}'" ) . format ( component_name ) ) elif o_class == 'ScalarParam' : if obj . value is None : raise ValueError ( "Value not provided for mandatory parameter '{}'" . format ( component_name ) ) else : raise ValueError ( "Error! Object type {} not recognized for model element '{}'." . format ( o_class , component_name ) ) return True
Checks whether mandatory elements of a Pyomo model are populated,
and returns a clear error message if they don't exist.
Checks whether mandatory elements of a Pyomo model are populated and returns a clear error message if they don ' t exist .
"""
    Checks whether mandatory elements of a Pyomo model are populated,
    and returns a clear error message if they don't exist.

    Typically, this method is not used directly. Instead, the
    min_data_check() method will set up a BuildCheck that uses this
    function.

    If an argument is a set, it must have non-zero length.

    If an argument is an indexed parameter, it must have a value for
    every index in the indexed set. Do not use this for indexed params
    that have default values. If the set indexing a param is not
    mandatory and is empty, then the indexed parameter may be empty as
    well.

    If an argument is a simple parameter, it must have a value.

    This does not work with indexed sets.

    EXAMPLE:
    >>> from pyomo.environ import *
    >>> import switch_model.utilities as utilities
    >>> mod = ConcreteModel()
    >>> mod.set_A = Set(initialize=[1,2])
    >>> mod.paramA_full = Param(mod.set_A, initialize={1:'a',2:'b'}, within=Any)
    >>> mod.paramA_empty = Param(mod.set_A)
    >>> mod.set_B = Set()
    >>> mod.paramB_empty = Param(mod.set_B)
    >>> mod.paramC = Param(initialize=1)
    >>> mod.paramD = Param()
    >>> utilities.check_mandatory_components(mod, 'set_A', 'paramA_full')
    True
    >>> utilities.check_mandatory_components(mod, 'paramB_empty')
    True
    >>> utilities.check_mandatory_components(mod, 'paramC')
    True
    >>> utilities.check_mandatory_components(\
        mod, 'set_A', 'paramA_empty') # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: Values are not provided for every element of the mandatory parameter 'paramA_empty'. Missing data for 2 values, including: [1, 2]
    >>> utilities.check_mandatory_components(mod, 'set_A', 'set_B')
    Traceback (most recent call last):
        ...
    ValueError: No data is defined for the mandatory set 'set_B'.

    # Demonstration of incorporating this function into Pyomo's BuildCheck()
    >>> mod.min_dat_pass = BuildCheck(\
            rule=lambda m: utilities.check_mandatory_components(\
                m, 'set_A', 'paramA_full','paramB_empty', 'paramC'))
    """
model