Ejemplo n.º 1
0
def test_trust_region_for_unsuccessful_local_to_global_trust_region_reduced(
) -> None:
    tr = TrustRegion(NegativeLowerConfidenceBound(0).using(OBJECTIVE))
    dataset = Dataset(tf.constant([[0.1, 0.2], [-0.1, -0.2]]),
                      tf.constant([[0.4], [0.5]]))
    lower_bound = tf.constant([-2.2, -1.0])
    upper_bound = tf.constant([1.3, 3.3])
    search_space = Box(lower_bound, upper_bound)

    eps = 0.5 * (search_space.upper - search_space.lower) / 10
    previous_y_min = dataset.observations[0]
    is_global = False
    acquisition_space = Box(dataset.query_points[0] - eps,
                            dataset.query_points[0] + eps)
    previous_state = TrustRegion.State(acquisition_space, eps, previous_y_min,
                                       is_global)

    _, current_state = tr.acquire(search_space, {OBJECTIVE: dataset},
                                  {OBJECTIVE: QuadraticMeanAndRBFKernel()},
                                  previous_state)

    npt.assert_array_less(
        current_state.eps,
        previous_state.eps)  # current TR smaller than previous
    assert current_state.is_global
    npt.assert_array_almost_equal(current_state.acquisition_space.lower,
                                  lower_bound)
Ejemplo n.º 2
0
def test_ego(search_space: SearchSpace, expected_minimum: tf.Tensor) -> None:
    ego = EfficientGlobalOptimization(
        NegativeLowerConfidenceBound(0).using(OBJECTIVE))
    dataset = Dataset(tf.zeros([0, 2]), tf.zeros([0, 1]))
    query_point, _ = ego.acquire(search_space, {OBJECTIVE: dataset},
                                 {OBJECTIVE: QuadraticMeanAndRBFKernel()})
    npt.assert_array_almost_equal(query_point, expected_minimum, decimal=5)
Ejemplo n.º 3
0
def test_trust_region_for_unsuccessful_global_to_local_trust_region_unchanged(
) -> None:
    tr = TrustRegion(NegativeLowerConfidenceBound(0).using(OBJECTIVE))
    dataset = Dataset(tf.constant([[0.1, 0.2], [-0.1, -0.2]]),
                      tf.constant([[0.4], [0.5]]))
    lower_bound = tf.constant([-2.2, -1.0])
    upper_bound = tf.constant([1.3, 3.3])
    search_space = Box(lower_bound, upper_bound)

    eps = 0.5 * (search_space.upper - search_space.lower) / 10
    previous_y_min = dataset.observations[0]
    is_global = True
    acquisition_space = search_space
    previous_state = TrustRegion.State(acquisition_space, eps, previous_y_min,
                                       is_global)

    query_point, current_state = tr.acquire(
        search_space, {OBJECTIVE: dataset},
        {OBJECTIVE: QuadraticWithUnitVariance()}, previous_state)

    npt.assert_array_almost_equal(current_state.eps, previous_state.eps)
    assert not current_state.is_global
    npt.assert_array_less(lower_bound, current_state.acquisition_space.lower)
    npt.assert_array_less(current_state.acquisition_space.upper, upper_bound)
    assert query_point[0] in current_state.acquisition_space
Ejemplo n.º 4
0
def test_ego(search_space: SearchSpace, expected_minimum: tf.Tensor) -> None:
    ego = EfficientGlobalOptimization(
        NegativeLowerConfidenceBound(0).using(OBJECTIVE))
    dataset = Dataset(tf.constant([[]]), tf.constant([[]]))
    query_point, _ = ego.acquire(search_space, {OBJECTIVE: dataset},
                                 {OBJECTIVE: QuadraticWithUnitVariance()})
    npt.assert_array_almost_equal(query_point, expected_minimum, decimal=5)
Ejemplo n.º 5
0
def test_negative_lower_confidence_bound_builder_builds_negative_lower_confidence_bound() -> None:
    model = QuadraticMeanAndRBFKernel()
    beta = 1.96
    acq_fn = NegativeLowerConfidenceBound(beta).prepare_acquisition_function(
        Dataset(tf.zeros([0, 1]), tf.zeros([0, 1])), model
    )
    query_at = tf.linspace([-10], [10], 100)
    expected = -lower_confidence_bound(model, beta, query_at)
    npt.assert_array_almost_equal(acq_fn(query_at), expected)
