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)
def test_optimize_without_analytical_gradient_prediction(self): expected_optimum_position = [[0,0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = False self.mpi_acquisition = AcquisitionMPI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.mpi_acquisition.optimize() self.assertEqual(optimum_position, expected_optimum_position)
def test_ChecKGrads_MPI(self): acquisition_mpi = acquisition_for_test( AcquisitionMPI(self.model, self.feasible_region)) grad_mpi = GradientChecker(acquisition_mpi.acquisition_function, acquisition_mpi.d_acquisition_function, self.X_test) self.assertTrue(grad_mpi.checkgrad(tolerance=self.tolerance))
class TestMPIAcquisition(unittest.TestCase): 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) def test_acquisition_function(self): self.mock_model.predict.return_value = (1,3) self.mock_model.get_fmin.return_value = (0.1) weighted_acquisition = self.mpi_acquisition.acquisition_function(np.array([2,2])) expected_acquisition = np.array([[-0.38081792], [-0.38081792]]) self.assertTrue(np.isclose(weighted_acquisition, expected_acquisition).all()) def test_acquisition_function_withGradients(self): self.mock_model.predict_withGradients.return_value = (1,3,0.1,0.2) self.mock_model.get_fmin.return_value = 0.1 weighted_acquisition, weighted_gradient = self.mpi_acquisition.acquisition_function_withGradients(np.array([2,2])) self.assertTrue(np.isclose(weighted_acquisition, np.array([[-0.38081792],[-0.38081792]])).all()) self.assertTrue(np.isclose(weighted_gradient, np.array([[0.00499539, 0.00499539],[0.00499539,0.00499539]])).all()) def test_optimize_with_analytical_gradient_prediction(self): expected_optimum_position = [[0,0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = True self.mpi_acquisition = AcquisitionMPI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.mpi_acquisition.optimize() self.assertEqual(optimum_position, expected_optimum_position) def test_optimize_without_analytical_gradient_prediction(self): expected_optimum_position = [[0,0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = False self.mpi_acquisition = AcquisitionMPI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.mpi_acquisition.optimize() self.assertEqual(optimum_position, expected_optimum_position)
def build_acquisition(X_init, space, aquisition_function, model): aquisition_optimizer = GPyOpt.optimization.AcquisitionOptimizer(space, eps=0) if(aquisition_function['type'] == 'ei'): aquisition_function = AcquisitionEI(model=model, space=space, optimizer=aquisition_optimizer,jitter=aquisition_function['epsilon']) elif(aquisition_function['type']== 'pi'): aquisition_function = AcquisitionMPI(model=model, space=space, optimizer=aquisition_optimizer,jitter=aquisition_function['epsilon']) elif(aquisition_function['type'] == 'lcb'): lcb_const = np.sqrt( aquisition_function['upsilon']* (2* np.log( ((X_init.shape[0])**(X_init.shape[1]/2. + 2))*(np.pi**2)/(3. * aquisition_function['delta']) ))) aquisition_function = AcquisitionLCB(model=model, space=space, optimizer=aquisition_optimizer,exploration_weight=lcb_const) return aquisition_function
def acquisition_creator(self, acquisition_type, model, space, acquisition_optimizer, cost_withGradients, **kwargs): """ Acquisition chooser from the available options. Extra parameters can be passed via **kwargs. """ acquisition_type = acquisition_type model = model space = space acquisition_optimizer = acquisition_optimizer cost_withGradients = cost_withGradients acquisition_jitter = self.kwargs.get('acquisition_jitter', 0.01) acquisition_weight = self.kwargs.get('acquisition_weight', 2) # --- Choose the acquisition if acquisition_type is None or acquisition_type == 'EI': return AcquisitionEI(model, space, acquisition_optimizer, cost_withGradients, acquisition_jitter) elif acquisition_type == 'EI_MCMC': return AcquisitionEI_MCMC(model, space, acquisition_optimizer, cost_withGradients, acquisition_jitter) elif acquisition_type == 'MPI': return AcquisitionMPI(model, space, acquisition_optimizer, cost_withGradients, acquisition_jitter) elif acquisition_type == 'MPI_MCMC': return AcquisitionMPI_MCMC(model, space, acquisition_optimizer, cost_withGradients, acquisition_jitter) elif acquisition_type == 'LCB': return AcquisitionLCB(model, space, acquisition_optimizer, None, acquisition_weight) elif acquisition_type == 'LCB_MCMC': return AcquisitionLCB_MCMC(model, space, acquisition_optimizer, None, acquisition_weight) elif acquisition_type == 'Adaptive_LCB': return original_acq.AcquisitionLCBwithAdaptiveExplorationWeight( model, space, acquisition_optimizer, None, **kwargs) elif acquisition_type == 'Adaptive_LCB_MCMC': return original_acq.AcquisitionLCBwithAdaptiveExplorationWeight_MCMC( model, space, acquisition_optimizer, None, **kwargs) elif acquisition_type == 'Continuous_Time_Varying': return original_acq.AcquisitionContinuousTimeVarying( model, space, acquisition_optimizer, None, **kwargs) elif acquisition_type == 'Continuous_Time_Varying_MCMC': return original_acq.AcquisitionContinuousTimeVarying_MCMC( model, space, acquisition_optimizer, None, **kwargs) else: raise Exception('Invalid acquisition selected.')