def test_check_in_domain_with_only_bandit_parameters(): p1 = BanditParameter("bandit1", np.array([[3, 4.0, 0], [3, 4, 0], [4, 4, 1]])) p2 = BanditParameter("bandit2", np.array([[0, 1], [1, 1], [1.0, 0]])) one_bandit_space = ParameterSpace([p1]) x_test = np.array([[3.0, 4, 0], [4.0, 3, 0]]) in_domain = one_bandit_space.check_points_in_domain(x_test) assert np.array_equal(in_domain, np.array([True, False])) two_bandits_space = ParameterSpace([p1, p2]) x_test = np.array([[3.0, 4, 0, 0, 1], [4.0, 3, 0, 0, 1], [3, 4, 0, 1, 1]]) in_domain = two_bandits_space.check_points_in_domain(x_test) assert np.array_equal(in_domain, np.array([True, False, True]))
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_bandit_parameter_str_repr(): domain = np.array([[1, 2, 3], [2, 3, 4]]) for subparam_names in [['foo', 'bar'], None]: param = BanditParameter('foobar', domain.T, subparam_names) _ = str(param) _ = repr(param)
def test_bandit_parameter(): domain = np.array([[1, 1], [1, 2], [2, 2]]) param = BanditParameter('x', domain) assert param.name == 'x' assert not param.check_in_domain(np.array([1, 3])) assert param.check_in_domain(np.array([1, 2])) assert all(param.check_in_domain(np.array([[1, 1], [1, 2]]))) assert (param.round(np.array([[1, 1.2]])) == [1, 1]).all() assert all([isinstance(sp, DiscreteParameter) for sp in param.parameters]) assert all(param.check_in_domain(param.sample_uniform(10))) with pytest.raises(ValueError): # too many columns param.check_in_domain(np.array([[1, 0, 0], [0, 2, 0]])) with pytest.raises(ValueError): # not a 1d/2d array param.check_in_domain(np.array([[[1]]])) with pytest.raises(ValueError): # wrong type param.check_in_domain((1, 1)) with pytest.raises(ValueError): # wrong type param.check_in_domain(1)