Beispiel #1
0
    def test_check_neighbouring_config_diamond_str(self):
        diamond = ConfigurationSpace()
        head = CategoricalHyperparameter('head', ['red', 'green'])
        left = CategoricalHyperparameter('left', ['red', 'green'])
        right = CategoricalHyperparameter('right',
                                          ['red', 'green', 'blue', 'yellow'])
        bottom = CategoricalHyperparameter('bottom', ['red', 'green'])
        diamond.add_hyperparameters([head, left, right, bottom])
        diamond.add_condition(EqualsCondition(left, head, 'red'))
        diamond.add_condition(EqualsCondition(right, head, 'red'))
        diamond.add_condition(
            AndConjunction(EqualsCondition(bottom, left, 'green'),
                           EqualsCondition(bottom, right, 'green')))

        config = Configuration(diamond, {
            'bottom': 'red',
            'head': 'red',
            'left': 'green',
            'right': 'green'
        })
        hp_name = "head"
        index = diamond.get_idx_by_hyperparameter_name(hp_name)
        neighbor_value = 1

        new_array = ConfigSpace.c_util.change_hp_value(diamond,
                                                       config.get_array(),
                                                       hp_name, neighbor_value,
                                                       index)
        expected_array = np.array([1, np.nan, np.nan, np.nan])

        np.testing.assert_almost_equal(new_array, expected_array)
    def test_impute_inactive_hyperparameters(self):
        cs = smac.configspace.ConfigurationSpace()
        a = cs.add_hyperparameter(CategoricalHyperparameter('a', [0, 1]))
        b = cs.add_hyperparameter(CategoricalHyperparameter('b', [0, 1]))
        c = cs.add_hyperparameter(UniformFloatHyperparameter('c', 0, 1))
        cs.add_condition(EqualsCondition(b, a, 1))
        cs.add_condition(EqualsCondition(c, a, 0))
        cs.seed(1)

        configs = cs.sample_configuration(size=100)
        config_array = smac.configspace.convert_configurations_to_array(
            configs)
        for line in config_array:
            if line[0] == 0:
                self.assertTrue(np.isnan(line[1]))
            elif line[0] == 1:
                self.assertTrue(np.isnan(line[2]))

        model = RandomForestWithInstances(
            configspace=cs,
            types=np.zeros((3, ), dtype=np.uint),
            bounds=list(map(lambda x: (0, 1), range(10))),
            seed=1,
        )
        config_array = model._impute_inactive(config_array)
        for line in config_array:
            if line[0] == 0:
                self.assertEqual(line[1], 2)
            elif line[0] == 1:
                self.assertEqual(line[2], -1)
Beispiel #3
0
    def test_add_conditions(self):
        cs1 = ConfigurationSpace()
        cs2 = ConfigurationSpace()

        hp1 = cs1.add_hyperparameter(
            CategoricalHyperparameter("input1", [0, 1]))
        cs2.add_hyperparameter(hp1)
        hp2 = cs1.add_hyperparameter(
            CategoricalHyperparameter("input2", [0, 1]))
        cs2.add_hyperparameter(hp2)
        hp3 = cs1.add_hyperparameter(
            UniformIntegerHyperparameter("child1", 0, 10))
        cs2.add_hyperparameter(hp3)
        hp4 = cs1.add_hyperparameter(
            UniformIntegerHyperparameter("child2", 0, 10))
        cs2.add_hyperparameter(hp4)

        cond1 = EqualsCondition(hp2, hp3, 0)
        cond2 = EqualsCondition(hp1, hp3, 5)
        cond3 = EqualsCondition(hp1, hp4, 1)
        andCond = AndConjunction(cond2, cond3)

        cs1.add_conditions([cond1, andCond])
        cs2.add_condition(cond1)
        cs2.add_condition(andCond)

        self.assertEqual(str(cs1), str(cs2))
    def test_check_neighbouring_config_diamond(self):
        diamond = ConfigurationSpace()
        head = CategoricalHyperparameter('head', [0, 1])
        left = CategoricalHyperparameter('left', [0, 1])
        right = CategoricalHyperparameter('right', [0, 1, 2, 3])
        bottom = CategoricalHyperparameter('bottom', [0, 1])
        diamond.add_hyperparameters([head, left, right, bottom])
        diamond.add_condition(EqualsCondition(left, head, 0))
        diamond.add_condition(EqualsCondition(right, head, 0))
        diamond.add_condition(
            AndConjunction(EqualsCondition(bottom, left, 1),
                           EqualsCondition(bottom, right, 1)))

        config = Configuration(diamond, {
            'bottom': 0,
            'head': 0,
            'left': 1,
            'right': 1
        })
        hp_name = "head"
        index = diamond.get_idx_by_hyperparameter_name(hp_name)
        neighbor_value = 1

        new_array = change_hp_value(diamond, config.get_array(), hp_name,
                                    neighbor_value, index)
        expected_array = np.array([1, np.nan, np.nan, np.nan])

        np.testing.assert_almost_equal(new_array, expected_array)
