示例#1
0
 def test_is_fixed(self):
     f = functional_value()
     self.assertEqual(f.is_fixed(), True)
     self.assertEqual(is_fixed(f), True)
     f.fn = lambda: 2
     self.assertEqual(f.is_fixed(), True)
     self.assertEqual(is_fixed(f), True)
示例#2
0
    def test_call(self):
        f = functional_value()
        self.assertEqual(f(), None)
        self.assertIs(f.fn, None)
        f.fn = lambda: variable(value=1)
        self.assertIsNot(f.fn, None)
        # the function did not return numeric data
        # (always causes an exception)
        with self.assertRaises(TypeError):
            f(exception=False)
        with self.assertRaises(TypeError):
            f(exception=True)
        with self.assertRaises(TypeError):
            f()
        f.fn = lambda: None
        self.assertIsNot(f.fn, None)
        # the function did not return numeric data
        # (always causes an exception)
        with self.assertRaises(TypeError):
            f(exception=False)
        with self.assertRaises(TypeError):
            f(exception=True)
        with self.assertRaises(TypeError):
            f()

        def value_error():
            raise ValueError()

        f.fn = value_error
        self.assertIsNot(f.fn, None)
        self.assertEqual(f(exception=False), None)
        with self.assertRaises(ValueError):
            f(exception=True)
        with self.assertRaises(ValueError):
            f()
示例#3
0
 def test_is_constant(self):
     f = functional_value()
     self.assertEqual(f.is_constant(), False)
     self.assertEqual(is_constant(f), False)
     f.fn = lambda: 2
     self.assertEqual(f.is_constant(), False)
     self.assertEqual(is_constant(f), False)
示例#4
0
 def test_potentially_variable(self):
     f = functional_value()
     self.assertEqual(f.is_potentially_variable(), False)
     self.assertEqual(is_potentially_variable(f), False)
     f.fn = lambda: 2
     self.assertEqual(f.is_potentially_variable(), False)
     self.assertEqual(is_potentially_variable(f), False)
示例#5
0
 def test_is_constant(self):
     f = functional_value()
     self.assertEqual(f.is_constant(), False)
     self.assertEqual(is_constant(f), False)
     f.fn = lambda: 2
     self.assertEqual(f.is_constant(), False)
     self.assertEqual(is_constant(f), False)
示例#6
0
 def test_is_fixed(self):
     f = functional_value()
     self.assertEqual(f.is_fixed(), True)
     self.assertEqual(is_fixed(f), True)
     f.fn = lambda: 2
     self.assertEqual(f.is_fixed(), True)
     self.assertEqual(is_fixed(f), True)
示例#7
0
 def test_call(self):
     f = functional_value()
     self.assertEqual(f(), None)
     self.assertIs(f.fn, None)
     f.fn = lambda: variable(value=1)
     self.assertIsNot(f.fn, None)
     # the function did not return numeric data
     # (always causes an exception)
     with self.assertRaises(TypeError):
         f(exception=False)
     with self.assertRaises(TypeError):
         f(exception=True)
     with self.assertRaises(TypeError):
         f()
     f.fn = lambda: None
     self.assertIsNot(f.fn, None)
     # the function did not return numeric data
     # (always causes an exception)
     with self.assertRaises(TypeError):
         f(exception=False)
     with self.assertRaises(TypeError):
         f(exception=True)
     with self.assertRaises(TypeError):
         f()
     def value_error():
         raise ValueError()
     f.fn = value_error
     self.assertIsNot(f.fn, None)
     self.assertEqual(f(exception=False), None)
     with self.assertRaises(ValueError):
         f(exception=True)
     with self.assertRaises(ValueError):
         f()
示例#8
0
 def test_potentially_variable(self):
     f = functional_value()
     self.assertEqual(f.is_potentially_variable(), False)
     self.assertEqual(is_potentially_variable(f), False)
     f.fn = lambda: 2
     self.assertEqual(f.is_potentially_variable(), False)
     self.assertEqual(is_potentially_variable(f), False)
示例#9
0
 def test_init(self):
     f = functional_value()
     self.assertTrue(f.parent is None)
     self.assertEqual(f.ctype, IParameter)
     self.assertEqual(f.fn, None)
     self.assertEqual(f(), None)
     x = [1, 2]
     f.fn = lambda: max(x)
     self.assertEqual(f(), 2)
     x[0] = 3
     self.assertEqual(f(), 3)
示例#10
0
 def test_init(self):
     f = functional_value()
     self.assertTrue(f.parent is None)
     self.assertEqual(f.ctype, IParameter)
     self.assertEqual(f.fn, None)
     self.assertEqual(f(), None)
     x = [1,2]
     f.fn = lambda: max(x)
     self.assertEqual(f(), 2)
     x[0] = 3
     self.assertEqual(f(), 3)
示例#11
0
 def test_polynomial_degree(self):
     f = functional_value()
     self.assertEqual(f.polynomial_degree(), 0)
     self.assertEqual((f**2).polynomial_degree(), 0)
     self.assertIs(f.fn, None)
     with self.assertRaises(ValueError):
         (f**2)()
     f.fn = lambda: 2
     self.assertEqual(f.polynomial_degree(), 0)
     self.assertEqual((f**2).polynomial_degree(), 0)
     self.assertEqual(f(), 2)
     self.assertEqual((f**2)(), 4)
