Пример #1
0
    def test_greater_and_less_condition(self):
        child = Constant('child', 'child')
        hp1 = UniformFloatHyperparameter("float", 0, 5)
        hp2 = UniformIntegerHyperparameter("int", 0, 5)
        hp3 = OrdinalHyperparameter("ord", list(range(6)))

        for hp in [hp1, hp2, hp3]:
            gt = GreaterThanCondition(child, hp, 1)
            self.assertFalse(gt.evaluate({hp.name: 0}))
            self.assertTrue(gt.evaluate({hp.name: 2}))
            self.assertFalse(gt.evaluate({hp.name: None}))

            lt = LessThanCondition(child, hp, 1)
            self.assertTrue(lt.evaluate({hp.name: 0}))
            self.assertFalse(lt.evaluate({hp.name: 2}))
            self.assertFalse(lt.evaluate({hp.name: None}))

        hp4 = CategoricalHyperparameter("cat", list(range(6)))
        self.assertRaisesRegexp(
            ValueError, "Parent hyperparameter in a > or < "
            "condition must be a subclass of "
            "NumericalHyperparameter or "
            "OrdinalHyperparameter, but is "
            "<class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
            GreaterThanCondition, child, hp4, 1)
        self.assertRaisesRegexp(
            ValueError, "Parent hyperparameter in a > or < "
            "condition must be a subclass of "
            "NumericalHyperparameter or "
            "OrdinalHyperparameter, but is "
            "<class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
            LessThanCondition, child, hp4, 1)

        hp5 = OrdinalHyperparameter("ord",
                                    ['cold', 'luke warm', 'warm', 'hot'])

        gt = GreaterThanCondition(child, hp5, 'warm')
        self.assertTrue(gt.evaluate({hp5.name: 'hot'}))
        self.assertFalse(gt.evaluate({hp5.name: 'cold'}))

        lt = LessThanCondition(child, hp5, 'warm')
        self.assertTrue(lt.evaluate({hp5.name: 'luke warm'}))
        self.assertFalse(lt.evaluate({hp5.name: 'warm'}))
    def test_greater_and_less_condition(self):
        child = Constant('child', 'child')
        hp1 = UniformFloatHyperparameter("float", 0, 5)
        hp2 = UniformIntegerHyperparameter("int", 0, 5)
        hp3 = OrdinalHyperparameter("ord", list(range(6)))

        for hp in [hp1, hp2, hp3]:
            hyperparameter_idx = {child.name: 0, hp.name: 1}

            gt = GreaterThanCondition(child, hp, 1)
            gt.set_vector_idx(hyperparameter_idx)
            self.assertFalse(gt.evaluate({hp.name: 0}))
            self.assertTrue(gt.evaluate({hp.name: 2}))
            self.assertFalse(gt.evaluate({hp.name: None}))

            # Evaluate vector
            test_value = hp._inverse_transform(2)
            self.assertFalse(gt.evaluate_vector(np.array([np.NaN, 0])))
            self.assertTrue(gt.evaluate_vector(np.array([np.NaN, test_value])))
            self.assertFalse(gt.evaluate_vector(np.array([np.NaN, np.NaN])))

            lt = LessThanCondition(child, hp, 1)
            lt.set_vector_idx(hyperparameter_idx)
            self.assertTrue(lt.evaluate({hp.name: 0}))
            self.assertFalse(lt.evaluate({hp.name: 2}))
            self.assertFalse(lt.evaluate({hp.name: None}))

            # Evaluate vector
            test_value = hp._inverse_transform(2)
            self.assertTrue(lt.evaluate_vector(np.array([np.NaN, 0, 0, 0])))
            self.assertFalse(lt.evaluate_vector(np.array([np.NaN,
                                                          test_value])))
            self.assertFalse(lt.evaluate_vector(np.array([np.NaN, np.NaN])))

        hp4 = CategoricalHyperparameter("cat", list(range(6)))
        self.assertRaisesRegexp(
            ValueError, "Parent hyperparameter in a > or < "
            "condition must be a subclass of "
            "NumericalHyperparameter or "
            "OrdinalHyperparameter, but is "
            "<class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
            GreaterThanCondition, child, hp4, 1)
        self.assertRaisesRegexp(
            ValueError, "Parent hyperparameter in a > or < "
            "condition must be a subclass of "
            "NumericalHyperparameter or "
            "OrdinalHyperparameter, but is "
            "<class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
            LessThanCondition, child, hp4, 1)

        hp5 = OrdinalHyperparameter("ord",
                                    ['cold', 'luke warm', 'warm', 'hot'])

        hyperparameter_idx = {child.name: 0, hp5.name: 1}
        gt = GreaterThanCondition(child, hp5, 'warm')
        gt.set_vector_idx(hyperparameter_idx)
        self.assertTrue(gt.evaluate({hp5.name: 'hot'}))
        self.assertFalse(gt.evaluate({hp5.name: 'cold'}))

        self.assertTrue(gt.evaluate_vector(np.array([np.NaN, 3])))
        self.assertFalse(gt.evaluate_vector(np.array([np.NaN, 0])))

        lt = LessThanCondition(child, hp5, 'warm')
        lt.set_vector_idx(hyperparameter_idx)
        self.assertTrue(lt.evaluate({hp5.name: 'luke warm'}))
        self.assertFalse(lt.evaluate({hp5.name: 'warm'}))

        self.assertTrue(lt.evaluate_vector(np.array([np.NaN, 1])))
        self.assertFalse(lt.evaluate_vector(np.array([np.NaN, 2])))
