Changeset 3021


Ignore:
Timestamp:
Sep 19, 2010 9:44:44 PM (11 years ago)
Author:
jwatson
Message:

Various performance improvements to identify_vars.py.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/identify_vars.py

    r2999 r3021  
    3636    #
    3737    def _identify_variables(self, exp, model):
     38
    3839        try:
    3940            if exp.fixed_value():
     
    7374            if exp.id in model._var:
    7475                return
    75             exp.id = self.vnum
    76             model._var[self.vnum] = exp
    77             self.vnum += 1
     76            exp.id = self.var_count
     77            model._var[self.var_count] = exp
     78            self.var_count += 1
    7879        #
    7980        # Variable Base
     
    8687            if tmp.id in model._var:
    8788                return
    88             tmp.id = self.vnum
    89             model._var[self.vnum] = tmp
    90             self.vnum += 1           
     89            tmp.id = self.var_count
     90            model._var[self.var_count] = tmp
     91            self.var_count += 1           
    9192        #
    9293        # If not a constant, then this is an error
     
    9596            raise ValueError, "Unexpected expression type in identify_variables: " + str(type(exp))+" "+str(exp)
    9697
     98    #
     99    # this pre-processor computes a number of attributes of model components, in
     100    # addition to models themselves. for VarValue, ConstraintData, and ObjectiveData,
     101    # the routine assigns the "id" attribute. in addition, for VarValues, the routine
     102    # sets the "status" attribute - to used or unused, depending on whether it appears
     103    # in an active constraint/objective. on the Model object, the routine populates
     104    # the "statistics" (variable/constraint/objective counts) attribute, and builds
     105    # the _var, _con, and _obj dictionaries of a Model, which map the id attributes
     106    # to the respective VarValue, ConstraintData, or ObjectiveData object.
     107    #
     108    # NOTE: if variables are fixed, then this preprocessor will flag them as unused
     109    #       and consequently not create entries for them in the model _var map.
     110    #
    97111
    98112    def preprocess(self,model):
     
    101115        """
    102116
    103         Vars = model.active_components(Var)
    104         Con = model.active_components(Constraint)
    105         Obj = model.active_components(Objective)
     117        active_variables = model.active_components(Var)
     118        active_constraints = model.active_components(Constraint)
     119        active_objectives = model.active_components(Objective)
    106120
    107         Con.update(model.active_components(SOSConstraint))
     121        active_constraints.update(model.active_components(SOSConstraint))
    108122
    109         self.vnum=0
    110         self.cnum=0
    111         self.onum=0
     123        self.var_count=0
     124        self.constraint_count=0
     125        self.objective_count=0
    112126       
    113127        model.statistics.number_of_binary_variables = 0
     
    124138        # Until proven otherwise, all variables are unused.
    125139        #
    126         for var in Vars.values():
    127             for V in var._varval.keys():
    128                 var._varval[V].status = VarStatus.unused
    129                 var._varval[V].id = -1
     140        for var in active_variables.values():
     141            for var_value in var._varval.values():
     142                var_value.status = VarStatus.unused
     143                var_value.id = -1
    130144
    131145        #
     
    133147        # used in the objective and constraints.
    134148        #
    135         for key in Obj.keys():
    136             for ondx in Obj[key]._data:
    137                 if not Obj[key]._data[ondx].active:
     149        for name, objective in active_objectives.items():
     150            for index, objective_data in objective._data.items():
     151                if not objective_data.active:
    138152                    continue
    139153                try:
    140                     self._identify_variables(Obj[key]._data[ondx].expr, model)
     154                    self._identify_variables(objective_data.expr, model)
    141155                except ValueError, err:
    142                     raise ValueError, "Problem processing objective %s (index %s): %s" % (str(key), str(ondx), str(err))
     156                    raise ValueError, "Problem processing objective %s (index %s): %s" % (str(name), str(index), str(err))
    143157
    144                 Obj[key]._data[ondx].id = self.onum
    145                 self.onum += 1
     158                objective_data.id = self.objective_count
     159                model._obj[self.objective_count] = objective_data
     160                self.objective_count += 1
    146161
    147         for key in Con.keys():
    148             C = Con[key]
    149             for cndx in C.keys():
    150                 if not C._data[cndx].active:
     162        for name, constraint in active_constraints.items():
     163            for index, constraint_data in constraint._data.items():
     164                if not constraint_data.active:
    151165                    continue
    152166                try:
    153                     self._identify_variables(C._data[cndx].body, model)
     167                    self._identify_variables(constraint_data.body, model)
    154168                except ValueError, err:
    155                     raise ValueError, "Problem processing constraint %s (index %s): %s" % (str(key), str(cndx), str(err))
    156                 C._data[cndx].id = self.cnum
    157                 model._con[self.cnum] = C._data[cndx]
    158                 self.cnum += 1
     169                    raise ValueError, "Problem processing constraint %s (index %s): %s" % (str(name), str(index), str(err))
     170                constraint_data.id = self.constraint_count
     171                model._con[self.constraint_count] = constraint_data
     172                self.constraint_count += 1
    159173
    160174        #
     
    166180        # each variable value.
    167181        #
    168         for var in Vars.values():
     182        for var in active_variables.values():
    169183            model.statistics.number_of_binary_variables += len(var.binary_keys())
    170184            model.statistics.number_of_integer_variables += len(var.integer_keys())
     
    175189        #
    176190
    177         model.statistics.number_of_variables = self.vnum
    178         model.statistics.number_of_constraints = self.cnum
    179         model.statistics.number_of_objectives = self.onum
     191        model.statistics.number_of_variables = self.var_count
     192        model.statistics.number_of_constraints = self.constraint_count
     193        model.statistics.number_of_objectives = self.objective_count
    180194
    181195        return model
Note: See TracChangeset for help on using the changeset viewer.