Beispiel #5
0
    def test_check_configuration2(self):
        # Test that hyperparameters which are not active must not be set and
        # that evaluating forbidden clauses does not choke on missing
        # hyperparameters
        cs = ConfigurationSpace()
        classifier = CategoricalHyperparameter(
            "classifier", ["k_nearest_neighbors", "extra_trees"])
        metric = CategoricalHyperparameter("metric", ["minkowski", "other"])
        p = CategoricalHyperparameter("k_nearest_neighbors:p", [1, 2])
        metric_depends_on_classifier = EqualsCondition(metric, classifier,
                                                       "k_nearest_neighbors")
        p_depends_on_metric = EqualsCondition(p, metric, "minkowski")
        cs.add_hyperparameter(metric)
        cs.add_hyperparameter(p)
        cs.add_hyperparameter(classifier)
        cs.add_condition(metric_depends_on_classifier)
        cs.add_condition(p_depends_on_metric)

        forbidden = ForbiddenEqualsClause(metric, "other")
        cs.add_forbidden_clause(forbidden)

        configuration = Configuration(cs, dict(classifier="extra_trees"))

        # check backward compatibility with checking configurations instead of vectors
        cs.check_configuration(configuration)
 def test_condition_with_cycles(self):
     cs = ConfigurationSpace()
     hp1 = CategoricalHyperparameter("parent", [0, 1])
     cs.add_hyperparameter(hp1)
     hp2 = UniformIntegerHyperparameter("child", 0, 10)
     cs.add_hyperparameter(hp2)
     cond1 = EqualsCondition(hp2, hp1, 0)
     cs.add_condition(cond1)
     cond2 = EqualsCondition(hp1, hp2, 0)
     self.assertRaisesRegex(ValueError, r"Hyperparameter configuration "
                            r"contains a cycle \[\['child', 'parent'\]\]",
                            cs.add_condition, cond2)
Beispiel #7
0
    def test_get_hyperparameters_topological_sort(self):
        # and now for something more complicated
        cs = ConfigurationSpace()
        hp1 = CategoricalHyperparameter("input1", [0, 1])
        hp2 = CategoricalHyperparameter("input2", [0, 1])
        hp3 = CategoricalHyperparameter("input3", [0, 1])
        hp4 = CategoricalHyperparameter("input4", [0, 1])
        hp5 = CategoricalHyperparameter("input5", [0, 1])
        hp6 = Constant("AND", "True")
        # More top-level hyperparameters
        hp7 = CategoricalHyperparameter("input7", [0, 1])
        # Somewhat shuffled
        hyperparameters = [hp1, hp2, hp3, hp4, hp5, hp6, hp7]

        for hp in hyperparameters:
            cs.add_hyperparameter(hp)

        cond1 = EqualsCondition(hp6, hp1, 1)
        cond2 = NotEqualsCondition(hp6, hp2, 1)
        cond3 = InCondition(hp6, hp3, [1])
        cond4 = EqualsCondition(hp5, hp3, 1)
        cond5 = EqualsCondition(hp4, hp5, 1)
        cond6 = EqualsCondition(hp6, hp4, 1)
        cond7 = EqualsCondition(hp6, hp5, 1)

        conj1 = AndConjunction(cond1, cond2)
        conj2 = OrConjunction(conj1, cond3)
        conj3 = AndConjunction(conj2, cond6, cond7)

        cs.add_condition(cond4)
        hps = cs.get_hyperparameters()
        # AND is moved to the front because of alphabetical sorting
        for hp, idx in zip(hyperparameters, [1, 2, 3, 4, 6, 0, 5]):
            self.assertEqual(hps.index(hp), idx)
            self.assertEqual(cs._hyperparameter_idx[hp.name], idx)
            self.assertEqual(cs._idx_to_hyperparameter[idx], hp.name)

        cs.add_condition(cond5)
        hps = cs.get_hyperparameters()
        for hp, idx in zip(hyperparameters, [1, 2, 3, 6, 5, 0, 4]):
            self.assertEqual(hps.index(hp), idx)
            self.assertEqual(cs._hyperparameter_idx[hp.name], idx)
            self.assertEqual(cs._idx_to_hyperparameter[idx], hp.name)

        cs.add_condition(conj3)
        hps = cs.get_hyperparameters()
        # print(hps, hyperparameters)
        for hp, idx in zip(hyperparameters, [0, 1, 2, 5, 4, 6, 3]):
            # print(hp, idx)
            self.assertEqual(hps.index(hp), idx)
            self.assertEqual(cs._hyperparameter_idx[hp.name], idx)
        self.assertEqual(cs._idx_to_hyperparameter[idx], hp.name)
