def test_method(self): class Init(object): def a_init(self, m): return 0 def x_init(self, m, i): return i+1 def x2_init(self, m): return 0 def y_init(self, m, i, j): return j*(i+1) init = Init() m = ConcreteModel() a = Initializer(init.a_init) self.assertIs(type(a), ScalarCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, 1), 0) m.x = Var([1,2,3]) a = Initializer(init.x_init) self.assertIs(type(a), IndexedCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, 1), 2) a = Initializer(init.x2_init) self.assertIs(type(a), ScalarCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, 1), 0) m.y = Var([1,2,3], [4,5,6]) a = Initializer(init.y_init) self.assertIs(type(a), IndexedCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, (1, 4)), 8) b = CountedCallInitializer(m.x, a) self.assertIs(type(b), CountedCallInitializer) self.assertFalse(b.constant()) self.assertFalse(b.verified) self.assertFalse(a.contains_indices()) self.assertFalse(b._scalar) self.assertIs(a._fcn, b._fcn) c = b(None, 1) self.assertIs(type(c), CountedCallGenerator) self.assertEqual(next(c), 2) self.assertEqual(next(c), 3) self.assertEqual(next(c), 4)
def test_function(self): m = ConcreteModel() def a_init(m): return 0 a = Initializer(a_init) self.assertIs(type(a), ScalarCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, 1), 0) m.x = Var([1, 2, 3]) def x_init(m, i): return i + 1 a = Initializer(x_init) self.assertIs(type(a), IndexedCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, 1), 2) def x2_init(m): return 0 a = Initializer(x2_init) self.assertIs(type(a), ScalarCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, 1), 0) m.y = Var([1, 2, 3], [4, 5, 6]) def y_init(m, i, j): return j * (i + 1) a = Initializer(y_init) self.assertIs(type(a), IndexedCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) self.assertEqual(a(None, (1, 4)), 8) b = CountedCallInitializer(m.x, a) self.assertIs(type(b), CountedCallInitializer) self.assertFalse(b.constant()) self.assertFalse(b.verified) self.assertFalse(a.contains_indices()) self.assertFalse(b._scalar) self.assertIs(a._fcn, b._fcn) c = b(None, 1) self.assertIs(type(c), CountedCallGenerator) self.assertEqual(next(c), 2) self.assertEqual(next(c), 3) self.assertEqual(next(c), 4)
def __init__(self, *args, **kwargs): kwargs.setdefault('ctype', Complementarity) kwargs.setdefault('dense', False) _init = tuple(_arg for _arg in (kwargs.pop('initialize', None), kwargs.pop('rule', None), kwargs.pop('expr', None)) if _arg is not None) if len(_init) > 1: raise ValueError( "Duplicate initialization: Complementarity() only accepts " "one of 'initialize=', 'rule=', and 'expr='") elif _init: _init = _init[0] else: _init = None self._init_rule = Initializer(_init, treat_sequences_as_mappings=False, allow_generators=True) if self._init_rule is not None: kwargs['rule'] = Complementarity._complementarity_rule Block.__init__(self, *args, **kwargs) # HACK to make the "counted call" syntax work. We wait until # after the base class is set up so that is_indexed() is # reliable. if self._init_rule is not None \ and self._init_rule.__class__ is IndexedCallInitializer: self._init_rule = CountedCallInitializer(self, self._init_rule)
def __init__(self, **kwargs): """Constructor""" if 'expr' in kwargs: raise ValueError( "ConstraintList does not accept the 'expr' keyword") _rule = kwargs.pop('rule', None) args = (Set(dimen=1), ) super(ConstraintList, self).__init__(*args, **kwargs) self.rule = Initializer(_rule, treat_sequences_as_mappings=False, allow_generators=True) # HACK to make the "counted call" syntax work. We wait until # after the base class is set up so that is_indexed() is # reliable. if self.rule is not None and type(self.rule) is IndexedCallInitializer: self.rule = CountedCallInitializer(self, self.rule)