def test_unsupported_pyomo4_expressions(self): EXPR.set_expression_tree_format(expr_common.Mode.pyomo4_trees) m = self.m t = IndexTemplate(m.t) # Check multiplication by constant e = 5 * m.dv[t] == m.v[t] with self.assertRaises(TypeError): _check_productexpression(e, 0) EXPR.set_expression_tree_format(expr_common._default_mode)
def test_check_productexpression(self): m = self.m m.p = Param(initialize=5) m.mp = Param(initialize=5, mutable=True) m.y = Var() m.z = Var() t = IndexTemplate(m.t) # Check multiplication by constant e = 5 * m.dv[t] == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) e = m.v[t] == 5 * m.dv[t] temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) # Check multiplication by fixed param e = m.p * m.dv[t] == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) e = m.v[t] == m.p * m.dv[t] temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) # Check multiplication by mutable param e = m.mp * m.dv[t] == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) self.assertIs(m.mp, temp[1]._denominator[0]) e = m.v[t] == m.mp * m.dv[t] temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) self.assertIs(m.mp, temp[1]._denominator[0]) # Check multiplication by var e = m.y * m.dv[t] / m.z == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) self.assertIs(m.y, temp[1]._denominator[0]) self.assertIs(m.z, temp[1]._numerator[1]) e = m.v[t] == m.y * m.dv[t] / m.z temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) self.assertIs(m.y, temp[1]._denominator[0]) self.assertIs(m.z, temp[1]._numerator[1]) # Check having the DerivativeVar in the denominator e = m.y / (m.dv[t] * m.z) == m.mp temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) self.assertIs(m.mp, temp[1]._denominator[0]) self.assertIs(m.y, temp[1]._numerator[0]) self.assertIs(m.z, temp[1]._denominator[1]) e = m.mp == m.y / (m.dv[t] * m.z) temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR._ProductExpression) self.assertIs(m.mp, temp[1]._denominator[0]) self.assertIs(m.y, temp[1]._numerator[0]) self.assertIs(m.z, temp[1]._denominator[1]) # Check expression with no DerivativeVar e = m.v[t] * m.y / m.z == m.v[t] * m.y / m.z temp = _check_productexpression(e, 0) self.assertIsNone(temp) temp = _check_productexpression(e, 1) self.assertIsNone(temp)
def test_check_productexpression(self): m = self.m m.p = Param(initialize=5) m.mp = Param(initialize=5, mutable=True) m.y = Var() m.z = Var() t = IndexTemplate(m.t) # Check multiplication by constant e = 5 * m.dv[t] == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) e = m.v[t] == 5 * m.dv[t] temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) # Check multiplication by fixed param e = m.p * m.dv[t] == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) e = m.v[t] == m.p * m.dv[t] temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) # Check multiplication by mutable param e = m.mp * m.dv[t] == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) self.assertIs(m.mp, temp[1].arg(1).arg(0)) # Reciprocal self.assertIs(e.arg(1), temp[1].arg(0)) e = m.v[t] == m.mp * m.dv[t] temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) self.assertIs(m.mp, temp[1].arg(1).arg(0)) # Reciprocal self.assertIs(e.arg(0), temp[1].arg(0)) # Check multiplication by var e = m.y * m.dv[t] / m.z == m.v[t] temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) self.assertIs(e.arg(1), temp[1].arg(0).arg(0)) self.assertIs(m.z, temp[1].arg(0).arg(1)) e = m.v[t] == m.y * m.dv[t] / m.z temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) self.assertIs(e.arg(0), temp[1].arg(0).arg(0)) self.assertIs(m.z, temp[1].arg(0).arg(1)) # Check having the DerivativeVar in the denominator e = m.y / (m.dv[t] * m.z) == m.mp temp = _check_productexpression(e, 0) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) self.assertIs(m.y, temp[1].arg(0)) self.assertIs(e.arg(1), temp[1].arg(1).arg(0).arg(0)) e = m.mp == m.y / (m.dv[t] * m.z) temp = _check_productexpression(e, 1) self.assertIs(m.dv, temp[0]._base) self.assertIs(type(temp[1]), EXPR.ProductExpression) self.assertIs(m.y, temp[1].arg(0)) self.assertIs(e.arg(0), temp[1].arg(1).arg(0).arg(0)) # Check expression with no DerivativeVar e = m.v[t] * m.y / m.z == m.v[t] * m.y / m.z temp = _check_productexpression(e, 0) self.assertIsNone(temp) temp = _check_productexpression(e, 1) self.assertIsNone(temp)