tests package

Submodules

tests.gosac_benchmark module

class tests.gosac_benchmark.Problem(objf: Callable[[ndarray], ndarray], gfun: Callable[[ndarray], ndarray], iindex: tuple[int, ...], bounds: tuple[tuple[float, float], ...], xmin: tuple[float, ...] | None = None, fmin: float | None = None) None

Bases: object

A class to represent a problem for the GOSAC benchmark.

Attributes

objfCallable[[np.ndarray], np.ndarray]

The objective function. Receives a 2D array of shape (n, dim) and returns a 1D array of shape (n,).

gfunCallable[[np.ndarray], np.ndarray]

The constraint function. Receives a 2D array of shape (n, dim) and returns a 2D array of shape (n, gdim).

iindextuple[int, …]

The indices of the integer variables.

boundstuple[tuple[float, float], …]

The bounds of the variables.

xmintuple[float, …] | None

The known minimum of the objective function. If None, the minimum is unknown.

fminfloat | None

The value of the objective function at the known minimum. If None, the minimum is unknown.

bounds: tuple[tuple[float, float], ...]
fmin: Optional[float] = None
gfun: Callable[[ndarray], ndarray]
iindex: tuple[int, ...]
objf: Callable[[ndarray], ndarray]
xmin: Optional[tuple[float, ...]] = None
tests.gosac_benchmark.fRana(x: ndarray) ndarray
Return type:

ndarray

tests.gosac_benchmark.fWeierstrass(x: ndarray) ndarray
Return type:

ndarray

tests.test_acquisition module

Test the acquisition functions.

class tests.test_acquisition.MockEvaluabilitySurrogate(X_train: ndarray, Y_train: ndarray)

Bases: Surrogate

A mock evaluability surrogate model for testing purposes. When called, this model returns a 0.1 for the first point and 1.0 for all others.

property X: ndarray

Get the training input points.

Returns:

m-by-d matrix with m training points in a d-dimensional space.

property Y: ndarray

Get the training output values.

Returns:

m-by-n matrix with m training points and n target dimensions.

__call__(x: ndarray, i: int = -1, **kwargs) ndarray | Tuple[ndarray, ...]

Return 1.0 except for the first coord which returns 0.1.

Return type:

Union[ndarray, Tuple[ndarray, ...]]

check_initial_design(sample: ndarray) bool

Check if the sample is able to generate a valid surrogate.

Parameters:

sample (ndarray) – m-by-d matrix with m training points in a d-dimensional space.

Return type:

bool

Returns:

True if the sample is valid for building the surrogate model.

eval_kernel(x: ndarray, y: ndarray | None = None) ndarray

Evaluate the kernel function at a pair (x,y).

The structure of the kernel is the same as the one passed as parameter but with optimized hyperparameters.

Parameters:
  • x (ndarray) – First entry in the tuple (x,y).

  • y (Optional[ndarray]) – Second entry in the tuple (x,y). If None, use x. (default: None)

Return type:

ndarray

Returns:

Kernel evaluation result.

property iindex: ndarray

Return the indices of integer variables in the feature space.

Returns:

Tuple with indices of integer variables.

min_design_space_size(dim: int) int

Return the minimum design space size for a given space dimension.

Parameters:

dim (int) – Dimension of the space.

Return type:

int

Returns:

Minimum number of points needed to build the surrogate model.

reserve(n: int, dim: int, ntarget: int = 1) None

Reserve space for training data.

Parameters:
  • n (int) – Number of training points to reserve.

  • dim (int) – Dimension of the input space.

  • ntarget (int) – Dimension of the target space. (default: 1)

Return type:

None

reset_data() None

Reset the surrogate model training data.

This method is used to clear the training data of the surrogate model, allowing it to be reused for a new optimization run.

Return type:

None

update(x: ndarray, y: ndarray) None

Update the surrogate model with new training data.

Parameters:
  • x (ndarray) – m-by-d matrix with m new point coordinates in a d-dimensional space.

  • y (ndarray) – m-by-n matrix with m new function values and n target dimensions.

Return type:

None

class tests.test_acquisition.MockSurrogateModel(X_train: ndarray, Y_train: ndarray, iindex: ndarray = ())

Bases: Surrogate

A mock surrogate model for testing purposes. When called, this model returns the sum of the coordinates of the input points.

property X: ndarray

Get the training input points.

Returns:

m-by-d matrix with m training points in a d-dimensional space.

property Y: ndarray

Get the training output values.

Returns:

m-by-n matrix with m training points and n target dimensions.

__call__(x: ndarray, i: int = -1, **kwargs) ndarray | Tuple[ndarray, ...]

Return sum of coords (x + y).

Return type:

Union[ndarray, Tuple[ndarray, ...]]

