Changeset 2826
 Timestamp:
 Jul 20, 2010 6:41:55 PM (9 years ago)
 Location:
 coopr.pyomo/trunk/coopr/pyomo
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/base/constraint.py
r2824 r2826 219 219 raise IndexError, msg 220 220 self.add(None, _self_rule) 221 elif None not in self._index and len(self._index) > 0 \222 and _self_rule.func_code.co_argcount == 1:223 if pyomo.debug("verbose"): #pragma:nocover224 msg = ' Constructing constraint indices with rule that ' \225 'returns a dictionary'226 print msg227 constraints = _self_rule(self.model)228 for val in constraints:229 if val not in self._index:230 msg = "The index '%s' generated by the constructor rule " \231 'is not valid'232 raise IndexError, msg % str(val)233 ##constructed_indices.append(val)234 self.add(val, constraints[val])235 221 else: 236 222 for val in self._index: 
coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_con.py
r2422 r2826 258 258 self.failUnlessEqual(len(self.instance.c), 2) 259 259 260 def test_rule_option4(self):261 """Test rule option"""262 def f(model):263 res={}264 for i in [0,1,2]:265 ans=0266 for j in model.x.keys():267 ans = ans + model.x[j]268 ans *= i269 ans = ans < 0270 ans = ans > 0271 res[i]=ans272 return res273 self.model.x = Var(RangeSet(1,4),initialize=2)274 self.model.c = Constraint(self.model.A,rule=f)275 try:276 self.instance = self.model.create()277 self.fail("Expected IndexError")278 except IndexError:279 pass280 281 260 def test_dim(self): 282 261 """Test dim method""" … … 307 286 self.instance = self.model.create() 308 287 self.failUnlessEqual(len(self.instance.c),1) 309 310 def test_rule_error1(self):311 """Verify that errors in the rule dictionary are identified"""312 def f(model):313 res={}314 for i in model.A:315 if i%2 != 0:316 ans=0317 for j in model.x.keys():318 ans = ans + model.x[j]319 ans *= i320 ans = ans < 0321 ans = ans > 0322 res[i]=ans323 return res324 self.model.x = Var(RangeSet(1,4),initialize=2)325 self.model.c = Constraint(self.model.A,rule=f)326 self.instance = self.model.create()327 try:328 self.instance.c[1]()329 self.fail("Expected ValueError")330 except ValueError:331 pass332 self.instance.x.reset()333 self.failUnlessEqual(self.instance.c[1](), 8)334 self.failUnlessEqual(value(self.instance.c[1]), 8)335 self.failUnlessEqual(len(self.instance.c), 2)336 337 288 338 289
Note: See TracChangeset
for help on using the changeset viewer.