def test_in_condition(self):
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        hp3 = UniformIntegerHyperparameter("child2", 0, 10)

        self.assertRaisesRegexp(
            TypeError, "Argument 'hyperparameter' is not of"
            " type <class 'ConfigSpace.hyperparameters.Hyperparameter'>.",
            ForbiddenInClause, "HP1", 1)

        self.assertRaisesRegexp(
            ValueError, "Forbidden clause must be instantiated with a "
            "legal hyperparameter value for "
            "'parent, Type: Categorical, Choices: {0, 1}, "
            "Default: 0', but got '2'", ForbiddenInClause, hp1, [2])

        forb1 = ForbiddenInClause(hp2, [5, 6, 7, 8, 9])
        forb1_ = ForbiddenInClause(hp2, [9, 8, 7, 6, 5])
        forb2 = ForbiddenInClause(hp2, [5, 6, 7, 8])
        forb3 = ForbiddenInClause(hp3, [5, 6, 7, 8, 9])

        self.assertEqual(forb1, forb1_)
        self.assertNotEqual(forb1, forb2)
        self.assertNotEqual(forb1, forb3)
        self.assertEqual("Forbidden: child in {5, 6, 7, 8, 9}", str(forb1))

        self.assertRaisesRegexp(
            ValueError, "Is_forbidden must be called with the "
            "instanstatiated hyperparameter in the "
            "forbidden clause; you are missing "
            "'child'", forb1.is_forbidden, {'parent': 1})
        self.assertFalse(forb1.is_forbidden({'parent': 1}, strict=False))

        for i in range(0, 5):
            self.assertFalse(forb1.is_forbidden({'child': i}))

        for i in range(5, 10):
            self.assertTrue(forb1.is_forbidden({'child': i}))

        # Test forbidden on vector values
        hyperparameter_idx = {hp1.name: 0, hp2.name: 1}
        forb1.set_vector_idx(hyperparameter_idx)
        self.assertFalse(
            forb1.is_forbidden_vector([np.NaN, np.NaN], strict=False))
        self.assertFalse(forb1.is_forbidden_vector([np.NaN, 0]))
        correct_vector_value = hp2._inverse_transform(6)
        self.assertTrue(
            forb1.is_forbidden_vector([np.NaN, correct_vector_value]))
    def test_in_condition(self):
        hp1 = CategoricalHyperparameter("parent", [0, 1, 2, 3, 4])
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        hp3 = UniformIntegerHyperparameter("child2", 0, 10)
        hp4 = CategoricalHyperparameter("grandchild", ["hot", "cold", "warm"])

        self.assertRaisesRegex(
            ValueError,
            "Forbidden clause must be instantiated with a "
            "legal hyperparameter value for "
            "'parent, Type: Categorical, Choices: {0, 1, 2, 3, 4}, "
            "Default: 0', but got '5'",
            ForbiddenInClause,
            hp1,
            [5],
        )

        forb1 = ForbiddenInClause(hp2, [5, 6, 7, 8, 9])
        forb1_ = ForbiddenInClause(hp2, [9, 8, 7, 6, 5])
        forb2 = ForbiddenInClause(hp2, [5, 6, 7, 8])
        forb3 = ForbiddenInClause(hp3, [5, 6, 7, 8, 9])
        forb4 = ForbiddenInClause(hp4, ["hot", "cold"])
        forb4_ = ForbiddenInClause(hp4, ["hot", "cold"])
        forb5 = ForbiddenInClause(hp1, [3, 4])
        forb5_ = ForbiddenInClause(hp1, [3, 4])

        self.assertEqual(forb5, forb5_)
        self.assertEqual(forb4, forb4_)

        # print("\nTest1:")
        self.assertEqual(forb1, forb1_)
        # print("\nTest2:")
        self.assertNotEqual(forb1, forb2)
        # print("\nTest3:")
        self.assertNotEqual(forb1, forb3)
        # print("\nTest4:")
        self.assertEqual("Forbidden: child in {5, 6, 7, 8, 9}", str(forb1))
        # print("\nTest5:")
        self.assertRaisesRegex(
            ValueError, "Is_forbidden must be called with the "
            "instanstatiated hyperparameter in the "
            "forbidden clause; you are missing "
            "'child'", forb1.is_forbidden, {'parent': 1}, True)
        # print("\nTest6:")
        self.assertFalse(forb1.is_forbidden({'parent': 1}, strict=False))
        # print("\nTest7:")
        for i in range(0, 5):
            self.assertFalse(forb1.is_forbidden({'child': i}, True))
        # print("\nTest8:")
        for i in range(5, 10):
            self.assertTrue(forb1.is_forbidden({'child': i}, True))

        # Test forbidden on vector values
        hyperparameter_idx = {hp1.name: 0, hp2.name: 1}
        forb1.set_vector_idx(hyperparameter_idx)
        # print("\nTest9:")
        self.assertFalse(
            forb1.is_forbidden_vector(np.array([np.NaN, np.NaN]),
                                      strict=False))
        # print("\nTest10:")
        self.assertFalse(
            forb1.is_forbidden_vector(np.array([np.NaN, 0]), strict=False))
        correct_vector_value = hp2._inverse_transform(6)
        # print("\nTest11:")
        # print(correct_vector_value, np.array([np.NaN, correct_vector_value]))
        self.assertTrue(
            forb1.is_forbidden_vector(np.array([np.NaN, correct_vector_value]),
                                      strict=False))