check_initial_design(sample: ndarray) bool

Check if the sample is able to generate a valid surrogate.

Parameters:

sample (ndarray) – m-by-d matrix with m training points in a d-dimensional space.

Return type:

bool

Returns:

True if the sample is valid for building the surrogate model.

eval_kernel(x: ndarray, y: ndarray | None = None) ndarray

Evaluate the kernel function at a pair (x,y).

The structure of the kernel is the same as the one passed as parameter but with optimized hyperparameters.

Parameters:
  • x (ndarray) – First entry in the tuple (x,y).

  • y (Optional[ndarray]) – Second entry in the tuple (x,y). If None, use x. (default: None)

Return type:

ndarray

Returns:

Kernel evaluation result.

property iindex: ndarray

Return the indices of integer variables in the feature space.

Returns:

Tuple with indices of integer variables.

min_design_space_size(dim: int) int

Return the minimum design space size for a given space dimension.

Parameters:

dim (int) – Dimension of the space.

Return type:

int

Returns:

Minimum number of points needed to build the surrogate model.

reserve(n: int, dim: int, ntarget: int = 1) None

Reserve space for training data.

Parameters:
  • n (int) – Number of training points to reserve.

  • dim (int) – Dimension of the input space.

  • ntarget (int) – Dimension of the target space. (default: 1)

Return type:

None

reset_data() None

Reset the surrogate model training data.

This method is used to clear the training data of the surrogate model, allowing it to be reused for a new optimization run.

Return type:

None

update(x: ndarray, y: ndarray) None

Update the surrogate model with new training data.

Parameters:
  • x (ndarray) – m-by-d matrix with m new point coordinates in a d-dimensional space.

  • y (ndarray) – m-by-n matrix with m new function values and n target dimensions.

Return type:

None

class tests.test_acquisition.TestAlternatedAcquisition

Bases: object

Test suite for the AlternatedAcquisition class.

test_alternated_acquisition()

Test that the AlternatedAcquisition class correctly alternates between acquisition functions.

test_optimize_generates_expected_points(n_points, dims)

Test that the optimize() method generates point that are the correct shape and within bounds while alternating between acquisition functions.

class tests.test_acquisition.TestMaximizeDistance

Bases: object

Test suite for the MaximizeDistance acquisition function.

test_optimize_generates_expected_points(dims, n_points)

Test the output points of optimize().

Ensures that the generated points are: - Within the specified bounds. - The expected shape (n_points, dims). - The amount requested.

test_optimize_maximizes_min_distance()

Test that the optimize() method maximizes the minimum distance between points. Checks that the points returned are distinct and that they match expected values in simple scenarios.

test_optimize_with_mixedint()

Test that the optimize() method works with mixed integer bounds.

class tests.test_acquisition.TestTransitionSearch

Bases: object

Test suite for the TransitionSearch acquisition function.

test_generate_candidates()

Tests that the generate_candidates() method: - Generates the expected number of candidates. - All candidates are within the specified bounds.

test_optimize_generates_expected_points(dims, n_points)

Test the output points of optimize().

Ensures that the generated points are: - Within the specified bounds. - Have the expected shape (n_points, dims). - The amount requested.

test_select_candidates()

Test that the select_candidates() method:

  • Chooses the candidate further from evaluated points when function values are the same.

  • Chooses the candidate with lower function value when distances are the same.

  • Removes candidates that are below the evaluability threshold.

tests.test_gosac_bench module

tests.test_gosac_bench.test_benchmark(problem: Problem) None
Return type:

None

tests.test_gp module

Test the Gaussian Process model and helpers.

tests.test_gp.test_X(n: int, copy_X_train: bool)
tests.test_gp.test_expected_improvement()

tests.test_optimize module

Test the optimization routines.

tests.test_optimize.test_batched_sampling()
tests.test_optimize.test_callback(minimize)
tests.test_optimize.test_multiple_calls(minimize)

tests.test_rbf module

Test the RBF model.

class tests.test_rbf.TestRbfModel

Bases: object

rbf_model = <soogo.model.rbf.RbfModel object>
test_dim()
test_phi()
tests.test_rbf.test_median_lpf()

tests.test_sampling module

Test the sampling functions.

tests.test_sampling.test_iindex_mitchel91_sampler(boundx, n0: int)
tests.test_sampling.test_iindex_sampler(boundx, strategy: SamplingStrategy)
tests.test_sampling.test_mitchel91_sampler(dim: int, n0: int)
tests.test_sampling.test_normal_sampler(dim: int, strategy: SamplingStrategy)
tests.test_sampling.test_sampler(dim: int, strategy: SamplingStrategy)
tests.test_sampling.test_slhd(boundx)

Module contents