Beispiel #8
0
    def test_deactivate_inactive_hyperparameters(self):
        diamond = ConfigurationSpace()
        head = CategoricalHyperparameter('head', [0, 1])
        left = CategoricalHyperparameter('left', [0, 1])
        right = CategoricalHyperparameter('right', [0, 1])
        bottom = CategoricalHyperparameter('bottom', [0, 1])
        diamond.add_hyperparameters([head, left, right, bottom])
        diamond.add_condition(EqualsCondition(left, head, 0))
        diamond.add_condition(EqualsCondition(right, head, 0))
        diamond.add_condition(AndConjunction(EqualsCondition(bottom, left, 0),
                                             EqualsCondition(bottom, right, 0)))

        c = deactivate_inactive_hyperparameters({'head': 0, 'left': 0,
                                                 'right': 0, 'bottom': 0},
                                                 diamond)
        diamond._check_configuration_rigorous(c)

        c = deactivate_inactive_hyperparameters({'head': 1, 'left': 0,
                                                 'right': 0, 'bottom': 0},
                                                diamond)
        diamond._check_configuration_rigorous(c)

        c = deactivate_inactive_hyperparameters({'head': 0, 'left': 1,
                                                 'right': 0, 'bottom': 0},
                                                diamond)
        diamond._check_configuration_rigorous(c)

        diamond = ConfigurationSpace()
        head = CategoricalHyperparameter('head', [0, 1])
        left = CategoricalHyperparameter('left', [0, 1])
        right = CategoricalHyperparameter('right', [0, 1])
        bottom = CategoricalHyperparameter('bottom', [0, 1])
        diamond.add_hyperparameters([head, left, right, bottom])
        diamond.add_condition(EqualsCondition(left, head, 0))
        diamond.add_condition(EqualsCondition(right, head, 0))
        diamond.add_condition(OrConjunction(EqualsCondition(bottom, left, 0),
                                            EqualsCondition(bottom, right, 0)))

        c = deactivate_inactive_hyperparameters({'head': 0, 'left': 0,
                                                 'right': 0, 'bottom': 0},
                                                diamond)
        diamond._check_configuration_rigorous(c)

        c = deactivate_inactive_hyperparameters({'head': 1, 'left': 1,
                                                 'right': 0, 'bottom': 0},
                                                diamond)
        diamond._check_configuration_rigorous(c)

        c = deactivate_inactive_hyperparameters({'head': 0, 'left': 1,
                                                 'right': 0, 'bottom': 0},
                                                diamond)
        diamond._check_configuration_rigorous(c)


        plain = ConfigurationSpace()
        a = UniformIntegerHyperparameter('a', 0, 10)
        b = UniformIntegerHyperparameter('b', 0, 10)
        plain.add_hyperparameters([a, b])
        c = deactivate_inactive_hyperparameters({'a': 5, 'b': 6}, plain)
        plain.check_configuration(c)
Beispiel #9
0
    def test_keys(self):
        # A regression test to make sure issue #49 does no longer pop up. By
        # iterating over the configuration in the for loop, it should not raise
        # a KeyError if the child hyperparameter is inactive.
        cs = ConfigurationSpace()
        shrinkage = CategoricalHyperparameter(
            "shrinkage",
            ["None", "auto", "manual"],
            default_value="None",
        )
        shrinkage_factor = UniformFloatHyperparameter(
            "shrinkage_factor",
            0.,
            1.,
            0.5,
        )
        cs.add_hyperparameters([shrinkage, shrinkage_factor])

        cs.add_condition(EqualsCondition(shrinkage_factor, shrinkage,
                                         "manual"))

        for i in range(10):
            config = cs.sample_configuration()
            {
                hp_name: config[hp_name]
                for hp_name in config if config[hp_name] is not None
            }