示例#12
0
 def test_polynomial_degree(self):
     f = functional_value()
     self.assertEqual(f.polynomial_degree(), 0)
     self.assertEqual((f**2).polynomial_degree(), 0)
     self.assertIs(f.fn, None)
     with self.assertRaises(ValueError):
         (f**2)()
     f.fn = lambda: 2
     self.assertEqual(f.polynomial_degree(), 0)
     self.assertEqual((f**2).polynomial_degree(), 0)
     self.assertEqual(f(), 2)
     self.assertEqual((f**2)(), 4)
示例#13
0
 def test_pickle(self):
     f = functional_value()
     self.assertIs(f.fn, None)
     self.assertIs(f.parent, None)
     fup = pickle.loads(pickle.dumps(f))
     self.assertIs(fup.fn, None)
     self.assertIs(fup.parent, None)
     b = block()
     b.f = f
     self.assertIs(f.parent, b)
     bup = pickle.loads(pickle.dumps(b))
     fup = bup.f
     self.assertIs(fup.fn, None)
     self.assertIs(fup.parent, bup)
示例#14
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.
     f = functional_value()
     pprint(f)
     b = block()
     b.f = f
     pprint(f)
     pprint(b)
     m = block()
     m.b = b
     pprint(f)
     pprint(b)
     pprint(m)
示例#15
0
 def test_pickle(self):
     f = functional_value()
     self.assertIs(f.fn, None)
     self.assertIs(f.parent, None)
     fup = pickle.loads(
         pickle.dumps(f))
     self.assertIs(fup.fn, None)
     self.assertIs(fup.parent, None)
     b = block()
     b.f = f
     self.assertIs(f.parent, b)
     bup = pickle.loads(
         pickle.dumps(b))
     fup = bup.f
     self.assertIs(fup.fn, None)
     self.assertIs(fup.parent, bup)
示例#16
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.
     f = functional_value()
     pyomo.kernel.pprint(f)
     b = block()
     b.f = f
     pyomo.kernel.pprint(f)
     pyomo.kernel.pprint(b)
     m = block()
     m.b = b
     pyomo.kernel.pprint(f)
     pyomo.kernel.pprint(b)
     pyomo.kernel.pprint(m)
示例#17
0
 def test_dill(self):
     p = parameter(1)
     f = functional_value(lambda: p())
     self.assertEqual(f(), 1)
     fup = dill.loads(dill.dumps(f))
     p.value = 2
     self.assertEqual(f(), 2)
     self.assertEqual(fup(), 1)
     b = block()
     b.p = p
     b.f = f
     self.assertEqual(b.f(), 2)
     bup = dill.loads(dill.dumps(b))
     fup = bup.f
     b.p.value = 4
     self.assertEqual(b.f(), 4)
     self.assertEqual(bup.f(), 2)
     bup.p.value = 4
     self.assertEqual(bup.f(), 4)
示例#18
0
 def test_dill(self):
     p = parameter(1)
     f = functional_value(lambda: p())
     self.assertEqual(f(), 1)
     fup = dill.loads(
         dill.dumps(f))
     p.value = 2
     self.assertEqual(f(), 2)
     self.assertEqual(fup(), 1)
     b = block()
     b.p = p
     b.f = f
     self.assertEqual(b.f(), 2)
     bup = dill.loads(
         dill.dumps(b))
     fup = bup.f
     b.p.value = 4
     self.assertEqual(b.f(), 4)
     self.assertEqual(bup.f(), 2)
     bup.p.value = 4
     self.assertEqual(bup.f(), 4)
示例#19
0
 def test_is_parameter_type(self):
     f = functional_value()
     # GH: apparently is_parameter_type has something
     #     to do with mutability...
     self.assertEqual(f.is_parameter_type(), False)
示例#20
0
 def test_is_expression_type(self):
     f = functional_value()
     self.assertEqual(f.is_expression_type(), False)
示例#21
0
 def test_type(self):
     f = functional_value()
     self.assertTrue(isinstance(f, ICategorizedObject))
     self.assertTrue(isinstance(f, IParameter))
     self.assertTrue(isinstance(f, NumericValue))
示例#22
0
 def test_is_expression_type(self):
     f = functional_value()
     self.assertEqual(f.is_expression_type(), False)
示例#23
0
 def test_is_parameter_type(self):
     f = functional_value()
     # GH: apparently is_parameter_type has something
     #     to do with mutability...
     self.assertEqual(f.is_parameter_type(), False)
示例#24
0
 def test_type(self):
     f = functional_value()
     self.assertTrue(isinstance(f, ICategorizedObject))
     self.assertTrue(isinstance(f, IParameter))
     self.assertTrue(isinstance(f, NumericValue))
示例#25
0
 def test_ctype(self):
     f = functional_value()
     self.assertIs(f.ctype, IParameter)
     self.assertIs(type(f), functional_value)
     self.assertIs(type(f)._ctype, IParameter)
示例#26
0
 def test_ctype(self):
     f = functional_value()
     self.assertIs(f.ctype, IParameter)
     self.assertIs(type(f), functional_value)
     self.assertIs(type(f)._ctype, IParameter)