Changeset 2841


Ignore:
Timestamp:
Jul 23, 2010 5:35:01 PM (9 years ago)
Author:
prsteel
Message:

Introduces SharedComponent? objects, which are designed to be
efficiently shared between multiple Model instances.

Makes relevant changes to pyomo/base classes.

Location:
coopr.pyomo/trunk/coopr/pyomo/base
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/PyomoModel.py

    r2822 r2841  
    161161        if not _ctype is None:
    162162            return tmp[_ctype]
    163         print "ACTIVE",tmp
     163        #print "ACTIVE",tmp
    164164        return tmp
    165165
     
    267267        read in from a file.
    268268        """
     269
    269270        if self._defer_construction:
    270271            instance = self.clone()
     
    10321033    from inspect import isfunction
    10331034
    1034     def _deepcopy ( self, memo={} ):
     1035    def _deepcopy(self, memo={}):
    10351036        dup = Model()
    10361037        for i in self.__dict__:
    10371038            item = self.__dict__[i]
    10381039            if isfunction(item) or \
    1039                 isinstance( item, (int, long,float,bool, basestring)):
     1040                isinstance(item, (int, long, float, bool, basestring)) \
     1041                isinstance(item, SharedComponent):
    10401042                dup.__dict__[i] = item
    10411043            else:
  • coopr.pyomo/trunk/coopr/pyomo/base/component.py

    r2652 r2841  
    3333        pass
    3434
     35class SharedComponent(Plugin):
     36    """
     37    Component to be shared between Model objects.
     38
     39    As the name implies, these Component objects are designed to allow
     40    multiple Model instances to share a set of common components to
     41    save memory. SharedComponent objects should only respond to a
     42    construction call once.
     43
     44    Developer's note: SharedComponent does very little on its own. In
     45    fact all it does is override __deepcopy__ to return itself. For
     46    this reason it is very important that when you define classes that
     47    multiply inherit from SharedComponent and another class derived
     48    from Component, SharedComponent must come first in the list of
     49    base classes to ensure that SharedComponent.__deepcopy__ comes
     50    before the other overridden __deepcopy__ methods in the method
     51    resolution order.
     52
     53    Example:
     54    ------------------------------------------------------------------
     55   
     56    # Correct
     57    class Foo(SharedComponent, Param):
     58        pass
     59
     60    # Incorrect
     61    class Foo(Param, SharedComponent):
     62        pass
     63
     64    ------------------------------------------------------------------
     65
     66    In the incorrect case, Foo.__deepcopy__ references
     67    Param.__deepcopy__, which will perform an actual deep copy.
     68
     69    Note that this ordering is safe, since SharedComponent defines no
     70    other methods besides those which it inherits.
     71   
     72    """
     73
     74    implements(ISharedModelComponent)
     75
     76    def __deepcopy__(self, memo={}):
     77        """
     78        Sharing is implemented by transparently returning a reference
     79        to the same object when asked to copy itself.
     80       
     81        """
     82        return self
     83
    3584class DeveloperError(Exception):
    3685    """
    37     Exception class used to throw errors stemming from Pyomo programming
    38     errors, rather than user modeling errors (e.g., a component not declaring
    39     a 'ctype')
     86    Exception class used to throw errors stemming from Pyomo
     87    programming errors, rather than user modeling errors (e.g., a
     88    component not declaring a 'ctype')
     89
    4090    """
    4191
  • coopr.pyomo/trunk/coopr/pyomo/base/param.py

    r2768 r2841  
    463463        return self
    464464
    465 
     465class SharedParam(SharedComponent, Param):
     466    """ A Param object to be shared between models. """
     467
     468    def __new__(cls, *args, **kwds):
     469        if len(args) == 0:
     470            self = _SharedParamElement(*args, **kwds)
     471        else:
     472            self = _SharedParamArray(*args, **kwds)
     473        return self
     474
     475
     476class _SharedParamBase(SharedComponent, _ParamBase):
     477    """ _ParamBase object designed to be shared between Model instances """
     478
     479    def __init__(self, *args, **kwds):
     480        _ParamBase.__init__(self, *args, **kwds)
     481
     482
     483class _SharedParamElement(SharedComponent, _ParamElement):
     484    """ _ParamElement object designed to be shared between Model instances """
     485
     486    def __init__(self, *args, **kwds):
     487        _ParamElement.__init__(self, *args, **kwds)
     488
     489
     490class _SharedParamArray(SharedComponent, _ParamArray):
     491    """ _ParamElement object designed to be shared between Model instances """
     492
     493    def __init__(self, *args, **kwds):
     494        _ParamArray.__init__(self, *args, **kwds)
     495
  • coopr.pyomo/trunk/coopr/pyomo/base/plugin.py

    r2840 r2841  
    154154
    155155
    156 
    157156class IModelComponent(Interface):
    158157    pass
     158
     159
     160class ISharedModelComponent(Iterface):
     161
     162    def __deepcopy__(self, memo={}):
     163        pass
    159164
    160165ModelComponentFactory = CreatePluginFactory(IModelComponent)
Note: See TracChangeset for help on using the changeset viewer.