Ejemplo n.º 1
0
class TestOptimizerCreation(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestOptimizerCreation, self).__init__(*args, **kwargs)

        self.space = [
            {'name': 'var_1', 'type': 'continuous', 'domain': (-1, 1), 'dimensionality': 1},
            {'name': 'var_2', 'type': 'continuous', 'domain': (-1, 1), 'dimensionality': 1}
        ]
        self.design_space = Design_space(self.space)
        self.f = lambda x: np.sum(np.sin(x))

    def test_invalid_optimizer_name_raises_error(self):
        self.assertRaises(InvalidVariableNameError,
                          choose_optimizer, 'asd', None)

    def test_create_lbfgs_optimizer(self):
        optimizer = choose_optimizer('lbfgs', self.design_space.get_bounds())

        self.assertIsNotNone(optimizer)

    def test_create_direct_optimizer(self):
        optimizer = choose_optimizer('DIRECT', self.design_space.get_bounds())

        self.assertIsNotNone(optimizer)

    def test_create_cma_optimizer(self):
        optimizer = choose_optimizer('CMA', self.design_space.get_bounds())

        self.assertIsNotNone(optimizer)
Ejemplo n.º 2
0
    def __init__(self, *args, **kwargs):
        super(TestOptimizerCreation, self).__init__(*args, **kwargs)

        self.space = [
            {'name': 'var_1', 'type': 'continuous', 'domain': (-1, 1), 'dimensionality': 1},
            {'name': 'var_2', 'type': 'continuous', 'domain': (-1, 1), 'dimensionality': 1}
        ]
        self.design_space = Design_space(self.space)
        self.f = lambda x: np.sum(np.sin(x))
Ejemplo n.º 3
0
    def test_objective_anchor_points_with_duplicate(self):

        space = [{
            'name': 'var_1',
            'type': 'discrete',
            'domain': (-1, 2)
        }, {
            'name': 'var_2',
            'type': 'discrete',
            'domain': (0, 1)
        }, {
            'name': 'var_3',
            'type': 'categorical',
            'domain': (0, 1)
        }]

        design_space = Design_space(space)

        np.random.seed(666)

        design_type = "random"

        dummy_objective = lambda X: np.sum(X * X, axis=1)

        generator = ObjectiveAnchorPointsGenerator(design_space,
                                                   design_type,
                                                   dummy_objective,
                                                   num_samples=1000)

        initial_points = np.array([[-1, 1, 0], [-1, 1, 1]])

        duplicate_manager = DuplicateManager(design_space, initial_points)

        # There is a total of 2x2x2=8 possible configurations, minus the 2 defined in initial_points
        solution = np.array([[-1., 0., 1., 0.], [-1., 0., 0., 1.],
                             [2., 0., 1., 0.], [2., 0., 0., 1.],
                             [2., 1., 1., 0.], [2., 1., 0., 1.]])
        anchor_points = generator.get(num_anchor=6,
                                      duplicate_manager=duplicate_manager,
                                      unique=True)
        self.assertTrue(np.all(anchor_points == solution))

        all_points = np.vstack(
            (initial_points, design_space.zip_inputs(solution)))
        duplicate_manager_with_all_points = DuplicateManager(
            design_space, all_points)

        # There aren't any more candidates to generate, hence the exception
        self.assertRaises(
            Exception, lambda: generator.get(num_anchor=1,
                                             duplicate_manager=
                                             duplicate_manager_with_all_points,
                                             unique=True))
