Changeset 3341


Ignore:
Timestamp:
12/18/10 07:23:35 (3 years ago)
Author:
wehart
Message:

Removing the 'Shared*' components.

Reworking the logic of immutable params to work with SparseMapping? dictionaries.
(We'll work on the control semantics later...)

Location:
coopr.pyomo/trunk/coopr/pyomo
Files:
6 edited

Legend:

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

    r3310 r3341  
    3232 
    3333from block import Block 
    34 from component import SharedComponent 
    3534from sets import Set 
    3635 
  • coopr.pyomo/trunk/coopr/pyomo/base/component.py

    r3310 r3341  
    11 
    2 __all__ = ['Component', "SharedComponent"] 
     2__all__ = ['Component'] 
    33 
    44from pyutilib.component.core import Plugin, implements 
     
    3333 
    3434 
    35 class SharedComponent(object): 
    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     def __deepcopy__(self, memo={}): 
    75         """ 
    76         Sharing is implemented by transparently returning a reference 
    77         to the same object when asked to copy itself. 
    78  
    79         """ 
    80         return self 
    81  
    82  
    8335class DeveloperError(Exception): 
    8436    """ 
  • coopr.pyomo/trunk/coopr/pyomo/base/param.py

    r3310 r3341  
    99#  _________________________________________________________________________ 
    1010 
    11 __all__ = ['_ParamBase', 'Param', '_SharedParamBase', 'SharedParam' ] 
     11__all__ = ['_ParamBase', 'Param' ] 
    1212 
    1313import sys 
     
    2323from sets import _BaseSet 
    2424import pyomo 
    25 from pyutilib.misc import DictWithDefaultValue 
     25from pyutilib.misc import SparseMapping 
    2626 
    2727 
     
    6969           if type(val) is dict: 
    7070              raise IOError 
    71            self._immutable_data[self._immutable_index] = val 
     71           if not val is None: 
     72                self._immutable_data[self._immutable_index] = val 
    7273        self.__dict__[name] = val 
    7374 
     
    109110        self._paramval   = {} 
    110111        if not self.mutable: 
    111             self._paramdata = DictWithDefaultValue(defaultval) 
     112            self._paramdata = SparseMapping(default=defaultval, within=self.domain) 
    112113        self._default = _ParamValue( 
    113114          name   = self.name, 
     
    412413        if not self.mutable: 
    413414            return self._paramdata 
    414         tmp = DictWithDefaultValue(self._default.value) 
     415        tmp = SparseMapping(default=self._default.value, within=self.domain) 
    415416        for key in self.keys(): 
    416417            tmp[key] = self._paramval[key].value 
     
    441442 
    442443    def __call__(self, exception=True): 
    443         return self._paramval[None].value 
     444        try: 
     445            return self._paramval[None].value 
     446        except KeyError: 
     447            raise ValueError, "Undefined value for parameter %s" % self.name 
    444448 
    445449 
     
    471475 
    472476    def __call__(self, exception=True): 
    473         return self._paramdata[None] 
     477        try: 
     478            return self._paramdata[None] 
     479        except KeyError: 
     480            raise ValueError, "Undefined value for parameter %s" % self.name 
    474481 
    475482 
     
    538545        return self 
    539546 
    540 class SharedParam(SharedComponent, Param): 
    541     """ A Param object to be shared between models. """ 
    542  
    543     alias("SharedParam", "A Param shared between Model objects.") 
    544  
    545     def __new__(cls, *args, **kwds): 
    546         kwds["ctype"] = kwds.get("ctype", SharedParam) 
    547         if len(args) == 0: 
    548             self = _SharedParamElement(*args, **kwds) 
    549         else: 
    550             self = _SharedParamArray(*args, **kwds) 
    551         return self 
    552  
    553  
    554 class _SharedParamBase(SharedComponent, _ParamBase): 
    555     """ _ParamBase object designed to be shared between Model instances """ 
    556  
    557     def __init__(self, *args, **kwds): 
    558         kwds["ctype"] = kwds.get("ctype", _SharedParamBase) 
    559         _ParamBase.__init__(self, *args, **kwds) 
    560  
    561  
    562 class _SharedParamElement(SharedComponent, _ParamElement): 
    563     """ _ParamElement object designed to be shared between Model instances """ 
    564  
    565     def __init__(self, *args, **kwds): 
    566         kwds["ctype"] = kwds.get("ctype", _SharedParamElement) 
    567         _ParamElement.__init__(self, *args, **kwds) 
    568  
    569  
    570 class _SharedParamArray(SharedComponent, _ParamArray): 
    571     """ _ParamElement object designed to be shared between Model instances """ 
    572  
    573     def __init__(self, *args, **kwds): 
    574         kwds["ctype"] = kwds.get("ctype", _SharedParamArray) 
    575         _ParamArray.__init__(self, *args, **kwds) 
    576  
  • coopr.pyomo/trunk/coopr/pyomo/base/sets.py

    r3053 r3341  
    1010 
    1111__all__ = ['Set', '_BaseSet', '_SetContainer', '_SetArray', '_ProductSet', 
    12            'set_options', 'SharedSet', '_SharedBaseSet', '_SharedSetContainer', 
    13            '_SharedSetArray', '_SharedProductSet'] 
     12           'set_options'] 
    1413 
    1514from pyutilib.component.core import alias 
     
    10521051        return self 
    10531052 
    1054  
    1055  
    1056 class SharedSet(SharedComponent, Set): 
    1057  
    1058     alias("SharedSet", "A Set shared between Model objects.") 
    1059  
    1060     def __new__(cls, *args, **kwds): 
    1061         kwds["ctype"] = kwds.get("ctype", SharedSet) 
    1062         if args == (): 
    1063             self = _SharedSetContainer(*args, **kwds) 
    1064         else: 
    1065             self = _SharedSetArray(*args, **kwds) 
    1066         return self 
    1067  
    1068 class _SharedBaseSet(SharedComponent, _BaseSet): 
    1069  
    1070     def __init__(self, *args, **kwds): 
    1071         kwds["ctype"] = kwds.get("ctype", _SharedBaseSet) 
    1072         _BaseSet.__init__(self, *args, **kwds) 
    1073  
    1074 class _SharedSetContainer(SharedComponent, _SetContainer): 
    1075  
    1076     def __init__(self, *args, **kwds): 
    1077         kwds["ctype"] = kwds.get("ctype", _SharedSetContainer) 
    1078         _SetContainer.__init__(self, *args, **kwds) 
    1079  
    1080  
    1081 class _SharedSetArray(SharedComponent, _SetArray): 
    1082  
    1083     def __init__(self, *args, **kwds): 
    1084         kwds["ctype"] = kwds.get("ctype", _SharedSetArray) 
    1085         _SetArray.__init__(self, *args, **kwds) 
    1086  
    1087 class _SharedProductSet(SharedComponent, _ProductSet): 
    1088  
    1089     def __init__(self, *args, **kwds): 
    1090         kwds["ctype"] = kwds.get("ctype", _SharedProductSet) 
    1091         _ProductSet.__init__(self, *args, **kwds) 
  • coopr.pyomo/trunk/coopr/pyomo/tests/examples/test6.txt

    r3068 r3341  
    5757    Set data that is used to define a model instance. 
    5858 
    59  SharedParam 
    60     A Param shared between Model objects. 
    61  
    62  SharedSet 
    63     A Set shared between Model objects. 
    64  
    6559 Var 
    6660    Decision variables in a model. 
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_param.py

    r3340 r3341  
    756756            pass 
    757757 
    758 class TestParamError(PyomoModel): 
    759  
    760     def test_value(self): 
    761         p = Param(name='name', mutable=self.mutable) 
    762         try: 
    763             value(p) 
    764             self.fail("expected ValueError") 
    765         except ValueError: 
    766             pass 
    767         self.failUnlessEqual(p.value,None) 
    768  
    769  
    770 class MTestParamError(TestParamError): 
    771  
    772     def config_mutability(self): 
    773         self.mutable = True 
    774  
    775758 
    776759 
Note: See TracChangeset for help on using the changeset viewer.