"""Utilities for acquisition functions."""from__future__importannotationsfromtypingimportTYPE_CHECKINGfrombaybe.acquisition.baseimportAcquisitionFunctionifTYPE_CHECKING:frombotorch.utils.multi_objective.box_decompositions.box_decompositionimport(BoxDecomposition,)fromtorchimportTensor
[docs]defstr_to_acqf(name:str,/)->AcquisitionFunction:"""Create an ACQF object from a given ACQF name."""returnAcquisitionFunction.from_dict({"type":name})
[docs]defconvert_acqf(acqf:AcquisitionFunction|str,/)->AcquisitionFunction:"""Convert an ACQF name into an ACQF object (with ACQF object passthrough)."""returnacqfifisinstance(acqf,AcquisitionFunction)elsestr_to_acqf(acqf)
[docs]defmake_partitioning(predictions:Tensor,ref_point:Tensor,alpha:float|None)->BoxDecomposition:"""Create a :class:`~botorch.utils.multi_objective.box_decompositions.box_decomposition.BoxDecomposition` object for the given predictions and reference point. For details on the arguments, see :class:`~botorch.utils.multi_objective.box_decompositions.non_dominated.NondominatedPartitioning`. Args: predictions: The predictions tensor of shape (n_samples, n_outputs). ref_point: The reference point tensor of shape (n_outputs,). alpha: Optional threshold parameter controlling the partitioning generation. Hypercells with a volume fraction (relative to the total Pareto set hypervolume) less than the specified value will be dropped, leading to more approximation but faster computation. Raises: ValueError: If the predictions or reference point do not have the expected shapes. Returns: A partitioning object for hypervolume acquisition functions. """# noqa: E501frombotorch.acquisition.input_constructorsimport(get_default_partitioning_alpha,)frombotorch.utils.multi_objective.box_decompositions.non_dominatedimport(FastNondominatedPartitioning,NondominatedPartitioning,)ifpredictions.ndim!=2:raiseValueError(f"Predictions must be a 2-D tensor, got shape {predictions.shape}.")ifref_point.ndim!=1:raiseValueError(f"Reference point must be a 1-D tensor, got shape {ref_point.shape}.")if(n_p:=predictions.shape[1])!=(n_r:=len(ref_point)):raiseValueError(f"Predictions dimensionality {n_p} does not match reference point "f"dimensionality {n_r}.")alpha=(get_default_partitioning_alpha(num_objectives=len(ref_point))ifalphaisNoneelsealpha)# alpha=0 means requesting an exact partitioning, for which there is a specialized# faster algorithm availableifalpha==0:returnFastNondominatedPartitioning(ref_point=ref_point,Y=predictions)returnNondominatedPartitioning(ref_point=ref_point,Y=predictions,alpha=alpha)