Ejemplo n.º 4
0
    def setUp(self):
        kwargs = {
            'n_samples': 1000,
            'n_burnin': 100,
            'subsample_interval': 5,
            'step_size': 1e-1,
            'leapfrog_steps': 20,
            'optimize_restarts': 10,
            'num_inducing': 15,
            'acquisition_transformation': 'softplus',
            'acquisition_jitter': 0.02,
            'acquisition_weight': 2.5,
            'acquisition_transformation': 'softplus'
        }

        ## --- Defaults for some of the tests
        self.space = Design_space(space=[{
            'name': 'var1',
            'type': 'continuous',
            'domain': (-10, 10),
            'dimensionality': 2
        }])
        self.cost = CostModel(None)
        self.arguments_manager = ArgumentsManager(kwargs)
        self.model = self.arguments_manager.model_creator(model_type='GP',
                                                          exact_feval=True,
                                                          space=self.space)
        self.acquisition_optimizer = AcquisitionOptimizer(self.space)
        self.acquisition = self.arguments_manager.acquisition_creator(
            'EI', self.model, self.space, self.acquisition_optimizer,
            self.cost)
Ejemplo n.º 5
0
    def setUp(self):
        self.mock_model = Mock()
        self.mock_optimizer = Mock()
        domain = [{'name': 'var_1', 'type': 'continuous', 'domain': (-5,5), 'dimensionality': 2}]
        self.space = Design_space(domain, None)

        self.mpi_acquisition = AcquisitionMPI(self.mock_model, self.space, self.mock_optimizer)
Ejemplo n.º 6
0
    def test_rand_anchor_points_without_duplicate(self):

        space = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }, {
            'name': 'var_2',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_3',
            'type': 'categorical',
            'domain': (0, 1)
        }]

        design_space = Design_space(space)

        np.random.seed(666)

        design_type = "random"

        generator = RandomAnchorPointsGenerator(design_space,
                                                design_type,
                                                num_samples=10)

        scores = generator.get_anchor_point_scores(np.arange(3).reshape(3, 1))

        assert np.linalg.norm(scores - np.array([0., 1., 2.])) < tolerance

        assert np.linalg.norm(
            generator.get(num_anchor=2) -
            np.array([[-2.19900984, 0., 0., 1.], [-0.02338332, 2., 1., 0.]])
        ) < tolerance
Ejemplo n.º 7
0
    def test_duplicate_with_ignored_and_pending(self):
        space = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }, {
            'name': 'var_2',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_3',
            'type': 'categorical',
            'domain': (0, 1)
        }]
        design_space = Design_space(space)

        np.random.seed(666)

        number_points = 5

        zipped_X = initial_design("random", design_space, number_points)
        pending_zipped_X = initial_design("random", design_space,
                                          number_points)
        ignored_zipped_X = initial_design("random", design_space,
                                          number_points)

        d = DuplicateManager(design_space, zipped_X, pending_zipped_X,
                             ignored_zipped_X)

        duplicate_in_pending_state = np.atleast_2d(
            pending_zipped_X[0, :].copy())

        assert d.is_zipped_x_duplicate(duplicate_in_pending_state)

        assert d.is_unzipped_x_duplicate(
            design_space.unzip_inputs(duplicate_in_pending_state))

        duplicate_in_ignored_state = np.atleast_2d(
            ignored_zipped_X[0, :].copy())

        assert d.is_zipped_x_duplicate(duplicate_in_ignored_state)

        assert d.is_unzipped_x_duplicate(
            design_space.unzip_inputs(duplicate_in_ignored_state))
Ejemplo n.º 8
0
    def test_random_design_with_bandit_only(self):
        space = [self.bandit_variable]
        self.design_space = Design_space(space)
        initial_points_count = 3

        samples = initial_design('random', self.design_space,
                                 initial_points_count)

        self.assertEqual(len(samples), initial_points_count)
Ejemplo n.º 9
0
    def test_random_design_with_constraints(self):
        constraints = [{'name': 'const_1', 'constraint': 'x[:,0]**2 - 1'}]
        self.design_space = Design_space(self.space, constraints=constraints)
        initial_points_count = 10

        samples = initial_design('random', self.design_space,
                                 initial_points_count)

        self.assert_samples_against_space(samples)
        self.assertTrue((samples[:, 0]**2 - 1 < 0).all())
