Esempio n. 1
0
    def test_quicksum(self):
        m = ConcreteModel()
        m.y = Var(domain=Binary)
        m.c = Constraint(expr=quicksum([m.y, m.y], linear=True) == 1)

        lbl = NumericLabeler('x')
        smap = SymbolMap(lbl)
        tc = StorageTreeChecker(m)
        self.assertEqual(("x1 + x1", False),
                         expression_to_string(m.c.body, tc, smap=smap))
        m.x = Var()
        m.c2 = Constraint(expr=quicksum([m.x, m.y], linear=True) == 1)
        self.assertEqual(("x2 + x1", False),
                         expression_to_string(m.c2.body, tc, smap=smap))

        m.y.fix(1)
        lbl = NumericLabeler('x')
        smap = SymbolMap(lbl)
        tc = StorageTreeChecker(m)
        self.assertEqual(("1 + 1", False),
                         expression_to_string(m.c.body, tc, smap=smap))
        m.x = Var()
        m.c2 = Constraint(expr=quicksum([m.x, m.y], linear=True) == 1)
        self.assertEqual(("x1 + 1", False),
                         expression_to_string(m.c2.body, tc, smap=smap))
Esempio n. 2
0
 def test_power_function_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     self.assertEquals(expression_to_string(m.x**-3, lbl, smap=smap),
                       "power(x1, -3)")
     self.assertEquals(expression_to_string(m.x**0.33, smap=smap),
                       "x1 ** 0.33")
     self.assertEquals(expression_to_string(pow(m.x, 2), smap=smap),
                       "power(x1, 2)")
Esempio n. 3
0
 def test_power_function_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(m.x**-3, tc, lbl, smap=smap),
                      ("power(x1, (-3))", False))
     self.assertEqual(expression_to_string(m.x**0.33, tc, smap=smap),
                      ("x1 ** 0.33", False))
     self.assertEqual(expression_to_string(pow(m.x, 2), tc, smap=smap),
                      ("power(x1, 2)", False))
Esempio n. 4
0
 def test_power_function_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(
         m.x ** -3, tc, lbl, smap=smap), "power(x1, (-3))")
     self.assertEqual(expression_to_string(
         m.x ** 0.33, tc, smap=smap), "x1 ** 0.33")
     self.assertEqual(expression_to_string(
         pow(m.x, 2), tc, smap=smap), "power(x1, 2)")
Esempio n. 5
0
 def test_dnlp_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     m.y = Var()
     m.z = Var()
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(ceil(m.x), tc, lbl, smap=smap),
                      ("ceil(x1)", True))
     self.assertEqual(expression_to_string(floor(m.x), tc, lbl, smap=smap),
                      ("floor(x1)", True))
     self.assertEqual(expression_to_string(abs(m.x), tc, lbl, smap=smap),
                      ("abs(x1)", True))
Esempio n. 6
0
 def test_negative_float_double_operator(self):
     m = ConcreteModel()
     m.x = Var()
     m.y = Var()
     m.z = Var(bounds=(0, 6))
     m.c = Constraint(expr=(m.x * m.y * -2) == 0)
     m.c2 = Constraint(expr=m.z**-1.5 == 0)
     m.o = Objective(expr=m.z)
     m.y.fix(-7)
     m.x.fix(4)
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(m.c.body, tc, smap=smap),
                      ("4*(-7)*(-2)", False))
     self.assertEqual(expression_to_string(m.c2.body, tc, smap=smap),
                      ("x1 ** (-1.5)", False))
Esempio n. 7
0
 def test_negative_float_double_operator(self):
     m = ConcreteModel()
     m.x = Var()
     m.y = Var()
     m.z = Var(bounds=(0, 6))
     m.c = Constraint(expr=(m.x * m.y * -2) == 0)
     m.c2 = Constraint(expr=m.z ** -1.5 == 0)
     m.o = Objective(expr=m.z)
     m.y.fix(-7)
     m.x.fix(4)
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(
         m.c.body, tc, smap=smap), "4*(-7)*(-2)")
     self.assertEqual(expression_to_string(
         m.c2.body, tc, smap=smap), "x1 ** (-1.5)")
Esempio n. 8
0
 def test_fixed_var_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     m.y = Var()
     m.z = Var()
     m.z.fix(-3)
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(
         m.x + m.y - m.z, tc, lbl, smap=smap), ("x1 + x2 + 3", False))
     m.z.fix(-400)
     self.assertEqual(expression_to_string(
         m.z + m.y - m.z, tc, smap=smap), ("(-400) + x2 + 400", False))
     m.z.fix(8.8)
     self.assertEqual(expression_to_string(
         m.x + m.z - m.y, tc, smap=smap), ("x1 + 8.8 - x2", False))
     m.z.fix(-8.8)
     self.assertEqual(expression_to_string(
         m.x * m.z - m.y, tc, smap=smap), ("x1*(-8.8) - x2", False))
Esempio n. 9
0
 def test_fixed_var_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     m.y = Var()
     m.z = Var()
     m.z.fix(-3)
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     self.assertEquals(
         expression_to_string(m.x + m.y - m.z, lbl, smap=smap),
         "x1 + x2 - (-3)")
     m.z.fix(-400)
     self.assertEquals(expression_to_string(m.z + m.y - m.z, smap=smap),
                       "(-400) + x2 - (-400)")
     m.z.fix(8.8)
     self.assertEquals(expression_to_string(m.x + m.z - m.y, smap=smap),
                       "x1 + (8.8) - x2")
     m.z.fix(-8.8)
     self.assertEquals(expression_to_string(m.x * m.z - m.y, smap=smap),
                       "x1*(-8.8) - x2")
