Changeset 3001


Ignore:
Timestamp:
Sep 8, 2010 1:37:23 PM (9 years ago)
Author:
jwatson
Message:

Improving the efficiency of objective/constraint iteration schemes in compute_canonical_repn preprocessor. Added some comments to objective/constraint base classes.

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

Legend:

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

    r2906 r3001  
    182182            self._data[None].name=tmpname
    183183        self.rule = tmprule
    184         self.trivial=False
     184        self.trivial = False # True if all constraint indicies have trivial expressions.
    185185
    186186    def clear(self):
  • coopr.pyomo/trunk/coopr/pyomo/base/objective.py

    r2999 r3001  
    9797        self.sense   = tmpsense
    9898        self.rule    = tmprule
    99         self.trivial = False
     99        self.trivial = False # True if all objective indicies have trivial expressions.
    100100        self._constructed = False
    101101
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/compute_canonical_repn.py

    r2930 r3001  
    2525    """
    2626
     27    # all modifications are in-place, i.e., components of the
     28    # input instance are modified.
     29
    2730    pyutilib.component.core.implements(IPyomoPresolveAction)
    2831
     
    3841        The main routine to perform the preprocess
    3942        """
     43
    4044        if getattr(model,'skip_canonical_repn',False):
    4145            return model
     46       
    4247        #
    4348        # Objectives
    4449        #
    45         Obj = model.active_components(Objective)
    46         for key in Obj.keys():
    47             obj = Obj[key]
     50        active_objectives = model.active_components(Objective)
     51        for key, obj in active_objectives.iteritems():
    4852            # number of objective indicies with non-trivial expressions
    49             nt = 0
    50             for ondx in obj:
    51                 if not obj._data[ondx].active:
     53            num_nontrivial = 0
     54           
     55            for ondx, objective_data in obj._data.iteritems():
     56                if not objective_data.active:
    5257                    continue
    53                 if obj._data[ondx].expr is None:
     58                if objective_data.expr is None:
    5459                    raise ValueError, "No expression has been defined for objective %s" % str(key)
     60               
    5561                try:
    56                     obj._data[ondx].repn = coopr.pyomo.expr.generate_canonical_repn(obj._data[ondx].expr, context=obj, key=ondx)
     62                    objective_data.repn = coopr.pyomo.expr.generate_canonical_repn(objective_data.expr, context=obj, key=ondx)
    5763                except Exception, e:
    5864                    print "Error generating a canonical representation for objective %s (index %s)" % (str(key), str(ondx))
    5965                    raise e
    60                 if not coopr.pyomo.expr.is_constant(obj._data[ondx].repn):
    61                     nt += 1
    62             if nt == 0:
     66                if not coopr.pyomo.expr.is_constant(objective_data.repn):
     67                    num_nontrivial += 1
     68                   
     69            if num_nontrivial == 0:
    6370                obj.trivial = True
    6471        #
    6572        # Constraints
    6673        #
    67         Con = model.active_components(Constraint)
    68         for key in Con.keys():
    69             con = Con[key]
     74        active_constraints = model.active_components(Constraint)
     75        for key, con in active_constraints.iteritems():
    7076            # number of constraint indicies with non-trivial bodies
    71             nt = 0
    72             for cndx in con:
    73                 if not con._data[cndx].active:
     77            num_nontrivial = 0
     78           
     79            for cndx, constraint_data in con._data.iteritems():
     80                if not constraint_data.active:
    7481                    continue
    75                 if con._data[cndx].body is None:
     82                if constraint_data.body is None:
    7683                    raise ValueError, "No expression has been defined for the body of constraint %s" % str(key)
     84               
    7785                try:
    78                     con._data[cndx].repn = coopr.pyomo.expr.generate_canonical_repn(con._data[cndx].body, context=con, key=cndx)
     86                    constraint_data.repn = coopr.pyomo.expr.generate_canonical_repn(constraint_data.body, context=con, key=cndx)
    7987                except Exception, e:
    8088                    print "Error generating a canonical representation for constraint %s (index %s)" % (str(key), str(cndx))
    8189                    raise e
    82                 if not coopr.pyomo.expr.is_constant(con._data[cndx].repn):
    83                     nt += 1
    84             if nt == 0:
     90                if not coopr.pyomo.expr.is_constant(constraint_data.repn):
     91                    num_nontrivial += 1
     92                   
     93            if num_nontrivial == 0:
    8594                con.trivial = True
    8695        #
Note: See TracChangeset for help on using the changeset viewer.