def test_build_new_GreaterThanIntCondition(self): expected = "a real [0.0, 1.0] [0.5]\n" \ "b integer [0, 10] [5]\n\n" \ "b | a > 0.5" cs = ConfigurationSpace() a = UniformFloatHyperparameter("a", 0, 1, 0.5) b = UniformIntegerHyperparameter("b", 0, 10, 5) cs.add_hyperparameter(a) cs.add_hyperparameter(b) cond = GreaterThanCondition(b, a, 0.5) cs.add_condition(cond) value = pcs_new.write(cs) self.assertEqual(expected, value) expected = "a integer [0, 10] [5]\n" \ "b integer [0, 10] [5]\n\n" \ "b | a > 5" cs = ConfigurationSpace() a = UniformIntegerHyperparameter("a", 0, 10, 5) b = UniformIntegerHyperparameter("b", 0, 10, 5) cs.add_hyperparameter(a) cs.add_hyperparameter(b) cond = GreaterThanCondition(b, a, 5) cs.add_condition(cond) value = pcs_new.write(cs) self.assertEqual(expected, value)
def _construct_gt_condition( condition: Dict, cs: ConfigurationSpace, ) -> GreaterThanCondition: return GreaterThanCondition( child=cs.get_hyperparameter(condition['child']), parent=cs.get_hyperparameter(condition['parent']), value=condition['value'], )
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 condition_specification(child_name, condition, configuration_space): # specifies the condition type child = configuration_space.get_hyperparameter(child_name) parent_name = condition[0] parent = configuration_space.get_hyperparameter(parent_name) operation = condition[1] if operation == 'in': restrictions = condition[3:-1:2] for i in range(len(restrictions)): if isinstance(parent, FloatHyperparameter): restrictions[i] = float(restrictions[i]) elif isinstance(parent, IntegerHyperparameter): restrictions[i] = int(restrictions[i]) if len(restrictions) == 1: condition = EqualsCondition(child, parent, restrictions[0]) else: condition = InCondition(child, parent, values=restrictions) return condition else: restrictions = condition[2] if isinstance(parent, FloatHyperparameter): restrictions = float(restrictions) elif isinstance(parent, IntegerHyperparameter): restrictions = int(restrictions) if operation == '==': condition = EqualsCondition(child, parent, restrictions) elif operation == '!=': condition = NotEqualsCondition(child, parent, restrictions) else: if isinstance(parent, FloatHyperparameter): restrictions = float(restrictions) elif isinstance(parent, IntegerHyperparameter): restrictions = int(restrictions) elif isinstance(parent, OrdinalHyperparameter): pass else: raise ValueError('The parent of a conditional hyperparameter ' 'must be either a float, int or ordinal ' 'hyperparameter, but is %s.' % type(parent)) if operation == '<': condition = LessThanCondition(child, parent, restrictions) elif operation == '>': condition = GreaterThanCondition(child, parent, restrictions) return condition
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])))
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])))