def _construct_or_condition( condition: Dict, cs: ConfigurationSpace, ) -> OrConjunction: conditions = [ _construct_condition(cond, cs) for cond in condition['conditions'] ] return OrConjunction(*conditions)
def _build_or_conjunction(conjunction: OrConjunction) -> Dict: child = conjunction.get_descendant_literal_conditions()[0].child.name cond_list = list() for component in conjunction.components: cond_list.append(_build_condition(component)) return { 'child': child, 'type': 'OR', 'conditions': cond_list, }
def test_or_conjunction(self): self.assertRaises(TypeError, AndConjunction, "String1", "String2") hp1 = CategoricalHyperparameter("input1", [0, 1]) hp2 = CategoricalHyperparameter("input2", [0, 1]) hp3 = CategoricalHyperparameter("input3", [0, 1]) hp4 = Constant("Or", "True") cond1 = EqualsCondition(hp4, hp1, 1) self.assertRaises(ValueError, OrConjunction, cond1) cond2 = EqualsCondition(hp4, hp2, 1) cond3 = EqualsCondition(hp4, hp3, 1) andconj1 = OrConjunction(cond1, cond2) andconj1_ = OrConjunction(cond1, cond2) self.assertEqual(andconj1, andconj1_) # Test setting vector idx hyperparameter_idx = { hp1.name: 0, hp2.name: 1, hp3.name: 2, hp4.name: 3 } andconj1.set_vector_idx(hyperparameter_idx) self.assertEqual(andconj1.get_parents_vector(), [0, 1]) self.assertEqual(andconj1.get_children_vector(), [3, 3]) andconj2 = OrConjunction(cond2, cond3) self.assertNotEqual(andconj1, andconj2) andconj3 = OrConjunction(cond1, cond2, cond3) self.assertEqual( "(Or | input1 == 1 || Or | input2 == 1 || Or | " "input3 == 1)", str(andconj3))
def test_write_OrConjunction_condition(self): import numpy as np expected = "lp '--lp ' c {mi,bo}\ntemp '--temp ' r (2.000000, 5.000000)\nls '--ls ' c {sa,ca,ny}| temp==3.0 || lp %in% c(bo)\n" temp = UniformFloatHyperparameter("temp", np.exp(2), np.exp(5), log=True) ls = CategoricalHyperparameter("ls", ["sa", "ca", "ny"], "sa") lp = CategoricalHyperparameter("lp", ["mi", "bo"], "bo") cs = ConfigurationSpace() cs.add_hyperparameter(temp) cs.add_hyperparameter(lp) cs.add_hyperparameter(ls) c1 = EqualsCondition(ls, temp, np.exp(3)) c2 = InCondition(ls, lp, ['bo']) c3 = OrConjunction(c1, c2) cs.add_condition(c3) value = irace.write(cs) self.assertEqual(expected, value)
def test_nested_conjunctions(self): 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") cond1 = EqualsCondition(hp6, hp1, 1) cond2 = EqualsCondition(hp6, hp2, 1) cond3 = EqualsCondition(hp6, hp3, 1) cond4 = EqualsCondition(hp6, hp4, 1) cond5 = EqualsCondition(hp6, hp5, 1) conj1 = AndConjunction(cond1, cond2) conj2 = OrConjunction(conj1, cond3) conj3 = AndConjunction(conj2, cond4, cond5) # TODO: this does not look nice, And should depend on a large # conjunction, there should not be many ANDs inside this string! self.assertEqual( "(((AND | input1 == 1 && AND | input2 == 1) || AND | " "input3 == 1) && AND | input4 == 1 && AND | input5 " "== 1)", str(conj3))
def read(pcs_string, debug=False): configuration_space = ConfigurationSpace() conditions = [] forbidden = [] # some statistics ct = 0 cont_ct = 0 cat_ct = 0 ord_ct = 0 line_ct = 0 for line in pcs_string: line_ct += 1 if "#" in line: # It contains a comment pos = line.find("#") line = line[:pos] # Remove quotes and whitespaces at beginning and end ori_line = line line = line.replace('"', "").replace("'", "") line = line.strip() if "|" in line: # It's a condition try: c = pp_condition.parseString(line) conditions.append(c) except pyparsing.ParseException: raise NotImplementedError("Could not parse condition: %s" % line) continue if "}" not in line and "]" not in line: continue if line.startswith("{") and line.endswith("}"): forbidden.append(line) continue if len(line.strip()) == 0: continue ct += 1 param = None create = {"int": UniformIntegerHyperparameter, "float": UniformFloatHyperparameter, "categorical": CategoricalHyperparameter, "ordinal": OrdinalHyperparameter } try: param_list = pp_cont_param.parseString(line) name = param_list[0] if param_list[1] == 'integer': paramtype = 'int' elif param_list[1] == 'real': paramtype = 'float' else: paramtype = None if paramtype in ['int', 'float']: log = param_list[10:] param_list = param_list[:10] if len(log) > 0: log = log[0] lower = float(param_list[3]) upper = float(param_list[5]) log_on = True if "log" in log else False default_value = float(param_list[8]) param = create[paramtype](name=name, lower=lower, upper=upper, q=None, log=log_on, default_value=default_value) cont_ct += 1 except pyparsing.ParseException: pass try: if "categorical" in line: param_list = pp_cat_param.parseString(line) name = param_list[0] #choices = [choice for choice in param_list[3:-4:2]] ori_line_segs = ori_line.split('{') choices_str = '[' + ori_line_segs[1].split('}')[0] + ']' choices = json.loads(choices_str) #default_value = param_list[-2] ori_line_segs = ori_line.split('[') default_str = ori_line_segs[-1].split(']')[0] default_value = json.loads(default_str) param = create["categorical"](name=name, choices=choices, default_value=default_value) cat_ct += 1 elif "ordinal" in line: param_list = pp_ord_param.parseString(line) name = param_list[0] #sequence = [seq for seq in param_list[3:-4:2]] ori_line_segs = ori_line.split('{') sequence_str = '[' + ori_line_segs[1].split('}')[0] + ']' sequence = json.loads(sequence_str) #default_value = param_list[-2] ori_line_segs = ori_line.split('[') default_str = ori_line_segs[-1].split(']')[0] default_value = json.loads(default_str) param = create["ordinal"](name=name, sequence=sequence, default_value=default_value) ord_ct += 1 except pyparsing.ParseException: pass if param is None: raise NotImplementedError("Could not parse: %s" % line) configuration_space.add_hyperparameter(param) for clause in forbidden: param_list = pp_forbidden_clause.parseString(clause) tmp_list = [] clause_list = [] for value in param_list[1:]: if len(tmp_list) < 3: tmp_list.append(value) else: # So far, only equals is supported by SMAC if tmp_list[1] == '=': # TODO maybe add a check if the hyperparameter is # actually in the configuration space clause_list.append(ForbiddenEqualsClause( configuration_space.get_hyperparameter(tmp_list[0]), tmp_list[2])) else: raise NotImplementedError() tmp_list = [] configuration_space.add_forbidden_clause(ForbiddenAndConjunction( *clause_list)) conditions_per_child = OrderedDict() for condition in conditions: child_name = condition[0] if child_name not in conditions_per_child: conditions_per_child[child_name] = list() conditions_per_child[child_name].append(condition) for child_name in conditions_per_child: for condition in conditions_per_child[child_name]: condition = condition[2:] condition = ' '.join(condition) if '||' in str(condition): ors = [] # 1st case we have a mixture of || and && if '&&' in str(condition): ors_combis = [] for cond_parts in str(condition).split('||'): condition = str(cond_parts).split('&&') # if length is 1 it must be or if len(condition) == 1: element_list = condition[0].split() ors_combis.append(condition_specification(child_name, element_list, configuration_space)) else: # now taking care of ands ands = [] for and_part in condition: element_list = [element for part in condition for element in and_part.split()] ands.append(condition_specification(child_name, element_list, configuration_space)) ors_combis.append(AndConjunction(*ands)) mixed_conjunction = OrConjunction(*ors_combis) configuration_space.add_condition(mixed_conjunction) else: # 2nd case: we only have ors for cond_parts in str(condition).split('||'): element_list = [element for element in cond_parts.split()] ors.append(condition_specification(child_name, element_list, configuration_space)) or_conjunction = OrConjunction(*ors) configuration_space.add_condition(or_conjunction) else: # 3rd case: we only have ands if '&&' in str(condition): ands = [] for cond_parts in str(condition).split('&&'): element_list = [element for element in cond_parts.split()] ands.append(condition_specification(child_name, element_list, configuration_space)) and_conjunction = AndConjunction(*ands) configuration_space.add_condition(and_conjunction) else: # 4th case: we have a normal condition element_list = [element for element in condition.split()] normal_condition = condition_specification(child_name, element_list, configuration_space) configuration_space.add_condition(normal_condition) return configuration_space
def test_read_new_configuration_space_complex_conditionals(self): classi = OrdinalHyperparameter("classi", [ "random_forest", "extra_trees", "k_nearest_neighbors", "something" ]) knn_weights = CategoricalHyperparameter("knn_weights", ["uniform", "distance"]) weather = OrdinalHyperparameter( "weather", ["sunny", "rainy", "cloudy", "snowing"]) temperature = CategoricalHyperparameter("temperature", ["high", "low"]) rain = CategoricalHyperparameter("rain", ["yes", "no"]) gloves = OrdinalHyperparameter("gloves", ["none", "yarn", "leather", "gortex"]) heur1 = CategoricalHyperparameter("heur1", ["off", "on"]) heur2 = CategoricalHyperparameter("heur2", ["off", "on"]) heur_order = CategoricalHyperparameter("heur_order", ["heur1then2", "heur2then1"]) gloves_condition = OrConjunction( EqualsCondition(gloves, rain, "yes"), EqualsCondition(gloves, temperature, "low")) heur_condition = AndConjunction( EqualsCondition(heur_order, heur1, "on"), EqualsCondition(heur_order, heur2, "on")) and_conjunction = AndConjunction( NotEqualsCondition(knn_weights, classi, "extra_trees"), EqualsCondition(knn_weights, classi, "random_forest")) Cl_condition = OrConjunction( EqualsCondition(knn_weights, classi, "k_nearest_neighbors"), and_conjunction, EqualsCondition(knn_weights, classi, "something")) and1 = AndConjunction(EqualsCondition(temperature, weather, "rainy"), EqualsCondition(temperature, weather, "cloudy")) and2 = AndConjunction( EqualsCondition(temperature, weather, "sunny"), NotEqualsCondition(temperature, weather, "snowing")) another_condition = OrConjunction(and1, and2) complex_conditional_space = ConfigurationSpace() complex_conditional_space.add_hyperparameter(classi) complex_conditional_space.add_hyperparameter(knn_weights) complex_conditional_space.add_hyperparameter(weather) complex_conditional_space.add_hyperparameter(temperature) complex_conditional_space.add_hyperparameter(rain) complex_conditional_space.add_hyperparameter(gloves) complex_conditional_space.add_hyperparameter(heur1) complex_conditional_space.add_hyperparameter(heur2) complex_conditional_space.add_hyperparameter(heur_order) complex_conditional_space.add_condition(gloves_condition) complex_conditional_space.add_condition(heur_condition) complex_conditional_space.add_condition(Cl_condition) complex_conditional_space.add_condition(another_condition) complex_cs = list() complex_cs.append( "classi ordinal {random_forest,extra_trees,k_nearest_neighbors, something} [random_forest]" ) complex_cs.append( "knn_weights categorical {uniform, distance} [uniform]") complex_cs.append( "weather ordinal {sunny, rainy, cloudy, snowing} [sunny]") complex_cs.append("temperature categorical {high, low} [high]") complex_cs.append("rain categorical { yes, no } [yes]") complex_cs.append( "gloves ordinal { none, yarn, leather, gortex } [none]") complex_cs.append("heur1 categorical { off, on } [off]") complex_cs.append("heur2 categorical { off, on } [off]") complex_cs.append( "heur_order categorical { heur1then2, heur2then1 } [heur1then2]") complex_cs.append("gloves | rain == yes || temperature == low") complex_cs.append("heur_order | heur1 == on && heur2 == on") complex_cs.append( "knn_weights | classi == k_nearest_neighbors || classi != extra_trees && classi == random_forest || classi == something" ) complex_cs.append( "temperature | weather == rainy && weather == cloudy || weather == sunny && weather != snowing" ) cs_new = pcs_new.read(complex_cs) self.assertEqual(cs_new, complex_conditional_space)