paref.interfaces.moo_algorithms.blackbox_function#
Classes
Generic interface for blackbox functions used in Paref |
- class paref.interfaces.moo_algorithms.blackbox_function.BlackboxFunction[source]#
Bases:
objectGeneric interface for blackbox functions used in Paref
This class provides a generic interface for blackbox functions. In Paref, the evaluations of the blackbox function need to be stored and can then be accessed within the BlackboxFunction class. In addition, this class stores all the information about the blackbox function
\[f:S \to \mathbb{R}^d.\]This consists of the following
The design space S: Of which dimension is S? How is S defined? Currently, this supports:
cubes (characterized be its bounds), i.e.
\[S=\prod_{i=1}^n[a_i,b_i] \subset \mathbb{R}^n\]The target space: What is the dimension of the target space, i.e. what is d?
The assignment f: For a given x in S, what is the value f(x)?
Examples
Lets say the blackbox function has the following mathematical expression
\[f:[0,1]\times [0,1]\to \mathbb{R}^2,f(x)=(x_2^2,x_1-x_2)\]Then, the pythonic blackbox function will be implemented as follows
>>> class BlackboxFunctionExample(BlackboxFunction): >>> def __call__(self, x: np.ndarray) -> np.ndarray: >>> return np.array([x[1] ** 2, x[0] - x[1]]) >>> >>> @property >>> def dimension_design_space(self) -> int: >>> return 2 >>> >>> @property >>> def dimension_target_space(self) -> int: >>> return 2
- abstract __call__(x: ndarray | list) ndarray[source]#
Apply blackbox function to input and store the tuple (input,output) in self._evaluations
- Parameters:
x (np.ndarray) – input to which the blackbox function is applied
- Returns:
output of blackbox function applied to input
- Return type:
np.ndarray
- perform_lhc(n: int) None[source]#
Perform Latin Hypercube Sampling
The latin hypercube sampling (LHC) is a stratified (random) sampling method. It is often used as a powerful initial sampling method in order to explore the design space.
- Parameters:
n (int) – number of LHC samples
- save(path: str) None[source]#
Save evaluations to npy-file
For each evaluation, the input and output are concatenated and stored in a row of the npy-file.
- Parameters:
path (str) – path to file
- property allow_batch_evaluation: bool#
Allow batch evaluation of blackbox function
- Returns:
True if batch evaluation is allowed, False otherwise
- Return type:
bool
- abstract property design_space: Bounds#
Characterization of design space
Currently, this supports:
cubes (characterized be its bounds), i.e.
\[S=\prod_{i=1}^n[a_i,b_i] \subset \mathbb{R}^n\]- Returns:
pythonic representation of design space
- Return type:
Union[Bounds]
- abstract property dimension_design_space: int#
returns: dimension of design space :rtype: int
- abstract property dimension_target_space: int#
returns: dimension of target space :rtype: int
- property evaluations: List#
returns: numpy array of evaluations: each element of the form [input,value of blackbox function at input] :rtype: np.ndarray
- property pareto_front: ndarray#
Return Pareto front of evaluation
- Returns:
Pareto front of target vectors of evaluations
- Return type:
np.ndarray
- property x: ndarray#
Numpy array of inputs of all evaluations
- Returns:
array of inputs of all evaluations
- Return type:
np.ndarray
- property y: ndarray#
Numpy array of outputs of all evaluations
- Returns:
array of outputs of all evaluations
- Return type:
np.ndarray