Esempio n. 1
0
 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))
Esempio n. 2
0
    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)
Esempio n. 3
0
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
Esempio n. 4
0
    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
Esempio n. 5
0
    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.')
Esempio n. 6
0
	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()
Esempio n. 7
0
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')
Esempio n. 8
0
    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)