Beispiel #1
0
    def test_branin(self):
        branin = Branin()

        # Check batch computation
        n_points = 10
        X = np.random.rand(n_points, branin.n_dims)
        X[:, 0] = X[:, 0].dot(branin.X_upper[0] - branin.X_lower[0]) + branin.X_lower[0]
        X[:, 1] = X[:, 1].dot(branin.X_upper[1] - branin.X_lower[1]) + branin.X_lower[1]

        y = branin.evaluate(X)

        assert len(y.shape) == 2
        assert y.shape[0] == n_points
        assert y.shape[1] == 1

        # Check single computation
        X = np.array([np.random.rand(branin.n_dims)])

        X[:, 0] = X[:, 0].dot(branin.X_upper[0] - branin.X_lower[0]) + branin.X_lower[0]
        X[:, 1] = X[:, 1].dot(branin.X_upper[1] - branin.X_lower[1]) + branin.X_lower[1]

        y = branin.evaluate(X)
        assert y.shape[0] == 1

        # Check optimas
        X = branin.opt
        y = branin.evaluate(X)

        assert np.all(np.round(y, 6) == np.array([branin.fopt]))
Beispiel #2
0
class TestMaximizers2D(unittest.TestCase):

    def setUp(self):

        self.branin = Branin()

        n_points = 5
        rng = np.random.RandomState(42)
        self.X = init_random_uniform(self.branin.X_lower,
                                     self.branin.X_upper,
                                     n_points,
                                     rng=rng)
        

        self.Y = self.branin.evaluate(self.X)

        kernel = GPy.kern.Matern52(input_dim=self.branin.n_dims)
        self.model = GPyModel(kernel, optimize=True,
                              noise_variance=1e-4,
                              num_restarts=10)

        self.model.train(self.X, self.Y)
        self.acquisition_func = EI(self.model,
                                   X_upper=self.branin.X_upper,
                                   X_lower=self.branin.X_lower,
                                   par=0.1)

    def test_direct(self):
        maximizer = Direct(self.acquisition_func,
                           self.branin.X_lower,
                           self.branin.X_upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert x.shape[1] == self.branin.n_dims
        assert np.all(x[:, 0] >= self.branin.X_lower[0])
        assert np.all(x[:, 1] >= self.branin.X_lower[1])
        assert np.all(x[:, 0] <= self.branin.X_upper[0])
        assert np.all(x[:, 1] <= self.branin.X_upper[1])
        assert np.all(x < self.branin.X_upper)

    def test_stochastic_local_search(self):
        maximizer = StochasticLocalSearch(self.acquisition_func,
                                          self.branin.X_lower,
                                          self.branin.X_upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert x.shape[1] == self.branin.n_dims
        assert np.all(x[:, 0] >= self.branin.X_lower[0])
        assert np.all(x[:, 1] >= self.branin.X_lower[1])
        assert np.all(x[:, 0] <= self.branin.X_upper[0])
        assert np.all(x[:, 1] <= self.branin.X_upper[1])
        assert np.all(x < self.branin.X_upper)

    def test_cmaes(self):
        maximizer = CMAES(self.acquisition_func,
                          self.branin.X_lower,
                          self.branin.X_upper)

        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert x.shape[1] == self.branin.n_dims
        assert np.all(x[:, 0] >= self.branin.X_lower[0])
        assert np.all(x[:, 1] >= self.branin.X_lower[1])
        assert np.all(x[:, 0] <= self.branin.X_upper[0])
        assert np.all(x[:, 1] <= self.branin.X_upper[1])
        assert np.all(x < self.branin.X_upper)
class TestMaximizers2D(unittest.TestCase):
    def setUp(self):

        self.branin = Branin()

        n_points = 5
        rng = np.random.RandomState(42)
        self.X = init_random_uniform(self.branin.X_lower,
                                     self.branin.X_upper,
                                     n_points,
                                     rng=rng)

        self.Y = self.branin.evaluate(self.X)

        kernel = GPy.kern.Matern52(input_dim=self.branin.n_dims)
        self.model = GPyModel(kernel,
                              optimize=True,
                              noise_variance=1e-4,
                              num_restarts=10)

        self.model.train(self.X, self.Y)
        self.acquisition_func = EI(self.model,
                                   X_upper=self.branin.X_upper,
                                   X_lower=self.branin.X_lower,
                                   par=0.1)

    def test_direct(self):
        maximizer = Direct(self.acquisition_func, self.branin.X_lower,
                           self.branin.X_upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert x.shape[1] == self.branin.n_dims
        assert np.all(x[:, 0] >= self.branin.X_lower[0])
        assert np.all(x[:, 1] >= self.branin.X_lower[1])
        assert np.all(x[:, 0] <= self.branin.X_upper[0])
        assert np.all(x[:, 1] <= self.branin.X_upper[1])
        assert np.all(x < self.branin.X_upper)

    def test_stochastic_local_search(self):
        maximizer = StochasticLocalSearch(self.acquisition_func,
                                          self.branin.X_lower,
                                          self.branin.X_upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert x.shape[1] == self.branin.n_dims
        assert np.all(x[:, 0] >= self.branin.X_lower[0])
        assert np.all(x[:, 1] >= self.branin.X_lower[1])
        assert np.all(x[:, 0] <= self.branin.X_upper[0])
        assert np.all(x[:, 1] <= self.branin.X_upper[1])
        assert np.all(x < self.branin.X_upper)

    def test_cmaes(self):
        maximizer = CMAES(self.acquisition_func, self.branin.X_lower,
                          self.branin.X_upper)

        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert x.shape[1] == self.branin.n_dims
        assert np.all(x[:, 0] >= self.branin.X_lower[0])
        assert np.all(x[:, 1] >= self.branin.X_lower[1])
        assert np.all(x[:, 0] <= self.branin.X_upper[0])
        assert np.all(x[:, 1] <= self.branin.X_upper[1])
        assert np.all(x < self.branin.X_upper)