adasamp package

adasamp.sampling

Adaptive sampling: Adaptive optimization algorithm for black-box multi-objective optimization problems with binary constraints on the foundation of Bayes optimization.

class adasamp.sampling.AdaptiveSampler(simulation_func, X_limits, Y_ref, iterations, Y_model, f_model, initial_samples=0, virtual_iterations=1, initial_sampling_func='random', utility_parameter_options={}, decision_parameter_options={}, X_initial_sample_limits=None, callback_func=None, stopping_condition_func=None, seed=None, verbose=False, save_memory_flag=False)[source]

Bases: object

Adaptive sampler.

Parameters:
  • simulation_func (callable) – Function calculating the goals and feasibilities for given features. Must be of the form simulation_func(X, **kwargs) and returns a tuple (Y, f), where X is an ndarray of shape (n_samples, X_dim) and kwargs is a dict of any additional fixed parameters needed to completely specify the function. The returned value Y is an ndarray of shape (n_samples, Y_dim) representing the resulting goal functions and f is an ndarray of shape (n_samples,) representing the resulting binary feasibilities. The kwargs parameter is provided when starting the adaptive sampling run via sample. The adaptive sampling run aims to maximize Y s.t. f == True.

  • X_limits (list of float tuples (pairs)) – Feature space limits given by a list of pairs of lower and upper bounds: [ (x1min, x1max), (x2min, x2max), ... ]. This list also specifies the dimensionality X_dim = len(X_limits) of the feature space.

  • Y_ref (list of float) – Goal space reference point of the form [ y1min, y2min, ... ]. All resulting goal function values must be dominated by the reference point (w.r.t. maximization) or undesired behaviour might occur. This list also specifies the dimensionality Y_dim = len(Y_ref) of the goal space.

  • iterations (int) – Number of adaptive sampling iterations.

  • Y_model (RegressionModel) – Estimator object for the internal regression problem of predicting Y (goals) from X (features). See RegressionModel for details.

  • f_model (ClassificationModel) – Estimator object for the internal classification problem of predicting f (feasibilities) from X (features). See ClassificationModel for details.

  • initial_samples (int, optional (default: 0)) – Number of initial samples to calculate before starting the adaptive sampling loop.

  • virtual_iterations (int, optional (default: 1)) – Number of virtual adaptive sampling iterations. Specifies the number of suggested samples per adaptive sampling iteration. Must be at least 1.

  • initial_sampling_func (str or callable, optional (default: "random")) – Function suggesting the initial sampling points. Can either be a string or a callable. The string can either be ‘random’ for uniformly distributed random samples or ‘factorial’ for a (full or reduced) factorial design of experiments. The callable must be of the form initial_sampling_func(initial_samples, X_initial_sample_limits, seed), where initial_samples (int) represents the number of initial samples, X_initial_sample_limits` (list of tuples) the respective feature space limits and seed (int) a given random seed.

  • utility_parameter_options (dict, optional (default: {})) –

    Set parameters specifying the utility function. If not set, default values are used. The following parameters are available (=default values):

    • entropy_weight=1: entropic weight

    • optimization_weight=1: optimality weight

    • repulsion_weight=1: repulsion weight

    • repulsion_gamma=1: repulsion coefficient

    • repulsion_distance_func=”default”: distance function (either “default” or a callable of the form repulsion_distance_func(x, y) returning the scalar distance of two points x and y.)

    • evi_gamma = 1: Pareto volume parameter

    • sector_cutoff = 1: Pareto volume cutoff

  • decision_parameter_options (dict, optional (default: {})) –

    Set decision specifying the utility function. If not set, default values are used. The following parameters are available (=default values):

    • popsize=15: differential evolution setting

    • maxiter=1000: differential evolution setting

    • tol=.01: differential evolution setting

    • atol=.05: differential evolution setting

    • polish=True: differential evolution setting

    • polish_extratol=.1: differential evolution polishing setting

    • polish_maxfun=100: differential evolution polishing setting

    • de_workers=-1: number of workers (-1: use all available)

    • polish_workers=-1: number of workers (-1: use all available)

  • X_initial_sample_limits (list of tuples or None, optional (default: None)) – Feature space limits for the initial sampling given by a list of pairs of lower and upper bounds in analogy to X_limits. If set to None, X_limits is used instead.

  • callback_func (callable or None, optional (default: None)) – Function which is called after every adaptive sampling iteration and after the inital sampling. Must be of the form callback_func(sampler, X, Y, f, iteration), where sampler is the AdaptiveSampler object (self), X is an ndarray of shape (n_samples, X_dim), Y is an ndarray of shape (n_samples, Y_dim) and f is an ndarray of shape (n_samples,) representing all samples until the current iteration given by iteration (int or None for the inital sampling call). The return value is stored in the info property. The callback function ignored if set to None.

  • stopping_condition_func (callable or None, optional (default: None)) – Function evaluating a specified stopping criterion. Must be of the form stopping_condition_func(X, Y, f), where X` is an ndarray of shape (n_samples, X_dim), Y is an ndarray of shape (n_samples, Y_dim) and f is an ndarray of shape (n_samples,) representing all samples. The function is called at the end of every adaptive sampling iteration. Its return value is used to determine whether the adaptive sampling loop is stopped prematurely before the number of iterations is reached: a True return value leads to a stop. The stopping condition function ignored if set to None.

  • seed (int or None, optional (default: None)) – Random seed used for all non-deterministic parts of the algorithm. If set to None, an unspecified (pseudo-random) seed is used.

  • verbose (bool, optional (default: False)) – Set to True to print status messages. Use the logging module if enabled.

  • save_memory_flag (bool, optional (default: False)) – Set to True to activate the memory saving mode, which switches to a memory efficient Pareto volume calculation at the cost of a possibly longer runtime.

