Asynchronous Bayesian optimization with Trieste#

In this notebook we demonstrate Trieste’s ability to perform asynchronous Bayesian optimisation, as is suitable for scenarios where the objective function can be run for several points in parallel but where observations might return back at different times. To avoid wasting resources waiting for the evaluation of the whole batch, we immediately request the next point asynchronously, taking into account points that are still being evaluated. Besides saving resources, asynchronous approach also can potentially improve sample efficiency in comparison with synchronous batch strategies, although this is highly dependent on the use case.

To contrast this approach with regular batch optimization, this notebook also shows how to run parallel synchronous batch approach.

[1]:
# silence TF warnings and info messages, only print errors
# https://stackoverflow.com/questions/35911252/disable-tensorflow-debugging-information
import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import tensorflow as tf

tf.get_logger().setLevel("ERROR")
import numpy as np
import time
import timeit

First, let’s define a simple objective that will emulate evaluations taking variable time. We will be using a classic Bayesian optimisation benchmark function Branin with a sleep call inserted in the middle of the calculation to emulate delay. Our sleep delay is a scaled sum of all input values to make sure delays are uneven.

[2]:
from trieste.objectives import scaled_branin


def objective(points, sleep=True):
    if points.shape[1] != 2:
        raise ValueError(
            f"Incorrect input shape, expected (*, 2), got {points.shape}"
        )

    observations = []
    for point in points:
        observation = scaled_branin(point)
        if sleep:
            # insert some artificial delay
            # increases linearly with the absolute value of points
            # which means our evaluations will take different time
            delay = 3 * np.sum(point)
            pid = os.getpid()
            print(
                f"Process {pid}: Objective: pretends like it's doing something for {delay:.2}s",
                flush=True,
            )
            time.sleep(delay)
        observations.append(observation)

    return np.array(observations)


# test the defined objective function
objective(np.array([[0.1, 0.5]]), sleep=False)
[2]:
array([[-0.42052567]])

As always, we need to prepare the model and some initial data to kick-start the optimization process.

[3]:
from trieste.space import Box
from trieste.data import Dataset
from trieste.objectives import SCALED_BRANIN_MINIMUM

search_space = Box([0, 0], [1, 1])
num_initial_points = 3
initial_query_points = search_space.sample(num_initial_points)
initial_observations = objective(initial_query_points.numpy(), sleep=False)
initial_data = Dataset(
    query_points=initial_query_points,
    observations=tf.constant(initial_observations, dtype=tf.float64),
)

import gpflow
from trieste.models.gpflow import GaussianProcessRegression, build_gpr

# We set the likelihood variance to a small number because
# we are dealing with a noise-free problem.
gpflow_model = build_gpr(initial_data, search_space, likelihood_variance=1e-7)
model = GaussianProcessRegression(gpflow_model)


# these imports will be used later for optimization
from trieste.acquisition import LocalPenalization
from trieste.acquisition.rule import (
    AsynchronousGreedy,
    EfficientGlobalOptimization,
)
from trieste.ask_tell_optimization import AskTellOptimizer

Multiprocessing setup#

To keep this notebook as reproducible as possible, we will only be using Python’s multiprocessing package here. In this section we will explain our setup and define some common code to be used later.

In both synchronous and asynchronous scenarios we will have a fixed set of worker processes performing observations. We will also have a main process responsible for optimization process with Trieste. When Trieste suggests a new point, it is inserted into a points queue. One of the workers picks this point from the queue, performs the observation, and inserts the output into the observations queue. The main process then picks up the observation from the queue, at which moment it either waits for the rest of the points in the batch to come back (synchronous scenario) or immediately suggests a new point (asynchronous scenario). This process continues either for a certain number of iterations or until we accumulate necessary number of observations.

The overall setup is illustrated in this diagram: multiprocessing setup

[4]:
# Necessary multiprocessing primitives
from multiprocessing import Manager, Process

We now define several common functions to implement the described setup. First we define a worker function that will be running a single observation in a separate process. Worker takes both queues as an input, reads next point from the points queue, makes an observation, and inserts observed data into the observations queue.

[5]:


def observer_proc(points_queue, observations_queue): pid = os.getpid() while True: point_to_observe = points_queue.get() if point_to_observe is None: return print( f"Process {pid}: Observer : observing data at point {point_to_observe}", flush=True, ) new_observation = objective(point_to_observe, sleep=enable_sleep_delays) new_data = (point_to_observe, new_observation) print(f"Process {pid}: Observer : observed data {new_data}", flush=True) observations_queue.put(new_data)

Next we define two helper functions, one is to create a certain number of worker processes, and another is to terminate them once we are done.

[6]:


def create_worker_processes(n_workers, points_queue, obseverations_queue): observer_processes = [] for i in range(n_workers): worker_proc = Process( target=observer_proc, args=(points_queue, obseverations_queue) ) worker_proc.daemon = True worker_proc.start() observer_processes.append(worker_proc) return observer_processes def terminate_processes(processes): for prc in processes: prc.terminate() prc.join() prc.close()

Finally we set some common parameters. See comments below for explanation of what each one means.

[7]:
# Number of worker processes to run simultaneously
# Setting this to 1 will turn both setups into non-batch sequential optimization
num_workers = 3
# Number of iterations to run the sycnhronous scenario for
num_iterations = 10
# Number of observations to collect in the asynchronous scenario
num_observations = num_workers * num_iterations
# Set this flag to False to disable sleep delays in case you want the notebook to execute quickly
enable_sleep_delays = True

Asynchronous optimization#

This section runs the asynchronous optimization routine. We first setup the ask/tell optimizer as we cannot hand over the evaluation of the objective to Trieste. Next we create thread-safe queues for points and observations, and run the optimization loop.

Crucially, even though we are using batch acquisition function Local Penalization, we specify batch size of 1. This is because we don’t really want a batch. Since the amount of workers we have is fixed, whenever we see a new observation we only need one point back. However this process can only be done with acquisition functions that implement greedy batch collection strategies, because they are able to take into account points that are currently being observed (in Trieste we call them “pending”). Trieste currently provides two such functions: Local Penalization and GIBBON. Notice that we use AsynchronousGreedy rule specifically designed for using greedy batch acquisition functions in asynchronous scenarios.

[8]:

# setup Ask Tell BO local_penalization_acq = LocalPenalization(search_space, num_samples=2000) local_penalization_rule = AsynchronousGreedy(builder=local_penalization_acq) # type: ignore async_bo = AskTellOptimizer( search_space, initial_data, model, local_penalization_rule ) # retrieve process id for nice logging pid = os.getpid() # create point and observation queues m = Manager() pq = m.Queue() oq = m.Queue() # keep track of all workers we have launched observer_processes = [] # counter to keep track of collected observations points_observed = 0 start = timeit.default_timer() try: observer_processes = create_worker_processes(num_workers, pq, oq) # init the queue with first batch of points for _ in range(num_workers): point = async_bo.ask() pq.put(np.atleast_2d(point.numpy())) while points_observed < num_observations: # keep asking queue for new observations until one arrives try: new_data = oq.get_nowait() print( f"Process {pid}: Main : received data {new_data}", flush=True, ) except: continue # new_data is a tuple of (point, observation value) # here we turn it into a Dataset and tell of it Trieste points_observed += 1 new_data = Dataset( query_points=tf.constant(new_data[0], dtype=tf.float64), observations=tf.constant(new_data[1], dtype=tf.float64), ) async_bo.tell(new_data) # now we can ask Trieste for one more point # and feed that back into the points queue point = async_bo.ask() print(f"Process {pid}: Main : acquired point {point}", flush=True) pq.put(np.atleast_2d(point)) finally: terminate_processes(observer_processes) stop = timeit.default_timer() # Collect the observations, compute the running time async_lp_observations = ( async_bo.to_result().try_get_final_dataset().observations - SCALED_BRANIN_MINIMUM ) async_lp_time = stop - start print(f"Got {len(async_lp_observations)} observations in {async_lp_time:.2f}s")
Process 2734: Observer : observing data at point [[0.06706061 0.56467463]]
Process 2734: Objective: pretends like it's doing something for 1.9s
Process 2738: Observer : observing data at point [[0.11566917 0.47840984]]
Process 2738: Objective: pretends like it's doing something for 1.8s
Process 2742: Observer : observing data at point [[0.11392956 0.64826324]]
Process 2742: Objective: pretends like it's doing something for 2.3s
Process 2734: Observer : observed data (array([[0.06706061, 0.56467463]]), array([[-0.30329469]]))
Process 2696: Main     : received data (array([[0.06706061, 0.56467463]]), array([[-0.30329469]]))
Process 2696: Main     : acquired point [[0.13661612 0.56394595]]
Process 2734: Observer : observing data at point [[0.13661612 0.56394595]]
Process 2734: Objective: pretends like it's doing something for 2.1s
Process 2738: Observer : observed data (array([[0.11566917, 0.47840984]]), array([[-0.48524013]]))
Process 2696: Main     : received data (array([[0.11566917, 0.47840984]]), array([[-0.48524013]]))
Process 2742: Observer : observed data (array([[0.11392956, 0.64826324]]), array([[-0.88197697]]))
Process 2696: Main     : acquired point [[0.13193324 0.58566691]]
Process 2738: Observer : observing data at point [[0.13193324 0.58566691]]Process 2696: Main     : received data (array([[0.11392956, 0.64826324]]), array([[-0.88197697]]))