Пример #3
0
    def test_greater_and_less_condition(self):
        child = Constant('child', 'child')
        hp1 = UniformFloatHyperparameter("float", 0, 5)
        hp2 = UniformIntegerHyperparameter("int", 0, 5)
        hp3 = OrdinalHyperparameter("ord", list(range(6)))

        for hp in [hp1, hp2, hp3]:
            hyperparameter_idx = {
                child.name: 0,
                hp.name: 1
            }

            gt = GreaterThanCondition(child, hp, 1)
            gt.set_vector_idx(hyperparameter_idx)
            self.assertFalse(gt.evaluate({hp.name: 0}))
            self.assertTrue(gt.evaluate({hp.name: 2}))
            self.assertFalse(gt.evaluate({hp.name: None}))

            # Evaluate vector
            test_value = hp._inverse_transform(2)
            self.assertFalse(gt.evaluate_vector(np.array([np.NaN, 0])))
            self.assertTrue(gt.evaluate_vector(np.array([np.NaN, test_value])))
            self.assertFalse(gt.evaluate_vector(np.array([np.NaN, np.NaN])))

            lt = LessThanCondition(child, hp, 1)
            lt.set_vector_idx(hyperparameter_idx)
            self.assertTrue(lt.evaluate({hp.name: 0}))
            self.assertFalse(lt.evaluate({hp.name: 2}))
            self.assertFalse(lt.evaluate({hp.name: None}))

            # Evaluate vector
            test_value = hp._inverse_transform(2)
            self.assertTrue(lt.evaluate_vector(np.array([np.NaN, 0, 0, 0])))
            self.assertFalse(lt.evaluate_vector(np.array([np.NaN, test_value])))
            self.assertFalse(lt.evaluate_vector(np.array([np.NaN, np.NaN])))

        hp4 = CategoricalHyperparameter("cat", list(range(6)))
        self.assertRaisesRegex(ValueError, "Parent hyperparameter in a > or < "
                                           "condition must be a subclass of "
                                           "NumericalHyperparameter or "
                                           "OrdinalHyperparameter, but is "
                                           "<cdef class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
                               GreaterThanCondition, child, hp4, 1)
        self.assertRaisesRegex(ValueError, "Parent hyperparameter in a > or < "
                                           "condition must be a subclass of "
                                           "NumericalHyperparameter or "
                                           "OrdinalHyperparameter, but is "
                                           "<cdef class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
                               LessThanCondition, child, hp4, 1)

        hp5 = OrdinalHyperparameter("ord", ['cold', 'luke warm', 'warm', 'hot'])

        hyperparameter_idx = {
            child.name: 0,
            hp5.name: 1
        }
        gt = GreaterThanCondition(child, hp5, 'warm')
        gt.set_vector_idx(hyperparameter_idx)
        self.assertTrue(gt.evaluate({hp5.name: 'hot'}))
        self.assertFalse(gt.evaluate({hp5.name: 'cold'}))

        self.assertTrue(gt.evaluate_vector(np.array([np.NaN, 3])))
        self.assertFalse(gt.evaluate_vector(np.array([np.NaN, 0])))

        lt = LessThanCondition(child, hp5, 'warm')
        lt.set_vector_idx(hyperparameter_idx)
        self.assertTrue(lt.evaluate({hp5.name: 'luke warm'}))
        self.assertFalse(lt.evaluate({hp5.name: 'warm'}))

        self.assertTrue(lt.evaluate_vector(np.array([np.NaN, 1])))
        self.assertFalse(lt.evaluate_vector(np.array([np.NaN, 2])))