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.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer)
def test_optimize_without_analytical_gradient_prediction(self): """Test that acquisition function optimize method returns expected optimum without analytical gradient prediction """ expected_optimum_position = [[0, 0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = False self.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.ei_acquisition.optimize() assert optimum_position == expected_optimum_position
class TestEIAcquisition(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.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) def test_acquisition_function(self): """Test that acquisition function returns correct weighted acquisition """ self.mock_model.predict.return_value = (1, 3) self.mock_model.get_fmin.return_value = 0.1 weighted_acquisition = self.ei_acquisition.acquisition_function(np.array([2,2])) assert np.isclose(weighted_acquisition, np.array([[-0.79646919], [-0.79646919]])).all() def test_acquisition_function_withGradients(self): """Test that acquisition function with gradients returns correct weight acquisition and gradient """ self.mock_model.predict_withGradients.return_value = (1, 1, 0.1, 0.1) self.mock_model.get_fmin.return_value = 0.1 weighted_acquisition, weighted_gradient = self.ei_acquisition.acquisition_function_withGradients(np.array([2,2])) assert np.isclose(weighted_acquisition, np.array([[-0.0986038],[-0.0986038]])).all() assert np.isclose(weighted_gradient, np.array([[-0.00822768, -0.00822768], [-0.00822768, -0.00822768]])).all() def test_optimize_with_analytical_gradient_prediction(self): """Test that acquisition function optimize method returns expected optimum with analytical gradient prediction """ expected_optimum_position = [[0, 0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = True self.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.ei_acquisition.optimize() assert optimum_position == expected_optimum_position def test_optimize_without_analytical_gradient_prediction(self): """Test that acquisition function optimize method returns expected optimum without analytical gradient prediction """ expected_optimum_position = [[0, 0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = False self.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.ei_acquisition.optimize() assert optimum_position == expected_optimum_position
class TestEIAcquisition(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.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) def test_acquisition_function(self): """Test that acquisition function returns correct weighted acquisition """ self.mock_model.predict.return_value = (1, 3) self.mock_model.get_fmin.return_value = 0.1 weighted_acquisition = self.ei_acquisition.acquisition_function(np.array([2,2])) assert np.isclose(weighted_acquisition, np.array([[-0.79646919], [-0.79646919]])).all() def test_acquisition_function_withGradients(self): """Test that acquisition function with gradients returns correct weight acquisition and gradient """ self.mock_model.predict_withGradients.return_value = (1, 1, 0.1, 0.1) self.mock_model.get_fmin.return_value = 0.1 weighted_acquisition, weighted_gradient = self.ei_acquisition.acquisition_function_withGradients(np.array([2,2])) assert np.isclose(weighted_acquisition, np.array([[-0.0986038],[-0.0986038]])).all() assert np.isclose(weighted_gradient, np.array([[-0.00822768, -0.00822768], [-0.00822768, -0.00822768]])).all() def test_optimize_with_analytical_gradient_prediction(self): """Test that acquisition function optimize method returns expected optimum with analytical gradient prediction """ expected_optimum_position = [[0, 0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = True self.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.ei_acquisition.optimize() assert optimum_position == expected_optimum_position def test_optimize_without_analytical_gradient_prediction(self): """Test that acquisition function optimize method returns expected optimum without analytical gradient prediction """ expected_optimum_position = [[0, 0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = False self.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.ei_acquisition.optimize() assert optimum_position == expected_optimum_position
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()
def plot_acquisition(obj, d_opt, filename=None): # GPyOpt part: Interpolation bounds = obj.space.get_bounds() x_grid = np.linspace(bounds[0][0], bounds[0][1], 1000) x_grid = x_grid.reshape(len(x_grid), 1) AEI = AcquisitionEI(obj.model, obj.space) acqu = AEI._compute_acq(x_grid) acqu_normalized = (-acqu - min(-acqu)) / (max(-acqu - min(-acqu))) m, s = obj.model.predict(x_grid) fig = plt.figure(figsize=(10, 6)) ax = fig.add_subplot(111) # Plot Model # Note that we have to flip the GPyOpt utility ax.plot(x_grid, -m, 'k-', lw=1, alpha=0.6) ax.plot(x_grid, -(m + np.sqrt(s)), 'k-', alpha=0.2) ax.plot(x_grid, -(m - np.sqrt(s)), 'k-', alpha=0.2) ax.fill_between(x_grid.reshape(-1), -(m + np.sqrt(s)).reshape(-1), -(m - np.sqrt(s)).reshape(-1), color='steelblue', alpha=0.5, label=r'68\% C.L.') # Plot Evals X = obj.X Y = normalize(obj.Y) ax.plot(X, -1 * Y, 'r.', markersize=10, label='Bayes. Opt. Evaluations') # Plot optimum ax.axvline(d_opt, ls='--', c='g', label='Optimal Design') ax.set_xlabel(r'Design variable d') ax.set_ylabel(r'$U(d)$') ax.legend(loc='bottom right', prop={'size': 14}) ax.grid(True, ls='--') # ax.set_title('Utility Function') # ax.tick_params(labelsize=25) plt.tight_layout() if filename: plt.savefig('{}.pdf'.format(filename)) plt.savefig('{}.png'.format(filename)) else: plt.savefig('./utility.pdf') plt.savefig('./utility.png')
def test_ChecKGrads_EI(self): acquisition_ei = acquisition_for_test( AcquisitionEI(self.model, self.feasible_region)) grad_ei = GradientChecker(acquisition_ei.acquisition_function, acquisition_ei.d_acquisition_function, self.X_test) self.assertTrue(grad_ei.checkgrad(tolerance=self.tolerance))
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.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer)
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
class TestEIAcquisitionWithCategoricalVariables(unittest.TestCase): 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() def test_acquisition_function(self): """Test that acquisition function does correct constraint(s) check""" y = [1.37, 8.22, 4.2, 0.55, 3.14] self.ei_acquisition._compute_acq.return_value = np.array(y)[:, None] correct_y = [-y[0]] + [0,0] + [-y[3]] + [0] x_unzipped = np.array([[0, 1, 3.3, -3.3], [1, 0, 1.5, 4.7], [0, 1, 4.1, 4.5], [1, 0, -4.1, 4.5], [1, 0, 5, 3]]) acquisitions = self.ei_acquisition.acquisition_function(x_unzipped) assert np.isclose(acquisitions, np.array(correct_y)[:, None]).all() def test_acquisition_function_withGradients(self): """Test that acquisition function does correct constraint(s) check with gradients""" y = [1.37, 8.22, 4.2, 0.55, 3.14] y_grad = [.3, .7, -.5, .1, -.02] self.ei_acquisition._compute_acq_withGradients.return_value = np.array(y)[:, None], np.array(y_grad)[:, None] correct_y = [-y[0]] + [0,0] + [-y[3]] + [0] correct_y_grad = [-y_grad[0]] + [0,0] + [-y_grad[3]] + [0] x_unzipped = np.array([[0, 1, 3.3, -3.3], [1, 0, 1.5, 4.7], [0, 1, 4.1, 4.5], [1, 0, -4.1, 4.5], [1, 0, 5, 3]]) acquisitions, gradients = self.ei_acquisition.acquisition_function_withGradients(x_unzipped) assert np.isclose(acquisitions, np.array(correct_y)[:, None]).all() assert np.isclose(gradients, np.array(correct_y_grad)[:, None]).all()
def test_optimize_without_analytical_gradient_prediction(self): """Test that acquisition function optimize method returns expected optimum without analytical gradient prediction """ expected_optimum_position = [[0, 0]] self.mock_optimizer.optimize.return_value = expected_optimum_position self.mock_model.analytical_gradient_prediction = False self.ei_acquisition = AcquisitionEI(self.mock_model, self.space, self.mock_optimizer) optimum_position = self.ei_acquisition.optimize() assert optimum_position == expected_optimum_position
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.')
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)