Exemplo n.º 1
0
def catg_space():
    return ParameterSpace([
        ContinuousParameter('x1', 0, 15),
        CategoricalParameter('x2', OneHotEncoding([0, 1, 2, 3])),
        CategoricalParameter('x3', OneHotEncoding([1, 2, 3, 4, 5])),
        ContinuousParameter('x4', -2, 3)
    ])
Exemplo n.º 2
0
def catg_space():
    return ParameterSpace([
        ContinuousParameter("x1", 0, 15),
        CategoricalParameter("x2", OneHotEncoding(["A", "B", "C", "D"])),
        CategoricalParameter("x3", OneHotEncoding([1, 2, 3, 4, 5])),
        ContinuousParameter("x4", -2, 3),
    ])
def test_local_search_acquisition_optimizer(simple_square_acquisition):
    space = ParameterSpace(
        [CategoricalParameter("x", OrdinalEncoding(np.arange(0, 100)))])
    optimizer = LocalSearchAcquisitionOptimizer(space, 1000, 3)

    opt_x, opt_val = optimizer.optimize(simple_square_acquisition)
    # ordinal encoding is as integers 1, 2, ...
    np.testing.assert_array_equal(opt_x, np.array([[1.0]]))
    np.testing.assert_array_equal(opt_val, np.array([[0.0]]))

    class UnknownParameter(Parameter):
        def __init__(self, name: str):
            self.name = name

        def sample_uniform(num_points):
            return np.random.randint(0, 1, (num_points, 1))

    space.parameters.append(UnknownParameter("y"))
    with pytest.raises(TypeError):
        optimizer.optimize(simple_square_acquisition)
    space.parameters.pop()

    class UnknownEncoding(Encoding):
        def __init__(self):
            super().__init__([1], [[1]])

    space.parameters.append(CategoricalParameter("y", UnknownEncoding()))
    with pytest.raises(TypeError):
        optimizer.optimize(simple_square_acquisition)
    space.parameters.pop()
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))
Exemplo n.º 5
0
def test_categorical_variables():
    np.random.seed(123)

    def objective(x):
        return np.array(np.sum(x, axis=1).reshape(-1, 1))

    carol_spirits = ['past', 'present', 'yet to come']
    encoding = OneHotEncoding(carol_spirits)
    parameter_space = ParameterSpace([
        ContinuousParameter('real_param', 0.0, 1.0),
        CategoricalParameter('categorical_param', encoding)
    ])

    random_design = RandomDesign(parameter_space)
    x_init = random_design.get_samples(10)

    assert x_init.shape == (10, 4)
    assert np.all(np.logical_or(x_init[:, 1:3] == 0.0, x_init[:, 1:3] == 1.0))

    y_init = objective(x_init)

    gpy_model = GPy.models.GPRegression(x_init, y_init)
    gpy_model.Gaussian_noise.fix(1)
    model = GPyModelWrapper(gpy_model)

    acquisition = ExpectedImprovement(model)

    loop = BayesianOptimizationLoop(parameter_space, model, acquisition)
    loop.run_loop(objective, 5)

    assert len(loop.loop_state.Y) == 15
    assert np.all(
        np.logical_or(loop.loop_state.X[:, 1:3] == 0.0,
                      loop.loop_state.X[:, 1:3] == 1.0))
def test_categorical_variables():
    np.random.seed(123)

    def objective(x):
        return np.array(np.sum(x, axis=1).reshape(-1, 1))

    carol_spirits = ["past", "present", "yet to come"]
    encoding = OneHotEncoding(carol_spirits)
    parameter_space = ParameterSpace(
        [ContinuousParameter("real_param", 0.0, 1.0), CategoricalParameter("categorical_param", encoding)]
    )

    random_design = LatinDesign(parameter_space)
    x_init = random_design.get_samples(10)

    assert x_init.shape == (10, 4)
    assert np.all(np.logical_or(x_init[:, 1:3] == 0.0, x_init[:, 1:3] == 1.0))

    y_init = objective(x_init)

    gpy_model = GPy.models.GPRegression(x_init, y_init)
    gpy_model.Gaussian_noise.fix(1)
    model = GPyModelWrapper(gpy_model)

    loop = ExperimentalDesignLoop(parameter_space, model)
    loop.run_loop(objective, 5)

    assert len(loop.loop_state.Y) == 15
    assert np.all(np.logical_or(loop.loop_state.X[:, 1:3] == 0.0, loop.loop_state.X[:, 1:3] == 1.0))
Exemplo n.º 7
0
def test_random_search_acquisition_optimizer(simple_square_acquisition):
    space = ParameterSpace([CategoricalParameter('x', OrdinalEncoding(np.arange(0, 100)))])
    optimizer = RandomSearchAcquisitionOptimizer(space, 1000)

    opt_x, opt_val = optimizer.optimize(simple_square_acquisition)
    # ordinal encoding is as integers 1, 2, ...
    assert_array_equal(opt_x, np.array([[1.]]))
    assert_array_equal(opt_val, np.array([[0.]]))
def test_categorical_parameter_check_in_domain(encoding):
    param = CategoricalParameter('v', encoding)

    assert param.check_in_domain(np.array([[1, 0, 0], [0, 0.5, 0]]))
    assert not param.check_in_domain(np.array([[2, 0, 0], [0, 0.5, 0]]))

    with pytest.raises(ValueError):  # wrong encoding dimension
        param.check_in_domain(np.array([[1, 0], [0, 0.5]]))
    with pytest.raises(ValueError):  # not a 2d array
        param.check_in_domain(np.array([1, 0, 0]))