Process 2738: Objective: pretends like it's doing something for 2.2s
Process 2734: Observer : observed data (array([[0.13661612, 0.56394595]]), array([[-0.8264767]]))
Process 2696: Main     : acquired point [[0.12677438 0.64863016]]
Process 2742: Observer : observing data at point [[0.12677438 0.64863016]]Process 2696: Main     : received data (array([[0.13661612, 0.56394595]]), array([[-0.8264767]]))

Process 2742: Objective: pretends like it's doing something for 2.3s
Process 2738: Observer : observed data (array([[0.13193324, 0.58566691]]), array([[-0.84868446]]))
Process 2696: Main     : acquired point [[0.13501233 0.63863542]]
Process 2696: Main     : received data (array([[0.13193324, 0.58566691]]), array([[-0.84868446]]))
Process 2734: Observer : observing data at point [[0.13501233 0.63863542]]
Process 2734: Objective: pretends like it's doing something for 2.3s
Process 2696: Main     : acquired point [[0.12941696 0.67565144]]
Process 2738: Observer : observing data at point [[0.12941696 0.67565144]]
Process 2738: Objective: pretends like it's doing something for 2.4s
Process 2742: Observer : observed data (array([[0.12677438, 0.64863016]]), array([[-0.93243573]]))
Process 2696: Main     : received data (array([[0.12677438, 0.64863016]]), array([[-0.93243573]]))
Process 2696: Main     : acquired point [[0.13171892 0.71520536]]
Process 2742: Observer : observing data at point [[0.13171892 0.71520536]]
Process 2742: Objective: pretends like it's doing something for 2.5s
Process 2734: Observer : observed data (array([[0.13501233, 0.63863542]]), array([[-0.94315503]]))
Process 2696: Main     : received data (array([[0.13501233, 0.63863542]]), array([[-0.94315503]]))
Process 2696: Main     : acquired point [[0.14912095 0.69517906]]
Process 2734: Observer : observing data at point [[0.14912095 0.69517906]]
Process 2734: Objective: pretends like it's doing something for 2.5s
Process 2738: Observer : observed data (array([[0.12941696, 0.67565144]]), array([[-0.97416287]]))
Process 2696: Main     : received data (array([[0.12941696, 0.67565144]]), array([[-0.97416287]]))
Process 2696: Main     : acquired point [[0.14646697 0.7442742 ]]
Process 2738: Observer : observing data at point [[0.14646697 0.7442742 ]]
Process 2738: Objective: pretends like it's doing something for 2.7s
Process 2742: Observer : observed data (array([[0.13171892, 0.71520536]]), array([[-1.0152402]]))
Process 2696: Main     : received data (array([[0.13171892, 0.71520536]]), array([[-1.0152402]]))
Process 2696: Main     : acquired point [[0.15525416 0.77950236]]
Process 2742: Observer : observing data at point [[0.15525416 0.77950236]]
Process 2734: Observer : observed data (array([[0.14912095, 0.69517906]]), array([[-1.01671002]]))Process 2742: Objective: pretends like it's doing something for 2.8s
Process 2696: Main     : received data (array([[0.14912095, 0.69517906]]), array([[-1.01671002]]))

Process 2696: Main     : acquired point [[0.14137269 0.80641843]]
Process 2734: Observer : observing data at point [[0.14137269 0.80641843]]
Process 2734: Objective: pretends like it's doing something for 2.8s
Process 2738: Observer : observed data (array([[0.14646697, 0.7442742 ]]), array([[-1.0350259]]))
Process 2696: Main     : received data (array([[0.14646697, 0.7442742 ]]), array([[-1.0350259]]))
Process 2696: Main     : acquired point [[0.12292047 0.9054774 ]]
Process 2738: Observer : observing data at point [[0.12292047 0.9054774 ]]
Process 2738: Objective: pretends like it's doing something for 3.1s
Process 2742: Observer : observed data (array([[0.15525416, 0.77950236]]), array([[-1.02212421]]))
Process 2696: Main     : received data (array([[0.15525416, 0.77950236]]), array([[-1.02212421]]))
Process 2734: Observer : observed data (array([[0.14137269, 0.80641843]]), array([[-1.0373086]]))
Process 2696: Main     : acquired point [[0.11541163 0.93566317]]
Process 2696: Main     : received data (array([[0.14137269, 0.80641843]]), array([[-1.0373086]]))
Process 2742: Observer : observing data at point [[0.11541163 0.93566317]]Process 2742: Objective: pretends like it's doing something for 3.2s

