Exemple #1
0
 def test_division_behavior(self):
     # make sure integers involved in Pyomo expression
     # use __future__ behavior
     e = noclone(parameter(value=2))
     self.assertIs(type(e.expr), parameter)
     self.assertEqual((1 / e)(), 0.5)
     self.assertEqual((parameter(1) / e)(), 0.5)
     # since the type returned is int, this should result
     # in the behavior used by the interpreter
     if six.PY3:
         self.assertEqual((1 / e.expr()), 0.5)
     else:
         self.assertEqual((1 / e.expr()), 0)
 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_is_fixed(self):
     p = parameter()
     self.assertEqual(p.is_fixed(), True)
     self.assertEqual(is_fixed(p), True)
     p.value = 1.0
     self.assertEqual(p.is_fixed(), True)
     self.assertEqual(is_fixed(p), True)
 def test_is_constant(self):
     p = parameter()
     self.assertEqual(p.is_constant(), False)
     self.assertEqual(is_constant(p), False)
     p.value = 1.0
     self.assertEqual(p.is_constant(), False)
     self.assertEqual(is_constant(p), False)
 def test_to_string(self):
     b = block()
     p = parameter()
     e = noclone(p**2)
     self.assertEqual(str(e.expr), "<parameter>**2.0")
     self.assertEqual(str(e), "{<parameter>**2.0}")
     e.to_string()
     out = StringIO()
     e.to_string(ostream=out)
     self.assertEqual(out.getvalue(), "<parameter>**2.0")
     e.to_string(verbose=False)
     out = StringIO()
     e.to_string(ostream=out, verbose=False)
     self.assertEqual(out.getvalue(), "<parameter>**2.0")
     e.to_string(verbose=True)
     out = StringIO()
     e.to_string(ostream=out, verbose=True)
     self.assertEqual(out.getvalue(), "{pow( <parameter> , 2.0 )}")
     b.e = e
     b.p = p
     self.assertNotEqual(p.name, None)
     e.to_string(verbose=True)
     out = StringIO()
     e.to_string(ostream=out, verbose=True)
     self.assertEqual(out.getvalue(), "{pow( " + p.name + " , 2.0 )}")
     self.assertEqual(out.getvalue(), "{pow( p , 2.0 )}")
     del b.e
     del b.p
Exemple #6
0
 def test_polynomial_degree(self):
     e = noclone(parameter())
     self.assertEqual(e.polynomial_degree(), 0)
     e = noclone(parameter(value=1))
     self.assertEqual(e.polynomial_degree(), 0)
     v = variable()
     v.value = 2
     e = noclone(v + 1)
     self.assertEqual(e.polynomial_degree(), 1)
     e = noclone(v**2 + v + 1)
     self.assertEqual(e.polynomial_degree(), 2)
     v.fix()
     self.assertEqual(e.polynomial_degree(), 0)
     e = noclone(v**v)
     self.assertEqual(e.polynomial_degree(), 0)
     v.free()
     self.assertEqual(e.polynomial_degree(), None)
Exemple #7
0
 def test_call(self):
     e = noclone(None)
     self.assertIs(e, None)
     e = noclone(1)
     self.assertEqual(e, 1)
     p = parameter()
     p.value = 2
     e = noclone(p + 1)
     self.assertEqual(e(), 3)
 def test_init(self):
     p = parameter()
     self.assertTrue(p.parent is None)
     self.assertEqual(p.ctype, Param)
     self.assertEqual(p.value, None)
     self.assertEqual(p(), None)
     p.value = 1
     self.assertEqual(p.value, 1)
     self.assertEqual(p(), 1)
Exemple #9
0
 def test_call(self):
     e = self._ctype_factory()
     self.assertEqual(e(), None)
     e.expr = 1
     self.assertEqual(e(), 1)
     p = parameter()
     p.value = 2
     e.expr = p + 1
     self.assertEqual(e(), 3)
Exemple #10
0
    def test_clone(self):

        p = parameter()
        e = noclone(p)
        self.assertTrue(e.clone() is e)
        self.assertTrue(e.clone().expr is p)
        sube = p**2 + 1
        e = noclone(sube)
        self.assertTrue(e.clone() is e)
        self.assertTrue(e.clone().expr is sube)