Ejemplo n.º 10
0
    def test_duplicate(self):
        space = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }, {
            'name': 'var_2',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_3',
            'type': 'categorical',
            'domain': (0, 1)
        }]
        design_space = Design_space(space)

        np.random.seed(666)

        number_points = 5

        zipped_X = initial_design("random", design_space, number_points)

        d = DuplicateManager(design_space, zipped_X)

        duplicate = np.atleast_2d(zipped_X[0, :].copy())

        assert d.is_zipped_x_duplicate(duplicate)

        assert d.is_unzipped_x_duplicate(design_space.unzip_inputs(duplicate))

        non_duplicate = np.array([[-2.5, 2., 0.]])

        for x in zipped_X:
            assert not np.all(non_duplicate == x)

        assert not d.is_zipped_x_duplicate(non_duplicate)

        assert not d.is_unzipped_x_duplicate(
            design_space.unzip_inputs(non_duplicate))
Ejemplo n.º 11
0
    def test_nonrandom_designs_with_constrains(self):
        constraints = [{'name': 'const_1', 'constraint': 'x[:,0]**2 - 1'}]
        self.design_space = Design_space(self.space, constraints=constraints)
        initial_points_count = 10

        with self.assertRaises(InvalidConfigError):
            initial_design('grid', self.design_space, initial_points_count)

        with self.assertRaises(InvalidConfigError):
            initial_design('latin', self.design_space, initial_points_count)

        with self.assertRaises(InvalidConfigError):
            initial_design('sobol', self.design_space, initial_points_count)
Ejemplo n.º 12
0
	def setUp(self):
		self.mock_model = Mock()
		self.mock_optimizer = Mock()
		
		domain = [{'name': 'var_1', 'type': 'categorical', 'domain': (0, 1)}, {'name': 'var_2', 'type': 'continuous', 'domain': (-5,5), 'dimensionality': 2}]
		# con_1: if var_1 is 0, var_2_1 must be <= -1
		# con_2: 3 * (var_2_1 + var_2_2) <= 24
		constraints = [{'name': 'con_1', 'constraint': '(x[:,0] == 0) * (x[:,1] + 1)'}, 
						{'name': 'con_2', 'constraint': ' 3 * (x[:,1] + x[:,2]) - 24'}]
		self.space = Design_space(domain, constraints)

		self.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer)
		self.ei_acquisition._compute_acq = Mock()
		self.ei_acquisition._compute_acq_withGradients = Mock()
Ejemplo n.º 13
0
    def setUp(self):
        np.random.seed(123)
        domain = [{
            'name': 'var1',
            'type': 'continuous',
            'domain': (-5, 5),
            'dimensionality': 5
        }]
        space = Design_space(domain)
        func = alpine1(input_dim=5, bounds=space.get_bounds())
        bo = BayesianOptimization(f=func.f, domain=domain)
        context = {'var1_1': 0.3, 'var1_2': 0.4}
        context_manager = ContextManager(space, context)
        x0 = np.array([[0, 0, 0, 0, 0]])

        # initialize the model in a least intrusive way possible
        bo.suggest_next_locations()

        f = bo.acquisition.acquisition_function
        f_df = bo.acquisition.acquisition_function_withGradients
        self.problem_with_context = OptimizationWithContext(
            x0=x0, f=f, df=None, f_df=f_df, context_manager=context_manager)
        self.x = np.array([[3, -3, 3]])
Ejemplo n.º 14
0
    def setUp(self):
        self.mock_model = Mock()
        self.mock_optimizer = Mock()
        self.expected_optimum_position = [[0, 0]]
        self.mock_optimizer.optimize.return_value = self.expected_optimum_position, self.expected_optimum_position
        domain = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-5, 5),
            'dimensionality': 2
        }]
        self.space = Design_space(domain, None)
        self.mock_optimizer.context_manager = ContextManager(self.space)
        self.ei_acquisition = AcquisitionEI(self.mock_model, self.space,
                                            self.mock_optimizer)

        self.random_batch = RandomBatch(self.ei_acquisition, 10)
