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)
def test_trust_region_raises_for_missing_datasets_key( datasets: dict[str, Dataset], models: dict[str, ProbabilisticModel]) -> None: search_space = Box([-1], [1]) rule = TrustRegion() with pytest.raises(KeyError): rule.acquire(search_space, datasets, models, None)
def test_trust_region_raises_for_missing_datasets_key( datasets: Dict[str, Dataset], models: Dict[str, ProbabilisticModel]) -> None: search_space = one_dimensional_range(-1, 1) rule = TrustRegion() with pytest.raises(KeyError): rule.acquire(search_space, datasets, models, None)
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
def test_trust_region_for_successful_local_to_global_trust_region_increased( rule: AcquisitionRule[TensorType, Box] ) -> None: tr = TrustRegion(rule) dataset = Dataset(tf.constant([[0.1, 0.2], [-0.1, -0.2]]), tf.constant([[0.4], [0.3]])) 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: QuadraticMeanAndRBFKernel()}, datasets={OBJECTIVE: dataset}, )(previous_state) assert current_state is not None npt.assert_array_less(previous_state.eps, current_state.eps) # current TR larger than previous assert current_state.is_global npt.assert_array_almost_equal(current_state.acquisition_space.lower, lower_bound) npt.assert_array_almost_equal(current_state.acquisition_space.upper, upper_bound)
def test_trust_region_successful_global_to_global_trust_region_unchanged( rule: AcquisitionRule[TensorType, Box], expected_query_point: TensorType ) -> None: tr = TrustRegion(rule) dataset = Dataset(tf.constant([[0.1, 0.2], [-0.1, -0.2]]), tf.constant([[0.4], [0.3]])) 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 previous_state = TrustRegion.State(search_space, eps, previous_y_min, is_global) current_state, query_point = tr.acquire( search_space, {OBJECTIVE: QuadraticMeanAndRBFKernel()}, datasets={OBJECTIVE: dataset}, )(previous_state) assert current_state is not None npt.assert_array_almost_equal(current_state.eps, previous_state.eps) assert current_state.is_global npt.assert_array_almost_equal(query_point, expected_query_point, 5) npt.assert_array_almost_equal(current_state.acquisition_space.lower, lower_bound) npt.assert_array_almost_equal(current_state.acquisition_space.upper, upper_bound)
def test_trust_region_for_default_state() -> None: tr = TrustRegion(NegativeLowerConfidenceBound(0)) 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_single(search_space, dataset, 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
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: QuadraticWithUnitVariance()}, 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
def test_trust_region_for_default_state( rule: AcquisitionRule[TensorType, Box], expected_query_point: TensorType ) -> None: tr = TrustRegion(rule) 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) state, query_point = tr.acquire_single(search_space, dataset, QuadraticMeanAndRBFKernel())(None) assert state is not None npt.assert_array_almost_equal(query_point, expected_query_point, 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
from trieste.utils.objectives import BRANIN_MINIMIZERS, BRANIN_MINIMUM, branin, mk_observer @random_seed @pytest.mark.parametrize( "num_steps, acquisition_rule", [ (20, EfficientGlobalOptimization()), ( 15, EfficientGlobalOptimization( BatchMonteCarloExpectedImprovement(sample_size=500).using(OBJECTIVE), num_query_points=2, ), ), (15, TrustRegion()), (17, ThompsonSampling(500, 3)), ], ) def test_optimizer_finds_minima_of_the_branin_function( num_steps: int, acquisition_rule: AcquisitionRule ) -> None: search_space = Box([0, 0], [1, 1]) def build_model(data: Dataset) -> GaussianProcessRegression: variance = tf.math.reduce_variance(data.observations) kernel = gpflow.kernels.Matern52(variance, tf.constant([0.2, 0.2], tf.float64)) gpr = gpflow.models.GPR((data.query_points, data.observations), kernel, noise_variance=1e-5) gpflow.utilities.set_trainable(gpr.likelihood, False) return GaussianProcessRegression(gpr)
from trieste.utils.objectives import ( branin, BRANIN_GLOBAL_MINIMUM, BRANIN_GLOBAL_ARGMIN, mk_observer, ) from tests.util.misc import random_seed @random_seed @pytest.mark.parametrize( "num_steps, acquisition_rule", [ (30, EfficientGlobalOptimization()), (22, TrustRegion()), (17, ThompsonSampling(500, 3)), ], ) def test_optimizer_finds_minima_of_the_branin_function( num_steps: int, acquisition_rule: AcquisitionRule) -> None: search_space = Box(tf.constant([0.0, 0.0], tf.float64), tf.constant([1.0, 1.0], tf.float64)) def build_model(data: Dataset) -> GaussianProcessRegression: variance = tf.math.reduce_variance(data.observations) kernel = gpflow.kernels.Matern52(variance, tf.constant([0.2, 0.2], tf.float64)) gpr = gpflow.models.GPR((data.query_points, data.observations), kernel, noise_variance=1e-5)
from trieste.types import State, TensorType @random_seed @pytest.mark.parametrize( "num_steps, reload_state, acquisition_rule_fn", cast( List[Tuple[int, bool, Union[Callable[[], AcquisitionRule[TensorType, Box]], Callable[[], AcquisitionRule[State[TensorType, Union[ AsynchronousGreedy.State, TrustRegion.State], ], Box, ], ], ], ]], [ (20, False, lambda: EfficientGlobalOptimization()), (20, True, lambda: EfficientGlobalOptimization()), (15, False, lambda: TrustRegion()), (15, True, lambda: TrustRegion()), ( 10, False, lambda: EfficientGlobalOptimization( LocalPenalizationAcquisitionFunction( BRANIN_SEARCH_SPACE, ).using(OBJECTIVE), num_query_points=3, ), ), ( 30, False, lambda: AsynchronousGreedy( LocalPenalizationAcquisitionFunction(
AsynchronousGreedy( LocalPenalizationAcquisitionFunction( BRANIN_SEARCH_SPACE, ).using(OBJECTIVE), ), ), ( 10, EfficientGlobalOptimization( GIBBON( BRANIN_SEARCH_SPACE, ).using(OBJECTIVE), num_query_points=2, ), ), (15, TrustRegion()), ( 15, TrustRegion( EfficientGlobalOptimization( MinValueEntropySearch(BRANIN_SEARCH_SPACE, num_fourier_features=1000).using( OBJECTIVE ) ) ), ), (10, DiscreteThompsonSampling(500, 3)), (10, DiscreteThompsonSampling(500, 3, num_fourier_features=1000)), ], ), )