Beispiel #10
0
    def test_setitem(self):
        '''
        Checks overriding a sampled configuration
        '''
        pcs = ConfigurationSpace()
        pcs.add_hyperparameter(UniformIntegerHyperparameter('x0', 1, 5, default_value=1))
        x1 = pcs.add_hyperparameter(
            CategoricalHyperparameter('x1', ['ab', 'bc', 'cd', 'de'], default_value='ab')
        )

        # Condition
        x2 = pcs.add_hyperparameter(CategoricalHyperparameter('x2', [1, 2]))
        pcs.add_condition(EqualsCondition(x2, x1, 'ab'))

        # Forbidden
        x3 = pcs.add_hyperparameter(CategoricalHyperparameter('x3', [1, 2]))
        pcs.add_forbidden_clause(ForbiddenEqualsClause(x3, 2))

        conf = pcs.get_default_configuration()

        # failed because it's a invalid configuration
        with self.assertRaisesRegex(ValueError, "Illegal value '0' for hyperparameter x0"):
            conf['x0'] = 0

        # failed because the variable didn't exists
        with self.assertRaisesRegex(
            KeyError,
            "Hyperparameter 'x_0' does not exist in this configuration space.",
        ):
            conf['x_0'] = 1

        # failed because forbidden clause is violated
        with self.assertRaisesRegex(
            ForbiddenValueError,
            "Given vector violates forbidden clause Forbidden: x3 == 2",
        ):
            conf['x3'] = 2
        self.assertEqual(conf['x3'], 1)

        # successful operation 1
        x0_old = conf['x0']
        if x0_old == 1:
            conf['x0'] = 2
        else:
            conf['x0'] = 1
        x0_new = conf['x0']
        self.assertNotEqual(x0_old, x0_new)
        pcs._check_configuration_rigorous(conf)
        self.assertEqual(conf['x2'], 1)

        # successful operation 2
        x1_old = conf['x1']
        if x1_old == 'ab':
            conf['x1'] = 'cd'
        else:
            conf['x1'] = 'ab'
        x1_new = conf['x1']
        self.assertNotEqual(x1_old, x1_new)
        pcs._check_configuration_rigorous(conf)
        self.assertRaises(KeyError, conf.__getitem__, 'x2')
Beispiel #11
0
 def test_hyperparameters_with_valid_condition(self):
     cs = ConfigurationSpace()
     hp1 = CategoricalHyperparameter("parent", [0, 1])
     cs.add_hyperparameter(hp1)
     hp2 = UniformIntegerHyperparameter("child", 0, 10)
     cs.add_hyperparameter(hp2)
     cond = EqualsCondition(hp2, hp1, 0)
     cs.add_condition(cond)
     self.assertEqual(len(cs._hyperparameters), 2)
Beispiel #12
0
    def test_add_second_condition_wo_conjunction(self):
        hp1 = CategoricalHyperparameter("input1", [0, 1])
        hp2 = CategoricalHyperparameter("input2", [0, 1])
        hp3 = Constant("And", "True")

        cond1 = EqualsCondition(hp3, hp1, 1)
        cond2 = EqualsCondition(hp3, hp2, 1)

        cs = ConfigurationSpace()
        cs.add_hyperparameter(hp1)
        cs.add_hyperparameter(hp2)
        cs.add_hyperparameter(hp3)

        cs.add_condition(cond1)
        self.assertRaisesRegex(
            ValueError, r"Adding a second condition \(different\) for a "
            r"hyperparameter is ambigouos and "
            r"therefore forbidden. Add a conjunction "
            r"instead!", cs.add_condition, cond2)
Beispiel #13
0
 def test_get_conditions(self):
     cs = ConfigurationSpace()
     hp1 = CategoricalHyperparameter("parent", [0, 1])
     cs.add_hyperparameter(hp1)
     hp2 = UniformIntegerHyperparameter("child", 0, 10)
     cs.add_hyperparameter(hp2)
     self.assertEqual([], cs.get_conditions())
     cond1 = EqualsCondition(hp2, hp1, 0)
     cs.add_condition(cond1)
     self.assertEqual([cond1], cs.get_conditions())