Exemple #11
0
 def test_clone(self):
     e = self._ctype_factory()
     self.assertTrue(e.clone() is e)
     p = parameter()
     e.expr = p
     self.assertTrue(e.clone() is e)
     self.assertTrue(e.clone().expr is p)
     sube = p**2 + 1
     e.expr = sube
     self.assertTrue(e.clone() is e)
     self.assertTrue(e.clone().expr is sube)
 def test_polynomial_degree(self):
     p = parameter()
     self.assertEqual(p.polynomial_degree(), 0)
     self.assertEqual((p**2).polynomial_degree(), 0)
     self.assertEqual(p.value, None)
     with self.assertRaises(ValueError):
         (p**2)()
     p.value = 1.0
     self.assertEqual(p.polynomial_degree(), 0)
     self.assertEqual((p**2).polynomial_degree(), 0)
     self.assertEqual(p.value, 1.0)
     self.assertEqual((p**2)(), 1.0)
Exemple #13
0
    def test_is_fixed(self):
        v = variable()
        e = noclone(v + 1)
        self.assertEqual(e.is_fixed(), False)
        self.assertEqual(is_fixed(e), False)
        v.fix()
        self.assertEqual(e.is_fixed(), True)
        self.assertEqual(is_fixed(e), True)

        e = noclone(parameter())
        self.assertEqual(e.is_fixed(), True)
        self.assertEqual(is_fixed(e), True)
Exemple #14
0
 def test_init_NumericValue(self):
     v = variable()
     p = parameter()
     e = expression()
     d = data_expression()
     o = objective()
     for obj in (v, v + 1, v**2, p, p + 1, p**2, e, e + 1, e**2, d, d + 1,
                 d**2, o, o + 1, o**2):
         self.assertTrue(isinstance(noclone(obj), NumericValue))
         self.assertTrue(isinstance(noclone(obj), IIdentityExpression))
         self.assertTrue(isinstance(noclone(obj), noclone))
         self.assertIs(noclone(obj).expr, obj)
Exemple #15
0
 def testis_potentially_variable(self):
     e = noclone(variable())
     self.assertEqual(e.is_potentially_variable(), True)
     self.assertEqual(potentially_variable(e), True)
     e = noclone(parameter())
     self.assertEqual(e.is_potentially_variable(), False)
     self.assertEqual(potentially_variable(e), False)
     e = noclone(expression())
     self.assertEqual(e.is_potentially_variable(), True)
     self.assertEqual(potentially_variable(e), True)
     e = noclone(data_expression())
     self.assertEqual(e.is_potentially_variable(), False)
     self.assertEqual(potentially_variable(e), False)
Exemple #16
0
 def test_is_constant(self):
     e = self._ctype_factory()
     self.assertEqual(e.is_constant(), False)
     self.assertEqual(is_constant(e), False)
     e.expr = 1
     self.assertEqual(e.is_constant(), False)
     self.assertEqual(is_constant(e), False)
     p = parameter()
     self.assertEqual(p.is_constant(), False)
     self.assertEqual(is_constant(p), False)
     p.value = 2
     e.expr = p + 1
     self.assertEqual(e.is_constant(), False)
     self.assertEqual(is_constant(e), False)
 def test_pickle(self):
     p = parameter(value=1.0)
     self.assertEqual(p.value, 1.0)
     self.assertEqual(p.parent, None)
     pup = pickle.loads(pickle.dumps(p))
     self.assertEqual(pup.value, 1.0)
     self.assertEqual(pup.parent, None)
     b = block()
     b.p = p
     self.assertIs(p.parent, b)
     bup = pickle.loads(pickle.dumps(b))
     pup = bup.p
     self.assertEqual(pup.value, 1.0)
     self.assertIs(pup.parent, bup)