Process 2696: Main     : acquired point [[0.10104099 1.        ]]
Process 2734: Observer : observing data at point [[0.10104099 1.        ]]
Process 2734: Objective: pretends like it's doing something for 3.3s
Process 2738: Observer : observed data (array([[0.12292047, 0.9054774 ]]), array([[-1.01622693]]))
Process 2696: Main     : received data (array([[0.12292047, 0.9054774 ]]), array([[-1.01622693]]))
Process 2696: Main     : acquired point [[0.12938598 0.80391864]]
Process 2738: Observer : observing data at point [[0.12938598 0.80391864]]
Process 2738: Objective: pretends like it's doing something for 2.8s
Process 2742: Observer : observed data (array([[0.11541163, 0.93566317]]), array([[-1.00531175]]))
Process 2696: Main     : received data (array([[0.11541163, 0.93566317]]), array([[-1.00531175]]))
Process 2696: Main     : acquired point [[0.13208078 0.79499707]]
Process 2742: Observer : observing data at point [[0.13208078 0.79499707]]
Process 2742: Objective: pretends like it's doing something for 2.8s
Process 2734: Observer : observed data (array([[0.10104099, 1.        ]]), array([[-0.96817007]]))
Process 2696: Main     : received data (array([[0.10104099, 1.        ]]), array([[-0.96817007]]))
Process 2696: Main     : acquired point [[0.13265838 0.79328786]]
Process 2734: Observer : observing data at point [[0.13265838 0.79328786]]
Process 2734: Objective: pretends like it's doing something for 2.8s
Process 2738: Observer : observed data (array([[0.12938598, 0.80391864]]), array([[-1.04675999]]))
Process 2696: Main     : received data (array([[0.12938598, 0.80391864]]), array([[-1.04675999]]))
Process 2696: Main     : acquired point [[0.63717385 0.30069592]]
Process 2738: Observer : observing data at point [[0.63717385 0.30069592]]
Process 2738: Objective: pretends like it's doing something for 2.8s
Process 2742: Observer : observed data (array([[0.13208078, 0.79499707]]), array([[-1.04593967]]))
Process 2696: Main     : received data (array([[0.13208078, 0.79499707]]), array([[-1.04593967]]))
Process 2696: Main     : acquired point [[0.62694348 0.14872651]]
Process 2742: Observer : observing data at point [[0.62694348 0.14872651]]
Process 2742: Objective: pretends like it's doing something for 2.3s
Process 2734: Observer : observed data (array([[0.13265838, 0.79328786]]), array([[-1.04572495]]))
Process 2696: Main     : received data (array([[0.13265838, 0.79328786]]), array([[-1.04572495]]))
Process 2738: Observer : observed data (array([[0.63717385, 0.30069592]]), array([[-0.70836508]]))
Process 2696: Main     : acquired point [[0.64579568 0.45345688]]
Process 2734: Observer : observing data at point [[0.64579568 0.45345688]]Process 2696: Main     : received data (array([[0.63717385, 0.30069592]]), array([[-0.70836508]]))

Process 2734: Objective: pretends like it's doing something for 3.3s
Process 2742: Observer : observed data (array([[0.62694348, 0.14872651]]), array([[-0.90824752]]))
Process 2696: Main     : acquired point [[0.59294811 0.94287514]]
Process 2696: Main     : received data (array([[0.62694348, 0.14872651]]), array([[-0.90824752]]))
Process 2738: Observer : observing data at point [[0.59294811 0.94287514]]
Process 2738: Objective: pretends like it's doing something for 4.6s
Process 2696: Main     : acquired point [[0.55769751 0.        ]]
Process 2742: Observer : observing data at point [[0.55769751 0.        ]]
Process 2742: Objective: pretends like it's doing something for 1.7s
Process 2734: Observer : observed data (array([[0.64579568, 0.45345688]]), array([[-0.30105944]]))
Process 2696: Main     : received data (array([[0.64579568, 0.45345688]]), array([[-0.30105944]]))
Process 2742: Observer : observed data (array([[0.55769751, 0.        ]]), array([[-0.95733442]]))
Process 2696: Main     : acquired point [[0.52359827 0.05997723]]
Process 2734: Observer : observing data at point [[0.52359827 0.05997723]]Process 2696: Main     : received data (array([[0.55769751, 0.        ]]), array([[-0.95733442]]))