Beispiel #14
0
    def test_add_conjunction(self):
        hp1 = CategoricalHyperparameter("input1", [0, 1])
        hp2 = CategoricalHyperparameter("input2", [0, 1])
        hp3 = CategoricalHyperparameter("input3", [0, 1])
        hp4 = Constant("And", "True")

        cond1 = EqualsCondition(hp4, hp1, 1)
        cond2 = EqualsCondition(hp4, hp2, 1)
        cond3 = EqualsCondition(hp4, hp3, 1)

        andconj1 = AndConjunction(cond1, cond2, cond3)

        cs = ConfigurationSpace()
        cs.add_hyperparameter(hp1)
        cs.add_hyperparameter(hp2)
        cs.add_hyperparameter(hp3)
        cs.add_hyperparameter(hp4)

        cs.add_condition(andconj1)
        self.assertNotIn(hp4, cs.get_all_unconditional_hyperparameters())
    def test_add_configuration_space_conjunctions(self):
        cs1 = ConfigurationSpace()
        cs2 = ConfigurationSpace()

        hp1 = cs1.add_hyperparameter(CategoricalHyperparameter("input1", [0, 1]))
        hp2 = cs1.add_hyperparameter(CategoricalHyperparameter("input2", [0, 1]))
        hp3 = cs1.add_hyperparameter(UniformIntegerHyperparameter("child1", 0, 10))
        hp4 = cs1.add_hyperparameter(UniformIntegerHyperparameter("child2", 0, 10))

        cond1 = EqualsCondition(hp2, hp3, 0)
        cond2 = EqualsCondition(hp1, hp3, 5)
        cond3 = EqualsCondition(hp1, hp4, 1)
        andCond = AndConjunction(cond2, cond3)

        cs1.add_conditions([cond1, andCond])
        cs2.add_configuration_space(prefix='test', configuration_space=cs1)

        self.assertEqual(str(cs2).count('test:'), 10)
        # Check that they're equal except for the "test:" prefix
        self.assertEqual(str(cs1), str(cs2).replace('test:', ''))
Beispiel #16
0
 def test_get_hyperparameters_topological_sort_simple(self):
     for iteration in range(10):
         cs = ConfigurationSpace()
         hp1 = CategoricalHyperparameter("parent", [0, 1])
         cs.add_hyperparameter(hp1)
         hp2 = UniformIntegerHyperparameter("child", 0, 10)
         cs.add_hyperparameter(hp2)
         cond1 = EqualsCondition(hp2, hp1, 0)
         cs.add_condition(cond1)
         # This automatically checks the configuration!
         Configuration(cs, dict(parent=0, child=5))
Beispiel #17
0
    def add_to_config_space(self, name, cs):
        control_name = "{}:control".format(name)

        type_name_to_dist = self.type_name_to_dist(name)
        type_names = list(type_name_to_dist)
        control = CategoricalHyperparameter(name=control_name,
                                            choices=type_names,
                                            default_value=type_names[0])

        cs.add_hyperparameter(control)
        for type_name, dist in type_name_to_dist.items():
            cs_hp = dist.add_to_config_space(type_name, cs)
            cs.add_condition(EqualsCondition(cs_hp, control, type_name))
Beispiel #18
0
 def __condition(self, item: Dict, store: Dict):
     child = item["_child"]
     child = store[child]
     parent = item["_parent"]
     parent = store[parent]
     value = (item["_values"])
     if (isinstance(value, list) and len(value) == 1):
         value = value[0]
     if isinstance(value, list):
         cond = InCondition(child, parent, list(map(hp_def._encode, value)))
     else:
         cond = EqualsCondition(child, parent, hp_def._encode(value))
     return cond
Beispiel #19
0
 def test_get_hyperparamforbidden_clauseseters(self):
     cs = ConfigurationSpace()
     self.assertEqual(0, len(cs.get_hyperparameters()))
     hp1 = CategoricalHyperparameter("parent", [0, 1])
     cs.add_hyperparameter(hp1)
     self.assertEqual([hp1], cs.get_hyperparameters())
     hp2 = UniformIntegerHyperparameter("child", 0, 10)
     cs.add_hyperparameter(hp2)
     cond1 = EqualsCondition(hp2, hp1, 1)
     cs.add_condition(cond1)
     self.assertEqual([hp1, hp2], cs.get_hyperparameters())
     # TODO: I need more tests for the topological sort!
     self.assertEqual([hp1, hp2], cs.get_hyperparameters())
