Changeset 2648


Ignore:
Timestamp:
Jun 7, 2010 4:46:44 PM (11 years ago)
Author:
khunter
Message:

First use of Logger

Used to let user know what constraints or objects are being created.
Will eventually correct the magic number to be set with a Logger
configuration file or command line option.

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

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/expr/canonical_repn.py

    r2640 r2648  
    1111__all__ = ['generate_canonical_repn', 'as_expr', 'is_constant', 'is_linear', 'is_quadratic', 'is_nonlinear']
    1212
     13import logging
     14
    1315#import pyutilib.component.core
    14 from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction, Model
     16from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction, Model, \
     17                             Constraint, Objective
    1518from coopr.pyomo.base import expr
    1619from coopr.pyomo.base.var import _VarValue, Var
    1720import copy
    1821
     22
     23Logger = logging.getLogger('Pyomo')
    1924#
    2025# A frozen dictionary that can be hashed.  This dictionary isn't _really_
     
    5055# log(y[1]) + x[1]*x[1]         {2:{{0:2}:1.0}, None:log(y[1])}
    5156#
    52 def generate_canonical_repn(expr):
     57def generate_canonical_repn(expr, **kwargs):
     58    context = kwargs.pop('context', None)
     59    key     = kwargs.pop('key', None)
     60
     61    # Prettify output of user notification "What's it doin' now?!"
     62    if ( context is not None ):
     63        name = " '%s" % context.name
     64        if ( isinstance( context, Constraint ) ):
     65            item = 'Constraint'
     66        elif ( isinstance( context, Objective ) ):
     67            item = 'Objective'
     68    else:
     69        item = 'unknown object'
     70        name = ''
     71        Logger.warning("Argument 'context' not passed")
     72
     73    if ( key is not None ):
     74        index = str(key)
     75        if ( type(key) is tuple ):
     76            index = index[1:-1] # remove tuple
     77        index = "[%s]'" % index
     78    else:
     79        index = "'"
     80        if ( '' == name ):
     81            index = ''
     82
     83    Logger.info("Generating expression for %(item)s%(name)s%(index)s" %
     84        { 'item' : item, 'name' : name, 'index' : index } )
     85
    5386    return collect_canonical_repn(expr)
    5487
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/compute_canonical_repn.py

    r2359 r2648  
    5353                    raise ValueError, "No expression has been defined for objective %s" % str(key)
    5454                try:
    55                     obj._data[ondx].repn = coopr.pyomo.expr.generate_canonical_repn(obj._data[ondx].expr)
     55                    obj._data[ondx].repn = coopr.pyomo.expr.generate_canonical_repn(obj._data[ondx].expr, context=obj, key=ondx)
    5656                except Exception, e:
    5757                    print "Error generating a canonical representation for objective %s (index %s)" % (str(key), str(ondx))
     
    7474                if con._data[cndx].body is None:
    7575                    raise ValueError, "No expression has been defined for the body of constraint %s" % str(key)
    76                 try:                 
    77                     con._data[cndx].repn = coopr.pyomo.expr.generate_canonical_repn(con._data[cndx].body)
     76                try:
     77                    con._data[cndx].repn = coopr.pyomo.expr.generate_canonical_repn(con._data[cndx].body, context=con, key=cndx)
    7878                except Exception, e:
    7979                    print "Error generating a canonical representation for constraint %s (index %s)" % (str(key), str(cndx))
Note: See TracChangeset for help on using the changeset viewer.