Example #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)
    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]])