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_) 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_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 _construct_or_condition( condition: Dict, cs: ConfigurationSpace, ) -> OrConjunction: conditions = [ _construct_condition(cond, cs) for cond in condition['conditions'] ] return OrConjunction(*conditions)
def get_complete_configspace(): """Creates a configspace that includes all kinds of parameters with complicated values. The idea is to provide a configspace that can be used to check modules using ConfigSpace as a dependency to check compatibility with e.g. Constants, log-scale, etc. Returns ------- cs: ConfigurationSpace cs containing all kinds of parameters """ cs = ConfigurationSpace() hp = {} # Add Constants for all allowed types ('int', 'float', 'string') hp['alpha'] = Constant("alpha", 0.0001) hp['tol'] = Constant("tol", '0.0001') hp['verbose'] = Constant("verbose", 1) # Add numericals # Add Floats hp['beta1'] = UniformFloatHyperparameter("beta1", 0.85, 0.95, log=False) hp['power_t'] = NormalFloatHyperparameter("power_t", mu=0.5, sigma=0.1, log=False) # Add Ints hp['momentum'] = UniformIntegerHyperparameter("momentum", 0, 100, False) hp['beta2'] = NormalIntegerHyperparameter("beta2", mu=1, sigma=0.001, log=False) # Add Floats (log) hp['learning_rate_init'] = UniformFloatHyperparameter("learning_rate_init", 0.0001, 0.1, log=True) hp['random1'] = NormalFloatHyperparameter("NormalFloat", mu=0, sigma=1, default_value=1, log=True) # Add Ints (log) hp['random2'] = UniformIntegerHyperparameter("UniformInt", 2, 100, log=True) hp['random3'] = NormalIntegerHyperparameter("NormalInt", mu=0, sigma=1, default_value=1, log=True) # Add Categorical for allowed types hp['activation'] = CategoricalHyperparameter('activation', choices=['identity', 'logistic', 'tanh', 'relu']) hp['solver'] = CategoricalHyperparameter('solver', choices=[-2, 0, 2]) # corrresponds to: ‘lbfgs’, ‘sgd’, ‘adam’ hp['batch_size_auto'] = CategoricalHyperparameter('batch_size_auto', choices=[True, False]) hp['learning_rate'] = CategoricalHyperparameter('learning_rate', choices=[-0.5, 0.0, 0.5]) # corresponds to {‘constant’, ‘invscaling’, ‘adaptive’} # Add Ordinal hp['batch_size'] = OrdinalHyperparameter('batch_size', sequence=[32, 64.0, '128']) for k, v in hp.items(): cs.add_hyperparameter(v) # learning_rate only with sgd c = InCondition(hp['learning_rate'], hp['solver'], [0]) c = EqualsCondition(hp['momentum'], hp['solver'], 0) # learning_rate_init only with sgd or adam cs.add_condition(OrConjunction(EqualsCondition(hp['learning_rate'], hp['solver'], 0), # sgd EqualsCondition(hp['learning_rate'], hp['solver'], 2))) # adam # batch_size only with not batch_size_auto cs.add_condition(NotEqualsCondition(hp['batch_size'], hp['batch_size_auto'], True)) # complicated way for solver == sgd #cs.add_condition(AndConjunction(LessThanCondition(hp['power_t'], hp['solver'], 1), # GreaterThanCondition(hp['power_t'], hp['solver'], -1))) # betas with adam cs.add_condition(EqualsCondition(hp['beta1'], hp['solver'], 2)) cs.add_condition(EqualsCondition(hp['beta2'], hp['solver'], 2)) return cs
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 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)
def read(pcs_string, debug=False): """ Reads in a :py:class:`~ConfigSpace.configuration_space.ConfigurationSpace` definition from a pcs file. Example ------- >>> from ConfigSpace.read_and_write import pcs_new >>> with open('configspace.pcs', 'r') as fh: >>> restored_conf = pcs_new.read(fh) Parameters ---------- pcs_string : str ConfigSpace definition in pcs format debug : bool Provides debug information. Defaults to False. Returns ------- :py:class:`~ConfigSpace.configuration_space.ConfigurationSpace` The restored ConfigurationSpace object """ 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 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]] default_value = param_list[-2] 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]] default_value = param_list[-2] 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 read(pcs_string, debug=False): """ Read in a :py:class:`~ConfigSpace.configuration_space.ConfigurationSpace` definition from a pcs file. Example ------- .. testsetup:: pcs_new_test from ConfigSpace import ConfigurationSpace import ConfigSpace.hyperparameters as CSH from ConfigSpace.read_and_write import pcs_new cs = ConfigurationSpace() cs.add_hyperparameter(CSH.CategoricalHyperparameter('a', choices=[1, 2, 3])) with open('configspace.pcs_new', 'w') as f: f.write(pcs_new.write(cs)) .. doctest:: pcs_new_test >>> from ConfigSpace.read_and_write import pcs_new >>> with open('configspace.pcs_new', 'r') as fh: ... deserialized_conf = pcs_new.read(fh) Parameters ---------- pcs_string : str ConfigSpace definition in pcs format debug : bool Provides debug information. Defaults to False. Returns ------- :py:class:`~ConfigSpace.configuration_space.ConfigurationSpace` The deserialized ConfigurationSpace object """ 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 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]] default_value = param_list[-2] 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]] default_value = param_list[-2] 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] == '=': hp = configuration_space.get_hyperparameter(tmp_list[0]) if isinstance(hp, NumericalHyperparameter): if isinstance(hp, IntegerHyperparameter): forbidden_value = int(tmp_list[2]) elif isinstance(hp, FloatHyperparameter): forbidden_value = float(tmp_list[2]) else: raise NotImplementedError if forbidden_value < hp.lower or forbidden_value > hp.upper: raise ValueError( f'forbidden_value is set out of the bound, it needs to' f' be set between [{hp.lower}, {hp.upper}]' f' but its value is {forbidden_value}') elif isinstance( hp, (CategoricalHyperparameter, OrdinalHyperparameter)): hp_values = hp.choices if isinstance(hp, CategoricalHyperparameter)\ else hp.sequence forbidden_value_in_hp_values = tmp_list[2] in hp_values if forbidden_value_in_hp_values: forbidden_value = tmp_list[2] else: raise ValueError( f'forbidden_value is set out of the allowed value ' f'sets, it needs to be one member from {hp_values} ' f'but its value is {forbidden_value}') else: raise ValueError('Unsupported Hyperparamter sorts') clause_list.append( ForbiddenEqualsClause( configuration_space.get_hyperparameter( tmp_list[0]), forbidden_value)) 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