Process 2734: Objective: pretends like it's doing something for 1.8s
Process 2696: Main     : acquired point [[0.45281818 0.03288724]]
Process 2742: Observer : observing data at point [[0.45281818 0.03288724]]
Process 2742: Objective: pretends like it's doing something for 1.5s
Process 2738: Observer : observed data (array([[0.59294811, 0.94287514]]), array([[1.9536631]]))
Process 2696: Main     : received data (array([[0.59294811, 0.94287514]]), array([[1.9536631]]))
Process 2734: Observer : observed data (array([[0.52359827, 0.05997723]]), array([[-0.98988479]]))
Process 2696: Main     : acquired point [[0.46587895 0.18051837]]
Process 2696: Main     : received data (array([[0.52359827, 0.05997723]]), array([[-0.98988479]]))
Process 2738: Observer : observing data at point [[0.46587895 0.18051837]]
Process 2738: Objective: pretends like it's doing something for 1.9s
Process 2742: Observer : observed data (array([[0.45281818, 0.03288724]]), array([[-0.72183786]]))
Process 2696: Main     : acquired point [[0.42987361 0.20255295]]
Process 2734: Observer : observing data at point [[0.42987361 0.20255295]]Process 2696: Main     : received data (array([[0.45281818, 0.03288724]]), array([[-0.72183786]]))

Process 2734: Objective: pretends like it's doing something for 1.9s
Process 2738: Observer : observed data (array([[0.46587895, 0.18051837]]), array([[-0.92963737]]))
Process 2696: Main     : acquired point [[0.5409136  0.18603423]]
Process 2696: Main     : received data (array([[0.46587895, 0.18051837]]), array([[-0.92963737]]))
Process 2742: Observer : observing data at point [[0.5409136  0.18603423]]
Process 2742: Objective: pretends like it's doing something for 2.2s
Process 2734: Observer : observed data (array([[0.42987361, 0.20255295]]), array([[-0.82336659]]))
Process 2696: Main     : acquired point [[0.54557969 0.17544402]]
Process 2738: Observer : observing data at point [[0.54557969 0.17544402]]Process 2696: Main     : received data (array([[0.42987361, 0.20255295]]), array([[-0.82336659]]))

Process 2738: Objective: pretends like it's doing something for 2.2s
Process 2742: Observer : observed data (array([[0.5409136 , 0.18603423]]), array([[-1.04263089]]))
Process 2696: Main     : acquired point [[0.54689274 0.16601434]]
Process 2696: Main     : received data (array([[0.5409136 , 0.18603423]]), array([[-1.04263089]]))
Process 2734: Observer : observing data at point [[0.54689274 0.16601434]]

Process 2734: Objective: pretends like it's doing something for 2.1sProcess 2738: Observer : observed data (array([[0.54557969, 0.17544402]]), array([[-1.0443133]]))
Process 2696: Main     : acquired point [[0.93940537 0.        ]]
Process 2742: Observer : observing data at point [[0.93940537 0.        ]]Process 2696: Main     : received data (array([[0.54557969, 0.17544402]]), array([[-1.0443133]]))

Process 2742: Objective: pretends like it's doing something for 2.8s
Process 2734: Observer : observed data (array([[0.54689274, 0.16601434]]), array([[-1.04571046]]))Process 2696: Main     : acquired point [[1.        0.1959442]]

Got 33 observations in 42.99s

Synchronous parallel optimization#

This section runs the synchronous parallel optimization with Trieste. We again use Local Penalization acquisition function, but this time with batch size equal to the number of workers we have available. Once Trieste suggests the batch, we add all points to the point queue, and workers immediatelly pick them up, one point per worker. Therefore all points in the batch are evaluated in parallel.

[9]:
# setup Ask Tell BO
gpflow_model = build_gpr(initial_data, search_space, likelihood_variance=1e-7)
model = GaussianProcessRegression(gpflow_model)

local_penalization_acq = LocalPenalization(search_space, num_samples=2000)
local_penalization_rule = EfficientGlobalOptimization(  # type: ignore
    num_query_points=num_workers, builder=local_penalization_acq
)

sync_bo = AskTellOptimizer(
    search_space, initial_data, model, local_penalization_rule
)


# retrieve process id for nice logging
pid = os.getpid()
# create point and observation queues
m = Manager()
pq = m.Queue()
oq = m.Queue()
# keep track of all workers we have launched
observer_processes = []