Esempio n. 10
0
 def test_fixed_var_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     m.y = Var()
     m.z = Var()
     m.z.fix(-3)
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(
         m.x + m.y - m.z, tc, lbl, smap=smap), "x1 + x2 - (-3)")
     m.z.fix(-400)
     self.assertEqual(expression_to_string(
         m.z + m.y - m.z, tc, smap=smap), "(-400) + x2 - (-400)")
     m.z.fix(8.8)
     self.assertEqual(expression_to_string(
         m.x + m.z - m.y, tc, smap=smap), "x1 + 8.8 - x2")
     m.z.fix(-8.8)
     self.assertEqual(expression_to_string(
         m.x * m.z - m.y, tc, smap=smap), "x1*(-8.8) - x2")
Esempio n. 11
0
 def test_arcfcn_to_string(self):
     m = ConcreteModel()
     m.x = Var()
     lbl = NumericLabeler('x')
     smap = SymbolMap(lbl)
     tc = StorageTreeChecker(m)
     self.assertEqual(expression_to_string(asin(m.x), tc, lbl, smap=smap),
                      ("arcsin(x1)", False))
     self.assertEqual(expression_to_string(acos(m.x), tc, lbl, smap=smap),
                      ("arccos(x1)", False))
     self.assertEqual(expression_to_string(atan(m.x), tc, lbl, smap=smap),
                      ("arctan(x1)", False))
     with self.assertRaisesRegexp(
             RuntimeError,
             "GAMS files cannot represent the unary function asinh"):
         expression_to_string(asinh(m.x), tc, lbl, smap=smap)
     with self.assertRaisesRegexp(
             RuntimeError,
             "GAMS files cannot represent the unary function acosh"):
         expression_to_string(acosh(m.x), tc, lbl, smap=smap)
     with self.assertRaisesRegexp(
             RuntimeError,
             "GAMS files cannot represent the unary function atanh"):
         expression_to_string(atanh(m.x), tc, lbl, smap=smap)
Esempio n. 12
0
    def test_expr_xfrm(self):
        from pyomo.repn.plugins.gams_writer import (expression_to_string,
                                                    StorageTreeChecker)
        from pyomo.core.expr.symbol_map import SymbolMap
        M = ConcreteModel()
        M.abc = Var()

        smap = SymbolMap()
        tc = StorageTreeChecker(M)

        expr = M.abc**2.0
        self.assertEqual(str(expr), "abc**2.0")
        self.assertEqual(expression_to_string(expr, tc, smap=smap),
                         ("power(abc, 2.0)", False))

        expr = log(M.abc**2.0)
        self.assertEqual(str(expr), "log(abc**2.0)")
        self.assertEqual(expression_to_string(expr, tc, smap=smap),
                         ("log(power(abc, 2.0))", False))

        expr = log(M.abc**2.0) + 5
        self.assertEqual(str(expr), "log(abc**2.0) + 5")
        self.assertEqual(expression_to_string(expr, tc, smap=smap),
                         ("log(power(abc, 2.0)) + 5", False))

        expr = exp(M.abc**2.0) + 5
        self.assertEqual(str(expr), "exp(abc**2.0) + 5")
        self.assertEqual(expression_to_string(expr, tc, smap=smap),
                         ("exp(power(abc, 2.0)) + 5", False))

        expr = log(M.abc**2.0)**4
        self.assertEqual(str(expr), "log(abc**2.0)**4")
        self.assertEqual(expression_to_string(expr, tc, smap=smap),
                         ("power(log(power(abc, 2.0)), 4)", False))

        expr = log(M.abc**2.0)**4.5
        self.assertEqual(str(expr), "log(abc**2.0)**4.5")
        self.assertEqual(expression_to_string(expr, tc, smap=smap),
                         ("log(power(abc, 2.0)) ** 4.5", False))
Esempio n. 13
0
    def test_expr_xfrm(self):
        from pyomo.repn.plugins.gams_writer import (
            expression_to_string, StorageTreeChecker)
        from pyomo.core.expr.symbol_map import SymbolMap
        M = ConcreteModel()
        M.abc = Var()

        smap = SymbolMap()
        tc = StorageTreeChecker(M)

        expr = M.abc**2.0
        self.assertEqual(str(expr), "abc**2.0")
        self.assertEqual(expression_to_string(
            expr, tc, smap=smap), "power(abc, 2.0)")

        expr = log(M.abc**2.0)
        self.assertEqual(str(expr), "log(abc**2.0)")
        self.assertEqual(expression_to_string(
            expr, tc, smap=smap), "log(power(abc, 2.0))")

        expr = log(M.abc**2.0) + 5
        self.assertEqual(str(expr), "log(abc**2.0) + 5")
        self.assertEqual(expression_to_string(
            expr, tc, smap=smap), "log(power(abc, 2.0)) + 5")

        expr = exp(M.abc**2.0) + 5
        self.assertEqual(str(expr), "exp(abc**2.0) + 5")
        self.assertEqual(expression_to_string(
            expr, tc, smap=smap), "exp(power(abc, 2.0)) + 5")

        expr = log(M.abc**2.0)**4
        self.assertEqual(str(expr), "log(abc**2.0)**4")
        self.assertEqual(expression_to_string(
            expr, tc, smap=smap), "power(log(power(abc, 2.0)), 4)")

        expr = log(M.abc**2.0)**4.5
        self.assertEqual(str(expr), "log(abc**2.0)**4.5")
        self.assertEqual(expression_to_string(
            expr, tc, smap=smap), "log(power(abc, 2.0)) ** 4.5")