Beispiel #20
0
    def test_impute_inactive_hyperparameters(self):
        cs = ConfigurationSpace()
        a = cs.add_hyperparameter(CategoricalHyperparameter('a', [0, 1]))
        b = cs.add_hyperparameter(CategoricalHyperparameter('b', [0, 1]))
        c = cs.add_hyperparameter(UniformFloatHyperparameter('c', 0, 1))
        cs.add_condition(EqualsCondition(b, a, 1))
        cs.add_condition(EqualsCondition(c, a, 0))
        cs.seed(1)

        configs = cs.sample_configuration(size=100)
        config_array = convert_configurations_to_array(configs)
        for line in config_array:
            if line[0] == 0:
                self.assertTrue(np.isnan(line[1]))
            elif line[0] == 1:
                self.assertTrue(np.isnan(line[2]))

        gp = get_gp(3, np.random.RandomState(1))
        config_array = gp._impute_inactive(config_array)
        for line in config_array:
            if line[0] == 0:
                self.assertEqual(line[1], -1)
            elif line[0] == 1:
                self.assertEqual(line[2], -1)
Beispiel #21
0
 def test_get_types_with_inactive(self):
     cs = ConfigurationSpace()
     a = cs.add_hyperparameter(CategoricalHyperparameter('a', ['a', 'b']))
     b = cs.add_hyperparameter(UniformFloatHyperparameter('b', 1, 5))
     c = cs.add_hyperparameter(UniformIntegerHyperparameter('c', 3, 7))
     d = cs.add_hyperparameter(Constant('d', -5))
     e = cs.add_hyperparameter(OrdinalHyperparameter('e', ['cold', 'hot']))
     f = cs.add_hyperparameter(CategoricalHyperparameter('f', ['x', 'y']))
     cs.add_condition(EqualsCondition(b, a, 'a'))
     cs.add_condition(EqualsCondition(c, a, 'a'))
     cs.add_condition(EqualsCondition(d, a, 'a'))
     cs.add_condition(EqualsCondition(e, a, 'a'))
     cs.add_condition(EqualsCondition(f, a, 'a'))
     types, bounds = get_types(cs, None)
     np.testing.assert_array_equal(types, [2, 0, 0, 2, 0, 3])
     self.assertEqual(bounds[0][0], 2)
     self.assertFalse(np.isfinite(bounds[0][1]))
     np.testing.assert_array_equal(bounds[1], [-1, 1])
     np.testing.assert_array_equal(bounds[2], [-1, 1])
     self.assertEqual(bounds[3][0], 2)
     self.assertFalse(np.isfinite(bounds[3][1]))
     np.testing.assert_array_equal(bounds[4], [0, 2])
     self.assertEqual(bounds[5][0], 3)
     self.assertFalse(np.isfinite(bounds[5][1]))
