Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Trust Region Bayesian Optimization

Synopsis

#include <boost/math/optimization/turbo.hpp>

namespace boost::math::optimization {

template <typename ArgumentContainer> struct turbo_parameters {
    using Real = typename ArgumentContainer::value_type;
    ArgumentContainer lower_bounds;
    ArgumentContainer upper_bounds;
    ArgumentContainer const * initial_guess = nullptr;
    size_t max_function_calls = 0;
    size_t initial_points = 0;
    size_t batch_size = 1;
    size_t candidates = 0;
    size_t max_training_points = 200;
    size_t hyperparameter_steps = 50;
};

template <typename ArgumentContainer>
void validate_turbo_parameters(turbo_parameters<ArgumentContainer> const &params);

template <typename ArgumentContainer, class Func, class URBG>
ArgumentContainer turbo(const Func cost_function,
                        turbo_parameters<ArgumentContainer> const &params,
                        URBG &gen,
                        std::invoke_result_t<Func, ArgumentContainer> target_value
                          = std::numeric_limits<std::invoke_result_t<Func, ArgumentContainer>>::quiet_NaN(),
                        std::atomic<bool> *cancellation = nullptr,
                        std::atomic<std::invoke_result_t<Func, ArgumentContainer>> *current_minimum_cost = nullptr,
                        std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> *queries = nullptr);

} // namespaces

The turbo function searches for a global minimum of a function on a box with TuRBO-1, the trust region Bayesian optimization algorithm of Eriksson, Pearce, Gardner, Turner and Poloczek. It is meant for expensive functions: it spends milliseconds of its own time per function call, so that it can spend as few calls as possible.

TuRBO fits a Gaussian process (an ARD Matérn-5/2 kernel with a constant mean and a noise term) to the points it has evaluated, but uses it only in a trust region: a box centred at the best point found so far, stretched along each coordinate in proportion to the lengthscale the Gaussian process learned for it. Each new point is the minimizer, over a few hundred candidate points in the trust region, of a random function drawn from the posterior (Thompson sampling). The trust region doubles in size after three batches in a row that improve on its best point, and halves after ⌈max(4, n)/ q/⌉ batches in a row that do not, where /q is the batch size; when it becomes too small, TuRBO forgets it and starts again from a new random design, so that the search as a whole is global.

TuRBO is a good choice when each function call takes much longer than a few milliseconds and the budget is a few hundred to a few thousand calls. It finds good values quickly, but it does not converge to high accuracy: the trust region never gets smaller than 2-7 of the box, and the Gaussian process treats differences in function values smaller than its noise level as noise. For cheap functions, or when the minimum is needed to many digits, multilevel coordinate search, differential evolution, jSO or CMA-ES are better choices, or the result of TuRBO can be polished by a local method.

Parameters

validate_turbo_parameters throws a std::domain_error if the bounds or initial guess are invalid, and a std::invalid_argument if batch_size, candidates or max_training_points is out of range. turbo calls it before the first function evaluation.

The function

template <typename ArgumentContainer, class Func, class URBG>
ArgumentContainer turbo(const Func cost_function,
                        turbo_parameters<ArgumentContainer> const &params,
                        URBG &gen,
                        std::invoke_result_t<Func, ArgumentContainer> target_value
                          = std::numeric_limits<std::invoke_result_t<Func, ArgumentContainer>>::quiet_NaN(),
                        std::atomic<bool> *cancellation = nullptr,
                        std::atomic<std::invoke_result_t<Func, ArgumentContainer>> *current_minimum_cost = nullptr,
                        std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> *queries = nullptr)

Parameters:

Returns:

The ArgumentContainer corresponding to the minimum cost found by the optimization. If the computation is cancelled before the first function call, it is the midpoint of the box (or the initial guess).

Termination

As in the paper, there is no convergence criterion: TuRBO restarts until it is stopped. The computation stops when the target_value is reached, when max_function_calls calls have been made, or when cancellation is set. These are checked before every call, even in the middle of a batch, so max_function_calls is never exceeded.

Cost

Each iteration fits the Gaussian process, with hyperparameter_steps Cholesky factorizations and inverses of an N x N matrix, where N is the number of training points, and draws the Thompson samples, with a Cholesky factorization of the M x M posterior covariance of the M candidates. That is O(hyperparameter_steps N3 + M3 + M2N) operations per iteration. A trust region lasts about 25/n/ function calls, so N reaches the default cap of 200 only for n of about 8 or more. On an Apple M1 Pro core, an iteration with the default parameters takes about 1 ms for n = 2, 4 ms for n = 4, 8 ms for n = 5, 12 ms for n = 6, and 85 ms for n = 10. Setting max_training_points to 100 and candidates to 500 brings the last down to about 15 ms. All of the linear algebra is hand-rolled, and done in double precision whatever the Real type of the arguments.

Implementation notes

The trust region, its update rules, the Thompson sampling, the Gaussian process and the restarts follow Section 2 of the paper and its supplement, and the parameters follow the authors' code (github.com/uber-research/TuRBO, turbo_1.py and gp.py) where the two differ. Each Gaussian process fit starts from the same hyperparameters (lengthscales 0.5, output scale 1, noise variance 0.005, mean 0), and runs 50 steps of Adam with learning rate 0.1 on the negative log marginal likelihood, with analytic gradients, over hyperparameters mapped to the real line by scaled sigmoids onto the intervals [0.005, 2] (lengthscales), [0.05, 20] (output scale) and [0.0005, 0.2] (noise variance), as GPyTorch does. The inputs are scaled to the unit cube, and the function values standardized by their median and standard deviation. The departures from the paper and the authors' code are:

Only TuRBO-1, with a single trust region, is implemented; TuRBO-m maintains m trust regions and allocates each batch among them. The paper's choice of centre for noisy functions (the point with the smallest posterior mean) is not implemented: the centre is always the best point evaluated, as in the authors' code.

Examples

The Branin function has three global minima with value 0.397887 on the box [-5, 10]×[0, 15]:

#include <boost/math/optimization/turbo.hpp>
#include <array>
#include <cmath>
#include <iostream>
#include <random>

int main() {
    using boost::math::optimization::turbo;
    using boost::math::optimization::turbo_parameters;
    using ArgType = std::array<double, 2>;
    auto branin = [](ArgType const & v) {
        double const pi = 3.14159265358979323846;
        double x = v[0];
        double y = v[1];
        double t = y - 5.1/(4*pi*pi)*x*x + 5*x/pi - 6;
        return t*t + 10*(1 - 1/(8*pi))*std::cos(x) + 10;
    };
    turbo_parameters<ArgType> params;
    params.lower_bounds = {-5, 0};
    params.upper_bounds = {10, 15};
    params.max_function_calls = 200;
    std::mt19937_64 gen(42);
    auto x = turbo(branin, params, gen);
    std::cout << "f(" << x[0] << ", " << x[1] << ") = " << branin(x) << "\n";
}

After 200 calls the value is typically within a few times 10-4 of the minimum: over 30 seeds, the median distance was 3.7×10-4 and the largest 1.7×10-3.

References

PrevUpHomeNext