Ejemplo n.º 15
0
 def test_input_warping_model(self):
     config1 = [{
         'name': 'var_1',
         'type': 'continuous',
         'domain': (-3, 1),
         'dimensionality': 2
     }, {
         'name': 'var_2',
         'type': 'discrete',
         'domain': (-3, 1),
         'dimensionality': 1
     }]
     space1 = Design_space(config1)
     m = InputWarpedGPModel(space1)
     np.random.seed(0)
     X = np.random.randn(50, 3)
     Y = np.sum(np.sin(X), 1).reshape(50, 1)
     m._create_model(X, Y)
Ejemplo n.º 16
0
    def test_latin_design_with_multiple_continuous_variables(self):
        self.space.extend([{
            'name': 'var_5',
            'type': 'continuous',
            'domain': (0, 5),
            'dimensionality': 2
        }, {
            'name': 'var_6',
            'type': 'continuous',
            'domain': (-5, 5),
            'dimensionality': 1
        }])
        self.design_space = Design_space(self.space)

        init_points_count = 10
        samples = initial_design('latin', self.design_space, init_points_count)
        self.assertEqual(len(samples), init_points_count)
        self.assert_samples_against_space(samples)
Ejemplo n.º 17
0
    def test_ts_anchor_points_without_duplicate(self):

        space = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }, {
            'name': 'var_2',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_3',
            'type': 'categorical',
            'domain': (0, 1)
        }]

        design_space = Design_space(space)

        np.random.seed(666)

        design_type = "random"

        # We mock a model
        class dummy_model:
            def predict(self, X):
                n = X.shape[0]
                return np.zeros(n), np.ones(n)

        generator = ThompsonSamplingAnchorPointsGenerator(design_space,
                                                          design_type,
                                                          dummy_model(),
                                                          num_samples=10)

        scores = generator.get_anchor_point_scores(np.arange(3).reshape(3, 1))

        assert np.linalg.norm(
            scores - np.array([0.82418808, 0.479966, 1.17346801])) < tolerance

        assert np.linalg.norm(
            generator.get(num_anchor=2) -
            np.array([[-2.54856939, 2., 1., 0.], [0.09791782, 1., 1., 0.]])
        ) < tolerance
Ejemplo n.º 18
0
    def setUp(self):
        np.random.seed(1)

        X = np.array([[-1.5, -1], [1, 1.5], [3, 3]])
        y = 2 * -np.array([[-0.1], [.3], [.9]])
        bounds = [(-5, 5)]
        input_dim = X.shape[1]
        kern = GPy.kern.RBF(input_dim, variance=1., lengthscale=1.)
        self.model = GPModel(kern,
                             noise_var=0.0,
                             max_iters=0,
                             optimize_restarts=0)
        self.model.updateModel(X, y, None, None)
        domain = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': bounds[0],
            'dimensionality': 2
        }]
        self.space = Design_space(domain)

        self.mock_optimizer = Mock()
Ejemplo n.º 19
0
    def test_gpmodel_predict_covariance(self):
        config = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-1, 1),
            'dimensionality': 1
        }, {
            'name': 'var_2',
            'type': 'continuous',
            'domain': (-1, 1),
            'dimensionality': 1
        }]
        space = Design_space(config)
        model = GPModel(space)
        mock_mean = np.array([[2.0], [-2.0]])
        mock_variance = np.array([[4.0, 3.0], [4.5, 9.0]])
        model.model = Mock()
        model.model.predict.return_value = (mock_mean, mock_variance)

        v = model.predict_covariance(np.ones((2, 2)))

        assert_allclose(v, mock_variance, atol=1e-5)
Ejemplo n.º 20
0
    def setUp(self):
        self.space = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }, {
            'name': 'var_2',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_3',
            'type': 'categorical',
            'domain': (0, 1, 2)
        }]
        self.design_space = Design_space(self.space)

        self.bandit_variable = {
            'name': 'stations',
            'type': 'bandit',
            'domain': np.array([[1, 1], [2, 2], [3, 3], [4, 4]])
        }