Exemple #18
0
 def test_division_behavior(self):
     # make sure integers involved in Pyomo expression
     # use __future__ behavior
     e = self._ctype_factory()
     e.expr = 2
     self.assertIs(type(e.expr), int)
     self.assertEqual((1 / e)(), 0.5)
     self.assertEqual((parameter(1) / e)(), 0.5)
     # since the type returned is int, this should result
     # in the behavior used by the interpreter
     if six.PY3:
         self.assertEqual((1 / e.expr), 0.5)
     else:
         self.assertEqual((1 / e.expr), 0)
 def test_pprint(self):
     # Not really testing what the output is, just that
     # an error does not occur. The pprint functionality
     # is still in the early stages.
     p = parameter()
     pyomo.core.kernel.pprint(p)
     b = block()
     b.p = p
     pyomo.core.kernel.pprint(p)
     pyomo.core.kernel.pprint(b)
     m = block()
     m.b = b
     pyomo.core.kernel.pprint(p)
     pyomo.core.kernel.pprint(b)
     pyomo.core.kernel.pprint(m)
Exemple #20
0
    def test_is_constant(self):
        v = variable()
        e = noclone(v)
        self.assertEqual(e.is_constant(), False)
        self.assertEqual(is_constant(e), False)
        v.fix(1)
        self.assertEqual(e.is_constant(), False)
        self.assertEqual(is_constant(e), False)

        p = parameter()
        e = noclone(p)
        self.assertEqual(p.is_constant(), False)
        self.assertEqual(is_constant(p), False)

        self.assertEqual(is_constant(noclone(1)), True)
Exemple #21
0
 def test_to_string(self):
     b = block()
     p = parameter()
     e = noclone(p**2)
     self.assertEqual(str(e.expr), "<parameter>**2")
     self.assertEqual(str(e), "{(<parameter>**2)}")
     self.assertEqual(e.to_string(), "(<parameter>**2)")
     self.assertEqual(e.to_string(verbose=False), "(<parameter>**2)")
     self.assertEqual(e.to_string(verbose=True), "{pow(<parameter>, 2)}")
     b.e = e
     b.p = p
     self.assertNotEqual(p.name, None)
     self.assertEqual(e.to_string(verbose=True), "{pow(" + p.name + ", 2)}")
     self.assertEqual(e.to_string(verbose=True), "{pow(p, 2)}")
     del b.e
     del b.p
Exemple #22
0
    def test_to_string(self):
        b = block()
        e = self._ctype_factory()
        label = str(e)
        self.assertNotEqual(label, None)
        self.assertEqual(e.name, None)

        self.assertEqual(str(e.expr), "None")
        self.assertEqual(str(e), label)
        self.assertEqual(e.to_string(), label + "{Undefined}")
        self.assertEqual(e.to_string(verbose=False), label + "{Undefined}")
        self.assertEqual(e.to_string(verbose=True), label + "{Undefined}")
        b.e = e
        self.assertNotEqual(e.name, None)
        self.assertEqual(e.to_string(verbose=True), "e{Undefined}")
        del b.e
        self.assertEqual(e.name, None)

        e.expr = 1
        self.assertEqual(str(e.expr), "1")
        self.assertEqual(str(e), label)
        self.assertEqual(e.to_string(), "1")
        self.assertEqual(e.to_string(verbose=False), "1")
        self.assertEqual(e.to_string(verbose=True), label + "{1}")
        b.e = e
        self.assertNotEqual(e.name, None)
        self.assertEqual(e.to_string(verbose=True), "e{1}")
        del b.e
        self.assertEqual(e.name, None)

        p = parameter()
        e.expr = p**2
        self.assertEqual(str(e.expr), "<parameter>**2")
        self.assertEqual(str(e), label)
        self.assertEqual(e.to_string(), "(<parameter>**2)")
        self.assertEqual(e.to_string(verbose=False), "(<parameter>**2)")
        self.assertEqual(e.to_string(verbose=True),
                         label + "{pow(<parameter>, 2)}")
        b.e = e
        b.p = p
        self.assertNotEqual(e.name, None)
        self.assertNotEqual(p.name, None)
        self.assertEqual(e.to_string(verbose=True),
                         e.name + "{pow(" + p.name + ", 2)}")
        self.assertEqual(e.to_string(verbose=True), "e{pow(p, 2)}")
        del b.e
        del b.p
