Esempio 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)
    ])
Esempio 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),
    ])
Esempio n. 3
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))
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))
Esempio n. 6
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]))
Esempio n. 7
0
def test_one_hot_encoding(categories):
    encoding = OneHotEncoding(categories)

    assert encoding.dimension == 3
    assert encoding.get_category([1, 0, 0]) == 'one'
    assert encoding.get_encoding('three') == [0, 0, 1]

    with pytest.raises(ValueError):
        encoding.get_category([1, 1, 0])

    with pytest.raises(ValueError):
        encoding.get_encoding("four")
Esempio n. 8
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]]))
Esempio n. 9
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.]]))
Esempio n. 10
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]))
Esempio n. 11
0
def test_one_hot_encoding_rounding(categories):
    encoding = OneHotEncoding(categories)

    check_rounding(encoding, np.array([[1, 0, 0]]), np.array([[1, 0, 0]]))
    check_rounding(encoding, np.array([[0.5, 0.1, 0.1]]), np.array([[1, 0, 0]]))
    check_rounding(encoding, np.array([[2, 0, 1.5]]), np.array([[1, 0, 0]]))
    check_rounding(encoding, np.array([[0.7, 0.75, 0.5]]), np.array([[0, 1, 0]]))

    with pytest.raises(ValueError):
        encoding.round(np.array([1, 0, 0]))

    with pytest.raises(ValueError):
        encoding.round(np.array([[1, 0]]))
Esempio n. 12
0
def encoding():
    # different types of volcanoes
    return OneHotEncoding(["strato", "shield", "dome"])
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)]
Esempio n. 14
0
def encoding():
    # different types of volcanoes
    return OneHotEncoding(['strato', 'shield', 'dome'])
Esempio n. 15
0
modelOptions.withScheduleCompression=True
#modelOptions.interface = "VAE"
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
Esempio n. 16
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])
Esempio n. 17
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])