start = timeit.default_timer()
try:
    observer_processes = create_worker_processes(num_workers, pq, oq)

    # BO loop starts here
    for i in range(num_iterations):
        print(f"Process {pid}: Main     : iteration {i} starts", flush=True)

        # get a batch of points from Trieste, send them to points queue
        # each worker picks up a point and processes it
        points = sync_bo.ask()
        for point in points.numpy():
            pq.put(point.reshape(1, -1))  # reshape is to make point a 2d array

        # now we wait for all workers to finish
        # we create an empty dataset and wait
        # until we collected as many observations in it
        # as there were points in the batch
        all_new_data = Dataset(
            tf.zeros((0, initial_data.query_points.shape[1]), tf.float64),
            tf.zeros((0, initial_data.observations.shape[1]), tf.float64),
        )
        while len(all_new_data) < num_workers:
            # this line blocks the process until new data is available in the queue
            new_data = oq.get()
            print(
                f"Process {pid}: Main     : received data {new_data}",
                flush=True,
            )

            new_data = Dataset(
                query_points=tf.constant(new_data[0], dtype=tf.float64),
                observations=tf.constant(new_data[1], dtype=tf.float64),
            )

            all_new_data = all_new_data + new_data

        # tell Trieste of new batch of observations
        sync_bo.tell(all_new_data)

finally:
    terminate_processes(observer_processes)
stop = timeit.default_timer()

# Collect the observations, compute the running time
sync_lp_observations = (
    sync_bo.to_result().try_get_final_dataset().observations
    - SCALED_BRANIN_MINIMUM
)
sync_lp_time = stop - start
print(f"Got {len(sync_lp_observations)} observations in {sync_lp_time:.2f}s")
Process 2696: Main     : iteration 0 starts
Process 3164: Observer : observing data at point [[0.06706029 0.56467536]]Process 3172: Observer : observing data at point [[0.11379725 0.64815497]]Process 3168: Observer : observing data at point [[0.11289741 0.48052178]]


Process 3172: Objective: pretends like it's doing something for 2.3sProcess 3168: Objective: pretends like it's doing something for 1.8s

Process 3164: Objective: pretends like it's doing something for 1.9s
Process 3168: Observer : observed data (array([[0.11289741, 0.48052178]]), array([[-0.46954139]]))
Process 2696: Main     : received data (array([[0.11289741, 0.48052178]]), array([[-0.46954139]]))
Process 3164: Observer : observed data (array([[0.06706029, 0.56467536]]), array([[-0.30329367]]))
Process 2696: Main     : received data (array([[0.06706029, 0.56467536]]), array([[-0.30329367]]))
Process 3172: Observer : observed data (array([[0.11379725, 0.64815497]]), array([[-0.88119548]]))
Process 2696: Main     : received data (array([[0.11379725, 0.64815497]]), array([[-0.88119548]]))
Process 2696: Main     : iteration 1 starts
Process 3172: Observer : observing data at point [[0.12757275 0.63309754]]Process 3164: Observer : observing data at point [[0.12731054 0.66189935]]Process 3168: Observer : observing data at point [[0.1272073  0.64685846]]

Process 3164: Objective: pretends like it's doing something for 2.4s
Process 3168: Objective: pretends like it's doing something for 2.3s
Process 3172: Objective: pretends like it's doing something for 2.3s

Process 3172: Observer : observed data (array([[0.12757275, 0.63309754]]), array([[-0.91231064]]))
Process 2696: Main     : received data (array([[0.12757275, 0.63309754]]), array([[-0.91231064]]))
Process 3168: Observer : observed data (array([[0.1272073 , 0.64685846]]), array([[-0.93133906]]))
Process 2696: Main     : received data (array([[0.1272073 , 0.64685846]]), array([[-0.93133906]]))
Process 3164: Observer : observed data (array([[0.12731054, 0.66189935]]), array([[-0.95196819]]))
Process 2696: Main     : received data (array([[0.12731054, 0.66189935]]), array([[-0.95196819]]))
Process 2696: Main     : iteration 2 starts
Process 3172: Observer : observing data at point [[0.1301285  0.73823284]]
Process 3168: Observer : observing data at point [[0.13018138 0.74782519]]
Process 3164: Observer : observing data at point [[0.13009631 0.72900693]]Process 3172: Objective: pretends like it's doing something for 2.6s
Process 3168: Objective: pretends like it's doing something for 2.6s

Process 3164: Objective: pretends like it's doing something for 2.6s
Process 3164: Observer : observed data (array([[0.13009631, 0.72900693]]), array([[-1.0225592]]))
Process 2696: Main     : received data (array([[0.13009631, 0.72900693]]), array([[-1.0225592]]))
Process 3172: Observer : observed data (array([[0.1301285 , 0.73823284]]), array([[-1.02817887]]))
Process 2696: Main     : received data (array([[0.1301285 , 0.73823284]]), array([[-1.02817887]]))
Process 3168: Observer : observed data (array([[0.13018138, 0.74782519]]), array([[-1.033244]]))
Process 2696: Main     : received data (array([[0.13018138, 0.74782519]]), array([[-1.033244]]))
Process 2696: Main     : iteration 3 starts
Process 3164: Observer : observing data at point [[0.14960483 0.8318749 ]]
Process 3168: Observer : observing data at point [[0.14897008 0.81015263]]Process 3172: Observer : observing data at point [[0.15045961 0.85482355]]Process 3164: Objective: pretends like it's doing something for 2.9s
Process 3168: Objective: pretends like it's doing something for 2.9s