property info

Current sampling information (statistics etc.) in form of a dictionary.

initial_sampling_factorial(initial_samples, X_initial_sample_limits, seed)[source]

Create an initial factorial design of experiments. If no full factorial design is possible, a random subsampling is used.

initial_sampling_random_uniform(initial_samples, X_initial_sample_limits, seed)[source]

Create a random initial design of experiments within the given limits of the feature space.

property opt_func

Current optimization function of the form opt_func(X, workers), where X is an ndarray of shape (1, X_dim) corresponding to a single sampling point. The property defaults to None if the optimization function has not yet been specified (i.e., None or callable).

sample(**kwargs)[source]

Start sampling.

Perform an adaptive sampling with this sampler and return the sampled results.

Parameters:

**kwargs (dict, optional) – Any additional fixed parameters needed to completely specify simulation_func.

Returns:

  • X (ndarray of shape (n_samples, X_dim)) – Resulting array of sampled features.

  • Y (ndarray of shape (n_samples, Y_dim)) – Resulting array of corresponding goals from the simulation.

  • f (ndarray of shape (n_samples,)) – Resulting array of corresponding binary feasibilities from the simulation.

adasamp.util

Helper toolbox for adaptive sampling.

adasamp.util.log_wrapper(verbose, level, msg)[source]

Wrapper to safely log messages.

Use the logging module if enabled and use print to std otherwise.

Parameters:
  • verbose (bool) – Set to True to log messages.

  • level (int) – Logging level for the logging module.

  • msg (str) – Actual message to log.

adasamp.models

Wrappers for adaptive sampling models.

class adasamp.models.ClassificationModel[source]

Bases: ABC

Classification model wrapper.

Estimator for the adaptive sampling classification problem of predicting f (binary feasibilities) from X (features).

abstract fit(X, f)[source]

Train estimator.

Is called at least once before any prediction. The class labels can be either True or False and are automatically converted to integers before the training.

Parameters:
  • X (ndarray of shape (n_samples, X_dim)) – Array of features (estimator inputs).

  • f (ndarray of shape (n_samples,)) – Array of binary classification labels (estimator targets).

abstract predict(X)[source]

Classifier prediction.

Parameters:

X (ndarray of shape (n_query_points, X_dim)) – Query points where the estimator is evaluated.

Returns:

f – Predicted labels at the query points.

Return type:

ndarray of shape (n_query_points,)

abstract predict_true_proba(X)[source]

Classifier probability prediction.

Returns the predicted probability of a True label at the respective query points.

Parameters:

X (ndarray of shape (n_query_points, X_dim)) – Query points where the estimator is evaluated.

Returns:

p – Predicted probability for a True label at the query points. It is assumed that 0 <= p <= 1.

Return type:

ndarray of shape (n_query_points,)

class adasamp.models.RegressionModel[source]

Bases: ABC

Regression model wrapper.

Estimator for the adaptive sampling regression problem of predicting Y (goals) from X (features).

abstract fit(X, Y)[source]

Train estimator.

Is called at least once before any prediction.

Parameters:
  • X (ndarray of shape (n_samples, X_dim)) – Array of features (estimator inputs).

  • Y (ndarray of shape (n_samples, Y_dim)) – Array of regression values (estimator targets).

abstract predict(X, return_std)[source]

Estimator prediction.

It is assumed that the predictive distribution is a Gaussian specified by a mean and a standard deviation.

