src.bo_optimizer module

Bayesian Optimization module using Gaussian Processes and Latin Hypercube Sampling.

This module provides the BayesianOptimizer class for performing Bayesian optimization to find optimal friction parameters for hydraulic simulations.

Example

>>> import numpy as np
>>> from src.bo_optimizer import BayesianOptimizer
>>> optimizer = BayesianOptimizer(initial_samples, obj_func, sampler, opt_args)
>>> best_point, best_value = optimizer.optimize()
class src.bo_optimizer.BayesianOptimizer(initial_samples: ndarray, obj_func: Callable, sampler: Callable, opt_args: dict, logger: Logger | None = None)[source]

Bases: object

Bayesian Optimization using Gaussian Processes with Latin Hypercube Sampling.

This class implements Bayesian optimization for finding optimal parameters using Gaussian Process Regression as a surrogate model.

obj_func

Objective function to minimize.

Type:

Callable

sampler

Sampler for generating candidate points.

Type:

Callable

opt_args

Optimization configuration arguments.

Type:

dict

sample

Current sample points.

Type:

np.ndarray

value

Objective function values at sample points.

Type:

np.ndarray

scaler

Scaler for normalizing samples.

Type:

StandardScaler

gp

Gaussian Process regressor model.

Type:

GaussianProcessRegressor

logger

Logger instance for tracking progress.

Type:

logging.Logger

Example

>>> initial_samples = (np.array([[1.0], [2.0]]), np.array([1.5, 2.5]))
>>> optimizer = BayesianOptimizer(initial_samples, objective_func, sampler, opts)
>>> best_point, best_value = optimizer.optimize()
_optimizer() Callable[source]

Create a custom optimizer for GP hyperparameter tuning.

Returns:

Custom optimizer function using L-BFGS-B.

Return type:

Callable

optimize(return_attempted_points=False)[source]

Perform Bayesian Optimization to find optimal parameters.

This method iteratively:
  1. Predicts candidate points using the Gaussian Process surrogate.

  2. Evaluates the objective function at candidate points.

  3. Updates the GP model with new observations.

  4. Stops when tolerance is reached or max iterations exceeded.

Parameters:

return_attempted_points – If True, return all attempted points.

Returns:

(best_point, best_value) or (best_point, best_value, attempted_points)
  • best_point (np.ndarray): Optimal parameter values found.

  • best_value (float): Objective function value at best_point.

  • attempted_points (list): All points tried during optimization (optional).

Return type:

Tuple

Raises:

ValueError – If test population and constraints are both unspecified.

Example

>>> best_params, best_score = optimizer.optimize()
>>> best_params, best_score, all_points = optimizer.optimize(return_attempted_points=True)