Process 3172: Objective: pretends like it's doing something for 3.0s

Process 3168: Observer : observed data (array([[0.14897008, 0.81015263]]), array([[-1.02326619]]))
Process 2696: Main     : received data (array([[0.14897008, 0.81015263]]), array([[-1.02326619]]))
Process 3164: Observer : observed data (array([[0.14960483, 0.8318749 ]]), array([[-1.01006826]]))
Process 2696: Main     : received data (array([[0.14960483, 0.8318749 ]]), array([[-1.01006826]]))
Process 3172: Observer : observed data (array([[0.15045961, 0.85482355]]), array([[-0.99049166]]))
Process 2696: Main     : received data (array([[0.15045961, 0.85482355]]), array([[-0.99049166]]))
Process 2696: Main     : iteration 4 starts
Process 3164: Observer : observing data at point [[0.13564802 0.7860137 ]]Process 3172: Observer : observing data at point [[0.13577578 0.7826956 ]]Process 3168: Observer : observing data at point [[0.13571171 0.78435347]]


Process 3164: Objective: pretends like it's doing something for 2.8sProcess 3168: Objective: pretends like it's doing something for 2.8s
Process 3172: Objective: pretends like it's doing something for 2.8s

Process 3172: Observer : observed data (array([[0.13577578, 0.7826956 ]]), array([[-1.0442316]]))Process 3168: Observer : observed data (array([[0.13571171, 0.78435347]]), array([[-1.04434919]]))
Process 2696: Main     : received data (array([[0.13571171, 0.78435347]]), array([[-1.04434919]]))

Process 2696: Main     : received data (array([[0.13577578, 0.7826956 ]]), array([[-1.0442316]]))
Process 3164: Observer : observed data (array([[0.13564802, 0.7860137 ]]), array([[-1.04444688]]))
Process 2696: Main     : received data (array([[0.13564802, 0.7860137 ]]), array([[-1.04444688]]))
Process 2696: Main     : iteration 5 starts
Process 3172: Observer : observing data at point [[0.09817991 0.93724694]]Process 3164: Observer : observing data at point [[0.10455055 0.89793317]]Process 3168: Observer : observing data at point [[0.09021561 0.99406468]]


Process 3172: Objective: pretends like it's doing something for 3.1s
Process 3164: Objective: pretends like it's doing something for 3.0sProcess 3168: Objective: pretends like it's doing something for 3.3s

Process 3164: Observer : observed data (array([[0.10455055, 0.89793317]]), array([[-1.03512476]]))
Process 2696: Main     : received data (array([[0.10455055, 0.89793317]]), array([[-1.03512476]]))
Process 3172: Observer : observed data (array([[0.09817991, 0.93724694]]), array([[-1.02031185]]))
Process 2696: Main     : received data (array([[0.09817991, 0.93724694]]), array([[-1.02031185]]))
Process 3168: Observer : observed data (array([[0.09021561, 0.99406468]]), array([[-0.98717107]]))
Process 2696: Main     : received data (array([[0.09021561, 0.99406468]]), array([[-0.98717107]]))
Process 2696: Main     : iteration 6 starts
Process 3164: Observer : observing data at point [[0.61659857 0.34026532]]Process 3172: Observer : observing data at point [[0.61842084 0.48249711]]Process 3168: Observer : observing data at point [[0.60659837 0.19914125]]


Process 3172: Objective: pretends like it's doing something for 3.3sProcess 3168: Objective: pretends like it's doing something for 2.4sProcess 3164: Objective: pretends like it's doing something for 2.9s


Process 3168: Observer : observed data (array([[0.60659837, 0.19914125]]), array([[-0.93438064]]))
Process 2696: Main     : received data (array([[0.60659837, 0.19914125]]), array([[-0.93438064]]))
Process 3164: Observer : observed data (array([[0.61659857, 0.34026532]]), array([[-0.70475136]]))
Process 2696: Main     : received data (array([[0.61659857, 0.34026532]]), array([[-0.70475136]]))
Process 3172: Observer : observed data (array([[0.61842084, 0.48249711]]), array([[-0.31937565]]))
Process 2696: Main     : received data (array([[0.61842084, 0.48249711]]), array([[-0.31937565]]))
Process 2696: Main     : iteration 7 starts
Process 3172: Observer : observing data at point [[0.66165952 0.        ]]Process 3168: Observer : observing data at point [[0.53058045 0.03474143]]Process 3164: Observer : observing data at point [[0.53072807 0.09993336]]


