def test_sample_categorical_dimension(self): """Test sample values for a categorical dimension""" space = Space() categories = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"] dim1 = Categorical("yolo1", categories) space.register(dim1) dim2 = Categorical("yolo2", categories, shape=(2)) space.register(dim2) tpe = TPE(space) obs_points = numpy.random.randint(0, 10, 100) obs_points = [categories[point] for point in obs_points] below_points = [obs_points[:25]] above_points = [obs_points[25:]] points = tpe.sample_one_dimension( dim1, 1, below_points, above_points, tpe._sample_categorical_point ) assert len(points) == 1 assert points[0] in categories obs_points_below = numpy.random.randint(0, 3, 25) obs_points_above = numpy.random.randint(3, 10, 75) below_points = [[categories[point] for point in obs_points_below]] above_points = [[categories[point] for point in obs_points_above]] points = tpe.sample_one_dimension( dim1, 1, below_points, above_points, tpe._sample_categorical_point ) assert len(points) == 1 assert points[0] in categories[:3] obs_points = numpy.random.randint(0, 10, 100) obs_points = [categories[point] for point in obs_points] below_points = [obs_points[:25], obs_points[25:50]] above_points = [obs_points[50:75], obs_points[75:]] points = tpe.sample_one_dimension( dim2, 2, below_points, above_points, tpe._sample_categorical_point ) assert len(points) == 2 assert points[0] in categories assert points[1] in categories tpe.n_ei_candidates = 0 points = tpe.sample_one_dimension( dim2, 2, below_points, above_points, tpe._sample_categorical_point ) assert len(points) == 0
def test_sample_int_dimension(self): """Test sample values for a integer dimension""" space = Space() dim1 = Integer("yolo1", "uniform", -10, 20) space.register(dim1) dim2 = Integer("yolo2", "uniform", -5, 10, shape=(2)) space.register(dim2) tpe = TPE(space) obs_points = numpy.random.randint(-10, 10, 100) below_points = [obs_points[:25]] above_points = [obs_points[25:]] points = tpe.sample_one_dimension( dim1, 1, below_points, above_points, tpe._sample_int_point ) points = numpy.asarray(points) assert len(points) == 1 assert all(points >= -10) assert all(points < 10) obs_points_below = numpy.random.randint(-10, 0, 25).reshape(1, 25) obs_points_above = numpy.random.randint(0, 10, 75).reshape(1, 75) points = tpe.sample_one_dimension( dim1, 1, obs_points_below, obs_points_above, tpe._sample_int_point ) points = numpy.asarray(points) assert len(points) == 1 assert all(points >= -10) assert all(points < 0) obs_points = numpy.random.randint(-5, 5, 100) below_points = [obs_points[:25], obs_points[25:50]] above_points = [obs_points[50:75], obs_points[75:]] points = tpe.sample_one_dimension( dim2, 2, below_points, above_points, tpe._sample_int_point ) points = numpy.asarray(points) assert len(points) == 2 assert all(points >= -10) assert all(points < 10) tpe.n_ei_candidates = 0 points = tpe.sample_one_dimension( dim2, 2, below_points, above_points, tpe._sample_int_point ) assert len(points) == 0