Changeset 3041


Ignore:
Timestamp:
Sep 23, 2010 11:10:03 PM (11 years ago)
Author:
wehart
Message:

Resolving #4057. If a function with <name>_rule is
in the same scope as a model component, then the component.rule attribute
is set to the function.

The parameter and set classes are specialized to use this self.rule
attribute when self.initialize is None.

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

Legend:

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

    r2911 r3041  
    5656    def __init__(self, *args, **kwargs):
    5757        """Constructor"""
    58         if 'ctype' in kwargs:
    59             tkwargs = {'ctype':kwargs['ctype']}
    60         else:
    61             tkwargs = {'ctype':Block}
     58        if 'ctype' in kwargs:
     59            tkwargs = {'ctype':kwargs['ctype']}
     60        else:
     61            tkwargs = {'ctype':Block}
    6262        IndexedComponent.__init__(self, *args, **tkwargs)
    63         #
     63        #
    6464        self.name=kwargs.get('name', 'unknown')
    6565        self._defer_construction=True
    66         self._parent_block = None
     66        self._parent_block = None
    6767        #
    6868        # Define component dictionary: component type -> instance
     
    8383
    8484    def concrete_mode(self):
    85         """Configure block to immediately construct components"""
     85        """Configure block to immediately construct components"""
    8686        self._defer_construction=False
    8787
    8888    def symbolic_mode(self):
    89         """Configure block to defer construction of components"""
     89        """Configure block to defer construction of components"""
    9090        self._defer_construction=True
    9191
    9292    def components(self, ctype=None):
    93         """
    94         Return information about the block components.  If ctype is None, return the dictionary
    95         that maps {component type -> {name -> instance}}.  Otherwise, return the dictionary
    96         that maps {name -> instance} for the specified component type.
    97         """
     93        """
     94        Return information about the block components.  If ctype is None, return the dictionary
     95        that maps {component type -> {name -> instance}}.  Otherwise, return the dictionary
     96        that maps {name -> instance} for the specified component type.
     97        """
    9898        if ctype is None:
    9999            return self._component
     
    103103
    104104    def active_components(self, _ctype=None):
    105         """
    106         Returns the active components in this block.  If _ctype is None, return the
    107         dictionary that maps {component type -> {name -> instance}}.  Otherwise, return
    108         the dictionary that maps {name -> instance} for the specified component type.
    109         """
     105        """
     106        Returns the active components in this block.  If _ctype is None, return the
     107        dictionary that maps {component type -> {name -> instance}}.  Otherwise, return
     108        the dictionary that maps {name -> instance} for the specified component type.
     109        """
    110110        tmp = {}
    111111        if _ctype is None:
     
    159159
    160160    def _clear_attribute(self,name):
    161         """
     161        """
    162162        Cleanup the pre-existing model attribute
    163         """
     163        """
    164164        if name in self._declarations:
    165165            self.__dict__[name]=None
    166             del self._component[ self._declarations[name].type() ][name]
     166            del self._component[ self._declarations[name].type() ][name]
    167167            del self._declarations[name]
    168168
     
    203203#                curr = self._parent_block
    204204
     205        frame = sys._getframe(2)
     206        locals_ = frame.f_locals
     207        if getattr(val,'rule',None) is None and val.name+'_rule' in locals_:
     208            val.rule = locals_[val.name+'_rule']
    205209        if not self._defer_construction:
    206210            val.construct(None)
     
    289293        else:
    290294            print >>ostream, "Model "+self.name
    291         #
     295        #
    292296        print >>ostream, ""
    293297        print >>ostream, "  Variables:"
     
    298302            for ndx in VAR:
    299303                VAR[ndx].display(prefix="    ",ostream=ostream)
    300         #
     304        #
    301305        print >>ostream, ""
    302306        print >>ostream, "  Objectives:"
     
    308312                OBJ[ndx].display(prefix="    ",ostream=ostream)
    309313        print >>ostream, ""
    310         #
     314        #
    311315        CON = self.active_components(Constraint)
    312316        print >>ostream, "  Constraints:"
  • coopr.pyomo/trunk/coopr/pyomo/base/param.py

    r3019 r3041  
    210210        except:
    211211            pass
     212
     213        if self._initialize is None:
     214            self._initialize = getattr(self,'rule',None)
    212215
    213216        #
  • coopr.pyomo/trunk/coopr/pyomo/base/sets.py

    r3037 r3041  
    227227            return
    228228        self._constructed=True
     229
     230        if self.initialize is None:
     231            self.initialize = getattr(self,'rule',None)
    229232        #
    230233        # Construct using the values list
     
    837840        if self.virtual:                                #pragma:nocover
    838841           raise TypeError, "It doesn't make sense to create a virtual set array"
     842
     843        if self.initialize is None:
     844            self.initialize = getattr(self,'rule',None)
    839845        #
    840846        # Construct using the values list
     
    974980            return
    975981        self._constructed=True
     982
     983        if self.initialize is None:
     984            self.initialize = getattr(self,'rule',None)
     985
    976986        if self.virtual:
    977987           if len(self.set_tuple) == 0:
Note: See TracChangeset for help on using the changeset viewer.