# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2020- The University of Tokyo
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import numpy as np
import pickle as pickle
from ._history import History
from ... import _mpi
from .. import discrete
from .. import utility
from .. import score as search_score
from ..._variable import Variable
from .._policy_common import _UnifiedObjectiveMethods
[ドキュメント]
class Policy(_UnifiedObjectiveMethods, discrete.Policy):
"""Multi objective Bayesian optimization with discrete search space by using unified objective function"""
def __init__(
self,
test_X,
num_objectives,
comm=None,
config=None,
initial_data=None,
rng=None,
):
"""
Initialize the Policy object
Parameters
----------
test_X: numpy.ndarray
The set of candidates. Each row vector represents the feature vector of each search candidate.
num_objectives: int
The number of objectives
comm: MPI.Comm, optional
MPI Communicator
config: physbo.misc.SetConfig, optional
initial_data: tuple[np.ndarray, np.ndarray], optional
The initial training datasets.
The first elements is the array of actions and the second is the array of value of objective functions
"""
self.num_objectives = num_objectives
super().__init__(
test_X, config=config, initial_data=initial_data, comm=comm, rng=rng
)
def _new_history(self):
return History(num_objectives=self.num_objectives)
def _write_initial_data(self, initial_data):
if len(initial_data) != 2:
msg = "ERROR: initial_data should be 2-elements tuple or list (actions and objectives)"
raise RuntimeError(msg)
actions, fs = initial_data
if fs.shape[1] != self.num_objectives:
msg = "ERROR: initial_data[1].shape[1] != num_objectives"
raise RuntimeError(msg)
if len(actions) != fs.shape[0]:
msg = "ERROR: len(initial_data[0]) != initial_data[1].shape[0]"
raise RuntimeError(msg)
self.write(actions, fs)
[ドキュメント]
def write(
self,
action,
t,
X=None,
time_total=None,
time_update_predictor=None,
time_get_action=None,
time_run_simulator=None,
):
self.history.write(
t,
action,
time_total=time_total,
time_update_predictor=time_update_predictor,
time_get_action=time_get_action,
time_run_simulator=time_run_simulator,
)
action = np.array(action)
N = len(action)
t = np.array(t)
# Ensure t is 2D: shape (N, num_objectives)
if t.ndim == 1:
if N == 1:
t = t.reshape(1, -1)
else:
raise ValueError(f"Number of actions is {N} > 1, but t is 1D array")
assert action.shape[0] == t.shape[0], "The number of actions and t must be the same"
assert t.shape[1] == self.num_objectives, "The number of objectives in t must be the same as num_objectives"
# Determine X and Z (different for each objective)
if X is None:
X = self.test.X[action, :]
Z = self.test.Z[:, action, :] if self.test.Z is not None else None
else:
Z = None
# Failed observations (any non-finite objective) are kept in the
# history and their actions are consumed, but they are not used
# for training.
valid = utility.finite_mask(t)
if np.any(valid):
X_ok = np.asarray(X)[valid]
t_ok = t[valid]
Z_ok = utility.mask_rows(Z, valid)
if self.new_data is None:
self.new_data = Variable(X=X_ok, t=t_ok, Z=Z_ok)
else:
self.new_data.add(X=X_ok, t=t_ok, Z=Z_ok)
# Add to single training Variable with full 2D t matrix and (k, N, n) Z
if self.training.X is None:
self.training = Variable(X=X_ok, t=t_ok, Z=Z_ok)
else:
self.training.add(X=X_ok, t=t_ok, Z=Z_ok)
# remove action from candidates if exists
self.space.exclude(action)
[ドキュメント]
def random_search(
self,
max_num_probes,
num_search_each_probe=1,
simulator=None,
is_disp=True,
disp_pareto_set=False,
):
return self._random_search(
max_num_probes,
num_search_each_probe,
simulator,
is_disp,
disp_pareto_set=disp_pareto_set,
)
[ドキュメント]
def bayes_search(
self,
training_list=None,
max_num_probes=None,
num_search_each_probe=1,
predictor=None,
is_disp=True,
disp_pareto_set=False,
simulator=None,
score="EI",
interval=0,
num_rand_basis=None,
unify_method=None,
optimizer=None,
ard=None,
ucb_beta=1.0,
):
"""
Performing Bayesian optimization by using unified objective function
Parameters
----------
training_list: list of physbo.Variable, optional
The training datasets.
max_num_probes: int, optional
The maximum number of searching process by Bayesian optimization.
num_search_each_probe: int, optional
The number of searching by Bayesian optimization at each process.
predictor: predictor object, optional
The predictor object.
is_disp: bool, optional
If true, process messages are outputted.
disp_pareto_set: bool, optional
If true, Pareto set is displayed.
simulator: callable, optional
The simulator function.
score: str or callable
TS, EI, PI, UCB, or a callable called positionally as
``score(predictor, training, test)`` returning a numpy.ndarray of
shape (test.X.shape[0],), with larger values preferred. Empty test
data skips the callable. Under MPI, the callable evaluates local
points and must not perform collective communication.
training contains unified targets of shape (N, 1).
interval: int, optional
The interval number of learning the hyper parameter.
If you set the negative value to interval, the hyper parameter learning is not performed.
If you set zero to interval, the hyper parameter learning is performed only at the first step.
A negative interval needs a learned predictor: the one learned by an
earlier bayes_search, or one given as an argument, read by load() or
set by hand. Otherwise, or if another kind of predictor is asked for
(see num_rand_basis), a RuntimeError is raised.
num_rand_basis: int, optional
The number of basis function. If you choose 0, ordinary Gaussian process run.
If None (default), the predictor the policy has is used, whatever its
kind; a Gaussian process is made if it has none yet.
The predictor is kept between calls of bayes_search. A new one is made,
to be learned, when the predictor is not of the kind asked for by
num_rand_basis or ard.
unify_method: callable
The unified objective function. It is a function or a callable object that maps a (N, num_objectives) numpy.ndarray of original objective functions to a (N, 1) numpy.ndarray of unified objective functions.
See physbo.search.unify for examples.
If it draws random numbers through an ``rng`` attribute (as
``ParEGO`` does) and was not given a generator of its own, it
uses the random number generator of the policy.
optimizer: Optimizer object, optional
This is for compatibility with the range-based Policies.
This is not used.
ard: bool, optional
If True, use Automatic Relevance Determination (ARD) for the Gaussian kernel.
If None (default), as in the predictor the policy has; without ARD if
it has none yet.
ucb_beta: float
Trade-off parameter between exploitation and exploration used for
string "UCB". The score is ``fmean + ucb_beta * fstd``.
Default is 1.0; ignored for callable scores.
Returns
-------
history: history object (physbo.search.discrete_unified.History)
"""
score = search_score.resolve(
score, alpha=self.config.search.alpha, ucb_beta=ucb_beta, rng=self.rng
)
assert unify_method is not None, "unify_method must be provided"
if num_rand_basis is not None and num_rand_basis < 0:
raise ValueError("num_rand_basis must be non-negative")
return self._bayes_search(
score,
training=training_list,
predictor=predictor,
max_num_probes=max_num_probes,
num_search_each_probe=num_search_each_probe,
simulator=simulator,
is_disp=is_disp,
interval=interval,
num_rand_basis=num_rand_basis,
ard=ard,
unify_method=unify_method,
disp_pareto_set=disp_pareto_set,
)
[ドキュメント]
def get_post_fmean(self, xs):
"""
Calculate mean value of predictor (post distribution)
Parameters
----------
xs: physbo.Variable or np.ndarray
input parameters to calculate mean value
shape is (num_points, num_parameters)
Returns
-------
fmean: numpy.ndarray
Mean value of the post distribution.
Returned shape is (num_points).
"""
X = self._make_variable_X(xs)
self._require_unified_training("get_post_fmean()")
if self.predictor is None:
self._warn_no_predictor("get_post_fmean()")
predictor = self._make_gp_predictor()
predictor.fit(self.training_unified, 0, comm=self.mpicomm, rng=self.rng)
predictor.prepare(self.training_unified)
return predictor.get_post_fmean(self.training_unified, X)
else:
self._update_predictor()
return self.predictor.get_post_fmean(self.training_unified, X)
[ドキュメント]
def get_post_fcov(self, xs, diag=True):
"""
Calculate covariance of predictor (post distribution)
Parameters
----------
xs: physbo.Variable or np.ndarray
input parameters to calculate covariance
shape is (num_points, num_parameters)
diag: bool
If true, only variances (diagonal elements) are returned.
Returns
-------
fcov: numpy.ndarray
Covariance matrix of the post distribution.
Returned shape is (num_points) if diag=true, (num_points, num_points) if diag=false.
"""
X = self._make_variable_X(xs)
self._require_unified_training("get_post_fcov()")
if self.predictor is None:
self._warn_no_predictor("get_post_fcov()")
predictor = self._make_gp_predictor()
predictor.fit(self.training_unified, 0, comm=self.mpicomm, rng=self.rng)
predictor.prepare(self.training_unified)
return predictor.get_post_fcov(self.training_unified, X, diag)
else:
self._update_predictor()
return self.predictor.get_post_fcov(self.training_unified, X, diag)
[ドキュメント]
def get_score(
self,
mode,
*,
actions=None,
xs=None,
predictor=None,
training=None,
parallel=True,
alpha=1,
ucb_beta=1.0,
):
"""
Calculate score (acquisition function)
Parameters
----------
mode: str or callable
TS, EI, PI, UCB, or a callable called positionally as
``mode(predictor, training, test)`` returning a numpy.ndarray of
shape (test.X.shape[0],), with larger values preferred. Empty test
data skips the callable but still participates in MPI gathering.
The callable must not perform collective communication. alpha and
ucb_beta apply only to string modes.
training contains unified targets of shape (N, 1).
actions: array of int
actions to calculate score
xs: physbo.Variable or np.ndarray
input parameters to calculate score
predictor: predictor object
predictor used to calculate score.
If not given, self.predictor will be used.
training:physbo.Variable
Training dataset.
If not given, self.training will be used.
parallel: bool
Calculate scores in parallel by MPI (default: True)
alpha: float
Tuning parameter which is used if mode = TS.
In TS, multi variation is tuned as np.random.multivariate_normal(mean, cov*alpha**2, size).
ucb_beta: float
Trade-off parameter between exploitation and exploration used for
string "UCB". The score is ``fmean + ucb_beta * fstd``.
Default is 1.0; ignored for callable scores.
Returns
-------
f: float or list of float
Score defined in each mode.
Raises
------
RuntimeError
If both *actions* and *xs* are given
Notes
-----
When neither *actions* nor *xs* are given, scores for actions not yet searched will be calculated.
When *parallel* is True, it is assumed that the function receives the same input (*actions* or *xs*) for all the ranks.
If you want to split the input array itself, set *parallel* be False and merge results by yourself.
"""
mode = search_score.resolve(
mode, alpha=alpha, ucb_beta=ucb_beta, rng=self.rng
)
if training is None:
training = self._require_unified_training("get_score()")
if training.X is None or training.X.shape[0] == 0:
msg = "ERROR: No training data is registered."
raise RuntimeError(msg)
if predictor is None:
if self.predictor is None:
self._warn_no_predictor("get_score()")
predictor = self._make_gp_predictor()
predictor.fit(training, 0, comm=self.mpicomm, rng=self.rng)
predictor.prepare(training)
else:
self._update_predictor()
predictor = self.predictor
def scores(actions):
# the share of this process of the candidates
if xs is not None:
if actions is not None:
raise RuntimeError("ERROR: both actions and xs are given")
test = self._make_variable_X(xs)
if parallel and self.mpisize > 1:
actions = np.array_split(
np.arange(test.X.shape[0]), self.mpisize
)
test = test.get_subset(actions[self.mpirank])
else:
if actions is None:
actions = self.actions
else:
if isinstance(actions, int):
actions = [actions]
if parallel and self.mpisize > 1:
actions = np.array_split(actions, self.mpisize)[
self.mpirank
]
test = self.test.get_subset(actions)
return search_score.score(
mode,
predictor=predictor,
training=training,
test=test,
alpha=alpha,
ucb_beta=ucb_beta,
)
comm = self.mpicomm if parallel and self.mpisize > 1 else None
# TS: one posterior sample for every process, drawn on rank 0
mode = search_score.share_sample(mode, predictor, training, comm)
f, error = _mpi.call(comm, scores, actions)
if comm is not None:
fs = _mpi.allgather(comm, f, error)
f = np.hstack(fs)
return f
[ドキュメント]
def save(self, file_history, file_training=None, file_predictor=None):
"""
Saving history, training and predictor into the corresponding files.
Parameters
----------
file_history: str
The name of the file that stores the information of the history.
file_training: str
The name of the file that stores the training dataset.
file_predictor: str
The name of the file that stores the predictor dataset.
Returns
-------
"""
if self.mpirank == 0:
self.history.save(file_history)
if file_training is not None:
self.training.save(file_training)
if file_predictor is not None:
with open(file_predictor, "wb") as f:
pickle.dump(self.predictor, f)
[ドキュメント]
def load(self, file_history, file_training=None, file_predictor=None):
"""
Loading files about history, training and predictor.
Parameters
----------
file_history: str
The name of the file that stores the information of the history.
file_training: str
The name of the file that stores the training dataset.
file_predictor: str
The name of the file that stores the predictor dataset.
Returns
-------
"""
self.history.load(file_history)
if file_training is None:
# rebuild the training data from the valid observations only
actions, t = self.history.export_valid()
X = self.test.X[actions, :]
self.training = Variable(X=X, t=t)
else:
self.training = Variable()
self.training.load(file_training)
if file_predictor is not None:
with open(file_predictor, "rb") as f:
self.predictor = pickle.load(f)
N = self.history.total_num_search
visited = self.history.chosen_actions[:N]
self.space.exclude(visited)
self._after_load()