Process 3168: Objective: pretends like it's doing something for 1.7s
Process 3164: Objective: pretends like it's doing something for 1.9sProcess 3172: Objective: pretends like it's doing something for 2.0s

Process 3168: Observer : observed data (array([[0.53058045, 0.03474143]]), array([[-0.97476118]]))
Process 2696: Main     : received data (array([[0.53058045, 0.03474143]]), array([[-0.97476118]]))
Process 3164: Observer : observed data (array([[0.53072807, 0.09993336]]), array([[-1.0280533]]))
Process 2696: Main     : received data (array([[0.53072807, 0.09993336]]), array([[-1.0280533]]))
Process 3172: Observer : observed data (array([[0.66165952, 0.        ]]), array([[-0.79129051]]))
Process 2696: Main     : received data (array([[0.66165952, 0.        ]]), array([[-0.79129051]]))
Process 2696: Main     : iteration 8 starts
Process 3168: Observer : observing data at point [[0.55124837 0.16600657]]Process 3164: Observer : observing data at point [[0.54996863 0.17773627]]
Process 3172: Observer : observing data at point [[0.55258902 0.15429995]]Process 3168: Objective: pretends like it's doing something for 2.2s

Process 3172: Objective: pretends like it's doing something for 2.1s
Process 3164: Objective: pretends like it's doing something for 2.2s

Process 3172: Observer : observed data (array([[0.55258902, 0.15429995]]), array([[-1.0449519]]))
Process 2696: Main     : received data (array([[0.55258902, 0.15429995]]), array([[-1.0449519]]))
Process 3168: Observer : observed data (array([[0.55124837, 0.16600657]]), array([[-1.04402651]]))
Process 2696: Main     : received data (array([[0.55124837, 0.16600657]]), array([[-1.04402651]]))
Process 3164: Observer : observed data (array([[0.54996863, 0.17773627]]), array([[-1.0419986]]))
Process 2696: Main     : received data (array([[0.54996863, 0.17773627]]), array([[-1.0419986]]))
Process 2696: Main     : iteration 9 starts
Process 3164: Observer : observing data at point [[0.42584584 0.08745112]]Process 3168: Observer : observing data at point [[0.46348299 0.19272818]]

Process 3172: Observer : observing data at point [[0.44616127 0.14405554]]Process 3168: Objective: pretends like it's doing something for 2.0s
Process 3164: Objective: pretends like it's doing something for 1.5s

Process 3172: Objective: pretends like it's doing something for 1.8s
Process 3164: Observer : observed data (array([[0.42584584, 0.08745112]]), array([[-0.68560661]]))
Process 2696: Main     : received data (array([[0.42584584, 0.08745112]]), array([[-0.68560661]]))
Process 3172: Observer : observed data (array([[0.44616127, 0.14405554]]), array([[-0.84076721]]))
Process 2696: Main     : received data (array([[0.44616127, 0.14405554]]), array([[-0.84076721]]))
Process 3168: Observer : observed data (array([[0.46348299, 0.19272818]]), array([[-0.92666338]]))
Process 2696: Main     : received data (array([[0.46348299, 0.19272818]]), array([[-0.92666338]]))
Got 33 observations in 42.36s

Comparison#

To compare outcomes of sync and async runs, let’s plot their respective regrets side by side, and print out the running time. For this toy problem we expect async scenario to run a little bit faster on machines with multiple CPU.

[10]:
from trieste.experimental.plotting import plot_regret
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 2)

sync_lp_min_idx = tf.squeeze(tf.argmin(sync_lp_observations, axis=0))
async_lp_min_idx = tf.squeeze(tf.argmin(async_lp_observations, axis=0))

plot_regret(
    sync_lp_observations.numpy(),
    ax[0],
    num_init=len(initial_data),
    idx_best=sync_lp_min_idx,
)
ax[0].set_yscale("log")
ax[0].set_ylabel("Regret")
ax[0].set_ylim(0.0000001, 100)
ax[0].set_xlabel("# evaluations")
ax[0].set_title(
    f"Sync LP, {len(sync_lp_observations)} points, time {sync_lp_time:.2f}"
)

plot_regret(
    async_lp_observations.numpy(),
    ax[1],
    num_init=len(initial_data),
    idx_best=async_lp_min_idx,
)
ax[1].set_yscale("log")
ax[1].set_ylabel("Regret")
ax[1].set_ylim(0.0000001, 100)
ax[1].set_xlabel("# evaluations")
ax[1].set_title(
    f"Async LP, {len(async_lp_observations)} points, time {async_lp_time:.2f}s"
)

fig.tight_layout()
../_images/notebooks_asynchronous_greedy_multiprocessing_19_0.png