def test_potentially_variable(self): p = parameter() self.assertEqual(p._potentially_variable(), False) self.assertEqual(potentially_variable(p), False) p.value = 1.0 self.assertEqual(p._potentially_variable(), False) self.assertEqual(potentially_variable(p), False)
def test_potentially_variable(self): v = variable() self.assertEqual(v._potentially_variable(), True) self.assertEqual(potentially_variable(v), True) self.assertEqual(v.fixed, False) self.assertEqual(v.value, None) v.value = 1.0 self.assertEqual(v._potentially_variable(), True) self.assertEqual(potentially_variable(v), True) self.assertEqual(v.fixed, False) self.assertEqual(v.value, 1.0) v.fix() self.assertEqual(v._potentially_variable(), True) self.assertEqual(potentially_variable(v), True) self.assertEqual(v.fixed, True) self.assertEqual(v.value, 1.0) v.value = None self.assertEqual(v._potentially_variable(), True) self.assertEqual(potentially_variable(v), True) self.assertEqual(v.fixed, True) self.assertEqual(v.value, None) v.free() self.assertEqual(v._potentially_variable(), True) self.assertEqual(potentially_variable(v), True) self.assertEqual(v.fixed, False) self.assertEqual(v.value, None)
def test_potentially_variable(self): e = noclone(variable()) self.assertEqual(e._potentially_variable(), True) self.assertEqual(potentially_variable(e), True) e = noclone(parameter()) self.assertEqual(e._potentially_variable(), False) self.assertEqual(potentially_variable(e), False) e = noclone(expression()) self.assertEqual(e._potentially_variable(), True) self.assertEqual(potentially_variable(e), True) e = noclone(data_expression()) self.assertEqual(e._potentially_variable(), False) self.assertEqual(potentially_variable(e), False)
def test_potentially_variable(self): e = self._ctype_factory() self.assertEqual(e._potentially_variable(), True) self.assertEqual(potentially_variable(e), True) e.expr = 1 self.assertEqual(e._potentially_variable(), True) self.assertEqual(potentially_variable(e), True) v = variable() v.value = 2 e.expr = v + 1 self.assertEqual(e._potentially_variable(), True) self.assertEqual(potentially_variable(e), True) v.fix() e.expr = v + 1 self.assertEqual(e._potentially_variable(), True) self.assertEqual(potentially_variable(e), True) self.assertEqual(e(), 3)
def test_potentially_variable(self): e = self._ctype_factory() self.assertEqual(e._potentially_variable(), False) self.assertEqual(potentially_variable(e), False) e.expr = 1 self.assertEqual(e._potentially_variable(), False) self.assertEqual(potentially_variable(e), False) p = parameter() e.expr = p**2 self.assertEqual(e._potentially_variable(), False) self.assertEqual(potentially_variable(e), False) a = self._ctype_factory() e.expr = (a * p)**2 / (p + 5) self.assertEqual(e._potentially_variable(), False) self.assertEqual(potentially_variable(e), False) a.expr = 2.0 p.value = 5.0 self.assertEqual(e._potentially_variable(), False) self.assertEqual(potentially_variable(e), False) self.assertEqual(e(), 10.0) v = variable() with self.assertRaises(ValueError): e.expr = v + 1
def __init__(self, variables, weights=None, level=1): self._parent = None self._active = True self._variables = tuple(variables) self._weights = None self._level = level if weights is None: self._weights = tuple(range(1,len(self._variables)+1)) else: self._weights = tuple(weights) for w in self._weights: if potentially_variable(w): raise ValueError( "Weights for Special Ordered Sets must be " "expressions restricted to data") assert len(self._variables) == len(self._weights) assert self._level >= 1
def _potentially_variable(self): """A boolean indicating whether this expression can reference variables.""" return potentially_variable(self._expr)
def expr(self, expr): if potentially_variable(expr): raise ValueError("Expression is not restricted to data.") self._expr = expr