Beispiel #3
0
    def test_in_condition(self):
        hp1 = CategoricalHyperparameter("parent", [0, 1, 2, 3, 4])
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        hp3 = UniformIntegerHyperparameter("child2", 0, 10)
        hp4 = CategoricalHyperparameter("grandchild", ["hot", "cold", "warm"])

        self.assertRaisesRegex(
            ValueError,
            "Forbidden clause must be instantiated with a "
            "legal hyperparameter value for "
            "'parent, Type: Categorical, Choices: {0, 1, 2, 3, 4}, "
            "Default: 0', but got '5'",
            ForbiddenInClause, hp1, [5],
        )

        forb1 = ForbiddenInClause(hp2, [5, 6, 7, 8, 9])
        forb1_ = ForbiddenInClause(hp2, [9, 8, 7, 6, 5])
        forb2 = ForbiddenInClause(hp2, [5, 6, 7, 8])
        forb3 = ForbiddenInClause(hp3, [5, 6, 7, 8, 9])
        forb4 = ForbiddenInClause(hp4, ["hot", "cold"])
        forb4_ = ForbiddenInClause(hp4, ["hot", "cold"])
        forb5 = ForbiddenInClause(hp1, [3, 4])
        forb5_ = ForbiddenInClause(hp1, [3, 4])

        self.assertEqual(forb5, forb5_)
        self.assertEqual(forb4, forb4_)

        # print("\nTest1:")
        self.assertEqual(forb1, forb1_)
        # print("\nTest2:")
        self.assertNotEqual(forb1, forb2)
        # print("\nTest3:")
        self.assertNotEqual(forb1, forb3)
        # print("\nTest4:")
        self.assertEqual("Forbidden: child in {5, 6, 7, 8, 9}", str(forb1))
        # print("\nTest5:")
        self.assertRaisesRegex(ValueError,
                               "Is_forbidden must be called with the "
                               "instanstatiated hyperparameter in the "
                               "forbidden clause; you are missing "
                               "'child'", forb1.is_forbidden,
                               {'parent': 1}, True)
        # print("\nTest6:")
        self.assertFalse(forb1.is_forbidden({'parent': 1}, strict=False))
        # print("\nTest7:")
        for i in range(0, 5):
            self.assertFalse(forb1.is_forbidden({'child': i}, True))
        # print("\nTest8:")
        for i in range(5, 10):
            self.assertTrue(forb1.is_forbidden({'child': i}, True))

        # Test forbidden on vector values
        hyperparameter_idx = {
            hp1.name: 0,
            hp2.name: 1
        }
        forb1.set_vector_idx(hyperparameter_idx)
        # print("\nTest9:")
        self.assertFalse(forb1.is_forbidden_vector(np.array([np.NaN, np.NaN]), strict=False))
        # print("\nTest10:")
        self.assertFalse(forb1.is_forbidden_vector(np.array([np.NaN, 0]), strict=False))
        correct_vector_value = hp2._inverse_transform(6)
        # print("\nTest11:")
        # print(correct_vector_value, np.array([np.NaN, correct_vector_value]))
        self.assertTrue(forb1.is_forbidden_vector(np.array([np.NaN, correct_vector_value]), strict=False))