class ParameterConstraintTest(TestCase):
    def setUp(self):
        self.constraint = ParameterConstraint(constraint_dict={
            "x": 2.0,
            "y": -3.0
        },
                                              bound=6.0)
        self.constraint_repr = "ParameterConstraint(2.0*x + -3.0*y <= 6.0)"

    def testEq(self):
        constraint1 = ParameterConstraint(constraint_dict={
            "x": 2.0,
            "y": -3.0
        },
                                          bound=6.0)
        constraint2 = ParameterConstraint(constraint_dict={
            "y": -3.0,
            "x": 2.0
        },
                                          bound=6.0)
        self.assertEqual(constraint1, constraint2)

        constraint3 = ParameterConstraint(constraint_dict={
            "x": 2.0,
            "y": -5.0
        },
                                          bound=6.0)
        self.assertNotEqual(constraint1, constraint3)

    def testProperties(self):
        self.assertEqual(self.constraint.constraint_dict["x"], 2.0)
        self.assertEqual(self.constraint.bound, 6.0)

    def testRepr(self):
        self.assertEqual(str(self.constraint), self.constraint_repr)

    def testValidate(self):
        parameters = {"x": 4, "z": 3}
        with self.assertRaises(ValueError):
            self.constraint.check(parameters)

        parameters = {"x": 4, "y": 1}
        self.assertTrue(self.constraint.check(parameters))

        self.constraint.bound = 4.0
        self.assertFalse(self.constraint.check(parameters))

    def testClone(self):
        constraint_clone = self.constraint.clone()
        self.assertEqual(self.constraint.bound, constraint_clone.bound)

        constraint_clone._bound = 7.0
        self.assertNotEqual(self.constraint.bound, constraint_clone.bound)

    def testCloneWithTransformedParameters(self):
        constraint_clone = self.constraint.clone_with_transformed_parameters(
            transformed_parameters={})
        self.assertEqual(self.constraint.bound, constraint_clone.bound)

        constraint_clone._bound = 7.0
        self.assertNotEqual(self.constraint.bound, constraint_clone.bound)
コード例 #2
0
class ParameterConstraintTest(TestCase):
    def setUp(self):
        self.constraint = ParameterConstraint(constraint_dict={
            "x": 2.0,
            "y": -3.0
        },
                                              bound=6.0)
        self.constraint_repr = "ParameterConstraint(2.0*x + -3.0*y <= 6.0)"

    def testEq(self):
        constraint1 = ParameterConstraint(constraint_dict={
            "x": 2.0,
            "y": -3.0
        },
                                          bound=6.0)
        constraint2 = ParameterConstraint(constraint_dict={
            "y": -3.0,
            "x": 2.0
        },
                                          bound=6.0)
        self.assertEqual(constraint1, constraint2)

        constraint3 = ParameterConstraint(constraint_dict={
            "x": 2.0,
            "y": -5.0
        },
                                          bound=6.0)
        self.assertNotEqual(constraint1, constraint3)

    def testProperties(self):
        self.assertEqual(self.constraint.constraint_dict["x"], 2.0)
        self.assertEqual(self.constraint.bound, 6.0)

    def testRepr(self):
        self.assertEqual(str(self.constraint), self.constraint_repr)

    def testValidate(self):
        parameters = {"x": 4, "z": 3}
        with self.assertRaises(ValueError):
            self.constraint.check(parameters)

        # check slack constraint
        parameters = {"x": 4, "y": 1}
        self.assertTrue(self.constraint.check(parameters))

        # check tight constraint (within numerical tolerance)
        parameters = {"x": 4, "y": (2 - 0.5e-8) / 3}
        self.assertTrue(self.constraint.check(parameters))

        # check violated constraint
        parameters = {"x": 4, "y": (2 - 0.5e-6) / 3}
        self.assertFalse(self.constraint.check(parameters))

    def testClone(self):
        constraint_clone = self.constraint.clone()
        self.assertEqual(self.constraint.bound, constraint_clone.bound)

        constraint_clone._bound = 7.0
        self.assertNotEqual(self.constraint.bound, constraint_clone.bound)

    def testCloneWithTransformedParameters(self):
        constraint_clone = self.constraint.clone_with_transformed_parameters(
            transformed_parameters={})
        self.assertEqual(self.constraint.bound, constraint_clone.bound)

        constraint_clone._bound = 7.0
        self.assertNotEqual(self.constraint.bound, constraint_clone.bound)

    def testSortable(self):
        constraint1 = ParameterConstraint(constraint_dict={
            "x": 2.0,
            "y": -3.0
        },
                                          bound=1.0)
        constraint2 = ParameterConstraint(constraint_dict={
            "y": -3.0,
            "x": 2.0
        },
                                          bound=6.0)
        self.assertTrue(constraint1 < constraint2)