Exemplo n.º 9
0
def test_random_search_acquisition_optimizer_with_context(simple_square_acquisition):
    space = ParameterSpace([CategoricalParameter('x', OrdinalEncoding(np.arange(0, 100))),
                            InformationSourceParameter(10)])
    optimizer = RandomSearchAcquisitionOptimizer(space, 1000)

    source_encoding = 1
    opt_x, opt_val = optimizer.optimize(simple_square_acquisition, {'source': source_encoding})
    assert_array_equal(opt_x, np.array([[1., source_encoding]]))
    assert_array_equal(opt_val, np.array([[0. + source_encoding]]))
Exemplo n.º 10
0
def test_gradient_acquisition_optimizer_categorical(simple_square_acquisition):
    space = ParameterSpace([
        ContinuousParameter("x", 0, 1),
        CategoricalParameter("y", OneHotEncoding(["A", "B"]))
    ])
    optimizer = GradientAcquisitionOptimizer(space)
    context = {"y": "B"}
    opt_x, opt_val = optimizer.optimize(simple_square_acquisition, context)
    assert_array_equal(opt_x, np.array([[0.0, 0.0, 1.0]]))
    assert_array_equal(opt_val, np.array([[2.0]]))
Exemplo n.º 11
0
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]))
Exemplo n.º 12
0
def test_gradient_acquisition_optimizer_categorical(simple_square_acquisition):
    space = ParameterSpace([
        ContinuousParameter('x', 0, 1),
        CategoricalParameter('y', OneHotEncoding(['A', 'B']))
    ])
    optimizer = GradientAcquisitionOptimizer(space)
    context = {'y': 'B'}
    opt_x, opt_val = optimizer.optimize(simple_square_acquisition, context)
    assert_array_equal(opt_x, np.array([[0., 0., 1.]]))
    assert_array_equal(opt_val, np.array([[2.]]))
Exemplo n.º 13
0
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]))
Exemplo n.º 14
0
def test_categorical_parameter_check_in_domain(encoding):
    param = CategoricalParameter('v', encoding)

    assert param.check_in_domain(np.array([[1, 0, 0], [0, 0.5, 0]]))
    assert not param.check_in_domain(np.array([[2, 0, 0], [0, 0.5, 0]]))

    with pytest.raises(ValueError):  # wrong encoding dimension
        param.check_in_domain(np.array([[1, 0], [0, 0.5]]))
    with pytest.raises(ValueError):  # not a 2d array
        param.check_in_domain(np.array([1, 0, 0]))
def test_local_search_acquisition_optimizer_with_context(
        simple_square_acquisition):
    space = ParameterSpace([
        CategoricalParameter("x", OrdinalEncoding(np.arange(0, 100))),
        InformationSourceParameter(10)
    ])
    optimizer = LocalSearchAcquisitionOptimizer(space, 1000, 3)

    source_encoding = 1
    opt_x, opt_val = optimizer.optimize(simple_square_acquisition,
                                        {"source": source_encoding})
    np.testing.assert_array_equal(opt_x, np.array([[1.0, source_encoding]]))
    np.testing.assert_array_equal(opt_val, np.array([[0.0 + source_encoding]]))
Exemplo n.º 16
0
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])
Exemplo n.º 17
0
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)]
Exemplo n.º 18
0
model = generateModel(modelOptions)


'''define parameter space'''
actionSpace = model.getActionSpace()
action = ActionSpace.Action(actionSpace)
parameterList = []
encodingList = []
groupNumber = 0
for space in actionSpace.spaces:
    if isinstance(space, spaces.MultiDiscrete):
        for index, noVariants in enumerate(space.nvec):
            options = range(noVariants)
            encoding = OneHotEncoding(options)
            encodingList.append(encoding)
            categoricalParam = CategoricalParameter('Param_%d_%d' % (groupNumber,index), encoding)
            parameterList.append(categoricalParam)
    elif isinstance(space, spaces.Box):
        if space.is_bounded():
            for index, low, high in zip(range(space.shape[0]), space.low, space.high):
                realParam = ContinuousParameter('Param_%d_%d' % (groupNumber, index), low, high)
                parameterList.append(realParam)
        else:
            for index in range(space.shape[0]):
                realParam = ContinuousParameter('Param_%d_%d' % (groupNumber, index),-5,5)#no unbounded param possible
                parameterList.append(realParam)

    else:
        raise NotImplementedError
    groupNumber +=1
space = ParameterSpace(parameterList)
Exemplo n.º 19
0
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])
Exemplo n.º 20
0
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])
Exemplo n.º 21
0
def test_categorical_parameter_rounding(encoding):
    expected = np.array([[1, 2, 4], [2, 3, 5]])
    encoding.round = mock.MagicMock(return_value=expected)
    param = CategoricalParameter('v', encoding)

    assert_array_equal(param.round(np.ones((2, 3))), expected)
Exemplo n.º 22
0
def test_categorical_parameter(encoding):
    param = CategoricalParameter('v', encoding)
    assert param.name == 'v'
    assert param.dimension == 3
    assert len(param.model_parameters) == 3
Exemplo n.º 23
0
def test_categorical_parameter_str_repr(encoding):
    param = CategoricalParameter('v', encoding)
    _ = str(param)
    _ = repr(param)
Exemplo n.º 24
0
def test_categorical_parameter_rounding(encoding):
    expected = np.array([[1, 2, 4], [2, 3, 5]])
    encoding.round = mock.MagicMock(return_value=expected)
    param = CategoricalParameter('v', encoding)

    assert_array_equal(param.round(np.ones((2, 3))), expected)