def test_local_search_acquisition_optimizer_neighbours(): np.random.seed(0) space = ParameterSpace([ CategoricalParameter('a', OneHotEncoding([1, 2, 3])), CategoricalParameter('b', OrdinalEncoding([0.1, 1, 2])), CategoricalParameter('c', OrdinalEncoding([0.1, 1, 2])), DiscreteParameter('d', [0.1, 1.2, 2.3]), ContinuousParameter('e', 0, 100), DiscreteParameter('no_neighbours', [1]), DiscreteParameter('f', [0.1, 1.2, 2.3]), ]) x = np.array([1, 0, 0, 1.6, 2.9, 0.1, 50, 1.2, 1.]) optimizer = LocalSearchAcquisitionOptimizer(space, 1000, 3, num_continuous=1) neighbourhood = optimizer._neighbours_per_parameter(x, space.parameters) assert_equal(np.array([[0, 1, 0], [0, 0, 1]]), neighbourhood[0]) assert_equal(np.array([[1], [3]]), neighbourhood[1]) assert_equal(np.array([[2]]), neighbourhood[2]) assert_equal(np.array([[1.2]]), neighbourhood[3]) assert_almost_equal(np.array([[53.5281047]]), neighbourhood[4]) assert_equal(np.empty((0, 1)), neighbourhood[5]) assert_equal(np.array([[0.1], [2.3]]), neighbourhood[6]) neighbours = optimizer._neighbours(x, space.parameters) assert_almost_equal(np.array([ [0, 1, 0, 2., 3., 0.1, 50., 1., 1.2], [0, 0, 1, 2., 3., 0.1, 50., 1., 1.2], [1, 0, 0, 1., 3., 0.1, 50., 1., 1.2], [1, 0, 0, 3., 3., 0.1, 50., 1., 1.2], [1, 0, 0, 2., 2., 0.1, 50., 1., 1.2], [1, 0, 0, 2., 3., 1.2, 50., 1., 1.2], [1, 0, 0, 2., 3., 0.1, 50.80031442, 1., 1.2], [1, 0, 0, 2., 3., 0.1, 50., 1., 0.1], [1, 0, 0, 2., 3., 0.1, 50., 1., 2.3], ]), space.round(neighbours))
def test_check_in_domain_with_bandit_parameter(): mixed_space_with_bandit = ParameterSpace([ ContinuousParameter("c", 1.0, 5.0), DiscreteParameter("d", [0, 1, 2]), CategoricalParameter("cat", OneHotEncoding(["blue", "red"])), BanditParameter("bandit", np.array([[0, 1], [1, 1], [1.0, 0]])), ]) x_test = np.array([[1.5, 0, 1.0, 0.0, 0, 1], [1.5, 0, 1.0, 0.0, 0.0, 0.0]]) in_domain = mixed_space_with_bandit.check_points_in_domain(x_test) assert np.array_equal(in_domain, np.array([True, False]))
def test_check_in_domain_with_bandit_parameter(): mixed_space_with_bandit = ParameterSpace([ ContinuousParameter('c', 1.0, 5.0), DiscreteParameter('d', [0, 1, 2]), CategoricalParameter('cat', OneHotEncoding(['blue', 'red'])), BanditParameter('bandit', np.array([[0, 1], [1, 1], [1., 0]])) ]) x_test = np.array([[1.5, 0, 1., 0., 0, 1], [1.5, 0, 1., 0., 0., 0.]]) in_domain = mixed_space_with_bandit.check_points_in_domain(x_test) assert np.array_equal(in_domain, np.array([True, False]))
def test_design_with_mixed_domain(encoding): p1 = ContinuousParameter("p1", 1.0, 5.0) p2 = CategoricalParameter("p2", encoding) p3 = DiscreteParameter("p3", [1, 2, 5, 6]) space = ParameterSpace([p1, p2, p3]) points_count = 5 designs = create_model_free_designs(space) for design in designs: points = design.get_samples(points_count) assert points_count == len(points) # columns count is 1 for continuous plus 1 for discrete plus number of categories columns_count = 1 + 1 + len(encoding.categories) assert all([len(p) == columns_count for p in points])
def test_get_bounds(): p1 = ContinuousParameter('c', 1.0, 5.0) p2 = DiscreteParameter('d', [1, 2, 3]) p3 = CategoricalParameter('cat', OneHotEncoding(['Maine Coon', 'Siamese'])) space = ParameterSpace([p1, p2, p3]) assert space.get_bounds() == [(1., 5.), (1., 3.), (0, 1), (0, 1)]
def test_single_value_in_domain_discrete_parameter(): param = DiscreteParameter('x', [0, 1, 2]) assert param.check_in_domain(0) is True assert param.check_in_domain(3) is False
def test_discrete_parameter_str_repr(): param = DiscreteParameter('x', [0, 1, 2]) _ = str(param) _ = repr(param)
def test_discrete_parameter(): param = DiscreteParameter('x', [0, 1, 2]) assert param.name == 'x' assert param.check_in_domain(np.array([0, 1])) is True assert param.check_in_domain(np.array([3])) is False assert param.check_in_domain(np.array([[1], [0]])) is True assert param.check_in_domain([1, 0]) is True assert param.check_in_domain(1) is True assert param.check_in_domain(0.5) is False with pytest.raises(ValueError): # too many columns param.check_in_domain(np.array([[1, 0], [0, 2]])) with pytest.raises(ValueError): # not a 1d/2d array param.check_in_domain(np.array([[[1]]]))
# Parameter Space Options __C.PARAMETERS_OPTS = edict() __C.PARAMETERS_OPTS.GRID_SIZE = [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ] __C.PARAMETERS_OPTS.EDGE_MAX_SPEED = [8, 25] # 28.8 km/h to 90 km/h __C.PARAMETERS_OPTS.MAX_SPEED = [5, 27] # 18 km/h to 144 km/h __C.PARAMETERS_OPTS.EDGE_LENGTH = [30, 70] # 30 meters to 150 meters __C.PARAMETERS_OPTS.NUM_LANES = [1, 2, 3] __C.PARAMETERS_OPTS.ACCEL = [1.5, 5] # 1.5 m/s^2 to 5 m/s^2 # Parameters __C.PARAMETERS = edict() __C.PARAMETERS.GRID_SIZE = DiscreteParameter( 'gridSize', domain=__C.PARAMETERS_OPTS.GRID_SIZE) __C.PARAMETERS.EDGE_MAX_SPEED = ContinuousParameter( 'edgeMaxSpeed', min_value=min(__C.PARAMETERS_OPTS.EDGE_MAX_SPEED), max_value=max(__C.PARAMETERS_OPTS.EDGE_MAX_SPEED)) __C.PARAMETERS.MAX_SPEED = ContinuousParameter( 'maxSpeed', min_value=min(__C.PARAMETERS_OPTS.MAX_SPEED), max_value=max(__C.PARAMETERS_OPTS.MAX_SPEED)) __C.PARAMETERS.EDGE_LENGTH = ContinuousParameter( 'edgeLength', min_value=min(__C.PARAMETERS_OPTS.EDGE_LENGTH), max_value=max(__C.PARAMETERS_OPTS.EDGE_LENGTH)) __C.PARAMETERS.NUM_LANES = DiscreteParameter( 'numberOfLanes', domain=__C.PARAMETERS_OPTS.NUM_LANES) __C.PARAMETERS.ACCEL = ContinuousParameter(
def get_parameter(name: str, mode: str, idx: int): if mode == 'locked': return DiscreteParameter(name, domain=[df.iloc[0][idx]]) else: return __C['PARAMETERS'][name]
def test_single_value_in_domain_discrete_parameter(): param = DiscreteParameter('x', [0, 1, 2]) assert param.check_in_domain(0) is True assert param.check_in_domain(3) is False
def test_discrete_parameter(): param = DiscreteParameter('x', [0, 1, 2]) assert param.name == 'x' assert param.check_in_domain(np.array([0, 1])) is True assert param.check_in_domain(np.array([3])) is False assert param.check_in_domain(np.array([[1], [0]])) is True assert param.check_in_domain([1, 0]) is True assert param.check_in_domain(1) is True assert param.check_in_domain(0.5) is False with pytest.raises(ValueError): # too many columns param.check_in_domain(np.array([[1, 0], [0, 2]])) with pytest.raises(ValueError): # not a 1d/2d array param.check_in_domain(np.array([[[1]]]))
# configs which survive to the next generation (and sorted) T = [ T[i] for i in np.argsort(val_losses)[0:int( n_i/eta )]] # the loss of the incumbent is simply the min of val_losses. incumbent_loss = np.min(val_losses) # return the first element of the survived config (which is the incumbent) and its loss return T[0], incumbent_loss if __name__ == '__main__': # define optimized hyperparameters list_params = [] list_params.append(ContinuousParameter("brightnessFactor", 0.6,1)) list_params.append(ContinuousParameter("colorFactor", 0, 1)) list_params.append(ContinuousParameter("sharpnessFactor", 0, 2)) list_params.append(DiscreteParameter("gaussianFactor", [0, 5])) list_params.append(ContinuousParameter("saltPepperFactor", 0,0.05)) list_params.append(DiscreteParameter("cutoutWidth", [0, 20])) list_params.append(DiscreteParameter("cutoutHeight", [0, 20])) list_params.append(DiscreteParameter("cutoutAreas", [0, 10])) # list_params.append(ContinuousParameter("minCrop", 0, 1)) # list_params.append(ContinuousParameter("maxCrop", 0, 1)) # list_params.append(ContinuousParameter("stayFactor", 0, 1)) list_params.append(DiscreteParameter("cropLeft", [0, 300])) list_params.append(DiscreteParameter("cropTop", [0, 300])) list_params.append(DiscreteParameter("cropRight", [0, 300])) list_params.append(DiscreteParameter("cropBottom", [0, 300])) # list_params.append(DiscreteParameter("rotateFactor", [0, 360])) list_params.append(DiscreteParameter("rotateFactor", [0, 25])) list_params.append(ContinuousParameter("scaleFactor", 0, 1))
def space_3d_mixed(): p1 = ContinuousParameter("c", 1.0, 5.0) p2 = DiscreteParameter("d", [1, 2, 3]) p3 = CategoricalParameter("cat", OneHotEncoding(["Maine Coon", "Siamese"])) return ParameterSpace([p1, p2, p3])
def space_3d_mixed(): p1 = ContinuousParameter('c', 1.0, 5.0) p2 = DiscreteParameter('d', [1, 2, 3]) p3 = CategoricalParameter('cat', OneHotEncoding(['Maine Coon', 'Siamese'])) return ParameterSpace([p1, p2, p3])
def test_discrete_parameter(): param = DiscreteParameter('x', [0, 1, 2]) assert param.name == 'x' assert param.check_in_domain(np.array([0, 1])) is True assert param.check_in_domain(np.array([3])) is False