Example #1
0
 def __setstate__(self, state):
     """
     This method must be defined for deepcopy/pickling because this
     class relies on component ids.
     """
     Component.__setstate__(self,state)
     ComponentMap.__setstate__(self,state)
Example #2
0
    def __init__(self, **kwds):

        # Suffix type information
        self._direction = None
        self._datatype = None
        self._rule = None

        # The suffix direction
        direction = kwds.pop('direction',Suffix.LOCAL)

        # The suffix datatype
        datatype = kwds.pop('datatype',Suffix.FLOAT)

        # The suffix construction rule
        # TODO: deprecate the use of 'rule'
        self._rule = kwds.pop('rule',None)
        self._rule = kwds.pop('initialize',self._rule)

        # Check that keyword values make sense (these function have
        # internal error checking).
        self.set_direction(direction)
        self.set_datatype(datatype)

        # Initialize base classes
        kwds.setdefault('ctype', Suffix)
        Component.__init__(self, **kwds)
        ComponentMap.__init__(self)

        if self._rule is None:
            self.construct()
Example #3
0
 def __init__(self, *args, **kwds):
     kwds.setdefault('ctype', ExternalFunction)
     Component.__init__(self, **kwds)
     self._constructed = True
     ### HACK ###
     # FIXME: We must declare an _index attribute because
     # block._add_temporary_set assumes ALL components define an
     # index.  Sigh.
     self._index = None
Example #4
0
 def __init__(self, obj):
     self._aliased_object = None
     if isinstance(obj, Alias):
         obj = obj._aliased_object()
     if obj is not None:
         self._aliased_object = weakref.ref(obj)
     ctype = Alias
     if isinstance(obj, Component):
         ctype = obj.type()
     else:
         if not isinstance(obj, ComponentData):
             raise TypeError("Aliased object must be an "
                             "instance of Component or ComponentData")
         ctype = obj.parent_component().type()
     Component.__init__(self, ctype=ctype)
Example #5
0
 def __init__(self, *args, **kwds):
     from pyomo.core.base.sets import process_setarg
     #
     kwds.pop('noruleinit', None)
     Component.__init__(self, **kwds)
     #
     self._data = {}
     #
     if len(args) == 0 or (len(args) == 1 and
                           args[0] is UnindexedComponent_set):
         #
         # If no indexing sets are provided, generate a dummy index
         #
         self._implicit_subsets = None
         self._index = UnindexedComponent_set
     elif len(args) == 1:
         #
         # If a single indexing set is provided, just process it.
         #
         self._implicit_subsets = None
         self._index = process_setarg(args[0])
     else:
         #
         # If multiple indexing sets are provided, process them all,
         # and store the cross-product of these sets.  The individual
         # sets need to stored in the Pyomo model, so the
         # _implicit_subsets class data is used for this temporary
         # storage.
         #
         # Example:  Pyomo allows things like
         # "Param([1,2,3], range(100), initialize=0)".  This
         # needs to create *3* sets: two SetOf components and then
         # the SetProduct.  That means that the component needs to
         # hold on to the implicit SetOf objects until the component
         # is assigned to a model (where the implicit subsets can be
         # "transferred" to the model).
         #
         tmp = [process_setarg(x) for x in args]
         self._implicit_subsets = tmp
         self._index = tmp[0].cross(*tmp[1:])
Example #6
0
 def __init__(self, *args, **kwds):
     from pyomo.core.base.sets import process_setarg
     #
     kwds.pop('noruleinit', None)
     Component.__init__(self, **kwds)
     #
     self._data = {}
     #
     if len(args) == 0:
         #
         # If no indexing sets are provided, generate a dummy index
         #
         self._implicit_subsets = None
         self._index = UnindexedComponent_set
     elif len(args) == 1:
         #
         # If a single indexing set is provided, just process it.
         #
         self._implicit_subsets = None
         self._index = process_setarg(args[0])
     else:
         #
         # If multiple indexing sets are provided, process them all,
         # and store the cross-product of these sets.  The individual
         # sets need to stored in the Pyomo model, so the
         # _implicit_subsets class data is used for this temporary
         # storage.
         #
         # Example:  Pyomo allows things like
         # "Param([1,2,3], range(100), initialize=0)".  This
         # needs to create *3* sets: two SetOf components and then
         # the SetProduct.  That means that the component needs to
         # hold on to the implicit SetOf objects until the component
         # is assigned to a model (where the implicit subsets can be
         # "transferred" to the model).
         #
         tmp = [process_setarg(x) for x in args]
         self._implicit_subsets = tmp
         self._index = tmp[0].cross(*tmp[1:])
Example #7
0
    def __init__(self, *args, **kwargs):
        """Construct a reference to an external function.

        There are two fundamental interfaces supported by
        :class:`ExternalFunction`: Python callback functions and AMPL
        external functions.

        **Python callback functions** (:class:`PythonCallbackFunction`
        interface)

        Python callback functions can be specified one of two ways:

        1. FGH interface:

          A single external function call with a signature matching the
          :meth:`evaluate_fgh()` method.

        2. Independent functions:

          One to three functions that can evaluate the function value,
          gradient of the function [partial derivatives] with respect to
          its inputs, and the Hessian of the function [partial second
          derivatives].  The ``function`` interface expects a function
          matching the prototype:

          .. code::

             def function(*args): float

          The ``gradient`` and ``hessian`` interface expect functions
          matching the prototype:

          .. code::

             def gradient_or_hessian(args, fixed=None): List[float]

          Where ``args`` is a tuple of function arguments and ``fixed``
          is either None or a list of values equal in length to ``args``
          indicating which arguments are currently fixed (``True``) or
          variable (``False``).

        **ASL function libraries** (:class:`AMPLExternalFunction` interface)

        Pyomo can also call functions compiled as part of an AMPL
        External Function library (see the `User-defined functions
        <https://www.ampl.com/REFS/HOOKING/#userdefinedfuncs>`_ section
        in the `Hooking your solver to AMPL
        <https://www.ampl.com/REFS/hooking3.pdf>`_ report).  Links to
        these functions are declared by creating an
        :class:`ExternalFunction` and passing the compiled library name
        (or path) to the ``library`` keyword and the name of the
        function to the ``function`` keyword.

        """
        self._units = kwargs.pop('units', None)
        if self._units is not None:
            self._units = units.get_units(self._units)
        self._arg_units = kwargs.pop('arg_units', None)
        if self._arg_units is not None:
            self._arg_units = [units.get_units(u) for u in self._arg_units]
        kwargs.setdefault('ctype', ExternalFunction)
        Component.__init__(self, **kwargs)
        self._constructed = True
        ### HACK ###
        # FIXME: We must declare an _index attribute because
        # block._add_temporary_set assumes ALL components define an
        # index.  Sigh.
        self._index_set = None
Example #8
0
 def __str__(self):
     return Component.__str__(self)
Example #9
0
 def pprint(self, *args, **kwds):
     return Component.pprint(self, *args, **kwds)
Example #10
0
 def deactivate(self):
     """Set the active attribute to False"""
     Component.deactivate(self)
     for component_data in itervalues(self):
         component_data._active = False
Example #11
0
 def activate(self):
     """Set the active attribute to True"""
     Component.activate(self)
     for component_data in itervalues(self):
         component_data._active = True
Example #12
0
 def __str__(self):
     return Component.__str__(self)
Example #13
0
 def pprint(self, *args, **kwds):
     return Component.pprint(self, *args, **kwds)