trieste.space#
This module contains implementations of various types of search space.
Module Contents#
- SearchSpaceType[source]#
A type variable bound to
SearchSpace
.
- EncoderFunction[source]#
Type alias for point encoders. These transform points from one search space to another.
- exception SampleTimeoutError[source]#
Bases:
Exception
Raised when sampling from a search space has timed out.
Initialize self. See help(type(self)) for accurate signature.
- class NonlinearConstraint(fun: Callable[[trieste.types.TensorType], trieste.types.TensorType], lb: Sequence[float] | trieste.types.TensorType, ub: Sequence[float] | trieste.types.TensorType, keep_feasible: bool = False)[source]#
Bases:
scipy.optimize.NonlinearConstraint
A wrapper class for nonlinear constraints on variables. The constraints expression is of the form:
lb <= fun(x) <= ub
- Parameters:
fun – The function defining the nonlinear constraints; with input shape […, D] and output shape […, 1], returning a scalar value for each input point.
lb – The lower bound of the constraint. Should be a scalar or of shape [1].
ub – The upper bound of the constraint. Should be a scalar or of shape [1].
keep_feasible – Keep the constraints feasible throughout optimization iterations if this is True.
- residual(points: trieste.types.TensorType) trieste.types.TensorType [source]#
Calculate the residuals between the constraint function and its lower/upper limits.
- Parameters:
points – The points to calculate the residuals for, with shape […, D].
- Returns:
A tensor containing the lower and upper residual values with shape […, 2].
- class LinearConstraint(A: trieste.types.TensorType, lb: Sequence[float] | trieste.types.TensorType, ub: Sequence[float] | trieste.types.TensorType, keep_feasible: bool = False)[source]#
Bases:
scipy.optimize.LinearConstraint
A wrapper class for linear constraints on variables. The constraints expression is of the form:
lb <= A @ x <= ub
- Parameters:
A – The matrix defining the linear constraints with shape [M, D], where M is the number of constraints.
lb – The lower bound of the constraint. Should be a scalar or of shape [M].
ub – The upper bound of the constraint. Should be a scalar or of shape [M].
keep_feasible – Keep the constraints feasible throughout optimization iterations if this is True.
- residual(points: trieste.types.TensorType) trieste.types.TensorType [source]#
Calculate the residuals between the constraint function and its lower/upper limits.
- Parameters:
points – The points to calculate the residuals for, with shape […, D].
- Returns:
A tensor containing the lower and upper residual values with shape […, M*2].
- class SearchSpace[source]#
Bases:
abc.ABC
A
SearchSpace
represents the domain over which an objective function is optimized.- abstract sample(num_samples: int, seed: int | None = None) trieste.types.TensorType [source]#
- Parameters:
num_samples – The number of points to sample from this search space.
seed – Random seed for reproducibility.
- Returns:
num_samples
i.i.d. random points, sampled uniformly from this search space.
- contains(value: trieste.types.TensorType) trieste.types.TensorType [source]#
Method for checking membership.
- Parameters:
value – A point or points to check for membership of this
SearchSpace
.- Returns:
A boolean array showing membership for each point in value.
- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
value
has a different dimensionality points from thisSearchSpace
.
- abstract _contains(value: trieste.types.TensorType) trieste.types.TensorType [source]#
Space-specific implementation of membership. Can assume valid input shape.
- Parameters:
value – A point or points to check for membership of this
SearchSpace
.- Returns:
A boolean array showing membership for each point in value.
- __contains__(value: trieste.types.TensorType) bool [source]#
Method called by in operator. Doesn’t support broadcasting as Python insists on converting the result to a boolean.
- Parameters:
value – A single point to check for membership of this
SearchSpace
.- Returns:
True if
value
is a member of this search space, else False.- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
value
has a different dimensionality from thisSearchSpace
.
- property dimension: trieste.types.TensorType[source]#
- Abstractmethod:
The number of inputs in this search space.
- property has_bounds: bool[source]#
- Abstractmethod:
Whether the search space has meaningful numerical bounds.
- property lower: trieste.types.TensorType[source]#
- Abstractmethod:
The lowest value taken by each search space dimension.
- property upper: trieste.types.TensorType[source]#
- Abstractmethod:
The highest value taken by each search space dimension.
- abstract product(other: SearchSpaceType) SearchSpaceType [source]#
- Parameters:
other – A search space of the same type as this search space.
- Returns:
The Cartesian product of this search space with the
other
.
- __mul__(other: SearchSpaceType) SearchSpaceType [source]#
- __mul__(other: SearchSpace) SearchSpace
- Parameters:
other – A search space.
- Returns:
The Cartesian product of this search space with the
other
. If both spaces are of the same type then this calls theproduct()
method. Otherwise, it generates aTaggedProductSearchSpace
.
- __pow__(other: int) SearchSpaceType [source]#
Return the Cartesian product of
other
instances of this search space. For example, for an exponent of 3, and search space s, this is s ** 3, which is equivalent to s * s * s.- Parameters:
other – The exponent, or number of instances of this search space to multiply together. Must be strictly positive.
- Returns:
The Cartesian product of
other
instances of this search space.- Raises:
tf.errors.InvalidArgumentError – If the exponent
other
is less than 1.
- discretize(num_samples: int) DiscreteSearchSpace [source]#
- Parameters:
num_samples – The number of points in the
DiscreteSearchSpace
.- Returns:
A discrete search space consisting of
num_samples
points sampled uniformly from this search space.- Raises:
NotImplementedError – If this
SearchSpace
has constraints.
- abstract __eq__(other: object) bool [source]#
- Parameters:
other – A search space.
- Returns:
Whether the search space is identical to this one.
- property constraints: Sequence[Constraint][source]#
The sequence of explicit constraints specified in this search space.
- abstract constraints_residuals(points: trieste.types.TensorType) trieste.types.TensorType [source]#
Return residuals for all the constraints in this
SearchSpace
.- Parameters:
points – The points to get the residuals for, with shape […, D].
- Returns:
A tensor of all the residuals with shape […, C], where C is the total number of constraints.
- Raises:
NotImplementedError – If this
SearchSpace
does not support constraints.
- is_feasible(points: trieste.types.TensorType) trieste.types.TensorType [source]#
Checks if points satisfy the explicit constraints of this
SearchSpace
. Note membership of the search space is not checked.- Parameters:
points – The points to check constraints feasibility for, with shape […, D].
- Returns:
A tensor of booleans. Returns True for each point if it is feasible in this search space, else False.
- Raises:
NotImplementedError – If this
SearchSpace
has constraints.
- class GeneralDiscreteSearchSpace(points: trieste.types.TensorType)[source]#
Bases:
SearchSpace
An ABC representing different types of discrete search spaces (not just numerical). This contains a default implementation using explicitly provided points which subclasses may ignore.
- Parameters:
points – The points that define the discrete space, with shape (‘N’, ‘D’).
- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
points
has an invalid shape.
- _contains(value: trieste.types.TensorType) trieste.types.TensorType [source]#
Space-specific implementation of membership. Can assume valid input shape.
- Parameters:
value – A point or points to check for membership of this
SearchSpace
.- Returns:
A boolean array showing membership for each point in value.
- class DiscreteSearchSpace(points: trieste.types.TensorType)[source]#
Bases:
GeneralDiscreteSearchSpace
A discrete
SearchSpace
representing a finite set of \(D\)-dimensional points in \(\mathbb{R}^D\).For example:
>>> points = tf.constant([[-1.0, 0.4], [-1.0, 0.6], [0.0, 0.4]]) >>> search_space = DiscreteSearchSpace(points) >>> assert tf.constant([0.0, 0.4]) in search_space >>> assert tf.constant([1.0, 0.5]) not in search_space
- Parameters:
points – The points that define the discrete space, with shape (‘N’, ‘D’).
- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
points
has an invalid shape.
- property lower: trieste.types.TensorType[source]#
The lowest value taken across all points by each search space dimension.
- property upper: trieste.types.TensorType[source]#
The highest value taken across all points by each search space dimension.
- product(other: DiscreteSearchSpace) DiscreteSearchSpace [source]#
Return the Cartesian product of the two
DiscreteSearchSpace
s. For example:>>> sa = DiscreteSearchSpace(tf.constant([[0, 1], [2, 3]])) >>> sb = DiscreteSearchSpace(tf.constant([[4, 5, 6], [7, 8, 9]])) >>> (sa * sb).points.numpy() array([[0, 1, 4, 5, 6], [0, 1, 7, 8, 9], [2, 3, 4, 5, 6], [2, 3, 7, 8, 9]], dtype=int32)
- Parameters:
other – A
DiscreteSearchSpace
withpoints
of the same dtype as this search space.- Returns:
The Cartesian product of the two
DiscreteSearchSpace
s.- Raises:
TypeError – If one
DiscreteSearchSpace
haspoints
of a different dtype to the other.
- class HasOneHotEncoder[source]#
Bases:
typing_extensions.Protocol
A categorical search space that contains default logic for one-hot encoding.
- one_hot_encoder(space: SearchSpace) EncoderFunction [source]#
A utility function for one-hot encoding a search space when it supports it.
- cast_encoder(encoder: EncoderFunction, input_dtype: tensorflow.DType | None = None, output_dtype: tensorflow.DType | None = None) EncoderFunction [source]#
A utility function for casting the input and/or output of an encoder.
- one_hot_encoded_space(space: SearchSpace) SearchSpace [source]#
A bounded search space corresponding to the one-hot encoding of the given space.
- class CategoricalSearchSpace(categories: int | Sequence[int] | Sequence[str] | Sequence[Sequence[str]], dtype: tensorflow.DType = DEFAULT_DTYPE)[source]#
Bases:
GeneralDiscreteSearchSpace
,HasOneHotEncoder
A categorical
SearchSpace
representing a finite set \(\mathcal{C}\) of categories, or a finite Cartesian product \(\mathcal{C}_1 \times \cdots \times \mathcal{C}_n\) of such sets.For example:
>>> CategoricalSearchSpace(5) CategoricalSearchSpace([('0', '1', '2', '3', '4')]) >>> CategoricalSearchSpace(["Red", "Green", "Blue"]) CategoricalSearchSpace([('Red', 'Green', 'Blue')]) >>> CategoricalSearchSpace([2,3]) CategoricalSearchSpace([('0', '1'), ('0', '1', '2')]) >>> CategoricalSearchSpace([["R", "G", "B"], ["Y", "N"]]) CategoricalSearchSpace([('R', 'G', 'B'), ('Y', 'N')])
Note that internally categories are represented by numeric indices:
>>> rgb = CategoricalSearchSpace(["Red", "Green", "Blue"]) >>> assert tf.constant([1], dtype=tf.float64) in rgb >>> assert tf.constant([3], dtype=tf.float64) not in rgb >>> rgb.to_tags(tf.constant([[1], [0], [2]])) <tf.Tensor: shape=(3, 1), dtype=string, numpy= array([[b'Green'], [b'Red'], [b'Blue']], dtype=object)>
- Parameters:
categories – Number of categories or category names. Can be an array for multidimensional spaces.
dtype – The dtype of the returned indices, either tf.float32 or tf.float64.
- property lower: trieste.types.TensorType[source]#
The lowest value taken by each search space dimension.
- property upper: trieste.types.TensorType[source]#
The highest value taken by each search space dimension.
- property one_hot_encoder: EncoderFunction[source]#
A one-hot encoder for the numerical indices. Note that binary categories are left unchanged instead of adding an unnecessary second feature.
- to_tags(indices: trieste.types.TensorType) trieste.types.TensorType [source]#
Convert a tensor of indices (such as one returned by
sample()
) to one of category tags.- Parameters:
indices – A tensor of integer indices.
- Returns:
A tensor of string tags.
- product(other: CategoricalSearchSpace) CategoricalSearchSpace [source]#
Return the Cartesian product of the two
CategoricalSearchSpace
s. For example:>>> rgb = CategoricalSearchSpace(["Red", "Green", "Blue"]) >>> yn = CategoricalSearchSpace(["Yes", "No"]) >>> rgb * yn CategoricalSearchSpace([('Red', 'Green', 'Blue'), ('Yes', 'No')])
- Parameters:
other – A
CategoricalSearchSpace
.- Returns:
The Cartesian product of the two
CategoricalSearchSpace
s.
- class Box(lower: Sequence[float], upper: Sequence[float], constraints: Sequence[Constraint] | None = None, ctol: float | trieste.types.TensorType = 1e-07)[source]#
- class Box(lower: trieste.types.TensorType, upper: trieste.types.TensorType, constraints: Sequence[Constraint] | None = None, ctol: float | trieste.types.TensorType = 1e-07)
Bases:
SearchSpace
Continuous
SearchSpace
representing a \(D\)-dimensional box in \(\mathbb{R}^D\). Mathematically it is equivalent to the Cartesian product of \(D\) closed bounded intervals in \(\mathbb{R}\).If
lower
andupper
are Sequences of floats (such as lists or tuples), they will be converted to tensors of dtype DEFAULT_DTYPE.- Parameters:
lower – The lower (inclusive) bounds of the box. Must have shape [D] for positive D, and if a tensor, must have float type.
upper – The upper (inclusive) bounds of the box. Must have shape [D] for positive D, and if a tensor, must have float type.
constraints – Sequence of explicit input constraints for this search space.
ctol – Tolerance to use to check constraints satisfaction.
- Raises:
ValueError (or tf.errors.InvalidArgumentError) –
If any of the following are true:
lower
andupper
have invalid shapes.lower
andupper
do not have the same floating point type.upper
is not greater or equal tolower
across all dimensions.
- property constraints: Sequence[Constraint][source]#
The sequence of explicit constraints specified in this search space.
- _contains(value: trieste.types.TensorType) trieste.types.TensorType [source]#
For each point in
value
, return True if the point is a member of this search space, else False. A point is a member if all of its coordinates lie in the closed intervals bounded by the lower and upper bounds.- Parameters:
value – A point or points to check for membership of this
SearchSpace
.- Returns:
A boolean array showing membership for each point in value.
- sample(num_samples: int, seed: int | None = None) trieste.types.TensorType [source]#
Sample randomly from the space.
- Parameters:
num_samples – The number of points to sample from this search space.
seed – Random seed for reproducibility.
- Returns:
num_samples
i.i.d. random points, sampled uniformly, from this search space with shape ‘[num_samples, D]’ , where D is the search space dimension.
- sample_halton(num_samples: int, seed: int | None = None) trieste.types.TensorType [source]#
Sample from the space using a Halton sequence. The resulting samples are guaranteed to be diverse and are reproducible by using the same choice of
seed
.- Parameters:
num_samples – The number of points to sample from this search space.
seed – Random seed for the halton sequence
- Returns:
num_samples
of points, using halton sequence with shape ‘[num_samples, D]’ , where D is the search space dimension.
- sample_sobol(num_samples: int, skip: int | None = None) trieste.types.TensorType [source]#
Sample a diverse set from the space using a Sobol sequence. If
skip
is specified, then the resulting samples are reproducible.- Parameters:
num_samples – The number of points to sample from this search space.
skip – The number of initial points of the Sobol sequence to skip
- Returns:
num_samples
of points, using sobol sequence with shape ‘[num_samples, D]’ , where D is the search space dimension.
- _sample_feasible_loop(num_samples: int, sampler: Callable[[], trieste.types.TensorType], max_tries: int = 100) trieste.types.TensorType [source]#
Rejection sampling using provided callable. Try
max_tries
number of times to findnum_samples
feasible points.- Parameters:
num_samples – The number of feasible points to sample from this search space.
sampler – Callable to return samples. Called potentially multiple times.
max_tries – Maximum attempts to sample the requested number of points.
- Returns:
num_samples
feasible points sampled usingsampler
.- Raises:
SampleTimeoutError – If
max_tries
are exhausted beforenum_samples
are sampled.
- sample_feasible(num_samples: int, seed: int | None = None, max_tries: int = 100) trieste.types.TensorType [source]#
Sample feasible points randomly from the space.
- Parameters:
num_samples – The number of feasible points to sample from this search space.
seed – Random seed for reproducibility.
max_tries – Maximum attempts to sample the requested number of points.
- Returns:
num_samples
i.i.d. random points, sampled uniformly, from this search space with shape ‘[num_samples, D]’ , where D is the search space dimension.- Raises:
SampleTimeoutError – If
max_tries
are exhausted beforenum_samples
are sampled.
- sample_halton_feasible(num_samples: int, seed: int | None = None, max_tries: int = 100) trieste.types.TensorType [source]#
Sample feasible points from the space using a Halton sequence. The resulting samples are guaranteed to be diverse and are reproducible by using the same choice of
seed
.- Parameters:
num_samples – The number of feasible points to sample from this search space.
seed – Random seed for the halton sequence
max_tries – Maximum attempts to sample the requested number of points.
- Returns:
num_samples
of points, using halton sequence with shape ‘[num_samples, D]’ , where D is the search space dimension.- Raises:
SampleTimeoutError – If
max_tries
are exhausted beforenum_samples
are sampled.
- sample_sobol_feasible(num_samples: int, skip: int | None = None, max_tries: int = 100) trieste.types.TensorType [source]#
Sample a diverse set of feasible points from the space using a Sobol sequence. If
skip
is specified, then the resulting samples are reproducible.- Parameters:
num_samples – The number of feasible points to sample from this search space.
skip – The number of initial points of the Sobol sequence to skip
max_tries – Maximum attempts to sample the requested number of points.
- Returns:
num_samples
of points, using sobol sequence with shape ‘[num_samples, D]’ , where D is the search space dimension.- Raises:
SampleTimeoutError – If
max_tries
are exhausted beforenum_samples
are sampled.
- product(other: Box) Box [source]#
Return the Cartesian product of the two
Box
es (concatenating their respective lower and upper bounds). For example:>>> unit_interval = Box([0.0], [1.0]) >>> square_at_origin = Box([-2.0, -2.0], [2.0, 2.0]) >>> new_box = unit_interval * square_at_origin >>> new_box.lower.numpy() array([ 0., -2., -2.]) >>> new_box.upper.numpy() array([1., 2., 2.])
- __eq__(other: object) bool [source]#
- Parameters:
other – A search space.
- Returns:
Whether the search space is identical to this one.
- constraints_residuals(points: trieste.types.TensorType) trieste.types.TensorType [source]#
Return residuals for all the constraints in this
SearchSpace
.- Parameters:
points – The points to get the residuals for, with shape […, D].
- Returns:
A tensor of all the residuals with shape […, C], where C is the total number of constraints.
- is_feasible(points: trieste.types.TensorType) trieste.types.TensorType [source]#
Checks if points satisfy the explicit constraints of this
SearchSpace
. Note membership of the search space is not checked.- Parameters:
points – The points to check constraints feasibility for, with shape […, D].
- Returns:
A tensor of booleans. Returns True for each point if it is feasible in this search space, else False.
- class CollectionSearchSpace(spaces: Sequence[SearchSpace], tags: Sequence[str] | None = None)[source]#
Bases:
SearchSpace
An abstract
SearchSpace
consisting of a collection of multipleSearchSpace
objects, each with a unique tag. This class provides functionality for accessing each individual space.Note that the individual spaces are not combined in any way.
Build a
CollectionSearchSpace
from a listspaces
of other spaces. Iftags
are provided then they form the identifiers of the subspaces, otherwise the subspaces are labelled numerically.- Parameters:
spaces – A sequence of
SearchSpace
objects representing the space’s subspacestags – An optional list of tags giving the unique identifiers of the space’s subspaces.
- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
spaces
has a different length totags
whentags
is provided or iftags
contains duplicates.
- property subspace_lower: Sequence[trieste.types.TensorType][source]#
The lowest values taken by each space dimension, in the same order as specified when initializing the space.
- property subspace_upper: Sequence[trieste.types.TensorType][source]#
The highest values taken by each space dimension, in the same order as specified when initializing the space.
- property subspace_tags: tuple[str, Ellipsis][source]#
Return the names of the subspaces contained in this space.
- property subspace_dimension: Sequence[trieste.types.TensorType][source]#
The number of inputs in each subspace, in the same order as specified when initializing the space.
- get_subspace(tag: str) SearchSpace [source]#
Return the domain of a particular subspace.
- Parameters:
tag – The tag specifying the target subspace.
- Returns:
Target subspace.
- subspace_sample(num_samples: int, seed: int | None = None) Sequence[trieste.types.TensorType] [source]#
Sample randomly from the space by sampling from each subspace and returning the resulting samples in the same order as specified when initializing the space.
- Parameters:
num_samples – The number of points to sample from each subspace.
seed – Optional tf.random seed.
- Returns:
num_samples
i.i.d. random points, sampled uniformly, from each search subspace with shape ‘[num_samples, D]’ , where D is the search space dimension.
- class TaggedProductSearchSpace(spaces: Sequence[SearchSpace], tags: Sequence[str] | None = None)[source]#
Bases:
CollectionSearchSpace
,HasOneHotEncoder
Product
SearchSpace
consisting of a product of multipleSearchSpace
. This class provides functionality for accessing either the resulting combined search space or each individual space. This class is useful for defining mixed search spaces, for example:context_space = DiscreteSearchSpace(tf.constant([[-0.5, 0.5]])) decision_space = Box([-1, -2], [2, 3]) mixed_space = TaggedProductSearchSpace(spaces=[context_space, decision_space])
Note: the dtype of all the component search spaces must be the same.
Note that this class assumes that individual points in product spaces are represented with their inputs in the same order as specified when initializing the space.
Build a
TaggedProductSearchSpace
from a listspaces
of other spaces. Iftags
are provided then they form the identifiers of the subspaces, otherwise the subspaces are labelled numerically.- Parameters:
spaces – A sequence of
SearchSpace
objects representing the space’s subspacestags – An optional list of tags giving the unique identifiers of the space’s subspaces.
- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
spaces
has a different length totags
whentags
is provided or iftags
contains duplicates.
- property lower: trieste.types.TensorType[source]#
The lowest values taken by each space dimension, concatenated across subspaces.
- property upper: trieste.types.TensorType[source]#
The highest values taken by each space dimension, concatenated across subspaces.
- property dimension: trieste.types.TensorType[source]#
The number of inputs in this product search space.
- fix_subspace(tag: str, values: trieste.types.TensorType) TaggedProductSearchSpace [source]#
Return a new
TaggedProductSearchSpace
with the specified subspace replaced with aDiscreteSearchSpace
containingvalues
as its points. This is useful if you wish to restrict subspaces to sets of representative points.- Parameters:
tag – The tag specifying the target subspace.
values – The values used to populate the new discrete subspace.z
- Returns:
New
TaggedProductSearchSpace
with the specified subspace replaced with aDiscreteSearchSpace
containingvalues
as its points.
- get_subspace_component(tag: str, values: trieste.types.TensorType) trieste.types.TensorType [source]#
Returns the components of
values
lying in a particular subspace.- Parameters:
tag – Subspace tag.
values – Points from the
TaggedProductSearchSpace
of shape [N,Dprod].
- Returns:
The sub-components of
values
lying in the specified subspace, of shape [N, Dsub], where Dsub is the dimensionality of the specified subspace.
- _contains(value: trieste.types.TensorType) trieste.types.TensorType [source]#
Return True if
value
is a member of this search space, else False. A point is a member if each of its subspace components lie in each subspace.Recall that individual points in product spaces are represented with their inputs in the same order as specified when initializing the space.
- Parameters:
value – A point to check for membership of this
SearchSpace
.- Returns:
True if
value
is a member of this search space, else False. May return a scalar boolean TensorType instead of the bool itself.- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
value
has a different dimensionality from the search space.
- sample(num_samples: int, seed: int | None = None) trieste.types.TensorType [source]#
Sample randomly from the space by sampling from each subspace and concatenating the resulting samples.
- Parameters:
num_samples – The number of points to sample from this search space.
seed – Optional tf.random seed.
- Returns:
num_samples
i.i.d. random points, sampled uniformly, from this search space with shape ‘[num_samples, D]’ , where D is the search space dimension.
- product(other: TaggedProductSearchSpace) TaggedProductSearchSpace [source]#
Return the Cartesian product of the two
TaggedProductSearchSpace
s, building a tree ofTaggedProductSearchSpace
s.- Parameters:
other – A search space of the same type as this search space.
- Returns:
The Cartesian product of this search space with the
other
.
- class TaggedMultiSearchSpace(spaces: Sequence[SearchSpace], tags: Sequence[str] | None = None)[source]#
Bases:
CollectionSearchSpace
A
SearchSpace
made up of a collection of multipleSearchSpace
subspaces, each with a unique tag. All subspaces must have the same dimensionality.Each subspace is treated as an independent space and not combined in any way. This class provides functionality for accessing all the subspaces at once by using the usual search space methods, as well as for accessing individual subspaces.
When accessing all subspaces at once from this class (e.g. lower(), upper(), sample()), the returned tensors have an extra dimension corresponding to the subspaces.
This class can be useful to represent a collection of search spaces that do not interact with each other. For example, it is used to implement batch trust region rules in the
BatchTrustRegion
class.Build a
TaggedMultiSearchSpace
from a listspaces
of other spaces. Iftags
are provided then they form the identifiers of the subspaces, otherwise the subspaces are labelled numerically.- Parameters:
spaces – A sequence of
SearchSpace
objects representing the space’s subspacestags – An optional list of tags giving the unique identifiers of the space’s subspaces.
- Raises:
ValueError (or tf.errors.InvalidArgumentError) – If
spaces
has a different length totags
whentags
is provided or iftags
contains duplicates.ValueError (or tf.errors.InvalidArgumentError) – If
spaces
has a different dimension to each other.
- property lower: trieste.types.TensorType[source]#
Returns the stacked lower bounds of all the subspaces.
- Returns:
The lower bounds of shape [V, D], where V is the number of subspaces and D is the dimensionality of each subspace.
- property upper: trieste.types.TensorType[source]#
Returns the stacked upper bounds of all the subspaces.
- Returns:
The upper bounds of shape [V, D], where V is the number of subspaces and D is the dimensionality of each subspace.
- sample(num_samples: int, seed: int | None = None) trieste.types.TensorType [source]#
Sample randomly from the space by sampling from each subspace and returning the resulting samples stacked along the second axis in the same order as specified when initializing the space.
- Parameters:
num_samples – The number of points to sample from each subspace.
seed – Optional tf.random seed.
- Returns:
num_samples
i.i.d. random points, sampled uniformly, from each search subspace with shape ‘[num_samples, V, D]’ , where V is the number of subspaces and D is the search space dimension.
- _contains(value: trieste.types.TensorType) trieste.types.TensorType [source]#
Return True if
value
is a member of this search space, else False. A point is a member if it is a member of any of the subspaces.- Parameters:
value – A point or points to check for membership of this
SearchSpace
.- Returns:
A boolean array showing membership for each point in value.
- product(other: TaggedMultiSearchSpace) TaggedMultiSearchSpace [source]#
Return a bigger collection of two
TaggedMultiSearchSpace
s, regenerating the tags.- Parameters:
other – A search space of the same type as this search space.
- Returns:
The product of this search space with the
other
.
- discretize(num_samples: int) DiscreteSearchSpace [source]#
- Parameters:
num_samples – The number of points in the
DiscreteSearchSpace
.- Returns:
A discrete search space consisting of
num_samples
points sampled uniformly from this search space.- Raises:
NotImplementedError – If this
SearchSpace
has constraints.