Beispiel #22
0
 def __condition(self, item: Dict, store: Dict, leader_model):
     child = add_leader_model(item["_child"], leader_model,
                              SERIES_CONNECT_LEADER_TOKEN)
     child = store[child]
     parent = add_leader_model(item["_parent"], leader_model,
                               SERIES_CONNECT_LEADER_TOKEN)
     parent = store[parent]
     value = (item["_values"])
     if (isinstance(value, list) and len(value) == 1):
         value = value[0]
     if isinstance(value, list):
         cond = InCondition(child, parent, list(map(smac_hdl._encode,
                                                    value)))
     else:
         cond = EqualsCondition(child, parent, smac_hdl._encode(value))
     return cond
    def test_add_configuration_space(self):
        cs = ConfigurationSpace()
        hp1 = cs.add_hyperparameter(CategoricalHyperparameter("input1", [0, 1]))
        cs.add_forbidden_clause(ForbiddenEqualsClause(hp1, 1))
        hp2 = cs.add_hyperparameter(UniformIntegerHyperparameter("child", 0, 10))
        cs.add_condition(EqualsCondition(hp2, hp1, 0))
        cs2 = ConfigurationSpace()
        cs2.add_configuration_space('prefix', cs, delimiter='__')
        self.assertEqual(str(cs2), '''Configuration space object:
  Hyperparameters:
    prefix__child, Type: UniformInteger, Range: [0, 10], Default: 5
    prefix__input1, Type: Categorical, Choices: {0, 1}, Default: 0
  Conditions:
    prefix__child | prefix__input1 == 0
  Forbidden Clauses:
    Forbidden: prefix__input1 == 1
''')
Beispiel #24
0
    def test_sample_configuration(self):
        cs = ConfigurationSpace()
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        cs.add_hyperparameter(hp1)
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cs.add_hyperparameter(hp2)
        cond1 = EqualsCondition(hp2, hp1, 0)
        cs.add_condition(cond1)
        # This automatically checks the configuration!
        Configuration(cs, dict(parent=0, child=5))

        # and now for something more complicated
        cs = ConfigurationSpace(seed=1)
        hp1 = CategoricalHyperparameter("input1", [0, 1])
        cs.add_hyperparameter(hp1)
        hp2 = CategoricalHyperparameter("input2", [0, 1])
        cs.add_hyperparameter(hp2)
        hp3 = CategoricalHyperparameter("input3", [0, 1])
        cs.add_hyperparameter(hp3)
        hp4 = CategoricalHyperparameter("input4", [0, 1])
        cs.add_hyperparameter(hp4)
        hp5 = CategoricalHyperparameter("input5", [0, 1])
        cs.add_hyperparameter(hp5)
        hp6 = Constant("AND", "True")
        cs.add_hyperparameter(hp6)

        cond1 = EqualsCondition(hp6, hp1, 1)
        cond2 = NotEqualsCondition(hp6, hp2, 1)
        cond3 = InCondition(hp6, hp3, [1])
        cond4 = EqualsCondition(hp5, hp3, 1)
        cond5 = EqualsCondition(hp4, hp5, 1)
        cond6 = EqualsCondition(hp6, hp4, 1)
        cond7 = EqualsCondition(hp6, hp5, 1)

        conj1 = AndConjunction(cond1, cond2)
        conj2 = OrConjunction(conj1, cond3)
        conj3 = AndConjunction(conj2, cond6, cond7)
        cs.add_condition(cond4)
        cs.add_condition(cond5)
        cs.add_condition(conj3)

        samples = []
        for i in range(5):
            cs.seed(1)
            samples.append([])
            for j in range(100):
                sample = cs.sample_configuration()
                samples[-1].append(sample)

            if i > 0:
                for j in range(100):
                    self.assertEqual(samples[-1][j], samples[-2][j])
Beispiel #25
0
    def test_condition_without_added_hyperparameters(self):
        cs = ConfigurationSpace()
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cond = EqualsCondition(hp2, hp1, 0)
        self.assertRaisesRegex(
            ValueError, "Child hyperparameter 'child' not "
            "in configuration space.", cs.add_condition, cond)
        cs.add_hyperparameter(hp1)
        self.assertRaisesRegex(
            ValueError, "Child hyperparameter 'child' not "
            "in configuration space.", cs.add_condition, cond)

        # Test also the parent hyperparameter
        cs2 = ConfigurationSpace()
        cs2.add_hyperparameter(hp2)
        self.assertRaisesRegex(
            ValueError, "Parent hyperparameter 'parent' "
            "not in configuration space.", cs2.add_condition, cond)
    def test_repr(self):
        cs1 = ConfigurationSpace()
        retval = cs1.__str__()
        self.assertEqual("Configuration space object:\n  Hyperparameters:\n",
                         retval)

        hp1 = CategoricalHyperparameter("parent", [0, 1])
        cs1.add_hyperparameter(hp1)
        retval = cs1.__str__()
        self.assertEqual("Configuration space object:\n  Hyperparameters:\n"
                         "    %s\n" % str(hp1), retval)

        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cond1 = EqualsCondition(hp2, hp1, 0)
        cs1.add_hyperparameter(hp2)
        cs1.add_condition(cond1)
        retval = cs1.__str__()
        self.assertEqual("Configuration space object:\n  Hyperparameters:\n"
                         "    %s\n    %s\n  Conditions:\n    %s\n" %
                         (str(hp2), str(hp1), str(cond1)), retval)
