Changeset 5669 for coopr.pyomo


Ignore:
Timestamp:
04/17/12 15:21:05 (2 years ago)
Author:
jdsiiro
Message:
  • Efficiency improvements to core numeric value manipulation functions.
  • Numeric constants are now shared (that is, all instances of a given constant (e.g., 0) point to the same instance of NumericConstant?).
File:
1 edited

Legend:

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

    r5668 r5669  
    3737 
    3838 
    39 def value(obj): 
     39def _old_value(obj): 
    4040    """ 
    4141    A utility function that returns the value of a Pyomo object or expression. 
     
    5858 
    5959 
    60 def fast_value(obj): 
     60def value(obj): 
    6161    """ 
    6262    A utility function that returns the value of a Pyomo object or expression. 
     
    9292    object is a constant 
    9393    """ 
    94      
    95     if obj.__class__ is int or obj.__class__ is float or obj.__class__ is long\ 
    96            or obj.__class__ is bool or obj.__class__ is str: 
    97         return True 
     94    # This method is rarely, if ever, called.  Plus, since the 
     95    # expression generation (and constraint generation) system converts 
     96    # everything to NumericValues, it is better (i.e., faster) to assume 
     97    # that the obj is a NumericValue 
    9898    try: 
    9999        return obj.is_constant() 
    100100    except AttributeError: 
    101         pass 
    102     raise ValueError("This object is not a constant value: %s" % (obj,)) 
    103  
     101        if obj.__class__ is (obj + 0).__class__ or obj.__class__ is str \ 
     102               or obj.__class__ is bool: 
     103            return True 
     104    raise ValueError("This object is not a constant numeric value: %s" 
     105                     % (obj,)) 
     106 
     107 
     108# It is very common to have only a few constants in a model, but those 
     109# constants get repeated many times.  KnownConstants lets us re-use / 
     110# share constants we have seen before. 
     111KnownConstants = {} 
    104112 
    105113def as_numeric(obj): 
     
    107115    Verify that this obj is a NumericValue or intrinsic value. 
    108116    """ 
     117    # int and float are *so* common that it pays to treat them specially 
    109118    if obj.__class__ is int or obj.__class__ is float: 
    110         #if obj in KnownConstants: 
    111         #    return KnownConstants[obj] 
    112         #else: 
    113         #    return KnownConstants.setdefault(obj, 
    114         return NumericConstant(None,None,obj) 
     119        if obj in KnownConstants: 
     120            return KnownConstants[obj] 
     121        else: 
     122            return KnownConstants.setdefault( 
     123                obj, NumericConstant(None,None,obj) ) 
    115124    try: 
    116125        return obj.as_numeric() 
     
    118127        pass 
    119128    try:  
    120         if obj.__class__ is (obj + 0).__class__ or obj.__class__ is bool:  
    121             return NumericConstant(None, None, obj)  
     129        if obj.__class__ is (obj + 0).__class__ or obj.__class__ is bool: 
     130            # obj may (or may not) be hashable, so we need this try 
     131            # block so that things proceed normally for non-hashable 
     132            # "numeric" types 
     133            try: 
     134                if obj in KnownConstants: 
     135                    return KnownConstants[obj] 
     136                else: 
     137                    return KnownConstants.setdefault( 
     138                        obj, NumericConstant(None,None,obj) ) 
     139            except: 
     140                return NumericConstant(None, None, obj)  
    122141    except:  
    123142        pass 
Note: See TracChangeset for help on using the changeset viewer.