Exemple #23
0
 def test_pprint(self):
     import pyomo.kernel
     # Not really testing what the output is, just that
     # an error does not occur. The pprint functionality
     # is still in the early stages.
     p = parameter()
     e = self._ctype_factory(p**2)
     pyomo.kernel.pprint(e)
     b = block()
     b.e = e
     pyomo.kernel.pprint(e)
     pyomo.kernel.pprint(b)
     m = block()
     m.b = b
     pyomo.kernel.pprint(e)
     pyomo.kernel.pprint(b)
     pyomo.kernel.pprint(m)
    def test_bad_weights(self):
        v = variable()
        with self.assertRaises(ValueError):
            s = sos([v], weights=[v])

        v.fix(1.0)
        with self.assertRaises(ValueError):
            s = sos([v], weights=[v])

        e = expression()
        with self.assertRaises(ValueError):
            s = sos([v], weights=[e])

        de = data_expression()
        s = sos([v], weights=[de])

        p = parameter()
        s = sos([v], weights=[p])
Exemple #25
0
    def test_polynomial_degree(self):
        e = self._ctype_factory()
        self.assertEqual(e.polynomial_degree(), 0)
        e.expr = 1
        self.assertEqual(e.polynomial_degree(), 0)
        p = parameter()
        e.expr = p**2
        self.assertEqual(e.polynomial_degree(), 0)
        a = self._ctype_factory()
        e.expr = (a * p)**2 / (p + 5)
        self.assertEqual(e.polynomial_degree(), 0)
        a.expr = 2.0
        p.value = 5.0
        self.assertEqual(e.polynomial_degree(), 0)
        self.assertEqual(e(), 10.0)

        v = variable()
        with self.assertRaises(ValueError):
            e.expr = v + 1
Exemple #26
0
    def testis_potentially_variable(self):
        e = self._ctype_factory()
        self.assertEqual(e.is_potentially_variable(), False)
        self.assertEqual(potentially_variable(e), False)
        e.expr = 1
        self.assertEqual(e.is_potentially_variable(), False)
        self.assertEqual(potentially_variable(e), False)
        p = parameter()
        e.expr = p**2
        self.assertEqual(e.is_potentially_variable(), False)
        self.assertEqual(potentially_variable(e), False)
        a = self._ctype_factory()
        e.expr = (a * p)**2 / (p + 5)
        self.assertEqual(e.is_potentially_variable(), False)
        self.assertEqual(potentially_variable(e), False)
        a.expr = 2.0
        p.value = 5.0
        self.assertEqual(e.is_potentially_variable(), False)
        self.assertEqual(potentially_variable(e), False)
        self.assertEqual(e(), 10.0)

        v = variable()
        with self.assertRaises(ValueError):
            e.expr = v + 1
Exemple #27
0
    def test_is_fixed(self):
        e = self._ctype_factory()
        self.assertEqual(e.is_fixed(), True)
        self.assertEqual(is_fixed(e), True)
        e.expr = 1
        self.assertEqual(e.is_fixed(), True)
        self.assertEqual(is_fixed(e), True)
        p = parameter()
        e.expr = p**2
        self.assertEqual(e.is_fixed(), True)
        self.assertEqual(is_fixed(e), True)
        a = self._ctype_factory()
        e.expr = (a * p)**2 / (p + 5)
        self.assertEqual(e.is_fixed(), True)
        self.assertEqual(is_fixed(e), True)
        a.expr = 2.0
        p.value = 5.0
        self.assertEqual(e.is_fixed(), True)
        self.assertEqual(is_fixed(e), True)
        self.assertEqual(e(), 10.0)

        v = variable()
        with self.assertRaises(ValueError):
            e.expr = v + 1
Exemple #28
0
 def test_aruments(self):
     e = noclone(parameter() + 1)
     self.assertEqual(len(tuple(e.args)), 1)
     self.assertTrue(tuple(e.args)[0] is e.expr)
Exemple #29
0
 def test_args(self):
     e = self._ctype_factory()
     p = parameter()
     e.expr = p + 1
     self.assertEqual(e.nargs(), 1)
     self.assertTrue(e.arg(0) is e.expr)
Exemple #30
0
 def test_aruments(self):
     e = self._ctype_factory()
     p = parameter()
     e.expr = p + 1
     self.assertEqual(len(tuple(e.args)), 1)
     self.assertTrue(tuple(e.args)[0] is e.expr)