Example #1
0
def test_lbfgs_with_gradient_and_context(lbfgs_context, objective, gradient,
                                         space):
    context = ContextManager(space, {'x': 0.5})
    x0 = np.array([1, 1])
    x, f = apply_optimizer(lbfgs_context, x0, space, objective, gradient, None,
                           context)
    assert np.all(np.isclose(x, np.array([0.5, 0])))
    def optimize(self,
                 acquisition: Acquisition,
                 context: Optional = None) -> Tuple[np.ndarray, np.ndarray]:
        """
        Optimizes the acquisition function.
        :param acquisition: The acquisition function to be optimized
        :param context: Optimization context.
                        Determines whether any variable values should be fixed during the optimization
        :return: Tuple of (location of maximum, acquisition value at maximizer)
        """
        if context is None:
            context = dict()
        else:
            self._validate_context_parameters(context)
        context_manager = ContextManager(self.space, context)
        max_x, max_value = self._optimize(acquisition, context_manager)

        # Optimization might not match any encoding exactly
        # Rounding operation here finds the closest encoding
        rounded_max_x = np.concatenate(tuple(
            self.space.round(max_x[:, i * self.batch_size:(i + 1) *
                                   self.batch_size])
            for i in range(self.batch_size)),
                                       axis=1)

        return max_x, max_value
Example #3
0
def test_context_manager_catg_expand_vector(catg_space, catg_context):
    context_manager = ContextManager(catg_space, catg_context)
    x = np.array([[4.0, -1], [3, 0.0]])
    x_expanded = context_manager.expand_vector(x)
    assert context_manager.space.dimensionality == 11
    assert x_expanded.shape == (2, context_manager.space.dimensionality)
    assert np.array_equal(
        x_expanded,
        np.array([[4.0, 1, 0, 0, 0, 0, 0, 1, 0, 0, -1.0],
                  [3, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0.0]]))
Example #4
0
def test_context_manager_incorrect_category(catg_space):
    erroneous_context = {"x2": 0, "x3": 3}
    with pytest.raises(ValueError):
        context_manager = ContextManager(catg_space, erroneous_context)
Example #5
0
def test_context_manager_expand_vector(space, context):
    context_manager = ContextManager(space, context)
    x = np.array([[0.5]])
    x_expanded = context_manager.expand_vector(x)
    assert x_expanded.ndim == 2
    assert np.array_equal(x_expanded, np.array([[0.3, 0.5]]))
Example #6
0
def test_context_manager_idxs(space, context):
    context_manager = ContextManager(space, context)
    assert context_manager.non_context_idxs == [1]
    assert context_manager.context_idxs == [0]
    assert context_manager.context_values == [0.3]