Ejemplo n.º 1
0
    def test_learn(self, mocked_gp):
        """Test the learning function."""
        # initialize
        mocked_env = mock.MagicMock(name='env', return_value="Batch")
        n = 5
        batch_size = 3
        coeffs = np.arange(n)
        meshgrid = np.meshgrid(coeffs, coeffs)
        subject = ucb.BatchGPUCB(batch_size, meshgrid, mocked_env, beta=1)

        # set up mocked functions
        subject.batch_sample = mock.MagicMock(name='batch sample')
        mocked_gp.return_value = mock.MagicMock(name='Mocked GP')
        mocked_gp.return_value.predict.return_value = ('Mu', 'Sigma')

        subject.learn()

        # test GP correctly called
        self.assertTrue(mocked_gp.return_value.fit.called)
        self.assertEqual(subject.mu, 'Mu')
        self.assertEqual(subject.sigma, 'Sigma')
        # check time step increase
        self.assertEqual(subject.T, 1)

        # test second time step
        subject = ucb.BatchGPUCB(batch_size, meshgrid, mocked_env, beta=1)
        subject.T = 1
        subject.learn()
        # check time step increase
        self.assertEqual(subject.T, 2)
Ejemplo n.º 2
0
 def test___init__(self):
     """Test initialization of the Batch GP UCB class."""
     # initialize
     mocked_env = mock.MagicMock(name='env')
     n = 5
     batch_size = 3
     coeffs = np.arange(n)
     meshgrid = np.meshgrid(coeffs, coeffs)
     subject = ucb.BatchGPUCB(batch_size, meshgrid, mocked_env, beta=1)
     # test assignment of additional attribute
     self.assertTrue(hasattr(subject, 'batch_size'))
     self.assertEqual(subject.batch_size, batch_size)
Ejemplo n.º 3
0
 def test_argsort_ucb(self):
     """Test getting the argsort of the UCB."""
     # initialize
     mocked_env = mock.MagicMock(name='env', return_value="Batch")
     n = 5
     batch_size = 3
     coeffs = np.arange(n)
     meshgrid = np.meshgrid(coeffs, coeffs)
     subject = ucb.BatchGPUCB(batch_size, meshgrid, mocked_env, beta=1)
     # making the max occur at starting at index 2 for an entire batch
     subject.mu[2:2 + batch_size] = 1
     return_val = subject.argsort_ucb()
     expected_val = np.arange(2, 2 + batch_size)
     np.testing.assert_array_equal(return_val, expected_val)
Ejemplo n.º 4
0
    def test_batch_sample(self):
        """Test the environment sampling."""
        # initialize
        mocked_env = mock.MagicMock(name='env')
        mocked_env.sample = mock.MagicMock(name='env_sample',
                                           return_value="Batch")
        n = 5
        batch_size = 3
        coeffs = np.arange(n)
        meshgrid = np.meshgrid(coeffs, coeffs)
        subject = ucb.BatchGPUCB(batch_size, meshgrid, mocked_env, beta=1)

        indices = [2, 4, 5]
        subject.batch_sample(indices)
        self.assertEqual(subject.Y[0], ["Batch", "Batch", "Batch"])
Ejemplo n.º 5
0
    def test_latin_hypercube_sample(self, mocked_lhs):
        """Test the Latin hypercube sampling on first iteration of learn."""
        mocked_env = mock.MagicMock(name='env')
        mocked_env.sample = mock.MagicMock(name='env_sample',
                                           return_value="Batch")

        mocked_lhs.return_value = mock.MagicMock(name='LH_sample',
                                                 return_value=[[3.0, 2.1],
                                                               [0.4, 1.1],
                                                               [0.0, 1.9]])

        n = 5
        batch_size = 3
        coeffs = np.arange(n)
        meshgrid = np.meshgrid(coeffs, coeffs)
        subject = ucb.BatchGPUCB(batch_size, meshgrid, mocked_env, beta=1)
        subject.latin_hypercube_sample()
        self.assertEqual(list(subject.X[0][0]), [3, 2])
        self.assertEqual(list(subject.X[0][1]), [0, 1])
        self.assertEqual(list(subject.X[0][2]), [0, 2])
        name, args, kwargs = mocked_lhs.mock_calls[0]
        self.assertEqual(kwargs['xlimits'].tolist(), [[0, n - 1], [0, n - 1]])