Beispiel #27
0
    def test_get_parent_and_children_of(self):
        cs = ConfigurationSpace()
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        cs.add_hyperparameter(hp1)
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cs.add_hyperparameter(hp2)
        cond1 = EqualsCondition(hp2, hp1, 0)
        cs.add_condition(cond1)

        self.assertEqual([hp1], cs.get_parents_of(hp2.name))
        self.assertEqual([hp1], cs.get_parents_of(hp2))
        self.assertEqual([hp2], cs.get_children_of(hp1.name))
        self.assertEqual([hp2], cs.get_children_of(hp1))

        self.assertRaisesRegex(
            KeyError, "Hyperparameter 'Foo' does not exist in this "
            "configuration space.", cs.get_parents_of, "Foo")
        self.assertRaisesRegex(
            KeyError, "Hyperparameter 'Foo' does not exist in this "
            "configuration space.", cs.get_children_of, "Foo")
Beispiel #28
0
def string2condition(cond_desc: str, hp_dict: dict):
    # Support EqualCondition and InCondition
    pattern_in = r'(.*?)\sin\s(.*?)}'
    pattern_equal = r'(.*?)\s==\s(.*)'
    matchobj_equal = re.match(pattern_equal, cond_desc)
    matchobj_in = re.match(pattern_in, cond_desc)
    if matchobj_equal:
        two_elements = matchobj_equal.group(1).split('|')
        child_name = two_elements[0][4:-1]
        parent_name = two_elements[1][1:]
        target_value = matchobj_equal.group(2)[1:-1]
        cond = EqualsCondition(hp_dict[child_name], hp_dict[parent_name], target_value)
    elif matchobj_in:
        two_elements = matchobj_in.group(1).split('|')
        child_name = two_elements[0][4:-1]
        parent_name = two_elements[1][1:]
        choice_str = matchobj_in.group(2).split(',')
        choices = [choice[2:-1] for choice in choice_str]
        cond = InCondition(hp_dict[child_name], hp_dict[parent_name], choices)
    else:
        raise ValueError("Unsupported condition type in config_space!")
    return cond
Beispiel #29
0
    def test_eq(self):
        # Compare empty configuration spaces
        cs1 = ConfigurationSpace()
        cs2 = ConfigurationSpace()
        self.assertEqual(cs1, cs2)

        # Compare to something which isn't a configuration space
        self.assertTrue(not (cs1 == "ConfigurationSpace"))

        # Compare to equal configuration spaces
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        hp3 = UniformIntegerHyperparameter("friend", 0, 5)
        cond1 = EqualsCondition(hp2, hp1, 0)
        cs1.add_hyperparameter(hp1)
        cs1.add_hyperparameter(hp2)
        cs1.add_condition(cond1)
        cs2.add_hyperparameter(hp1)
        cs2.add_hyperparameter(hp2)
        cs2.add_condition(cond1)
        self.assertEqual(cs1, cs2)
        cs1.add_hyperparameter(hp3)
        self.assertFalse(cs1 == cs2)
Beispiel #30
0
    def get_hyperparameter_search_space():
        C = UniformFloatHyperparameter("C",
                                       0.03125,
                                       32768,
                                       log=True,
                                       default_value=1.0)
        # No linear kernel here, because we have liblinear
        kernel = CategoricalHyperparameter(name="kernel",
                                           choices=["rbf", "poly", "sigmoid"],
                                           default_value="rbf")
        degree = UniformIntegerHyperparameter("degree", 2, 5, default_value=3)
        gamma = UniformFloatHyperparameter("gamma",
                                           3.0517578125e-05,
                                           8,
                                           log=True,
                                           default_value=0.1)
        coef0 = UniformFloatHyperparameter("coef0", -1, 1, default_value=0)
        shrinking = CategoricalHyperparameter("shrinking", ["True", "False"],
                                              default_value="True")
        tol = UniformFloatHyperparameter("tol",
                                         1e-5,
                                         1e-1,
                                         default_value=1e-3,
                                         log=True)
        # cache size is not a hyperparameter, but an argument to the program!
        max_iter = UnParametrizedHyperparameter("max_iter", 10000)

        cs = ConfigurationSpace()
        cs.add_hyperparameters(
            [C, kernel, degree, gamma, coef0, shrinking, tol, max_iter])

        degree_depends_on_poly = EqualsCondition(degree, kernel, "poly")
        coef0_condition = InCondition(coef0, kernel, ["poly", "sigmoid"])
        cs.add_condition(degree_depends_on_poly)
        cs.add_condition(coef0_condition)

        return cs