Changeset 3021
- Timestamp:
- Sep 19, 2010 9:44:44 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
coopr.pyomo/trunk/coopr/pyomo/preprocess/identify_vars.py
r2999 r3021 36 36 # 37 37 def _identify_variables(self, exp, model): 38 38 39 try: 39 40 if exp.fixed_value(): … … 73 74 if exp.id in model._var: 74 75 return 75 exp.id = self.v num76 model._var[self.v num] = exp77 self.v num+= 176 exp.id = self.var_count 77 model._var[self.var_count] = exp 78 self.var_count += 1 78 79 # 79 80 # Variable Base … … 86 87 if tmp.id in model._var: 87 88 return 88 tmp.id = self.v num89 model._var[self.v num] = tmp90 self.v num+= 189 tmp.id = self.var_count 90 model._var[self.var_count] = tmp 91 self.var_count += 1 91 92 # 92 93 # If not a constant, then this is an error … … 95 96 raise ValueError, "Unexpected expression type in identify_variables: " + str(type(exp))+" "+str(exp) 96 97 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 # 97 111 98 112 def preprocess(self,model): … … 101 115 """ 102 116 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) 106 120 107 Con.update(model.active_components(SOSConstraint))121 active_constraints.update(model.active_components(SOSConstraint)) 108 122 109 self.v num=0110 self.c num=0111 self.o num=0123 self.var_count=0 124 self.constraint_count=0 125 self.objective_count=0 112 126 113 127 model.statistics.number_of_binary_variables = 0 … … 124 138 # Until proven otherwise, all variables are unused. 125 139 # 126 for var in Vars.values():127 for V in var._varval.keys():128 var ._varval[V].status = VarStatus.unused129 var ._varval[V].id = -1140 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 130 144 131 145 # … … 133 147 # used in the objective and constraints. 134 148 # 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: 138 152 continue 139 153 try: 140 self._identify_variables( Obj[key]._data[ondx].expr, model)154 self._identify_variables(objective_data.expr, model) 141 155 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)) 143 157 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 146 161 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: 151 165 continue 152 166 try: 153 self._identify_variables( C._data[cndx].body, model)167 self._identify_variables(constraint_data.body, model) 154 168 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.cnum157 model._con[self.c num] = C._data[cndx]158 self.c num+= 1169 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 159 173 160 174 # … … 166 180 # each variable value. 167 181 # 168 for var in Vars.values():182 for var in active_variables.values(): 169 183 model.statistics.number_of_binary_variables += len(var.binary_keys()) 170 184 model.statistics.number_of_integer_variables += len(var.integer_keys()) … … 175 189 # 176 190 177 model.statistics.number_of_variables = self.v num178 model.statistics.number_of_constraints = self.c num179 model.statistics.number_of_objectives = self.o num191 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 180 194 181 195 return model
Note: See TracChangeset
for help on using the changeset viewer.