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 ScaledBranin
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 = ScaledBranin.objective(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
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:
[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
- ScaledBranin.minimum
)
async_lp_time = stop - start
print(f"Got {len(async_lp_observations)} observations in {async_lp_time:.2f}s")
Process 2647: Observer : observing data at point [[0.78993122 0.15340073]]
Process 2647: Objective: pretends like it's doing something for 2.8s
Process 2651: Observer : observing data at point [[0.67026053 0.12983291]]
Process 2651: Objective: pretends like it's doing something for 2.4s
Process 2655: Observer : observing data at point [[0.72347321 0.23039925]]
Process 2655: Objective: pretends like it's doing something for 2.9s
Process 2647: Observer : observed data (array([[0.78993122, 0.15340073]]), array([[-0.68144047]]))
Process 2605: Main : received data (array([[0.78993122, 0.15340073]]), array([[-0.68144047]]))
Process 2651: Observer : observed data (array([[0.67026053, 0.12983291]]), array([[-0.79145383]]))
Process 2655: Observer : observed data (array([[0.72347321, 0.23039925]]), array([[-0.5887207]]))
Process 2605: Main : acquired point [[0.82779125 0.08978007]]
Process 2605: Main : received data (array([[0.67026053, 0.12983291]]), array([[-0.79145383]]))
Process 2647: Observer : observing data at point [[0.82779125 0.08978007]]
Process 2647: Objective: pretends like it's doing something for 2.8s
Process 2605: Main : acquired point [[0.54686271 0.15726595]]
Process 2605: Main : received data (array([[0.72347321, 0.23039925]]), array([[-0.5887207]]))
Process 2651: Observer : observing data at point [[0.54686271 0.15726595]]
Process 2651: Objective: pretends like it's doing something for 2.1s
Process 2647: Observer : observed data (array([[0.82779125, 0.08978007]]), array([[-0.78427704]]))
Process 2651: Observer : observed data (array([[0.54686271, 0.15726595]]), array([[-1.0467141]]))
Process 2605: Main : acquired point [[0.61761484 0.01086754]]
Process 2655: Observer : observing data at point [[0.61761484 0.01086754]]Process 2605: Main : received data (array([[0.82779125, 0.08978007]]), array([[-0.78427704]]))
Process 2655: Objective: pretends like it's doing something for 1.9s
Process 2655: Observer : observed data (array([[0.61761484, 0.01086754]]), array([[-0.90496003]]))
Process 2605: Main : acquired point [[0.96747811 0.07470409]]
Process 2647: Observer : observing data at point [[0.96747811 0.07470409]]Process 2605: Main : received data (array([[0.54686271, 0.15726595]]), array([[-1.0467141]]))
Process 2647: Objective: pretends like it's doing something for 3.1s
Process 2605: Main : acquired point [[0.52865565 0. ]]
Process 2651: Observer : observing data at point [[0.52865565 0. ]]Process 2605: Main : received data (array([[0.61761484, 0.01086754]]), array([[-0.90496003]]))
Process 2651: Objective: pretends like it's doing something for 1.6s
Process 2647: Observer : observed data (array([[0.96747811, 0.07470409]]), array([[-1.00737277]]))
Process 2651: Observer : observed data (array([[0.52865565, 0. ]]), array([[-0.92810464]]))
Process 2605: Main : acquired point [[0.4977228 0.37968662]]
Process 2605: Main : received data (array([[0.96747811, 0.07470409]]), array([[-1.00737277]]))
Process 2655: Observer : observing data at point [[0.4977228 0.37968662]]
Process 2655: Objective: pretends like it's doing something for 2.6s
Process 2605: Main : acquired point [[1. 0.]]
Process 2605: Main : received data (array([[0.52865565, 0. ]]), array([[-0.92810464]]))
Process 2647: Observer : observing data at point [[1. 0.]]
Process 2647: Objective: pretends like it's doing something for 3.0s
Process 2655: Observer : observed data (array([[0.4977228 , 0.37968662]]), array([[-0.85213801]]))
Process 2605: Main : acquired point [[0.96938371 0. ]]
Process 2651: Observer : observing data at point [[0.96938371 0. ]]
Process 2605: Main : received data (array([[0.4977228 , 0.37968662]]), array([[-0.85213801]]))
Process 2651: Objective: pretends like it's doing something for 2.9sProcess 2647: Observer : observed data (array([[1., 0.]]), array([[-0.84406373]]))
Process 2605: Main : acquired point [[1. 0.02095792]]
Process 2655: Observer : observing data at point [[1. 0.02095792]]Process 2605: Main : received data (array([[1., 0.]]), array([[-0.84406373]]))
Process 2655: Objective: pretends like it's doing something for 3.1s
Process 2651: Observer : observed data (array([[0.96938371, 0. ]]), array([[-0.91856035]]))
Process 2605: Main : acquired point [[1. 0.19810119]]
Process 2605: Main : received data (array([[0.96938371, 0. ]]), array([[-0.91856035]]))
Process 2647: Observer : observing data at point [[1. 0.19810119]]
Process 2647: Objective: pretends like it's doing something for 3.6s
Process 2655: Observer : observed data (array([[1. , 0.02095792]]), array([[-0.87850539]]))
Process 2605: Main : acquired point [[0.49930382 0.1866217 ]]
Process 2605: Main : received data (array([[1. , 0.02095792]]), array([[-0.87850539]]))
Process 2651: Observer : observing data at point [[0.49930382 0.1866217 ]]
Process 2651: Objective: pretends like it's doing something for 2.1s
Process 2647: Observer : observed data (array([[1. , 0.19810119]]), array([[-1.01762985]]))
Process 2651: Observer : observed data (array([[0.49930382, 0.1866217 ]]), array([[-1.0094459]]))
Process 2605: Main : acquired point [[0.50779603 0.16453954]]
Process 2655: Observer : observing data at point [[0.50779603 0.16453954]]Process 2605: Main : received data (array([[1. , 0.19810119]]), array([[-1.01762985]]))
Process 2655: Objective: pretends like it's doing something for 2.0s
Process 2655: Observer : observed data (array([[0.50779603, 0.16453954]]), array([[-1.02131469]]))
Process 2605: Main : acquired point [[0.49842575 0.20838576]]
Process 2647: Observer : observing data at point [[0.49842575 0.20838576]]Process 2605: Main : received data (array([[0.49930382, 0.1866217 ]]), array([[-1.0094459]]))
Process 2647: Objective: pretends like it's doing something for 2.1s
Process 2647: Observer : observed data (array([[0.49842575, 0.20838576]]), array([[-1.00653119]]))
Process 2605: Main : acquired point [[0.1988589 1. ]]
Process 2605: Main : received data (array([[0.50779603, 0.16453954]]), array([[-1.02131469]]))
Process 2651: Observer : observing data at point [[0.1988589 1. ]]
Process 2651: Objective: pretends like it's doing something for 3.6s
Process 2605: Main : acquired point [[0.95967783 0.17275631]]
Process 2605: Main : received data (array([[0.49842575, 0.20838576]]), array([[-1.00653119]]))
Process 2655: Observer : observing data at point [[0.95967783 0.17275631]]
Process 2655: Objective: pretends like it's doing something for 3.4s
Process 2651: Observer : observed data (array([[0.1988589, 1. ]]), array([[-0.40894393]]))
Process 2605: Main : acquired point [[0.95978528 0.165054 ]]
Process 2605: Main : received data (array([[0.1988589, 1. ]]), array([[-0.40894393]]))
Process 2647: Observer : observing data at point [[0.95978528 0.165054 ]]
Process 2647: Objective: pretends like it's doing something for 3.4s
Process 2655: Observer : observed data (array([[0.95967783, 0.17275631]]), array([[-1.04692904]]))
Process 2605: Main : acquired point [[0. 1.]]
Process 2605: Main : received data (array([[0.95967783, 0.17275631]]), array([[-1.04692904]]))
Process 2651: Observer : observing data at point [[0. 1.]]
Process 2651: Objective: pretends like it's doing something for 3.0sProcess 2647: Observer : observed data (array([[0.95978528, 0.165054 ]]), array([[-1.04731006]]))
Process 2605: Main : acquired point [[0. 0.77710498]]
Process 2605: Main : received data (array([[0.95978528, 0.165054 ]]), array([[-1.04731006]]))
Process 2655: Observer : observing data at point [[0. 0.77710498]]
Process 2655: Objective: pretends like it's doing something for 2.3s
Process 2651: Observer : observed data (array([[0., 1.]]), array([[-0.71803081]]))
Process 2605: Main : acquired point [[0.39595756 1. ]]
Process 2647: Observer : observing data at point [[0.39595756 1. ]]
Process 2647: Objective: pretends like it's doing something for 4.2s
Process 2605: Main : received data (array([[0., 1.]]), array([[-0.71803081]]))
Process 2655: Observer : observed data (array([[0. , 0.77710498]]), array([[-0.22130238]]))
Process 2605: Main : acquired point [[0. 0.63031618]]
Process 2605: Main : received data (array([[0. , 0.77710498]]), array([[-0.22130238]]))
Process 2651: Observer : observing data at point [[0. 0.63031618]]
Process 2651: Objective: pretends like it's doing something for 1.9s
Process 2651: Observer : observed data (array([[0. , 0.63031618]]), array([[0.34084937]]))
Process 2647: Observer : observed data (array([[0.39595756, 1. ]]), array([[1.32096929]]))
Process 2605: Main : acquired point [[0.54042023 0.1893328 ]]
Process 2605: Main : received data (array([[0. , 0.63031618]]), array([[0.34084937]]))
Process 2655: Observer : observing data at point [[0.54042023 0.1893328 ]]
Process 2655: Objective: pretends like it's doing something for 2.2s
Process 2655: Observer : observed data (array([[0.54042023, 0.1893328 ]]), array([[-1.0417215]]))
Process 2605: Main : acquired point [[0.54062766 0.19324126]]
Process 2605: Main : received data (array([[0.39595756, 1. ]]), array([[1.32096929]]))
Process 2651: Observer : observing data at point [[0.54062766 0.19324126]]
Process 2651: Objective: pretends like it's doing something for 2.2s
Process 2651: Observer : observed data (array([[0.54062766, 0.19324126]]), array([[-1.04040559]]))
Process 2605: Main : acquired point [[0.54235692 0.19856402]]
Process 2605: Main : received data (array([[0.54042023, 0.1893328 ]]), array([[-1.0417215]]))
Process 2647: Observer : observing data at point [[0.54235692 0.19856402]]
Process 2647: Objective: pretends like it's doing something for 2.2s
Process 2605: Main : acquired point [[0.96900164 0.16887504]]
Process 2605: Main : received data (array([[0.54062766, 0.19324126]]), array([[-1.04040559]]))
Process 2655: Observer : observing data at point [[0.96900164 0.16887504]]
Process 2655: Objective: pretends like it's doing something for 3.4s
Process 2647: Observer : observed data (array([[0.54235692, 0.19856402]]), array([[-1.03799611]]))
Process 2605: Main : acquired point [[0.96877555 0.16778675]]
Process 2605: Main : received data (array([[0.54235692, 0.19856402]]), array([[-1.03799611]]))
Process 2651: Observer : observing data at point [[0.96877555 0.16778675]]
Process 2651: Objective: pretends like it's doing something for 3.4s
Process 2655: Observer : observed data (array([[0.96900164, 0.16887504]]), array([[-1.0462462]]))
Process 2605: Main : acquired point [[0.9688913 0.16997693]]
Process 2605: Main : received data (array([[0.96900164, 0.16887504]]), array([[-1.0462462]]))
Process 2647: Observer : observing data at point [[0.9688913 0.16997693]]
Process 2647: Objective: pretends like it's doing something for 3.4s
Process 2651: Observer : observed data (array([[0.96877555, 0.16778675]]), array([[-1.04629192]]))
Process 2605: Main : acquired point [[0.54166328 0.14834641]]
Process 2605: Main : received data (array([[0.96877555, 0.16778675]]), array([[-1.04629192]]))
Process 2655: Observer : observing data at point [[0.54166328 0.14834641]]
Process 2655: Objective: pretends like it's doing something for 2.1s
Process 2647: Observer : observed data (array([[0.9688913 , 0.16997693]]), array([[-1.0462986]]))
Process 2655: Observer : observed data (array([[0.54166328, 0.14834641]]), array([[-1.04729233]]))
Process 2605: Main : acquired point [[0.28837694 0.53143102]]
Process 2651: Observer : observing data at point [[0.28837694 0.53143102]]Process 2605: Main : received data (array([[0.9688913 , 0.16997693]]), array([[-1.0462986]]))
Process 2651: Objective: pretends like it's doing something for 2.5s
Process 2605: Main : acquired point [[0.23476887 0.64160768]]
Process 2605: Main : received data (array([[0.54166328, 0.14834641]]), array([[-1.04729233]]))
Process 2647: Observer : observing data at point [[0.23476887 0.64160768]]
Process 2647: Objective: pretends like it's doing something for 2.6s
Process 2651: Observer : observed data (array([[0.28837694, 0.53143102]]), array([[-0.70461866]]))
Process 2605: Main : acquired point [[0.96062286 0.16052281]]
Process 2655: Observer : observing data at point [[0.96062286 0.16052281]]
Process 2655: Objective: pretends like it's doing something for 3.4s
Got 33 observations in 76.58s
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
- ScaledBranin.minimum
)
sync_lp_time = stop - start
print(f"Got {len(sync_lp_observations)} observations in {sync_lp_time:.2f}s")
Process 2605: Main : iteration 0 starts
Process 3102: Observer : observing data at point [[0.79031955 0.15117187]]Process 3110: Observer : observing data at point [[0.66980635 0.13244062]]Process 3106: Observer : observing data at point [[0.754109 0.05611623]]
Process 3102: Objective: pretends like it's doing something for 2.8s
Process 3110: Objective: pretends like it's doing something for 2.4s
Process 3106: Objective: pretends like it's doing something for 2.4s
Process 3110: Observer : observed data (array([[0.66980635, 0.13244062]]), array([[-0.79162594]]))
Process 2605: Main : received data (array([[0.66980635, 0.13244062]]), array([[-0.79162594]]))
Process 3106: Observer : observed data (array([[0.754109 , 0.05611623]]), array([[-0.67650729]]))
Process 2605: Main : received data (array([[0.754109 , 0.05611623]]), array([[-0.67650729]]))
Process 3102: Observer : observed data (array([[0.79031955, 0.15117187]]), array([[-0.68351261]]))Process 2605: Main : received data (array([[0.79031955, 0.15117187]]), array([[-0.68351261]]))
Process 2605: Main : iteration 1 starts
Process 3110: Observer : observing data at point [[0.5956831 0.057747 ]]
Process 3106: Observer : observing data at point [[0.55725865 0.18569521]]Process 3102: Observer : observing data at point [[0.94685128 0.09428014]]
Process 3102: Objective: pretends like it's doing something for 3.1s
Process 3110: Objective: pretends like it's doing something for 2.0s
Process 3106: Objective: pretends like it's doing something for 2.2s
Process 3110: Observer : observed data (array([[0.5956831, 0.057747 ]]), array([[-0.97756296]]))
Process 2605: Main : received data (array([[0.5956831, 0.057747 ]]), array([[-0.97756296]]))
Process 3106: Observer : observed data (array([[0.55725865, 0.18569521]]), array([[-1.03430885]]))
Process 2605: Main : received data (array([[0.55725865, 0.18569521]]), array([[-1.03430885]]))
Process 3102: Observer : observed data (array([[0.94685128, 0.09428014]]), array([[-1.02795442]]))
Process 2605: Main : received data (array([[0.94685128, 0.09428014]]), array([[-1.02795442]]))
Process 2605: Main : iteration 2 starts
Process 3110: Observer : observing data at point [[1. 0.]]Process 3102: Observer : observing data at point [[1. 0.05920748]]Process 3106: Observer : observing data at point [[1. 0.19297825]]
Process 3102: Objective: pretends like it's doing something for 3.2s
Process 3110: Objective: pretends like it's doing something for 3.0s
Process 3106: Objective: pretends like it's doing something for 3.6s
Process 3110: Observer : observed data (array([[1., 0.]]), array([[-0.84406373]]))
Process 2605: Main : received data (array([[1., 0.]]), array([[-0.84406373]]))
Process 3102: Observer : observed data (array([[1. , 0.05920748]]), array([[-0.93155519]]))
Process 2605: Main : received data (array([[1. , 0.05920748]]), array([[-0.93155519]]))
Process 3106: Observer : observed data (array([[1. , 0.19297825]]), array([[-1.01742318]]))
Process 2605: Main : received data (array([[1. , 0.19297825]]), array([[-1.01742318]]))
Process 2605: Main : iteration 3 starts
Process 3110: Observer : observing data at point [[0.52592051 0.11712988]]Process 3102: Observer : observing data at point [[0.52036205 0.09093312]]
Process 3106: Observer : observing data at point [[0.51988438 0.14221711]]
Process 3102: Objective: pretends like it's doing something for 1.8s
Process 3110: Objective: pretends like it's doing something for 1.9s
Process 3106: Objective: pretends like it's doing something for 2.0s
Process 3102: Observer : observed data (array([[0.52036205, 0.09093312]]), array([[-1.00989192]]))
Process 2605: Main : received data (array([[0.52036205, 0.09093312]]), array([[-1.00989192]]))
Process 3110: Observer : observed data (array([[0.52592051, 0.11712988]]), array([[-1.0314453]]))
Process 2605: Main : received data (array([[0.52592051, 0.11712988]]), array([[-1.0314453]]))
Process 3106: Observer : observed data (array([[0.51988438, 0.14221711]]), array([[-1.03313471]]))
Process 2605: Main : received data (array([[0.51988438, 0.14221711]]), array([[-1.03313471]]))
Process 2605: Main : iteration 4 starts
Process 3102: Observer : observing data at point [[0.95627899 0.16759949]]Process 3110: Observer : observing data at point [[0.9562519 0.17744637]]
Process 3106: Observer : observing data at point [[0.95585124 0.15791774]]
Process 3102: Objective: pretends like it's doing something for 3.4sProcess 3110: Objective: pretends like it's doing something for 3.4s
Process 3106: Objective: pretends like it's doing something for 3.3s
Process 3106: Observer : observed data (array([[0.95585124, 0.15791774]]), array([[-1.04667267]]))
Process 2605: Main : received data (array([[0.95585124, 0.15791774]]), array([[-1.04667267]]))
Process 3102: Observer : observed data (array([[0.95627899, 0.16759949]]), array([[-1.04657711]]))
Process 2605: Main : received data (array([[0.95627899, 0.16759949]]), array([[-1.04657711]]))
Process 3110: Observer : observed data (array([[0.9562519 , 0.17744637]]), array([[-1.04554431]]))
Process 2605: Main : received data (array([[0.9562519 , 0.17744637]]), array([[-1.04554431]]))
Process 2605: Main : iteration 5 starts
Process 3106: Observer : observing data at point [[0.27645859 1. ]]
Process 3102: Observer : observing data at point [[0.42290808 0.60944589]]Process 3110: Observer : observing data at point [[0.4742754 0.42808493]]
Process 3106: Objective: pretends like it's doing something for 3.8s
Process 3110: Objective: pretends like it's doing something for 2.7sProcess 3102: Objective: pretends like it's doing something for 3.1s
Process 3110: Observer : observed data (array([[0.4742754 , 0.42808493]]), array([[-0.75994181]]))
Process 2605: Main : received data (array([[0.4742754 , 0.42808493]]), array([[-0.75994181]]))
Process 3102: Observer : observed data (array([[0.42290808, 0.60944589]]), array([[-0.33062686]]))
Process 2605: Main : received data (array([[0.42290808, 0.60944589]]), array([[-0.33062686]]))
Process 3106: Observer : observed data (array([[0.27645859, 1. ]]), array([[0.35572483]]))
Process 2605: Main : received data (array([[0.27645859, 1. ]]), array([[0.35572483]]))
Process 2605: Main : iteration 6 starts
Process 3102: Observer : observing data at point [[0.54187714 0.15464626]]Process 3110: Observer : observing data at point [[0.54220727 0.15342066]]
Process 3106: Observer : observing data at point [[0.54254296 0.15219818]]
Process 3102: Objective: pretends like it's doing something for 2.1s
Process 3110: Objective: pretends like it's doing something for 2.1s
Process 3106: Objective: pretends like it's doing something for 2.1s
Process 3102: Observer : observed data (array([[0.54187714, 0.15464626]]), array([[-1.0473547]]))Process 3110: Observer : observed data (array([[0.54220727, 0.15342066]]), array([[-1.04737978]]))
Process 3106: Observer : observed data (array([[0.54254296, 0.15219818]]), array([[-1.04739226]]))
Process 2605: Main : received data (array([[0.54220727, 0.15342066]]), array([[-1.04737978]]))
Process 2605: Main : received data (array([[0.54187714, 0.15464626]]), array([[-1.0473547]]))
Process 2605: Main : received data (array([[0.54254296, 0.15219818]]), array([[-1.04739226]]))
Process 2605: Main : iteration 7 starts
Process 3110: Observer : observing data at point [[0.96633176 0.16799197]]
Process 3106: Observer : observing data at point [[0.96660439 0.16953375]]Process 3102: Observer : observing data at point [[0.96607958 0.16645026]]Process 3110: Objective: pretends like it's doing something for 3.4s
Process 3102: Objective: pretends like it's doing something for 3.4sProcess 3106: Objective: pretends like it's doing something for 3.4s
Process 3110: Observer : observed data (array([[0.96633176, 0.16799197]]), array([[-1.04693435]]))
Process 2605: Main : received data (array([[0.96633176, 0.16799197]]), array([[-1.04693435]]))
Process 3102: Observer : observed data (array([[0.96607958, 0.16645026]]), array([[-1.04696302]]))
Process 2605: Main : received data (array([[0.96607958, 0.16645026]]), array([[-1.04696302]]))
Process 3106: Observer : observed data (array([[0.96660439, 0.16953375]]), array([[-1.04688369]]))
Process 2605: Main : received data (array([[0.96660439, 0.16953375]]), array([[-1.04688369]]))
Process 2605: Main : iteration 8 starts
Process 3102: Observer : observing data at point [[0.54793227 0.1423453 ]]Process 3106: Observer : observing data at point [[0.54820492 0.14154297]]
Process 3110: Observer : observing data at point [[0. 1.]]
Process 3106: Objective: pretends like it's doing something for 2.1s
Process 3110: Objective: pretends like it's doing something for 3.0s
Process 3102: Objective: pretends like it's doing something for 2.1s
Process 3106: Observer : observed data (array([[0.54820492, 0.14154297]]), array([[-1.0466276]]))
Process 2605: Main : received data (array([[0.54820492, 0.14154297]]), array([[-1.0466276]]))
Process 3102: Observer : observed data (array([[0.54793227, 0.1423453 ]]), array([[-1.04671671]]))
Process 2605: Main : received data (array([[0.54793227, 0.1423453 ]]), array([[-1.04671671]]))
Process 3110: Observer : observed data (array([[0., 1.]]), array([[-0.71803081]]))
Process 2605: Main : received data (array([[0., 1.]]), array([[-0.71803081]]))
Process 2605: Main : iteration 9 starts
Process 3106: Observer : observing data at point [[0. 0.69112232]]Process 3102: Observer : observing data at point [[0. 0.55109471]]Process 3110: Observer : observing data at point [[0. 0.80629764]]
Process 3102: Objective: pretends like it's doing something for 1.7sProcess 3106: Objective: pretends like it's doing something for 2.1sProcess 3110: Objective: pretends like it's doing something for 2.4s
Process 3102: Observer : observed data (array([[0. , 0.55109471]]), array([[0.72178853]]))
Process 2605: Main : received data (array([[0. , 0.55109471]]), array([[0.72178853]]))
Process 3106: Observer : observed data (array([[0. , 0.69112232]]), array([[0.08533817]]))
Process 2605: Main : received data (array([[0. , 0.69112232]]), array([[0.08533817]]))
Process 3110: Observer : observed data (array([[0. , 0.80629764]]), array([[-0.31085008]]))
Process 2605: Main : received data (array([[0. , 0.80629764]]), array([[-0.31085008]]))
Got 33 observations in 60.88s
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()