Parameters:
  • X (ndarray of shape (n_query_points, X_dim)) – Query points where the estimator is evaluated.

  • return_std (bool) – If set to True, return both the means and the standard deviations. If set to False, only return the means.

Returns:

  • Y_mu (ndarray of shape (n_query_points, Y_dim)) – Mean of the predictive distribution at the query points.

  • Y_sigma (ndarray of shape (n_query_points, Y_dim)) – Standard deviation of the predictive distribution at the query points. Covariances are not returned.

adasamp.models_sklearn

Helper classes for simple adaptive sampling models based on scikit-learn.

class adasamp.models_sklearn.AdvancedMultiOutputRegressor(model_constructor, predict_fun=None, **kwargs)[source]

Bases: BaseEstimator

Regression model with multiple, independent outputs.

For each output, a seperate sklearn-model is realized.

fit(X, y)[source]
get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

predict(X, return_std=False)[source]
score(X, y)[source]
set_params(**parameters)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

set_predict_request(*, return_std: bool | None | str = '$UNCHANGED$') AdvancedMultiOutputRegressor

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

return_std (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_std parameter in predict.

Returns:

self – The updated object.

Return type:

object

class adasamp.models_sklearn.MultivariateGPR(**kwargs)[source]

Bases: AdvancedMultiOutputRegressor

Gaussian process regressor with multiple, independent outputs.

An independent GaussianProcessRegressor for each output is realized.

set_predict_request(*, return_std: bool | None | str = '$UNCHANGED$') MultivariateGPR

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

return_std (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_std parameter in predict.

Returns:

self – The updated object.

Return type:

object

class adasamp.models_sklearn.Y_Model_GPR(kernel=1**2 * Matern(length_scale=1, nu=1.5), n_restarts_optimizer=5, random_state=0)[source]

Bases: RegressionModel

Gaussian process regressor with multiple, independent outputs including scaling.

Realizes a pipeline: Standardization, independent GaussianProcessRegressor for each output.

fit(X, Y)[source]

Train estimator.

Is called at least once before any prediction.

Parameters:
  • X (ndarray of shape (n_samples, X_dim)) – Array of features (estimator inputs).

  • Y (ndarray of shape (n_samples, Y_dim)) – Array of regression values (estimator targets).

predict(X, return_std)[source]

Estimator prediction.

It is assumed that the predictive distribution is a Gaussian specified by a mean and a standard deviation.

Parameters:
  • X (ndarray of shape (n_query_points, X_dim)) – Query points where the estimator is evaluated.

  • return_std (bool) – If set to True, return both the means and the standard deviations. If set to False, only return the means.

Returns:

  • Y_mu (ndarray of shape (n_query_points, Y_dim)) – Mean of the predictive distribution at the query points.

  • Y_sigma (ndarray of shape (n_query_points, Y_dim)) – Standard deviation of the predictive distribution at the query points. Covariances are not returned.

class adasamp.models_sklearn.f_Model_SVM(kernel='rbf', cv_dict={'C': array([1.00000000e-01, 2.78255940e-01, 7.74263683e-01, 2.15443469e+00, 5.99484250e+00, 1.66810054e+01, 4.64158883e+01, 1.29154967e+02, 3.59381366e+02, 1.00000000e+03]), 'gamma': array([1.00000000e-01, 2.78255940e-01, 7.74263683e-01, 2.15443469e+00, 5.99484250e+00, 1.66810054e+01, 4.64158883e+01, 1.29154967e+02, 3.59381366e+02, 1.00000000e+03])}, n_splits=3, random_state=0)[source]

Bases: ClassificationModel

Support vector classifier inlcuding scaling.

Realizes a pipeline: Standardization, SVM with GridSearchCV hyperparameter optimization.

fit(X, f)[source]

Train estimator.

Is called at least once before any prediction. The class labels can be either True or False and are automatically converted to integers before the training.

Parameters:
  • X (ndarray of shape (n_samples, X_dim)) – Array of features (estimator inputs).

  • f (ndarray of shape (n_samples,)) – Array of binary classification labels (estimator targets).

predict(X)[source]

Classifier prediction.

Parameters:

X (ndarray of shape (n_query_points, X_dim)) – Query points where the estimator is evaluated.

Returns:

f – Predicted labels at the query points.

Return type:

ndarray of shape (n_query_points,)

predict_true_proba(X)[source]

Classifier probability prediction.

Returns the predicted probability of a True label at the respective query points.

Parameters:

X (ndarray of shape (n_query_points, X_dim)) – Query points where the estimator is evaluated.

Returns:

p – Predicted probability for a True label at the query points. It is assumed that 0 <= p <= 1.

Return type:

ndarray of shape (n_query_points,)