Ejemplo n.º 21
0
    def test_objective_anchor_points_without_duplicate(self):

        space = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }, {
            'name': 'var_2',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_3',
            'type': 'categorical',
            'domain': (0, 1)
        }]

        design_space = Design_space(space)

        np.random.seed(666)

        design_type = "random"

        dummy_objective = lambda X: np.sum(X * X, axis=1)

        generator = ObjectiveAnchorPointsGenerator(design_space,
                                                   design_type,
                                                   dummy_objective,
                                                   num_samples=10)

        assert np.all(
            generator.get_anchor_point_scores(np.arange(3).reshape(3, 1)) ==
            np.array([0.0, 1.0, 4.0]))

        assert np.linalg.norm(
            generator.get(num_anchor=2) -
            np.array([[-0.02338332, 2., 1., 0.], [0.09791782, 2., 1., 0.]])
        ) < tolerance
Ejemplo n.º 22
0
    def test_context_hadler(self):
        space = [{
            'name': 'var1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 3
        }, {
            'name': 'var2',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var3',
            'type': 'continuous',
            'domain': (-5, 5)
        }, {
            'name': 'var4',
            'type': 'categorical',
            'domain': (0, 1)
        }]

        context = {'var1_1': 0.45, 'var3': 0.52}

        design_space = Design_space(space)
        np.random.seed(666)

        self.context_manager = ContextManager(space=design_space,
                                              context=context)

        noncontext_bounds = [(-3, 1), (-3, 1), (0, 3), (0, 1), (0, 1)]
        noncontext_index = [1, 2, 3, 5, 6]
        expanded_vector = np.array([[0.45, 0., 0., 0., 0.52, 0., 0.]])

        assert np.all(
            noncontext_bounds == self.context_manager.noncontext_bounds)
        assert np.all(
            noncontext_index == self.context_manager.noncontext_index)
        assert np.all(expanded_vector == self.context_manager._expand_vector(
            np.array([[0, 0, 0, 0, 0]])))
Ejemplo n.º 23
0
    def test_input_warping_indices(self):
        config1 = [{
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 2
        }, {
            'name': 'var_2',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }]
        warp_ind1 = [0, 1, 2]
        space1 = Design_space(config1)
        m1 = InputWarpedGPModel(space1)
        self.assertEqual(m1.warping_indices, warp_ind1)

        config2 = [{
            'name': 'var_1',
            'type': 'categorical',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_2',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }]
        warp_ind2 = [1]
        space2 = Design_space(config2)
        m2 = InputWarpedGPModel(space2)
        self.assertEqual(m2.warping_indices, warp_ind2)

        config3 = [{
            'name': 'var_1',
            'type': 'categorical',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_2',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }]
        warp_ind3 = [1]
        space3 = Design_space(config3)
        m3 = InputWarpedGPModel(space3)
        self.assertEqual(m3.warping_indices, warp_ind3)

        config4 = [{
            'name': 'var_3',
            'type': 'discrete',
            'domain': (0, 1, 2, 3)
        }, {
            'name': 'var_3',
            'type': 'continuous',
            'domain': (2, 4),
            'dimensionality': 2
        }, {
            'name': 'var_1',
            'type': 'continuous',
            'domain': (-3, 1),
            'dimensionality': 1
        }]
        warp_ind4 = [0, 1, 2, 3]
        space4 = Design_space(config4)
        m4 = InputWarpedGPModel(space4)
        self.assertEqual(m4.warping_indices, warp_ind4)

        config5 = [{
            'name': 'var_4',
            'type': 'bandit',
            'domain': np.array([[-2, -1], [0, 1]])
        }]
        warp_ind5 = []
        space5 = Design_space(config5)
        m5 = InputWarpedGPModel(space5)
        self.assertEqual(m5.warping_indices, warp_ind5)