Ejemplo n.º 6
0
def test_negative_lower_confidence_bound_builder_builds_negative_lower_confidence_bound(
) -> None:
    model = QuadraticWithUnitVariance()
    beta = 1.96
    acq_fn = NegativeLowerConfidenceBound(beta).prepare_acquisition_function(
        Dataset(tf.constant([[]]), tf.constant([[]])), model)
    query_at = tf.constant([[-3.], [-2.], [-1.], [0.], [1.], [2.], [3.]])
    expected = -lower_confidence_bound(model, beta, query_at)
    npt.assert_array_almost_equal(acq_fn(query_at), expected)
Ejemplo n.º 7
0
def test_locally_penalized_expected_improvement_raises_when_called_with_invalid_base(
) -> None:
    search_space = Box([0, 0], [1, 1])
    base_builder = NegativeLowerConfidenceBound()
    with pytest.raises(ValueError):
        LocalPenalizationAcquisitionFunction(
            search_space,
            base_acquisition_function_builder=base_builder  # type: ignore
        )
Ejemplo n.º 8
0
def test_reducers_on_lcb(reducer):
    m = 6
    beta = tf.convert_to_tensor(1.96, dtype=tf.float64)
    model = QuadraticWithUnitVariance()
    acqs = [NegativeLowerConfidenceBound(beta).using("foo") for _ in range(m)]
    acq = reducer.type_class(*acqs)
    acq_fn = acq.prepare_acquisition_function({"foo": reducer.dataset}, {"foo": model})
    individual_lcb = [-lower_confidence_bound(model, beta, reducer.query_point) for _ in range(m)]
    expected = reducer.raw_reduce_op(individual_lcb)
    desired = acq_fn(reducer.query_point)
    np.testing.assert_array_almost_equal(expected, desired)
Ejemplo n.º 9
0
def test_trust_region_for_default_state() -> None:
    tr = TrustRegion(NegativeLowerConfidenceBound(0).using(OBJECTIVE))
    dataset = Dataset(tf.constant([[0.1, 0.2]]), tf.constant([[0.012]]))
    lower_bound = tf.constant([-2.2, -1.0])
    upper_bound = tf.constant([1.3, 3.3])
    search_space = Box(lower_bound, upper_bound)

    query_point, state = tr.acquire(search_space, {OBJECTIVE: dataset},
                                    {OBJECTIVE: QuadraticMeanAndRBFKernel()},
                                    None)

    npt.assert_array_almost_equal(query_point, tf.constant([[0.0, 0.0]]), 5)
    npt.assert_array_almost_equal(state.acquisition_space.lower, lower_bound)
    npt.assert_array_almost_equal(state.acquisition_space.upper, upper_bound)
    npt.assert_array_almost_equal(state.y_min, [0.012])
    assert state.is_global
Ejemplo n.º 10
0
    expected = tf.reduce_mean(tf.reduce_max(tf.maximum(
        min_predictive_mean_at_known_points - mvn_samples, 0.0
    ), axis=-1), axis=0)
    # fmt: on

    builder = BatchMonteCarloExpectedImprovement(10_000)
    acq = builder.prepare_acquisition_function(mk_dataset([[0.3], [0.5]], [[0.09], [0.25]]), model)

    npt.assert_allclose(acq(xs), expected, rtol=0.05)


@pytest.mark.parametrize(
    "function, function_repr",
    [
        (ExpectedImprovement(), "ExpectedImprovement()"),
        (NegativeLowerConfidenceBound(1.96), "NegativeLowerConfidenceBound(1.96)"),
        (NegativePredictiveMean(), "NegativePredictiveMean()"),
        (ProbabilityOfFeasibility(0.5), "ProbabilityOfFeasibility(0.5)"),
        (ExpectedHypervolumeImprovement(), "ExpectedHypervolumeImprovement()"),
        (
            BatchMonteCarloExpectedImprovement(10_000),
            f"BatchMonteCarloExpectedImprovement(10000, jitter={DEFAULTS.JITTER})",
        ),
    ],
)
def test_single_model_acquisition_function_builder_reprs(function, function_repr) -> None:
    assert repr(function) == function_repr
    assert repr(function.using("TAG")) == f"{function_repr} using tag 'TAG'"
    assert (
        repr(ExpectedConstrainedImprovement("TAG", function.using("TAG"), 0.0))
        == f"ExpectedConstrainedImprovement('TAG', {function_repr} using tag 'TAG', 0.0)"