diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1986ba8a..78f19c0b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -83,10 +83,6 @@ jobs: - name: Show dependencies run: python -m pip list - - name: Test with pytest - run: | - python -m pytest tests -p no:warnings - - name: Test with pytest run: | python -m pytest src/hyperactive -p no:warnings @@ -121,10 +117,6 @@ jobs: - name: Show dependencies run: python -m pip list - - name: Test with pytest - run: | - python -m pytest tests --cov=hyperactive --cov-report=term-missing --cov-report=xml -p no:warnings - - name: Test with pytest run: | python -m pytest src/hyperactive -p no:warnings diff --git a/src/hyperactive/__init__.py b/src/hyperactive/__init__.py index 5106a46c..4405d413 100644 --- a/src/hyperactive/__init__.py +++ b/src/hyperactive/__init__.py @@ -14,9 +14,4 @@ __version__ = importlib.metadata.version("hyperactive") __license__ = "MIT" - -from .hyperactive import Hyperactive - -__all__ = [ - "Hyperactive", -] +__all__ = [] diff --git a/src/hyperactive/distribution.py b/src/hyperactive/distribution.py deleted file mode 100644 index 38b4c393..00000000 --- a/src/hyperactive/distribution.py +++ /dev/null @@ -1,49 +0,0 @@ -"""Distribution module for parallel processing in hyperparameter optimization. - -This module provides various methods for distributing optimization processes -across multiple cores or threads, including multiprocessing, pathos, and joblib. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -from sys import platform - -from tqdm import tqdm - -if platform.startswith("linux"): - initializer = tqdm.set_lock - initargs = (tqdm.get_lock(),) -else: - initializer = None - initargs = () - - -def single_process(process_func, process_infos): - """Execute processes sequentially in a single thread.""" - return [process_func(*info) for info in process_infos] - - -def multiprocessing_wrapper(process_func, process_infos, n_processes): - """Execute processes using multiprocessing library.""" - import multiprocessing as mp - - with mp.Pool(n_processes, initializer=initializer, initargs=initargs) as pool: - return pool.map(process_func, process_infos) - - -def pathos_wrapper(process_func, search_processes_paras, n_processes): - """Execute processes using pathos multiprocessing library.""" - import pathos.multiprocessing as pmp - - with pmp.Pool(n_processes, initializer=initializer, initargs=initargs) as pool: - return pool.map(process_func, search_processes_paras) - - -def joblib_wrapper(process_func, search_processes_paras, n_processes): - """Execute processes using joblib parallel processing.""" - from joblib import Parallel, delayed - - jobs = [delayed(process_func)(*info_dict) for info_dict in search_processes_paras] - return Parallel(n_jobs=n_processes)(jobs) diff --git a/src/hyperactive/hyperactive.py b/src/hyperactive/hyperactive.py deleted file mode 100644 index ea472b8e..00000000 --- a/src/hyperactive/hyperactive.py +++ /dev/null @@ -1,285 +0,0 @@ -"""Main Hyperactive module providing the primary optimization interface. - -This module contains the Hyperactive class, which is the main entry point -for hyperparameter optimization. It provides methods to add optimization -searches, run them, and retrieve results. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -import copy -import multiprocessing as mp -from typing import Union - -import pandas as pd - -from .optimizers import RandomSearchOptimizer -from .print_results import PrintResults -from .results import Results -from .run_search import run_search -from .search_space import SearchSpace - - -class Hyperactive: - """ - Initialize the Hyperactive class to manage optimization processes. - - Parameters - ---------- - - verbosity: List of verbosity levels - (default: ["progress_bar", "print_results", "print_times"]) - - distribution: String indicating the distribution method - (default: "multiprocessing") - - n_processes: Number of processes to run in parallel or "auto" - to determine automatically (default: "auto") - - Methods - ------- - - add_search: Add a new optimization search process with specified parameters - - run: Execute the optimization searches - - best_para: Get the best parameters for a specific search - - best_score: Get the best score for a specific search - - search_data: Get the search data for a specific search - """ - - def __init__( - self, - verbosity: list = ["progress_bar", "print_results", "print_times"], - distribution: str = "multiprocessing", - n_processes: Union[str, int] = "auto", - ): - super().__init__() - if verbosity is False: - verbosity = [] - - self.verbosity = verbosity - self.distribution = distribution - self.n_processes = n_processes - - self.opt_pros = {} - - def _create_shared_memory(self): - _bundle_opt_processes = {} - - for opt_pros in self.opt_pros.values(): - if opt_pros.memory != "share": - continue - name = opt_pros.objective_function.__name__ - - _bundle_opt_processes.setdefault(name, []).append(opt_pros) - - for opt_pros_l in _bundle_opt_processes.values(): - # Check if the lengths of the search spaces of all optimizers - # in the list are the same. - if len({len(opt_pros.s_space()) for opt_pros in opt_pros_l}) == 1: - manager = mp.Manager() # get new manager.dict - shared_memory = manager.dict() - for opt_pros in opt_pros_l: - opt_pros.memory = shared_memory - else: - for opt_pros in opt_pros_l: - opt_pros.memory = opt_pros_l[0].memory # get same manager.dict - - @staticmethod - def _default_opt(optimizer): - if isinstance(optimizer, str): - if optimizer == "default": - optimizer = RandomSearchOptimizer() - return copy.deepcopy(optimizer) - - @staticmethod - def _default_search_id(search_id, objective_function): - """Set default search ID based on objective function name if not provided.""" - if not search_id: - search_id = objective_function.__name__ - return search_id - - @staticmethod - def check_list(search_space): - """Validate that search space values are lists.""" - for key in search_space.keys(): - search_dim = search_space[key] - - error_msg = ( - f"Value in '{key}' of search space dictionary must be of type list" - ) - if not isinstance(search_dim, list): - print("Warning", error_msg) - # raise ValueError(error_msg) - - def add_search( - self, - objective_function: callable, - search_space: dict[str, list], - n_iter: int, - search_id=None, - optimizer: Union[str, type[RandomSearchOptimizer]] = "default", - n_jobs: int = 1, - initialize: dict[str, int] = {"grid": 4, "random": 2, "vertices": 4}, - constraints: list[callable] = None, - pass_through: dict = None, - callbacks: dict[str, callable] = None, - catch: dict = None, - max_score: float = None, - early_stopping: dict = None, - random_state: int = None, - memory: Union[str, bool] = "share", - memory_warm_start: pd.DataFrame = None, - ): - """ - Add a new optimization search process with specified parameters. - - Parameters - ---------- - - objective_function: The objective function to optimize. - - search_space: Dictionary defining the search space for optimization. - - n_iter: Number of iterations for the optimization process. - - search_id: Identifier for the search process (default: None). - - optimizer: The optimizer to use for the search process (default: "default"). - - n_jobs: Number of parallel jobs to run (default: 1). - - initialize: Dictionary specifying initialization parameters - (default: {"grid": 4, "random": 2, "vertices": 4}). - - constraints: List of constraint functions (default: None). - - pass_through: Dictionary of additional parameters to pass through - (default: None). - - callbacks: Dictionary of callback functions (default: None). - - catch: Dictionary of exceptions to catch during optimization (default: None). - - max_score: Maximum score to achieve (default: None). - - early_stopping: Dictionary specifying early stopping criteria (default: None). - - random_state: Seed for random number generation (default: None). - - memory: Option to share memory between processes (default: "share"). - - memory_warm_start: DataFrame containing warm start memory (default: None). - """ - self.check_list(search_space) - - constraints = constraints or [] - pass_through = pass_through or {} - callbacks = callbacks or {} - catch = catch or {} - early_stopping = early_stopping or {} - - optimizer = self._default_opt(optimizer) - search_id = self._default_search_id(search_id, objective_function) - s_space = SearchSpace(search_space) - - optimizer.setup_search( - objective_function=objective_function, - s_space=s_space, - n_iter=n_iter, - initialize=initialize, - constraints=constraints, - pass_through=pass_through, - callbacks=callbacks, - catch=catch, - max_score=max_score, - early_stopping=early_stopping, - random_state=random_state, - memory=memory, - memory_warm_start=memory_warm_start, - verbosity=self.verbosity, - ) - - n_jobs = mp.cpu_count() if n_jobs == -1 else n_jobs - - for _ in range(n_jobs): - nth_process = len(self.opt_pros) - self.opt_pros[nth_process] = optimizer - - def _print_info(self): - print_res = PrintResults(self.opt_pros, self.verbosity) - - if self.verbosity: - for _ in range(len(self.opt_pros)): - print("") - - for results in self.results_list: - nth_process = results["nth_process"] - print_res.print_process(results, nth_process) - - def run(self, max_time: float = None): - """ - Run the optimization process with an optional maximum time limit. - - Args: - max_time (float, optional): Maximum time limit for the optimization - process. Defaults to None. - """ - self._create_shared_memory() - - for opt in self.opt_pros.values(): - opt.max_time = max_time - - self.results_list = run_search( - self.opt_pros, self.distribution, self.n_processes - ) - - self.results_ = Results(self.results_list, self.opt_pros) - - self._print_info() - - def best_para(self, id_): - """ - Retrieve the best parameters for a specific ID from the results. - - Parameters - ---------- - - id_ (int): The ID of the parameters to retrieve. - - Returns - ------- - - Union[dict[str, Union[int, float]], None]: The best parameters for the - specified ID if found, otherwise None. - - Raises - ------ - - ValueError: If the objective function name is not recognized. - """ - return self.results_.best_para(id_) - - def best_score(self, id_): - """ - Return the best score for a specific ID from the results. - - Parameters - ---------- - - id_ (int): The ID for which the best score is requested. - """ - return self.results_.best_score(id_) - - def search_data(self, id_, times=False): - """Retrieve search data for a specific ID from the results. - - Optionally exclude evaluation and iteration times if 'times' is set to False. - - Parameters - ---------- - - id_ (int): The ID of the search data to retrieve. - - times (bool, optional): Whether to exclude evaluation and iteration times. - Defaults to False. - - Returns - ------- - - pd.DataFrame: The search data for the specified ID. - - columns are - - * "score" : float - The score of the search - * "n_columns" : int - The number of columns in the search space - * "metadata" : dict - The metadata returned by the search - - each row is a search iteration - - index is RangeIndex - """ - search_data_ = self.results_.search_data(id_) - - if not times: - search_data_.drop( - labels=["eval_times", "iter_times"], - axis=1, - inplace=True, - errors="ignore", - ) - return search_data_ diff --git a/src/hyperactive/integrations/__init__.py b/src/hyperactive/integrations/__init__.py index ac47e672..224f815b 100644 --- a/src/hyperactive/integrations/__init__.py +++ b/src/hyperactive/integrations/__init__.py @@ -3,9 +3,8 @@ copyright: hyperactive developers, MIT License (see LICENSE file) """ -from hyperactive.integrations.sklearn import HyperactiveSearchCV, OptCV +from hyperactive.integrations.sklearn import OptCV __all__ = [ - "HyperactiveSearchCV", "OptCV", ] diff --git a/src/hyperactive/integrations/sklearn/__init__.py b/src/hyperactive/integrations/sklearn/__init__.py index 461bccc6..d29cb0b6 100644 --- a/src/hyperactive/integrations/sklearn/__init__.py +++ b/src/hyperactive/integrations/sklearn/__init__.py @@ -3,10 +3,8 @@ copyright: hyperactive developers, MIT License (see LICENSE file) """ -from .hyperactive_search_cv import HyperactiveSearchCV from .opt_cv import OptCV __all__ = [ - "HyperactiveSearchCV", "OptCV", ] diff --git a/src/hyperactive/integrations/sklearn/hyperactive_search_cv.py b/src/hyperactive/integrations/sklearn/hyperactive_search_cv.py deleted file mode 100644 index 903d2442..00000000 --- a/src/hyperactive/integrations/sklearn/hyperactive_search_cv.py +++ /dev/null @@ -1,171 +0,0 @@ -"""Hyperactive cross-validation search for scikit-learn integration. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -from collections.abc import Callable -from typing import Union - -from sklearn.base import BaseEstimator, clone -from sklearn.base import BaseEstimator as SklearnBaseEstimator -from sklearn.metrics import check_scoring - -from hyperactive import Hyperactive -from hyperactive.experiment.integrations.sklearn_cv import SklearnCvExperiment - -from ...optimizers import RandomSearchOptimizer -from ._compat import _check_method_params, _safe_refit, _safe_validate_X_y -from .best_estimator import BestEstimator as _BestEstimator_ -from .checks import Checks - - -class HyperactiveSearchCV(BaseEstimator, _BestEstimator_, Checks): - """HyperactiveSearchCV class for hyperparameter tuning with sklearn. - - This class provides a hyperparameter tuning interface compatible with sklearn. - - Parameters - ---------- - - estimator: SklearnBaseEstimator - The estimator to be tuned. - - params_config: dict[str, list] - Dictionary containing the hyperparameter search space. - - optimizer: Union[str, type[RandomSearchOptimizer]], optional - The optimizer to be used for hyperparameter search, default is "default". - - n_iter: int, optional - Number of parameter settings that are sampled, default is 100. - - scoring: Callable | str | None, optional - Scoring method to evaluate the predictions on the test set. - - n_jobs: int, optional - Number of jobs to run in parallel, default is 1. - - random_state: int | None, optional - Random seed for reproducibility. - - refit: bool, optional - Refit the best estimator with the entire dataset, default is True. - - cv: int | "BaseCrossValidator" | Iterable | None, optional - Determines the cross-validation splitting strategy. - - Methods - ------- - - fit(X, y, **fit_params) - Fit the estimator and tune hyperparameters. - - score(X, y, **params) - Return the score of the best estimator on the input data. - """ - - _required_parameters = ["estimator", "optimizer", "params_config"] - - def __init__( - self, - estimator: "SklearnBaseEstimator", - params_config: dict[str, list], - optimizer: Union[str, type[RandomSearchOptimizer]] = "default", - n_iter: int = 100, - *, - scoring: Union[Callable, str, None] = None, - n_jobs: int = 1, - random_state: Union[int, None] = None, - refit: bool = True, - cv=None, - ): - super().__init__() - - self.estimator = estimator - self.params_config = params_config - self.optimizer = optimizer - self.n_iter = n_iter - self.scoring = scoring - self.n_jobs = n_jobs - self.random_state = random_state - self.refit = refit - self.cv = cv - - def _refit(self, X, y=None, **fit_params): - self.best_estimator_ = clone(self.estimator).set_params( - **clone(self.best_params_, safe=False) - ) - - self.best_estimator_.fit(X, y, **fit_params) - return self - - def _check_data(self, X, y): - return _safe_validate_X_y(self, X, y) - - @Checks.verify_fit - def fit(self, X, y, **fit_params): - """ - Fit the estimator using the provided training data. - - Parameters - ---------- - - X: array-like or sparse matrix, shape (n_samples, n_features) - The training input samples. - - y: array-like, shape (n_samples,) or (n_samples, n_outputs) - The target values. - - **fit_params: dict of string -> object - Additional fit parameters. - - Returns - ------- - - self: object - Returns the instance itself. - """ - X, y = self._check_data(X, y) - - fit_params = _check_method_params(X, params=fit_params) - self.scorer_ = check_scoring(self.estimator, scoring=self.scoring) - - experiment = SklearnCvExperiment( - estimator=self.estimator, - scoring=self.scorer_, - cv=self.cv, - X=X, - y=y, - ) - objective_function = experiment.score - - hyper = Hyperactive(verbosity=False) - hyper.add_search( - objective_function, - search_space=self.params_config, - optimizer=self.optimizer, - n_iter=self.n_iter, - n_jobs=self.n_jobs, - random_state=self.random_state, - ) - hyper.run() - - self.best_params_ = hyper.best_para(objective_function) - self.best_score_ = hyper.best_score(objective_function) - self.search_data_ = hyper.search_data(objective_function) - - _safe_refit(self, X, y, fit_params) - - return self - - def score(self, X, y=None, **params): - """ - Calculate the score of the best estimator on the input data. - - Parameters - ---------- - - X: array-like or sparse matrix of shape (n_samples, n_features) - The input samples. - - y: array-like of shape (n_samples,), default=None - The target values. - - **params: dict - Additional parameters to be passed to the scoring function. - - Returns - ------- - - float - The score of the best estimator on the input data. - """ - return self.scorer_(self.best_estimator_, X, y, **params) - - @property - def fit_successful(self): - """Fit Successful function.""" - self._fit_successful diff --git a/src/hyperactive/optimizers/__init__.py b/src/hyperactive/optimizers/__init__.py deleted file mode 100644 index c1a087b7..00000000 --- a/src/hyperactive/optimizers/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Optimizers package for Hyperactive. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -from .optimizers import * # noqa: F403 diff --git a/src/hyperactive/optimizers/constraint.py b/src/hyperactive/optimizers/constraint.py deleted file mode 100644 index bec8e764..00000000 --- a/src/hyperactive/optimizers/constraint.py +++ /dev/null @@ -1,27 +0,0 @@ -"""constraint module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - - -def gfo2hyper(search_space, para): - """Gfo2Hyper function.""" - values_dict = {} - for key, values in search_space.items(): - pos_ = int(para[key]) - values_dict[key] = values[pos_] - - return values_dict - - -class Constraint: - """Constraint class.""" - - def __init__(self, constraint, search_space): - self.constraint = constraint - self.search_space = search_space - - def __call__(self, para): - """Call constraint on parameters.""" - para = gfo2hyper(self.search_space, para) - return self.constraint(para) diff --git a/src/hyperactive/optimizers/dictionary.py b/src/hyperactive/optimizers/dictionary.py deleted file mode 100644 index f31d0b2b..00000000 --- a/src/hyperactive/optimizers/dictionary.py +++ /dev/null @@ -1,23 +0,0 @@ -"""dictionary module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - - -class DictClass: - """DictClass class.""" - - def __init__(self): - self.para_dict = {} - - def __getitem__(self, key): - """Get item from parameter dictionary.""" - return self.para_dict[key] - - def keys(self): - """Keys function.""" - return self.para_dict.keys() - - def values(self): - """Values function.""" - return self.para_dict.values() diff --git a/src/hyperactive/optimizers/hyper_gradient_conv.py b/src/hyperactive/optimizers/hyper_gradient_conv.py deleted file mode 100644 index b7161d93..00000000 --- a/src/hyperactive/optimizers/hyper_gradient_conv.py +++ /dev/null @@ -1,137 +0,0 @@ -"""hyper_gradient_conv module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - -import numpy as np -import pandas as pd - - -class HyperGradientConv: - """HyperGradientConv class.""" - - def __init__(self, s_space): - self.s_space = s_space - - def value2position(self, value: list) -> list: - """Convert values to positions.""" - return [ - np.abs(v - np.array(space_dim)).argmin() - for v, space_dim in zip(value, self.s_space.values_l) - ] - - def value2para(self, value: list) -> dict: - """Convert values to parameters.""" - return {key: p for key, p in zip(self.s_space.dim_keys, value)} - - def para2value(self, para: dict) -> list: - """Convert parameters to values.""" - return [para[para_name] for para_name in self.s_space.dim_keys] - - def position2value(self, position): - """Position2Value function.""" - return [ - space_dim[pos] for pos, space_dim in zip(position, self.s_space.values_l) - ] - - def para_func2str(self, para): - """Para Func2Str function.""" - return { - dim_key: ( - para[dim_key].__name__ - if self.s_space.data_types[dim_key] != "number" - else para[dim_key] - ) - for dim_key in self.s_space.dim_keys - } - - def value_func2str(self, value): - """Value Func2Str function.""" - try: - return value.__name__ - except AttributeError: - return value - - def conv_para(self, para_hyper): - """Conv Para function.""" - para_gfo = {} - for para in self.s_space.dim_keys: - value_hyper = para_hyper[para] - space_dim = list(self.s_space.func2str[para]) - - if self.s_space.data_types[para] == "number": - value_gfo = np.abs(value_hyper - np.array(space_dim)).argmin() - else: - value_hyper = self.value_func2str(value_hyper) - - if value_hyper in space_dim: - value_gfo = space_dim.index(value_hyper) - else: - raise ValueError(f"'{value_hyper}' was not found in '{para}'") - - para_gfo[para] = value_gfo - return para_gfo - - def conv_initialize(self, initialize): - """Conv Initialize function.""" - if "warm_start" in initialize: - warm_start_l = initialize["warm_start"] - warm_start_gfo = [self.conv_para(warm_start) for warm_start in warm_start_l] - initialize["warm_start"] = warm_start_gfo - - return initialize - - def get_list_positions(self, list1_values, search_dim): - """Get List Positions function.""" - return [search_dim.index(value2) for value2 in list1_values] - - def values2positions(self, values, search_dim): - """Values2Positions function.""" - return np.array(search_dim).searchsorted(values) - - def positions2results(self, positions): - """Positions2Results function.""" - results_dict = {} - - for para_name in self.s_space.dim_keys: - values_list = self.s_space[para_name] - pos_ = positions[para_name].values - values_ = [values_list[idx] for idx in pos_] - results_dict[para_name] = values_ - - results = pd.DataFrame.from_dict(results_dict) - - diff_list = np.setdiff1d(positions.columns, results.columns) - results[diff_list] = positions[diff_list] - - return results - - def conv_memory_warm_start(self, results): - """Conv Memory Warm Start function.""" - if results is None: - return results - - results.reset_index(inplace=True, drop=True) - - df_positions_dict = {} - for dim_key in self.s_space.dim_keys: - result_dim_values = list(results[dim_key].values) - search_dim = self.s_space.func2str[dim_key] - - if self.s_space.data_types[dim_key] == "object": - result_dim_values = [ - self.value_func2str(value) for value in result_dim_values - ] - - list1_positions = self.get_list_positions(result_dim_values, search_dim) - else: - list1_positions = self.values2positions(result_dim_values, search_dim) - - df_positions_dict[dim_key] = list1_positions - - results_new = pd.DataFrame(df_positions_dict) - - results_new["score"] = results["score"] - results_new.dropna(how="any", inplace=True) - - return results_new diff --git a/src/hyperactive/optimizers/hyper_optimizer.py b/src/hyperactive/optimizers/hyper_optimizer.py deleted file mode 100644 index 156b0010..00000000 --- a/src/hyperactive/optimizers/hyper_optimizer.py +++ /dev/null @@ -1,176 +0,0 @@ -"""hyper_optimizer module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - -from .constraint import Constraint -from .hyper_gradient_conv import HyperGradientConv -from .objective_function import ObjectiveFunction -from .optimizer_attributes import OptimizerAttributes - - -class HyperOptimizer(OptimizerAttributes): - """HyperOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__() - self.opt_params = opt_params - - def setup_search( - self, - objective_function, - s_space, - n_iter, - initialize, - constraints, - pass_through, - callbacks, - catch, - max_score, - early_stopping, - random_state, - memory, - memory_warm_start, - verbosity, - ): - """Set up search parameters.""" - self.objective_function = objective_function - self.s_space = s_space - self.n_iter = n_iter - - self.initialize = initialize - self.constraints = constraints - self.pass_through = pass_through - self.callbacks = callbacks - self.catch = catch - self.max_score = max_score - self.early_stopping = early_stopping - self.random_state = random_state - self.memory = memory - self.memory_warm_start = memory_warm_start - self.verbosity = verbosity - - if "progress_bar" in self.verbosity: - self.verbosity = ["progress_bar"] - else: - self.verbosity = [] - - def convert_results2hyper(self): - """Convert Results2Hyper function.""" - self.eval_times = sum(self.gfo_optimizer.eval_times) - self.iter_times = sum(self.gfo_optimizer.iter_times) - - if self.gfo_optimizer.best_para is not None: - value = self.hg_conv.para2value(self.gfo_optimizer.best_para) - position = self.hg_conv.position2value(value) - best_para = self.hg_conv.value2para(position) - self.best_para = best_para - else: - self.best_para = None - - self.best_score = self.gfo_optimizer.best_score - self.positions = self.gfo_optimizer.search_data - self.search_data = self.hg_conv.positions2results(self.positions) - - results_dd = self.gfo_optimizer.search_data.drop_duplicates( - subset=self.s_space.dim_keys, keep="first" - ) - self.memory_values_df = results_dd[ - self.s_space.dim_keys + ["score"] - ].reset_index(drop=True) - - def _setup_process(self, nth_process): - self.nth_process = nth_process - - self.hg_conv = HyperGradientConv(self.s_space) - - initialize = self.hg_conv.conv_initialize(self.initialize) - search_space_positions = self.s_space.positions - - # conv warm start for smbo from values into positions - if "warm_start_smbo" in self.opt_params: - self.opt_params["warm_start_smbo"] = self.hg_conv.conv_memory_warm_start( - self.opt_params["warm_start_smbo"] - ) - - gfo_constraints = [ - Constraint(constraint, self.s_space) for constraint in self.constraints - ] - - self.gfo_optimizer = self.optimizer_class( - search_space=search_space_positions, - initialize=initialize, - constraints=gfo_constraints, - random_state=self.random_state, - nth_process=nth_process, - **self.opt_params, - ) - - self.conv = self.gfo_optimizer.conv - - def search(self, nth_process, p_bar): - """Search function.""" - self._setup_process(nth_process) - - gfo_wrapper_model = ObjectiveFunction( - objective_function=self.objective_function, - optimizer=self.gfo_optimizer, - callbacks=self.callbacks, - catch=self.catch, - nth_process=self.nth_process, - ) - gfo_wrapper_model.pass_through = self.pass_through - - memory_warm_start = self.hg_conv.conv_memory_warm_start(self.memory_warm_start) - - gfo_objective_function = gfo_wrapper_model(self.s_space()) - - self.gfo_optimizer.init_search( - gfo_objective_function, - self.n_iter, - self.max_time, - self.max_score, - self.early_stopping, - self.memory, - memory_warm_start, - False, - ) - for nth_iter in range(self.n_iter): - if p_bar: - p_bar.set_description( - "[" - + str(nth_process) - + "] " - + str(self.objective_function.__name__) - + " (" - + self.optimizer_class.name - + ")", - ) - - self.gfo_optimizer.search_step(nth_iter) - if self.gfo_optimizer.stop.check(): - break - - if p_bar: - p_bar.set_postfix( - best_score=str(gfo_wrapper_model.optimizer.score_best), - best_pos=str(gfo_wrapper_model.optimizer.pos_best), - best_iter=str(gfo_wrapper_model.optimizer.p_bar._best_since_iter), - ) - - p_bar.update(1) - p_bar.refresh() - - self.gfo_optimizer.finish_search() - - self.convert_results2hyper() - - self._add_result_attributes( - self.best_para, - self.best_score, - self.gfo_optimizer.p_bar._best_since_iter, - self.eval_times, - self.iter_times, - self.search_data, - self.gfo_optimizer.random_seed, - ) diff --git a/src/hyperactive/optimizers/objective_function.py b/src/hyperactive/optimizers/objective_function.py deleted file mode 100644 index 2b4e38d6..00000000 --- a/src/hyperactive/optimizers/objective_function.py +++ /dev/null @@ -1,57 +0,0 @@ -"""objective_function module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - -from .dictionary import DictClass - - -def gfo2hyper(search_space, para): - """Gfo2Hyper function.""" - values_dict = {} - for _, key in enumerate(search_space.keys()): - pos_ = int(para[key]) - values_dict[key] = search_space[key][pos_] - - return values_dict - - -class ObjectiveFunction(DictClass): - """ObjectiveFunction class.""" - - def __init__(self, objective_function, optimizer, callbacks, catch, nth_process): - super().__init__() - - self.objective_function = objective_function - self.optimizer = optimizer - self.callbacks = callbacks - self.catch = catch - self.nth_process = nth_process - - self.nth_iter = 0 - - def run_callbacks(self, type_): - """Run Callbacks function.""" - if self.callbacks and type_ in self.callbacks: - [callback(self) for callback in self.callbacks[type_]] - - def __call__(self, search_space): - """Make object callable with search space.""" - - # wrapper for GFOs - def _model(para): - self.nth_iter = len(self.optimizer.pos_l) - para = gfo2hyper(search_space, para) - self.para_dict = para - - try: - self.run_callbacks("before") - results = self.objective_function(self) - self.run_callbacks("after") - except tuple(self.catch.keys()) as e: - results = self.catch[e.__class__] - - return results - - _model.__name__ = self.objective_function.__name__ - return _model diff --git a/src/hyperactive/optimizers/optimizer_attributes.py b/src/hyperactive/optimizers/optimizer_attributes.py deleted file mode 100644 index a72a20ce..00000000 --- a/src/hyperactive/optimizers/optimizer_attributes.py +++ /dev/null @@ -1,35 +0,0 @@ -"""optimizer_attributes module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - - -class OptimizerAttributes: - """OptimizerAttributes class.""" - - def __init__(self): - self.best_para = None - self.best_score = None - self.best_since_iter = None - self.eval_times = None - self.iter_times = None - self.search_data = None - self.random_seed = None - - def _add_result_attributes( - self, - best_para, - best_score, - best_since_iter, - eval_times, - iter_times, - search_data, - random_seed, - ): - self.best_para = best_para - self.best_score = best_score - self.best_since_iter = best_since_iter - self.eval_times = eval_times - self.iter_times = iter_times - self.search_data = search_data - self.random_seed = random_seed diff --git a/src/hyperactive/optimizers/optimizers.py b/src/hyperactive/optimizers/optimizers.py deleted file mode 100644 index 34dce684..00000000 --- a/src/hyperactive/optimizers/optimizers.py +++ /dev/null @@ -1,260 +0,0 @@ -"""optimizers module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - -from gradient_free_optimizers import ( - BayesianOptimizer as _BayesianOptimizer, -) -from gradient_free_optimizers import ( - DifferentialEvolutionOptimizer as _DifferentialEvolutionOptimizer, -) -from gradient_free_optimizers import ( - DirectAlgorithm as _DirectAlgorithm_, -) -from gradient_free_optimizers import ( - DownhillSimplexOptimizer as _DownhillSimplexOptimizer, -) -from gradient_free_optimizers import ( - EnsembleOptimizer as _EnsembleOptimizer, -) -from gradient_free_optimizers import ( - EvolutionStrategyOptimizer as _EvolutionStrategyOptimizer, -) -from gradient_free_optimizers import ( - ForestOptimizer as _ForestOptimizer, -) -from gradient_free_optimizers import ( - GeneticAlgorithmOptimizer as _GeneticAlgorithmOptimizer, -) -from gradient_free_optimizers import ( - GridSearchOptimizer as _GridSearchOptimizer, -) -from gradient_free_optimizers import ( - HillClimbingOptimizer as _HillClimbingOptimizer, -) -from gradient_free_optimizers import ( - LipschitzOptimizer as _LipschitzOptimizer_, -) -from gradient_free_optimizers import ( - ParallelTemperingOptimizer as _ParallelTemperingOptimizer, -) -from gradient_free_optimizers import ( - ParticleSwarmOptimizer as _ParticleSwarmOptimizer, -) -from gradient_free_optimizers import ( - PatternSearch as _PatternSearch, -) -from gradient_free_optimizers import ( - PowellsMethod as _PowellsMethod, -) -from gradient_free_optimizers import ( - RandomAnnealingOptimizer as _RandomAnnealingOptimizer, -) -from gradient_free_optimizers import ( - RandomRestartHillClimbingOptimizer as _RandomRestartHillClimbingOptimizer, -) -from gradient_free_optimizers import ( - RandomSearchOptimizer as _RandomSearchOptimizer, -) -from gradient_free_optimizers import ( - RepulsingHillClimbingOptimizer as _RepulsingHillClimbingOptimizer, -) -from gradient_free_optimizers import ( - SimulatedAnnealingOptimizer as _SimulatedAnnealingOptimizer, -) -from gradient_free_optimizers import ( - SpiralOptimization as _SpiralOptimization_, -) -from gradient_free_optimizers import ( - StochasticHillClimbingOptimizer as _StochasticHillClimbingOptimizer, -) -from gradient_free_optimizers import ( - TreeStructuredParzenEstimators as _TreeStructuredParzenEstimators, -) - -from .hyper_optimizer import HyperOptimizer - - -class HillClimbingOptimizer(HyperOptimizer): - """HillClimbingOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _HillClimbingOptimizer - - -class StochasticHillClimbingOptimizer(HyperOptimizer): - """StochasticHillClimbingOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _StochasticHillClimbingOptimizer - - -class RepulsingHillClimbingOptimizer(HyperOptimizer): - """RepulsingHillClimbingOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _RepulsingHillClimbingOptimizer - - -class SimulatedAnnealingOptimizer(HyperOptimizer): - """SimulatedAnnealingOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _SimulatedAnnealingOptimizer - - -class DownhillSimplexOptimizer(HyperOptimizer): - """DownhillSimplexOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _DownhillSimplexOptimizer - - -class RandomSearchOptimizer(HyperOptimizer): - """RandomSearchOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _RandomSearchOptimizer - - -class GridSearchOptimizer(HyperOptimizer): - """GridSearchOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _GridSearchOptimizer - - -class RandomRestartHillClimbingOptimizer(HyperOptimizer): - """RandomRestartHillClimbingOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _RandomRestartHillClimbingOptimizer - - -class RandomAnnealingOptimizer(HyperOptimizer): - """RandomAnnealingOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _RandomAnnealingOptimizer - - -class PowellsMethod(HyperOptimizer): - """PowellsMethod class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _PowellsMethod - - -class PatternSearch(HyperOptimizer): - """PatternSearch class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _PatternSearch - - -class ParallelTemperingOptimizer(HyperOptimizer): - """ParallelTemperingOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _ParallelTemperingOptimizer - - -class ParticleSwarmOptimizer(HyperOptimizer): - """ParticleSwarmOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _ParticleSwarmOptimizer - - -class SpiralOptimization(HyperOptimizer): - """SpiralOptimization class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _SpiralOptimization_ - - -class GeneticAlgorithmOptimizer(HyperOptimizer): - """GeneticAlgorithmOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _GeneticAlgorithmOptimizer - - -class EvolutionStrategyOptimizer(HyperOptimizer): - """EvolutionStrategyOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _EvolutionStrategyOptimizer - - -class DifferentialEvolutionOptimizer(HyperOptimizer): - """DifferentialEvolutionOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _DifferentialEvolutionOptimizer - - -class BayesianOptimizer(HyperOptimizer): - """BayesianOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _BayesianOptimizer - - -class LipschitzOptimizer(HyperOptimizer): - """LipschitzOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _LipschitzOptimizer_ - - -class DirectAlgorithm(HyperOptimizer): - """DirectAlgorithm class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _DirectAlgorithm_ - - -class TreeStructuredParzenEstimators(HyperOptimizer): - """TreeStructuredParzenEstimators class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _TreeStructuredParzenEstimators - - -class ForestOptimizer(HyperOptimizer): - """ForestOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _ForestOptimizer - - -class EnsembleOptimizer(HyperOptimizer): - """EnsembleOptimizer class.""" - - def __init__(self, **opt_params): - super().__init__(**opt_params) - self.optimizer_class = _EnsembleOptimizer diff --git a/src/hyperactive/optimizers/strategies/__init__.py b/src/hyperactive/optimizers/strategies/__init__.py deleted file mode 100644 index 3cf615ce..00000000 --- a/src/hyperactive/optimizers/strategies/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -"""Optimization strategies package for Hyperactive. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -from .custom_optimization_strategy import CustomOptimizationStrategy -from .optimization_strategy import BaseOptimizationStrategy - -__all__ = [ - "CustomOptimizationStrategy", - "BaseOptimizationStrategy", -] diff --git a/src/hyperactive/optimizers/strategies/custom_optimization_strategy.py b/src/hyperactive/optimizers/strategies/custom_optimization_strategy.py deleted file mode 100644 index 785cde15..00000000 --- a/src/hyperactive/optimizers/strategies/custom_optimization_strategy.py +++ /dev/null @@ -1,26 +0,0 @@ -"""custom_optimization_strategy module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - -from .optimization_strategy import BaseOptimizationStrategy - - -class CustomOptimizationStrategy(BaseOptimizationStrategy): - """CustomOptimizationStrategy class.""" - - def __init__(self): - super().__init__() - - self.optimizer_setup_l = [] - self.duration_sum = 0 - - def add_optimizer(self, optimizer, duration=1, early_stopping=None): - """Add Optimizer function.""" - self.duration_sum += duration - optimizer_setup = { - "optimizer": optimizer, - "duration": duration, - "early_stopping": early_stopping, - } - self.optimizer_setup_l.append(optimizer_setup) diff --git a/src/hyperactive/optimizers/strategies/optimization_strategy.py b/src/hyperactive/optimizers/strategies/optimization_strategy.py deleted file mode 100644 index 4dfc7370..00000000 --- a/src/hyperactive/optimizers/strategies/optimization_strategy.py +++ /dev/null @@ -1,133 +0,0 @@ -"""optimization_strategy module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - -from .optimizer_attributes import OptimizerAttributes - - -class BaseOptimizationStrategy(OptimizerAttributes): - """BaseOptimizationStrategy class.""" - - def __init__(self): - super().__init__() - - def setup_search( - self, - objective_function, - s_space, - n_iter, - initialize, - constraints, - pass_through, - callbacks, - catch, - max_score, - early_stopping, - random_state, - memory, - memory_warm_start, - verbosity, - ): - """Set up search parameters.""" - self.objective_function = objective_function - self.s_space = s_space - self.n_iter = n_iter - - self.initialize = initialize - self.constraints = constraints - self.pass_through = pass_through - self.callbacks = callbacks - self.catch = catch - self.max_score = max_score - self.early_stopping = early_stopping - self.random_state = random_state - self.memory = memory - self.memory_warm_start = memory_warm_start - self.verbosity = verbosity - - self._max_time = None - - if "progress_bar" in self.verbosity: - self.verbosity = [] - else: - self.verbosity = [] - - @property - def max_time(self): - """Max Time function.""" - return self._max_time - - @max_time.setter - def max_time(self, value): - """Max Time function.""" - self._max_time = value - - for optimizer_setup in self.optimizer_setup_l: - optimizer_setup["optimizer"].max_time = value - - def search(self, nth_process, p_bar): - """Search function.""" - for optimizer_setup in self.optimizer_setup_l: - hyper_opt = optimizer_setup["optimizer"] - duration = optimizer_setup["duration"] - opt_strat_early_stopping = optimizer_setup["early_stopping"] - - if opt_strat_early_stopping: - early_stopping = opt_strat_early_stopping - else: - early_stopping = self.early_stopping - - n_iter = round(self.n_iter * duration / self.duration_sum) - - # initialize - if self.best_para is not None: - initialize = {} - if "warm_start" in initialize: - initialize["warm_start"].append(self.best_para) - else: - initialize["warm_start"] = [self.best_para] - else: - initialize = dict(self.initialize) - - # memory_warm_start - if self.search_data is not None: - memory_warm_start = self.search_data - else: - memory_warm_start = self.memory_warm_start - - # warm_start_smbo - if ( - hyper_opt.optimizer_class.optimizer_type == "sequential" - and self.search_data is not None - ): - hyper_opt.opt_params["warm_start_smbo"] = self.search_data - - hyper_opt.setup_search( - objective_function=self.objective_function, - s_space=self.s_space, - n_iter=n_iter, - initialize=initialize, - constraints=self.constraints, - pass_through=self.pass_through, - callbacks=self.callbacks, - catch=self.catch, - max_score=self.max_score, - early_stopping=early_stopping, - random_state=self.random_state, - memory=self.memory, - memory_warm_start=memory_warm_start, - verbosity=self.verbosity, - ) - - hyper_opt.search(nth_process, p_bar) - - self._add_result_attributes( - hyper_opt.best_para, - hyper_opt.best_score, - hyper_opt.best_since_iter, - hyper_opt.eval_times, - hyper_opt.iter_times, - hyper_opt.search_data, - hyper_opt.gfo_optimizer.random_seed, - ) diff --git a/src/hyperactive/optimizers/strategies/optimizer_attributes.py b/src/hyperactive/optimizers/strategies/optimizer_attributes.py deleted file mode 100644 index d23ef375..00000000 --- a/src/hyperactive/optimizers/strategies/optimizer_attributes.py +++ /dev/null @@ -1,67 +0,0 @@ -"""optimizer_attributes module for Hyperactive optimization.""" - -# Email: simon.blanke@yahoo.com -# License: MIT License - -import pandas as pd - - -class OptimizerAttributes: - """OptimizerAttributes class.""" - - def __init__(self): - self.best_para = None - self.best_score = None - self.best_since_iter = None - self.eval_times = None - self.iter_times = None - self.search_data = None - self.random_seed = None - - def _add_result_attributes( - self, - best_para, - best_score, - best_since_iter, - eval_times, - iter_times, - search_data, - random_seed, - ): - if self.best_para is None: - self.best_para = best_para - else: - if best_score > self.best_score: - self.best_para = best_para - - if self.best_score is None: - self.best_score = best_score - else: - if best_score > self.best_score: - self.best_score = best_score - - if self.best_since_iter is None: - self.best_since_iter = best_since_iter - else: - if best_score > self.best_score: - self.best_since_iter = best_since_iter - - if self.eval_times is None: - self.eval_times = eval_times - else: - self.eval_times = self.eval_times + eval_times - - if self.iter_times is None: - self.iter_times = iter_times - else: - self.iter_times = self.iter_times + eval_times - - if self.search_data is None: - self.search_data = search_data - else: - self.search_data = pd.concat( - [self.search_data, search_data], ignore_index=True - ) - - if self.random_seed is None: - self.random_seed = random_seed diff --git a/src/hyperactive/print_results.py b/src/hyperactive/print_results.py deleted file mode 100644 index a7251f69..00000000 --- a/src/hyperactive/print_results.py +++ /dev/null @@ -1,177 +0,0 @@ -"""Results printing utilities for hyperparameter optimization. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -import logging - -import numpy as np - -indent = " " - - -class PrintResults: - """PrintResults class.""" - - def __init__(self, opt_pros, verbosity): - self.opt_pros = opt_pros - self.verbosity = verbosity - - def _print_times(self, eval_time, iter_time, n_iter): - opt_time = iter_time - eval_time - iterPerSec = n_iter / iter_time - - print( - indent, - "Evaluation time :", - eval_time, - "sec", - indent, - f"[{round(eval_time / iter_time * 100, 2)} %]", - ) - print( - indent, - "Optimization time :", - opt_time, - "sec", - indent, - f"[{round(opt_time / iter_time * 100, 2)} %]", - ) - if iterPerSec >= 1: - print( - indent, - "Iteration time :", - iter_time, - "sec", - indent, - f"[{round(iterPerSec, 2)} iter/sec]", - ) - else: - secPerIter = iter_time / n_iter - print( - indent, - "Iteration time :", - iter_time, - "sec", - indent, - f"[{round(secPerIter, 2)} sec/iter]", - ) - print(" ") - - def align_para_names(self, para_names): - """Align Para Names function.""" - str_lengths = [len(str_) for str_ in para_names] - max_length = max(str_lengths) - - para_names_align = {} - for para_name, str_length in zip(para_names, str_lengths): - added_spaces = max_length - str_length - para_names_align[para_name] = " " * added_spaces - - return para_names_align - - def _print_results( - self, - objective_function, - best_score, - best_para, - best_iter, - best_additional_results, - random_seed, - ): - print(f"\nResults: '{objective_function.__name__}'", " ") - if best_para is None: - print(indent, "Best score:", best_score, " ") - print(indent, "Best parameter set:", best_para, " ") - print(indent, "Best iteration:", best_iter, " ") - - else: - print(indent, "Best score:", best_score, " ") - - if best_additional_results: - print(indent, "Best additional results:") - add_results_names = list(best_additional_results.keys()) - add_results_names_align = self.align_para_names(add_results_names) - - for best_additional_result in best_additional_results.keys(): - added_spaces = add_results_names_align[best_additional_result] - print( - indent, - indent, - f"'{best_additional_result}'", - f"{added_spaces}:", - best_additional_results[best_additional_result], - " ", - ) - - if best_para: - print(indent, "Best parameter set:") - para_names = list(best_para.keys()) - para_names_align = self.align_para_names(para_names) - - for para_key in best_para.keys(): - added_spaces = para_names_align[para_key] - print( - indent, - indent, - f"'{para_key}'", - f"{added_spaces}:", - best_para[para_key], - " ", - ) - - print(indent, "Best iteration:", best_iter, " ") - - print(" ") - print(indent, "Random seed:", random_seed, " ") - print(" ") - - def print_process(self, results, nth_process): - """Print Process function.""" - verbosity = self.verbosity - objective_function = self.opt_pros[nth_process].objective_function - search_space = self.opt_pros[nth_process].s_space.search_space - - search_data = results["search_data"] - - try: - best_sample = search_data.iloc[search_data["score"].idxmax()] - - except TypeError: - logging.warning( - "Warning: Cannot index by location index with a non-integer key" - ) - - else: - best_score = best_sample["score"] - best_values = best_sample[list(search_space.keys())] - best_para = dict(zip(list(search_space.keys()), best_values)) - best_additional_results_df = best_sample.drop( - ["score"] + list(search_space.keys()) - ) - best_additional_results = best_additional_results_df.to_dict() - - best_iter = results["best_iter"] - eval_times = results["eval_times"] - iter_times = results["iter_times"] - random_seed = results["random_seed"] - - n_iter = self.opt_pros[nth_process].n_iter - - eval_time = np.array(eval_times).sum() - iter_time = np.array(iter_times).sum() - - if "print_results" in verbosity: - self._print_results( - objective_function, - best_score, - best_para, - best_iter, - best_additional_results, - random_seed, - ) - - if "print_times" in verbosity: - self._print_times(eval_time, iter_time, n_iter) diff --git a/src/hyperactive/process.py b/src/hyperactive/process.py deleted file mode 100644 index 0e550d1d..00000000 --- a/src/hyperactive/process.py +++ /dev/null @@ -1,38 +0,0 @@ -"""Process handling for hyperparameter optimization. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -from tqdm import tqdm - - -def _process_(nth_process, optimizer): - if "progress_bar" in optimizer.verbosity: - p_bar = tqdm( - position=nth_process, - total=optimizer.n_iter, - ascii=" ─", - colour="Yellow", - ) - else: - p_bar = None - - optimizer.search(nth_process, p_bar) - - if p_bar: - p_bar.colour = "GREEN" - p_bar.refresh() - p_bar.close() - - return { - "nth_process": nth_process, - "best_para": optimizer.best_para, - "best_score": optimizer.best_score, - "best_iter": optimizer.best_since_iter, - "eval_times": optimizer.eval_times, - "iter_times": optimizer.iter_times, - "search_data": optimizer.search_data, - "random_seed": optimizer.random_seed, - } diff --git a/src/hyperactive/results.py b/src/hyperactive/results.py deleted file mode 100644 index f793354e..00000000 --- a/src/hyperactive/results.py +++ /dev/null @@ -1,93 +0,0 @@ -"""Results handling for hyperparameter optimization. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -import numpy as np -import pandas as pd - - -class Results: - """Results class.""" - - def __init__(self, results_list, opt_pros): - self.results_list = results_list - self.opt_pros = opt_pros - - self.objFunc2results = {} - self.search_id2results = {} - - def _sort_results_objFunc(self, objective_function): - best_score = -np.inf - best_para = None - search_data = None - - search_data_list = [] - - for results_ in self.results_list: - nth_process = results_["nth_process"] - - opt = self.opt_pros[nth_process] - objective_function_ = opt.objective_function - search_space_ = opt.s_space() - params = list(search_space_.keys()) - - if objective_function_ != objective_function: - continue - - if results_["best_score"] > best_score: - best_score = results_["best_score"] - best_para = results_["best_para"] - - search_data = results_["search_data"] - search_data["eval_times"] = results_["eval_times"] - search_data["iter_times"] = results_["iter_times"] - - search_data_list.append(search_data) - - if len(search_data_list) > 0: - search_data = pd.concat(search_data_list) - - self.objFunc2results[objective_function] = { - "best_para": best_para, - "best_score": best_score, - "search_data": search_data, - "params": params, - } - - def _get_result(self, id_, result_name): - if id_ not in self.objFunc2results: - self._sort_results_objFunc(id_) - - search_data = self.objFunc2results[id_][result_name] - - return search_data - - def best_para(self, id_): - """Best Para function.""" - best_para_ = self._get_result(id_, "best_para") - - if best_para_ is not None: - return best_para_ - - raise ValueError("objective function name not recognized") - - def best_score(self, id_): - """Best Score function.""" - best_score_ = self._get_result(id_, "best_score") - - if best_score_ != -np.inf: - return best_score_ - - raise ValueError("objective function name not recognized") - - def search_data(self, id_): - """Search Data function.""" - search_data = self._get_result(id_, "search_data") - - if search_data is not None: - return search_data - - raise ValueError("objective function name not recognized") diff --git a/src/hyperactive/run_search.py b/src/hyperactive/run_search.py deleted file mode 100644 index b6a21b11..00000000 --- a/src/hyperactive/run_search.py +++ /dev/null @@ -1,57 +0,0 @@ -"""Search execution utilities for hyperparameter optimization. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -from .distribution import ( - joblib_wrapper, - multiprocessing_wrapper, - pathos_wrapper, - single_process, -) -from .process import _process_ - - -def proxy(args): - """Proxy function.""" - return _process_(*args) - - -dist_dict = { - "joblib": (joblib_wrapper, _process_), - "multiprocessing": (multiprocessing_wrapper, proxy), - "pathos": (pathos_wrapper, proxy), -} - - -def _get_distribution(distribution): - if callable(distribution): - return (distribution, _process_), {} - - elif isinstance(distribution, dict): - dist_key = next(iter(distribution)) - dist_paras = distribution[dist_key] - - return dist_dict[dist_key], dist_paras - - elif isinstance(distribution, str): - return dist_dict[distribution], {} - - -def run_search(opt_pros, distribution, n_processes): - """Run Search function.""" - process_infos = list(opt_pros.items()) - - if n_processes == "auto": - n_processes = len(process_infos) - - if n_processes == 1: - results_list = single_process(_process_, process_infos) - else: - (distribution, process_func), dist_paras = _get_distribution(distribution) - - results_list = distribution(process_func, process_infos, n_processes) - - return results_list diff --git a/src/hyperactive/search_space.py b/src/hyperactive/search_space.py deleted file mode 100644 index 3364267d..00000000 --- a/src/hyperactive/search_space.py +++ /dev/null @@ -1,155 +0,0 @@ -"""Search space utilities for hyperparameter optimization. - -Author: Simon Blanke -Email: simon.blanke@yahoo.com -License: MIT License -""" - -import numpy as np - - -class DictClass: - """DictClass class.""" - - def __init__(self, search_space): - self.search_space = search_space - - def __getitem__(self, key): - """Get item from search space.""" - return self.search_space[key] - - def keys(self): - """Keys function.""" - return self.search_space.keys() - - def values(self): - """Values function.""" - return self.search_space.values() - - def items(self): - """Items function.""" - return self.search_space.items() - - -class SearchSpace(DictClass): - """SearchSpace class.""" - - def __init__(self, search_space): - super().__init__(search_space) - self.search_space = search_space - - self.dim_keys = list(search_space.keys()) - self.values_l = list(self.search_space.values()) - - positions = {} - for key in search_space.keys(): - positions[key] = np.array(range(len(search_space[key]))) - self.positions = positions - - self.check_list() - self.check_non_num_values() - - self.data_types = self.dim_types() - self.func2str = self._create_num_str_ss() - - def __call__(self): - """Return search space dictionary.""" - return self.search_space - - def dim_types(self): - """Dim Types function.""" - data_types = {} - for dim_key in self.dim_keys: - dim_values = np.array(list(self.search_space[dim_key])) - try: - np.subtract(dim_values, dim_values) - np.array(dim_values).searchsorted(dim_values) - except (TypeError, ValueError): - _type_ = "object" - else: - _type_ = "number" - - data_types[dim_key] = _type_ - return data_types - - def _create_num_str_ss(self): - func2str = {} - for dim_key in self.dim_keys: - if self.data_types[dim_key] == "number": - func2str[dim_key] = self.search_space[dim_key] - else: - func2str[dim_key] = [] - - dim_values = self.search_space[dim_key] - for value in dim_values: - try: - func_name = value.__name__ - except AttributeError: - func_name = value - - func2str[dim_key].append(func_name) - return func2str - - def check_list(self): - """Check List function.""" - for dim_key in self.dim_keys: - search_dim = self.search_space[dim_key] - - err_msg = ( - f"\n Value in '{dim_key}' of search space dictionary must be of " - "type list \n" - ) - if not isinstance(search_dim, list): - raise ValueError(err_msg) - - @staticmethod - def is_function(value): - """Is Function function.""" - try: - value.__name__ - except AttributeError: - return False - else: - return True - - @staticmethod - def is_number(value): - """Is Number function.""" - try: - float(value) - value * 0.1 - value - 0.1 - value / 0.1 - except (TypeError, ValueError, ZeroDivisionError): - return False - else: - return True - - def _string_or_object(self, dim_key, dim_values): - for dim_value in dim_values: - is_str = isinstance(dim_value, str) - is_func = self.is_function(dim_value) - is_number = self.is_number(dim_value) - - if not is_str and not is_func and not is_number: - msg = ( - f"\n The value '{dim_value}' of type '{type(dim_value)}' in the " - f"search space dimension '{dim_key}' must be number, string or " - "function \n" - ) - raise ValueError(msg) - - def check_non_num_values(self): - """Check Non Num Values function.""" - for dim_key in self.dim_keys: - dim_values = np.array(list(self.search_space[dim_key])) - - try: - np.subtract(dim_values, dim_values) - np.array(dim_values).searchsorted(dim_values) - except (TypeError, ValueError): - self._string_or_object(dim_key, dim_values) - else: - if dim_values.ndim != 1: - msg = f"Array-like object in '{dim_key}' must be one dimensional" - raise ValueError(msg) diff --git a/tests/_local_test_optimization_strategies/__init__.py b/tests/_local_test_optimization_strategies/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/_local_test_optimization_strategies/_parametrize.py b/tests/_local_test_optimization_strategies/_parametrize.py deleted file mode 100644 index 0ce5d050..00000000 --- a/tests/_local_test_optimization_strategies/_parametrize.py +++ /dev/null @@ -1,109 +0,0 @@ -from hyperactive.optimizers import ( - BayesianOptimizer, - DirectAlgorithm, - DownhillSimplexOptimizer, - EvolutionStrategyOptimizer, - ForestOptimizer, - GridSearchOptimizer, - HillClimbingOptimizer, - LipschitzOptimizer, - ParallelTemperingOptimizer, - ParticleSwarmOptimizer, - PatternSearch, - PowellsMethod, - RandomAnnealingOptimizer, - RandomRestartHillClimbingOptimizer, - RandomSearchOptimizer, - RepulsingHillClimbingOptimizer, - SimulatedAnnealingOptimizer, - SpiralOptimization, - StochasticHillClimbingOptimizer, - TreeStructuredParzenEstimators, -) - -optimizers = ( - "Optimizer", - [ - (HillClimbingOptimizer), - (StochasticHillClimbingOptimizer), - (RepulsingHillClimbingOptimizer), - (SimulatedAnnealingOptimizer), - (DownhillSimplexOptimizer), - (RandomSearchOptimizer), - (GridSearchOptimizer), - (RandomRestartHillClimbingOptimizer), - (RandomAnnealingOptimizer), - (PowellsMethod), - (PatternSearch), - (ParallelTemperingOptimizer), - (ParticleSwarmOptimizer), - (SpiralOptimization), - (EvolutionStrategyOptimizer), - (BayesianOptimizer), - (LipschitzOptimizer), - (DirectAlgorithm), - (TreeStructuredParzenEstimators), - (ForestOptimizer), - ], -) - - -optimizers_strat = ( - "Optimizer_strat", - [ - (HillClimbingOptimizer), - (StochasticHillClimbingOptimizer), - (RepulsingHillClimbingOptimizer), - (SimulatedAnnealingOptimizer), - (DownhillSimplexOptimizer), - (RandomSearchOptimizer), - (GridSearchOptimizer), - (RandomRestartHillClimbingOptimizer), - (RandomAnnealingOptimizer), - (PowellsMethod), - (PatternSearch), - (ParallelTemperingOptimizer), - (ParticleSwarmOptimizer), - (SpiralOptimization), - (EvolutionStrategyOptimizer), - (BayesianOptimizer), - (LipschitzOptimizer), - (DirectAlgorithm), - (TreeStructuredParzenEstimators), - (ForestOptimizer), - ], -) - - -optimizers_non_smbo = ( - "Optimizer_non_smbo", - [ - (HillClimbingOptimizer), - (StochasticHillClimbingOptimizer), - (RepulsingHillClimbingOptimizer), - (SimulatedAnnealingOptimizer), - (DownhillSimplexOptimizer), - (RandomSearchOptimizer), - (GridSearchOptimizer), - (RandomRestartHillClimbingOptimizer), - (RandomAnnealingOptimizer), - (PowellsMethod), - (PatternSearch), - (ParallelTemperingOptimizer), - (ParticleSwarmOptimizer), - (SpiralOptimization), - (EvolutionStrategyOptimizer), - ], -) - - -optimizers_smbo = ( - "Optimizer_smbo", - [ - (BayesianOptimizer), - (LipschitzOptimizer), - (DirectAlgorithm), - (TreeStructuredParzenEstimators), - (ForestOptimizer), - ], -) diff --git a/tests/_local_test_optimization_strategies/_test_memory_warm_start.py b/tests/_local_test_optimization_strategies/_test_memory_warm_start.py deleted file mode 100644 index 0ed07622..00000000 --- a/tests/_local_test_optimization_strategies/_test_memory_warm_start.py +++ /dev/null @@ -1,153 +0,0 @@ -import time - -import numpy as np -import pytest - -from hyperactive import Hyperactive -from hyperactive.optimizers import GridSearchOptimizer -from hyperactive.optimizers.strategies import CustomOptimizationStrategy - -from ._parametrize import optimizers_non_smbo - - -def objective_function(opt): - time.sleep(0.01) - score = -(opt["x1"] * opt["x1"]) - return score - - -search_space = { - "x1": list(np.arange(0, 100, 1)), -} - - -def test_memory_Warm_start_0(): - optimizer1 = GridSearchOptimizer() - optimizer2 = GridSearchOptimizer() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.2) - opt_strat.add_optimizer(optimizer2, duration=0.8) - - n_iter = 1000 - - c_time = time.time() - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=True, - ) - hyper.run() - - d_time = time.time() - c_time - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 200 - assert len(optimizer2.search_data) == 800 - - assert optimizer1.best_score <= optimizer2.best_score - - print("\n d_time", d_time) - - assert d_time < 3 - - -def test_memory_Warm_start_1(): - optimizer1 = GridSearchOptimizer() - optimizer2 = GridSearchOptimizer() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.2) - opt_strat.add_optimizer(optimizer2, duration=0.8) - - n_iter = 100 - - search_space = { - "x1": list(np.arange(0, 1, 1)), - } - - c_time = time.time() - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=False, - ) - hyper.run() - - d_time = time.time() - c_time - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 20 - assert len(optimizer2.search_data) == 80 - - assert optimizer1.best_score <= optimizer2.best_score - - print("\n d_time", d_time) - - assert d_time > 0.95 - - -@pytest.mark.parametrize(*optimizers_non_smbo) -def test_memory_Warm_start_2(Optimizer_non_smbo): - optimizer1 = GridSearchOptimizer() - optimizer2 = Optimizer_non_smbo() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.5) - opt_strat.add_optimizer(optimizer2, duration=0.5) - - search_space = { - "x1": list(np.arange(0, 50, 1)), - } - - n_iter = 100 - - c_time = time.time() - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=True, - ) - hyper.run() - - d_time = time.time() - c_time - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 50 - assert len(optimizer2.search_data) == 50 - - assert optimizer1.best_score <= optimizer2.best_score - - print("\n d_time", d_time) - - assert d_time < 0.9 diff --git a/tests/_local_test_optimization_strategies/_test_memory_warm_start_smbo.py b/tests/_local_test_optimization_strategies/_test_memory_warm_start_smbo.py deleted file mode 100644 index 6d8dde6a..00000000 --- a/tests/_local_test_optimization_strategies/_test_memory_warm_start_smbo.py +++ /dev/null @@ -1,55 +0,0 @@ -import time - -import numpy as np -import pytest - -from hyperactive import Hyperactive -from hyperactive.optimizers import GridSearchOptimizer -from hyperactive.optimizers.strategies import CustomOptimizationStrategy - -from ._parametrize import optimizers_smbo - - -def objective_function(opt): - time.sleep(0.01) - score = -(opt["x1"] * opt["x1"]) - return score - - -search_space = { - "x1": list(np.arange(0, 100, 1)), -} - - -@pytest.mark.parametrize(*optimizers_smbo) -def test_memory_Warm_start_smbo_0(Optimizer_smbo): - optimizer1 = GridSearchOptimizer() - optimizer2 = Optimizer_smbo() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.8) - opt_strat.add_optimizer(optimizer2, duration=0.2) - - n_iter = 100 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=True, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 80 - assert len(optimizer2.search_data) == 20 - - assert optimizer1.best_score <= optimizer2.best_score diff --git a/tests/_local_test_optimization_strategies/_test_strategy_combinations.py b/tests/_local_test_optimization_strategies/_test_strategy_combinations.py deleted file mode 100644 index 628abb1e..00000000 --- a/tests/_local_test_optimization_strategies/_test_strategy_combinations.py +++ /dev/null @@ -1,162 +0,0 @@ -import numpy as np -import pytest - -from hyperactive import Hyperactive -from hyperactive.optimizers.strategies import CustomOptimizationStrategy - -from ._parametrize import optimizers, optimizers_strat - - -def objective_function(opt): - score = -(opt["x1"] * opt["x1"] + opt["x2"] * opt["x2"]) - return score - - -search_space = { - "x1": list(np.arange(-3, 3, 1)), - "x2": list(np.arange(-3, 3, 1)), -} - - -@pytest.mark.parametrize(*optimizers) -@pytest.mark.parametrize(*optimizers_strat) -def test_strategy_combinations_0(Optimizer, Optimizer_strat): - optimizer1 = Optimizer() - optimizer2 = Optimizer_strat() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.5) - opt_strat.add_optimizer(optimizer2, duration=0.5) - - n_iter = 30 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=False, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 15 - assert len(optimizer2.search_data) == 15 - - assert optimizer1.best_score <= optimizer2.best_score - - -@pytest.mark.parametrize(*optimizers) -@pytest.mark.parametrize(*optimizers_strat) -def test_strategy_combinations_1(Optimizer, Optimizer_strat): - optimizer1 = Optimizer() - optimizer2 = Optimizer_strat() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.1) - opt_strat.add_optimizer(optimizer2, duration=0.9) - - n_iter = 10 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=False, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 1 - assert len(optimizer2.search_data) == 9 - - assert optimizer1.best_score <= optimizer2.best_score - - -@pytest.mark.parametrize(*optimizers) -@pytest.mark.parametrize(*optimizers_strat) -def test_strategy_combinations_2(Optimizer, Optimizer_strat): - optimizer1 = Optimizer() - optimizer2 = Optimizer_strat() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.9) - opt_strat.add_optimizer(optimizer2, duration=0.1) - - n_iter = 10 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=False, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 9 - assert len(optimizer2.search_data) == 1 - - assert optimizer1.best_score <= optimizer2.best_score - - -@pytest.mark.parametrize(*optimizers) -@pytest.mark.parametrize(*optimizers_strat) -def test_strategy_combinations_3(Optimizer, Optimizer_strat): - optimizer1 = Optimizer() - optimizer2 = Optimizer_strat() - optimizer3 = Optimizer_strat() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=10) - opt_strat.add_optimizer(optimizer2, duration=20) - opt_strat.add_optimizer(optimizer3, duration=30) - - n_iter = 100 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=False, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - optimizer3 = hyper.opt_pros[0].optimizer_setup_l[2]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == round(n_iter * 10 / 60) - assert len(optimizer2.search_data) == round(n_iter * 20 / 60) - assert len(optimizer3.search_data) == round(n_iter * 30 / 60) - - assert optimizer1.best_score <= optimizer2.best_score <= optimizer3.best_score diff --git a/tests/_local_test_optimization_strategies/_test_strategy_multiprocessing.py b/tests/_local_test_optimization_strategies/_test_strategy_multiprocessing.py deleted file mode 100644 index dd378874..00000000 --- a/tests/_local_test_optimization_strategies/_test_strategy_multiprocessing.py +++ /dev/null @@ -1,64 +0,0 @@ -import numpy as np - - -def objective_function(opt): - score = -(opt["x1"] * opt["x1"] + opt["x2"] * opt["x2"]) - return score - - -search_space = { - "x1": list(np.arange(-3, 3, 1)), - "x2": list(np.arange(-3, 3, 1)), -} - -""" -@pytest.mark.parametrize(*optimizers_strat) -def test_strategy_multiprocessing_0(Optimizer_strat): - optimizer1 = RandomSearchOptimizer() - optimizer2 = Optimizer_strat() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.5) - opt_strat.add_optimizer(optimizer2, duration=0.5) - - n_iter = 25 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - n_jobs=2, - ) - hyper.run() - - -@pytest.mark.parametrize(*optimizers_strat) -def test_strategy_multiprocessing_1(Optimizer_strat): - optimizer1 = RandomSearchOptimizer() - optimizer2 = Optimizer_strat() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.5) - opt_strat.add_optimizer(optimizer2, duration=0.5) - - n_iter = 25 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - n_jobs=1, - ) - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - n_jobs=1, - ) - hyper.run() -""" diff --git a/tests/_local_test_timings/__init__.py b/tests/_local_test_timings/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/_local_test_timings/_search_space_list.py b/tests/_local_test_timings/_search_space_list.py deleted file mode 100644 index e83f9d90..00000000 --- a/tests/_local_test_timings/_search_space_list.py +++ /dev/null @@ -1,165 +0,0 @@ -import numpy as np -import pandas as pd - - -def search_space_setup(size=1000): - if size < 1000: - print("Error: Some search spaces cannot be created") - return - - pad_full = list(range(0, size)) - pad_cat = list(range(int(size / 3))) - pad_10 = list(range(int(size**0.1))) - - search_space_0 = { - "x1": pad_full, - } - - search_space_1 = { - "x1": pad_cat, - "x2": list(range(3)), - } - - search_space_2 = { - "x1": pad_10, - "x2": pad_10, - "x3": pad_10, - "x4": pad_10, - "x5": pad_10, - "x6": pad_10, - "x7": pad_10, - "x8": pad_10, - "x9": pad_10, - "x10": pad_10, - } - - search_space_3 = { - "x1": pad_10, - "x2": pad_10, - "x3": pad_10, - "x4": pad_10, - "x5": pad_10, - "x6": pad_10, - "x7": pad_10, - "x8": pad_10, - "x9": pad_10, - "x10": pad_10, - "x11": [1], - "x12": [1], - "x13": [1], - "x14": [1], - "x15": [1], - "x16": [1], - "x17": [1], - "x18": [1], - "x19": [1], - "x20": [1], - } - - search_space_4 = { - "x1": pad_cat, - "str1": ["0", "1", "2"], - } - - def func1(): - pass - - def func2(): - pass - - def func3(): - pass - - search_space_5 = { - "x1": pad_cat, - "func1": [func1, func2, func3], - } - - class class1: - def __init__(self): - pass - - class class2: - def __init__(self): - pass - - class class3: - def __init__(self): - pass - - def wr_func_1(): - return class1() - - def wr_func_2(): - return class2() - - def wr_func_3(): - return class3() - - search_space_6 = { - "x1": pad_cat, - "class_1": [wr_func_1, wr_func_2, wr_func_3], - } - - search_space_7 = { - "x1": pad_cat, - "class_obj_1": [wr_func_1, wr_func_2, wr_func_3], - } - - def wr_func_1(): - return [1, 0, 0] - - def wr_func_2(): - return [0, 1, 0] - - def wr_func_3(): - return [0, 0, 1] - - search_space_8 = { - "x1": pad_cat, - "list_1": [wr_func_1, wr_func_2, wr_func_3], - } - - def wr_func_1(): - return np.array([1, 0, 0]) - - def wr_func_2(): - return np.array([0, 1, 0]) - - def wr_func_3(): - return np.array([0, 0, 1]) - - search_space_9 = { - "x1": pad_cat, - "array_1": [wr_func_1, wr_func_2, wr_func_3], - } - - def wr_func_1(): - return pd.DataFrame(np.array([1, 0, 0])) - - def wr_func_2(): - return pd.DataFrame(np.array([0, 1, 0])) - - def wr_func_3(): - return pd.DataFrame(np.array([0, 0, 1])) - - search_space_10 = { - "x1": pad_cat, - "df_1": [wr_func_1, wr_func_2, wr_func_3], - } - - search_space_list = [ - (search_space_0), - (search_space_1), - (search_space_2), - (search_space_3), - (search_space_4), - (search_space_5), - (search_space_6), - (search_space_7), - (search_space_8), - (search_space_9), - (search_space_10), - ] - - return search_space_list diff --git a/tests/_local_test_timings/_test_memory.py b/tests/_local_test_timings/_test_memory.py deleted file mode 100644 index d3ac4e8b..00000000 --- a/tests/_local_test_timings/_test_memory.py +++ /dev/null @@ -1,159 +0,0 @@ -import time - -import numpy as np -import pandas as pd -from sklearn.datasets import load_breast_cancer -from sklearn.ensemble import GradientBoostingClassifier -from sklearn.model_selection import cross_val_score -from sklearn.tree import DecisionTreeClassifier - -from hyperactive import Hyperactive - - -def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - -search_space = { - "x1": list(np.arange(0, 10, 1)), -} - - -def test_memory_timeSave_0(): - data = load_breast_cancer() - X, y = data.data, data.target - - def objective_function(opt): - dtc = DecisionTreeClassifier(min_samples_split=opt["min_samples_split"]) - scores = cross_val_score(dtc, X, y, cv=5) - - return scores.mean() - - search_space = { - "min_samples_split": list(range(2, 20)), - } - - c_time1 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=100) - hyper.run() - diff_time1 = time.perf_counter() - c_time1 - - c_time2 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=100, memory=False) - hyper.run() - diff_time2 = time.perf_counter() - c_time2 - - assert diff_time1 < diff_time2 * 0.8 - - -def test_memory_timeSave_1(): - def objective_function(opt): - time.sleep(0.001) - return 1 - - search_space = { - "x1": list(np.arange(1, 30)), - } - - results = pd.DataFrame(np.arange(1, 30), columns=["x1"]) - results["score"] = 0 - - c_time1 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1000, - memory_warm_start=results, - ) - hyper.run() - diff_time1 = time.perf_counter() - c_time1 - - print("diff_time1", diff_time1) - - assert diff_time1 < 1 - - -def test_memory_warm_start(): - data = load_breast_cancer() - X, y = data.data, data.target - - def objective_function(opt): - dtc = DecisionTreeClassifier( - max_depth=opt["max_depth"], - min_samples_split=opt["min_samples_split"], - ) - scores = cross_val_score(dtc, X, y, cv=5) - - return scores.mean() - - search_space = { - "max_depth": list(np.arange(1, 10)), - "min_samples_split": list(np.arange(2, 20)), - } - - c_time1 = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=300) - hyper0.run() - diff_time1 = time.perf_counter() - c_time1 - - c_time2 = time.perf_counter() - - results0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=300, - memory_warm_start=results0, - ) - hyper1.run() - - diff_time2 = time.perf_counter() - c_time2 - - assert diff_time2 < diff_time1 * 0.5 - - -def test_memory_warm_start_manual(): - data = load_breast_cancer() - X, y = data.data, data.target - - def objective_function(opt): - dtc = GradientBoostingClassifier( - n_estimators=opt["n_estimators"], - ) - scores = cross_val_score(dtc, X, y, cv=5) - - return scores.mean() - - search_space = { - "n_estimators": list(np.arange(500, 502)), - } - - c_time_1 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=1) - hyper.run() - diff_time_1 = time.perf_counter() - c_time_1 - - memory_warm_start = pd.DataFrame( - [[500, 0.9], [501, 0.91]], columns=["n_estimators", "score"] - ) - - c_time = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, - search_space, - n_iter=10, - memory_warm_start=memory_warm_start, - ) - hyper0.run() - diff_time_2 = time.perf_counter() - c_time - - assert diff_time_1 * 0.5 > diff_time_2 diff --git a/tests/_local_test_timings/_test_memory_warm_start.py b/tests/_local_test_timings/_test_memory_warm_start.py deleted file mode 100644 index 505458e8..00000000 --- a/tests/_local_test_timings/_test_memory_warm_start.py +++ /dev/null @@ -1,441 +0,0 @@ -import time - -import numpy as np -import pandas as pd -import pytest - -from hyperactive import Hyperactive - -############################## create search spaces -size = 1000 - -dim_full = list(range(0, size)) -dim_cat = list(range(round(size / 3))) -dim_10 = list(range(round(size**0.1))) - -search_space_0 = { - "x1": dim_full, -} - -search_space_1 = { - "x1": dim_cat, - "x2": list(range(3)), -} - -search_space_2 = { - "x1": dim_10, - "x2": dim_10, - "x3": dim_10, - "x4": dim_10, - "x5": dim_10, - "x6": dim_10, - "x7": dim_10, - "x8": dim_10, - "x9": dim_10, - "x10": dim_10, -} - -search_space_3 = { - "x1": dim_10, - "x2": dim_10, - "x3": dim_10, - "x4": dim_10, - "x5": dim_10, - "x6": dim_10, - "x7": dim_10, - "x8": dim_10, - "x9": dim_10, - "x10": dim_10, - "x11": [1], - "x12": [1], - "x13": [1], - "x14": [1], - "x15": [1], -} - -search_space_4 = { - "x1": dim_cat, - "str1": ["0", "1", "2"], -} - - -def func1(): - pass - - -def func2(): - pass - - -def func3(): - pass - - -search_space_5 = { - "x1": dim_cat, - "func1": [func1, func2, func3], -} - - -class class1: - pass - - -class class2: - pass - - -class class3: - pass - - -def wr_func_1(): - return class1 - - -def wr_func_2(): - return class2 - - -def wr_func_3(): - return class3 - - -search_space_6 = { - "x1": dim_cat, - "class_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -class class1_o: - def __init__(self): - pass - - -class class2_o: - def __init__(self): - pass - - -class class3_o: - def __init__(self): - pass - - -def wr_func_1(): - return class1_o() - - -def wr_func_2(): - return class2_o() - - -def wr_func_3(): - return class3_o() - - -search_space_7 = { - "x1": dim_cat, - "class_obj_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return [1, 0, 0] - - -def wr_func_2(): - return [0, 1, 0] - - -def wr_func_3(): - return [0, 0, 1] - - -search_space_8 = { - "x1": dim_cat, - "list_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return np.array([1, 0, 0]) - - -def wr_func_2(): - return np.array([0, 1, 0]) - - -def wr_func_3(): - return np.array([0, 0, 1]) - - -search_space_9 = { - "x1": dim_cat, - "array_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return pd.DataFrame(np.array([1, 0, 0])) - - -def wr_func_2(): - return pd.DataFrame(np.array([0, 1, 0])) - - -def wr_func_3(): - return pd.DataFrame(np.array([0, 0, 1])) - - -search_space_10 = { - "x1": dim_cat, - "df_1": [wr_func_1, wr_func_2, wr_func_3], -} - -search_space_list = [ - (search_space_0), - (search_space_1), - (search_space_2), - (search_space_3), - (search_space_4), - (search_space_5), - (search_space_6), - (search_space_7), - (search_space_8), - (search_space_9), - (search_space_10), -] - -############################## start tests - - -def objective_function(opt): - time.sleep(0.001) - return 0 - - -""" -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_0(search_space): - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=20) - hyper0.run() - - search_data0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, search_space, n_iter=20, memory_warm_start=search_data0 - ) - hyper1.run() - - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_1(search_space): - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, - search_space, - n_iter=20, - n_jobs=2, - ) - hyper0.run() - - search_data0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=20, - n_jobs=2, - memory_warm_start=search_data0, - ) - hyper1.run() - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_2(search_space): - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, - search_space, - n_iter=20, - n_jobs=2, - ) - hyper0.run() - - search_data0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=20, - memory_warm_start=search_data0, - ) - hyper1.run() - - - - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_0(search_space): - n_iter = 1500 - - c_time = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=n_iter) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, search_space, n_iter=n_iter, memory_warm_start=search_data0 - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - d_time_frac = d_time_1 / d_time_2 - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - - assert d_time_frac > 1.5 -""" - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_1(search_space): - n_iter = 1500 - - c_time = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=n_iter, memory=True) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive(distribution="joblib") - hyper1.add_search( - objective_function, - search_space, - n_iter=n_iter, - n_jobs=1, - memory=True, - memory_warm_start=search_data0, - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - d_time_frac = d_time_1 / d_time_2 - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - - assert d_time_frac > 1.5 - - -""" -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_2(search_space): - n_iter = 1500 - - c_time = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, search_space, n_iter=n_iter, n_jobs=3, memory=True - ) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=n_iter, - n_jobs=2, - memory_warm_start=search_data0, - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - d_time_frac = d_time_1 / d_time_2 - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - - assert d_time_frac > 1.5 - - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_4(search_space): - - c_time = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=2000, n_jobs=2) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=2000, - memory_warm_start=search_data0, - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - # assert False - - assert d_time_1 * 0.75 > d_time_2 - - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_5(search_space): - - c_time = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, search_space, n_iter=2000, n_jobs=2 - ) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=2000, - n_jobs=2, - memory_warm_start=search_data0, - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - assert d_time_1 * 0.75 > d_time_2 -""" diff --git a/tests/_local_test_timings/_test_memory_warm_start_n_jobs.py b/tests/_local_test_timings/_test_memory_warm_start_n_jobs.py deleted file mode 100644 index a75a6031..00000000 --- a/tests/_local_test_timings/_test_memory_warm_start_n_jobs.py +++ /dev/null @@ -1,317 +0,0 @@ -import time - -import numpy as np -import pandas as pd -import pytest - -from hyperactive import Hyperactive - -############################## create search spaces -size = 1000 - -dim_full = list(range(0, size)) -dim_cat = list(range(round(size / 3))) -dim_10 = list(range(round(size**0.1))) - -search_space_0 = { - "x1": dim_full, -} - -search_space_1 = { - "x1": dim_cat, - "x2": list(range(3)), -} - -search_space_2 = { - "x1": dim_10, - "x2": dim_10, - "x3": dim_10, - "x4": dim_10, - "x5": dim_10, - "x6": dim_10, - "x7": dim_10, - "x8": dim_10, - "x9": dim_10, - "x10": dim_10, -} - -search_space_3 = { - "x1": dim_10, - "x2": dim_10, - "x3": dim_10, - "x4": dim_10, - "x5": dim_10, - "x6": dim_10, - "x7": dim_10, - "x8": dim_10, - "x9": dim_10, - "x10": dim_10, - "x11": [1], - "x12": [1], - "x13": [1], - "x14": [1], - "x15": [1], -} - -search_space_4 = { - "x1": dim_cat, - "str1": ["0", "1", "2"], -} - - -def func1(): - pass - - -def func2(): - pass - - -def func3(): - pass - - -search_space_5 = { - "x1": dim_cat, - "func1": [func1, func2, func3], -} - - -class class1: - pass - - -class class2: - pass - - -class class3: - pass - - -def wr_func_1(): - return class1 - - -def wr_func_2(): - return class2 - - -def wr_func_3(): - return class3 - - -search_space_6 = { - "x1": dim_cat, - "class_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -class class1_o: - def __init__(self): - pass - - -class class2_o: - def __init__(self): - pass - - -class class3_o: - def __init__(self): - pass - - -def wr_func_1(): - return class1_o() - - -def wr_func_2(): - return class2_o() - - -def wr_func_3(): - return class3_o() - - -search_space_7 = { - "x1": dim_cat, - "class_obj_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return [1, 0, 0] - - -def wr_func_2(): - return [0, 1, 0] - - -def wr_func_3(): - return [0, 0, 1] - - -search_space_8 = { - "x1": dim_cat, - "list_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return np.array([1, 0, 0]) - - -def wr_func_2(): - return np.array([0, 1, 0]) - - -def wr_func_3(): - return np.array([0, 0, 1]) - - -search_space_9 = { - "x1": dim_cat, - "array_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return pd.DataFrame(np.array([1, 0, 0])) - - -def wr_func_2(): - return pd.DataFrame(np.array([0, 1, 0])) - - -def wr_func_3(): - return pd.DataFrame(np.array([0, 0, 1])) - - -search_space_10 = { - "x1": dim_cat, - "df_1": [wr_func_1, wr_func_2, wr_func_3], -} - -search_space_list = [ - (search_space_0), - (search_space_1), - (search_space_2), - (search_space_3), - (search_space_4), - (search_space_5), - (search_space_6), - (search_space_7), - (search_space_8), - (search_space_9), - (search_space_10), -] - -############################## start tests - - -def objective_function(opt): - time.sleep(0.003) - return 0 - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_0(search_space): - n_iter = 1500 - - c_time = time.perf_counter() - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search( - objective_function, search_space, n_iter=n_iter, n_jobs=2, memory=True - ) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=n_iter, - memory_warm_start=search_data0, - memory=True, - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - d_time_frac = d_time_1 / d_time_2 - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - - assert d_time_frac > 1.5 - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_1(search_space): - n_iter = 1500 - - c_time = time.perf_counter() - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=n_iter, memory=True) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive(distribution="pathos") - hyper1.add_search( - objective_function, - search_space, - n_iter=n_iter, - n_jobs=2, - memory=True, - memory_warm_start=search_data0, - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - d_time_frac = d_time_1 / d_time_2 - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - - assert d_time_frac > 1.5 - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_memory_warm_start_n_jobs(search_space): - n_iter = 1500 - - c_time = time.perf_counter() - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search( - objective_function, search_space, n_iter=n_iter, n_jobs=2, memory=True - ) - hyper0.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper0.search_data(objective_function) - - c_time = time.perf_counter() - hyper1 = Hyperactive(distribution="pathos") - hyper1.add_search( - objective_function, - search_space, - n_iter=n_iter, - n_jobs=2, - memory=True, - memory_warm_start=search_data0, - ) - hyper1.run() - d_time_2 = time.perf_counter() - c_time - - d_time_frac = d_time_1 / d_time_2 - - print("\n d_time_1 ", d_time_1) - print("\n d_time_2 ", d_time_2) - - assert d_time_frac > 1.5 diff --git a/tests/_local_test_timings/_test_shared_memory.py b/tests/_local_test_timings/_test_shared_memory.py deleted file mode 100644 index 7cba7012..00000000 --- a/tests/_local_test_timings/_test_shared_memory.py +++ /dev/null @@ -1,285 +0,0 @@ -import time - -from hyperactive import Hyperactive - - -def model(opt): - time.sleep(0.001) - return 0 - - -def model1(opt): - time.sleep(0.001) - return 0 - - -def model2(opt): - time.sleep(0.001) - return 0 - - -def model3(opt): - time.sleep(0.001) - return 0 - - -def model4(opt): - time.sleep(0.001) - return 0 - - -search_space = { - "x1": list(range(2, 200)), -} - - -def test_shared_memory_0(): - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory="share", - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time - - n_jobs = 4 - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=n_jobs, - memory="share", - ) - hyper.run() - d_time_2 = time.perf_counter() - c_time - d_time_2 = d_time_2 / n_jobs - - print("\n d_time_1 \n", d_time_1) - print("\n d_time_2 \n", d_time_2) - - d_time = d_time_1 / d_time_2 - - assert d_time > 1.4 - - -def test_shared_memory_1(): - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory=True, - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time - - n_jobs = 4 - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=n_jobs, - memory=True, - ) - hyper.run() - d_time_2 = time.perf_counter() - c_time - d_time_2 = d_time_2 / n_jobs - - print("\n d_time_1 \n", d_time_1) - print("\n d_time_2 \n", d_time_2) - - d_time = d_time_1 / d_time_2 - - assert d_time > 0.7 - assert d_time < 1.3 - - -def test_shared_memory_2(): - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time - - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory="share", - ) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory="share", - ) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory="share", - ) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory="share", - ) - hyper.run() - d_time_2 = time.perf_counter() - c_time - d_time_2 = d_time_2 / 4 - - print("\n d_time_1 \n", d_time_1) - print("\n d_time_2 \n", d_time_2) - - d_time = d_time_1 / d_time_2 - - assert d_time > 1.4 - - -def test_shared_memory_3(): - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time - - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory=True, - ) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory=True, - ) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory=True, - ) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - memory=True, - ) - hyper.run() - d_time_2 = time.perf_counter() - c_time - d_time_2 = d_time_2 / 4 - - print("\n d_time_1 \n", d_time_1) - print("\n d_time_2 \n", d_time_2) - - d_time = d_time_1 / d_time_2 - - assert d_time > 0.7 - assert d_time < 1.3 - - -def test_shared_memory_warm_start_0(): - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper.search_data(model) - - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=4, - memory_warm_start=search_data0, - memory="share", - ) - hyper.run() - d_time_2 = time.perf_counter() - c_time - d_time_2 = d_time_2 / 4 - - print("\n d_time_1 \n", d_time_1) - print("\n d_time_2 \n", d_time_2) - - d_time = d_time_1 / d_time_2 - - assert d_time > 1.4 - - -def test_shared_memory_warm_start_1(): - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=1, - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time - - search_data0 = hyper.search_data(model) - - c_time = time.perf_counter() - hyper = Hyperactive(n_processes=1) - hyper.add_search( - model, - search_space, - n_iter=300, - n_jobs=4, - memory_warm_start=search_data0, - memory=True, - ) - hyper.run() - d_time_2 = time.perf_counter() - c_time - d_time_2 = d_time_2 / 4 - - print("\n d_time_1 \n", d_time_1) - print("\n d_time_2 \n", d_time_2) - - d_time = d_time_1 / d_time_2 - - assert d_time > 2 diff --git a/tests/_local_test_timings/_test_warm_start.py b/tests/_local_test_timings/_test_warm_start.py deleted file mode 100644 index 18856358..00000000 --- a/tests/_local_test_timings/_test_warm_start.py +++ /dev/null @@ -1,30 +0,0 @@ -import pytest - -from hyperactive import Hyperactive - -from ._search_space_list import search_space_setup - -search_space_list = search_space_setup() - - -def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_warm_start_0(search_space): - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=20) - hyper0.run() - - search_data0 = hyper0.best_para(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=20, - initialize={"warm_start": [search_data0]}, - ) - hyper1.run() diff --git a/tests/_local_test_timings/_test_warm_start_n_jobs.py b/tests/_local_test_timings/_test_warm_start_n_jobs.py deleted file mode 100644 index 7baef210..00000000 --- a/tests/_local_test_timings/_test_warm_start_n_jobs.py +++ /dev/null @@ -1,252 +0,0 @@ -import numpy as np -import pandas as pd -import pytest - -from hyperactive import Hyperactive - -############################## create search spaces -size = 1000 - -dim_full = list(range(0, size)) -dim_cat = list(range(round(size / 3))) -dim_10 = list(range(round(size**0.1))) - -search_space_0 = { - "x1": dim_full, -} - -search_space_1 = { - "x1": dim_cat, - "x2": list(range(3)), -} - -search_space_2 = { - "x1": dim_10, - "x2": dim_10, - "x3": dim_10, - "x4": dim_10, - "x5": dim_10, - "x6": dim_10, - "x7": dim_10, - "x8": dim_10, - "x9": dim_10, - "x10": dim_10, -} - -search_space_3 = { - "x1": dim_10, - "x2": dim_10, - "x3": dim_10, - "x4": dim_10, - "x5": dim_10, - "x6": dim_10, - "x7": dim_10, - "x8": dim_10, - "x9": dim_10, - "x10": dim_10, - "x11": [1], - "x12": [1], - "x13": [1], - "x14": [1], - "x15": [1], -} - -search_space_4 = { - "x1": dim_cat, - "str1": ["0", "1", "2"], -} - - -def func1(): - pass - - -def func2(): - pass - - -def func3(): - pass - - -search_space_5 = { - "x1": dim_cat, - "func1": [func1, func2, func3], -} - - -class class1: - pass - - -class class2: - pass - - -class class3: - pass - - -def wr_func_1(): - return class1 - - -def wr_func_2(): - return class2 - - -def wr_func_3(): - return class3 - - -search_space_6 = { - "x1": dim_cat, - "class_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -class class1_o: - def __init__(self): - pass - - -class class2_o: - def __init__(self): - pass - - -class class3_o: - def __init__(self): - pass - - -def wr_func_1(): - return class1_o() - - -def wr_func_2(): - return class2_o() - - -def wr_func_3(): - return class3_o() - - -search_space_7 = { - "x1": dim_cat, - "class_obj_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return [1, 0, 0] - - -def wr_func_2(): - return [0, 1, 0] - - -def wr_func_3(): - return [0, 0, 1] - - -search_space_8 = { - "x1": dim_cat, - "list_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return np.array([1, 0, 0]) - - -def wr_func_2(): - return np.array([0, 1, 0]) - - -def wr_func_3(): - return np.array([0, 0, 1]) - - -search_space_9 = { - "x1": dim_cat, - "array_1": [wr_func_1, wr_func_2, wr_func_3], -} - - -def wr_func_1(): - return pd.DataFrame(np.array([1, 0, 0])) - - -def wr_func_2(): - return pd.DataFrame(np.array([0, 1, 0])) - - -def wr_func_3(): - return pd.DataFrame(np.array([0, 0, 1])) - - -search_space_10 = { - "x1": dim_cat, - "df_1": [wr_func_1, wr_func_2, wr_func_3], -} - -search_space_list = [ - (search_space_0), - (search_space_1), - (search_space_2), - (search_space_3), - (search_space_4), - (search_space_5), - (search_space_6), - (search_space_7), - (search_space_8), - (search_space_9), - (search_space_10), -] - -############################## start tests - - -def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_warm_start_0(search_space): - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search(objective_function, search_space, n_iter=20, n_jobs=2) - hyper0.run() - - best_para_ = hyper0.best_para(objective_function) - - print("\n best_para_ \n", best_para_) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=20, - initialize={"warm_start": [best_para_]}, - ) - hyper1.run() - - -@pytest.mark.parametrize("search_space", search_space_list) -def test_warm_start_1(search_space): - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search(objective_function, search_space, n_iter=20, n_jobs=2) - hyper0.run() - - best_para_ = hyper0.best_para(objective_function) - - hyper1 = Hyperactive(distribution="pathos") - hyper1.add_search( - objective_function, - search_space, - n_iter=20, - initialize={"warm_start": [best_para_]}, - n_jobs=3, - ) - hyper1.run() diff --git a/tests/_test_examples.py b/tests/_test_examples.py deleted file mode 100644 index cd891e76..00000000 --- a/tests/_test_examples.py +++ /dev/null @@ -1,25 +0,0 @@ -import glob -import os -import subprocess -from subprocess import DEVNULL, STDOUT - -here = os.path.dirname(os.path.abspath(__file__)) - -files0 = glob.glob(here + "/../examples/*/*.py") -files1 = glob.glob(here + "/../examples/*.py") - -files = files0 + files1 - -print("run files:", files) - -for file_path in files: - file_name = str(file_path.rsplit("/", maxsplit=1)[1]) - - try: - print("\033[0;33;40m Testing", file_name, end="...\r") - subprocess.check_call(["python", file_path], stdout=DEVNULL, stderr=STDOUT) # noqa: S603, S607 - except subprocess.CalledProcessError: - print("\033[0;31;40m Error in", file_name) - else: - print("\033[0;32;40m", file_name, "is correct") -print("\n") diff --git a/tests/integrations/__init__.py b/tests/integrations/__init__.py deleted file mode 100644 index 0edd64e1..00000000 --- a/tests/integrations/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test package for integration tests with external libraries.""" diff --git a/tests/integrations/sklearn/__init__.py b/tests/integrations/sklearn/__init__.py deleted file mode 100644 index 294fa69c..00000000 --- a/tests/integrations/sklearn/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test package for sklearn integration tests.""" diff --git a/tests/integrations/sklearn/test_parametrize_with_checks.py b/tests/integrations/sklearn/test_parametrize_with_checks.py deleted file mode 100644 index 6ec62d76..00000000 --- a/tests/integrations/sklearn/test_parametrize_with_checks.py +++ /dev/null @@ -1,25 +0,0 @@ -"""Test module for sklearn parametrize_with_checks integration.""" - -from sklearn import svm -from sklearn.model_selection import KFold -from sklearn.utils.estimator_checks import parametrize_with_checks - -from hyperactive.integrations import HyperactiveSearchCV, OptCV -from hyperactive.opt import GridSearchSk as GridSearch -from hyperactive.optimizers import RandomSearchOptimizer - -svc = svm.SVC() -parameters = {"kernel": ["linear", "rbf"], "C": [1, 10]} -opt = RandomSearchOptimizer() -hyperactivecv = HyperactiveSearchCV(svc, parameters, opt) - -cv = KFold(n_splits=2, shuffle=True, random_state=42) -optcv = OptCV(estimator=svc, optimizer=GridSearch(param_grid=parameters), cv=cv) - -ESTIMATORS = [hyperactivecv, optcv] - - -@parametrize_with_checks(ESTIMATORS) -def test_estimators(estimator, check): - """Test estimators with sklearn estimator checks.""" - check(estimator) diff --git a/tests/integrations/sklearn/test_sklearn_api.py b/tests/integrations/sklearn/test_sklearn_api.py deleted file mode 100644 index e8f959c1..00000000 --- a/tests/integrations/sklearn/test_sklearn_api.py +++ /dev/null @@ -1,188 +0,0 @@ -"""Test module for sklearn API integration.""" - -import numpy as np -import pytest -from sklearn import datasets, svm -from sklearn.decomposition import PCA -from sklearn.exceptions import NotFittedError -from sklearn.naive_bayes import GaussianNB -from sklearn.utils.validation import check_is_fitted - -from hyperactive.integrations import HyperactiveSearchCV -from hyperactive.optimizers import RandomSearchOptimizer - -iris = datasets.load_iris() -X, y = iris.data, iris.target - -nb = GaussianNB() -svc = svm.SVC() -pca = PCA() - - -nb_params = { - "var_smoothing": [1e-9, 1e-8], -} -svc_params = {"kernel": ["linear", "rbf"], "C": [1, 10]} -pca_params = { - "n_components": [2, 3], -} - - -opt = RandomSearchOptimizer() - - -def test_fit(): - """Test fitting the HyperactiveSearchCV estimator.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - check_is_fitted(search) - - -def test_not_fitted(): - """Test behavior when estimator is not fitted.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - assert not search.fit_successful - - with pytest.raises(NotFittedError): - check_is_fitted(search) - - assert not search.fit_successful - - -def test_false_params(): - """Test error handling with invalid parameters.""" - search = HyperactiveSearchCV(svc, nb_params, opt) - with pytest.raises(ValueError): - search.fit(X, y) - - assert not search.fit_successful - - -def test_score(): - """Test scoring functionality of the fitted estimator.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - score = search.score(X, y) - - assert isinstance(score, float) - - -def test_classes_(): - """Test access to fitted classes.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - assert [0, 1, 2] == list(search.classes_) - - -def test_score_samples(): - """Test score_samples method raises AttributeError.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - with pytest.raises(AttributeError): - search.score_samples(X) - - -def test_predict(): - """Test prediction functionality.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - result = search.predict(X) - - assert isinstance(result, np.ndarray) - - -def test_predict_proba(): - """Test predict_proba method behavior.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - with pytest.raises(AttributeError): - search.predict_proba(X) - - search = HyperactiveSearchCV(nb, nb_params, opt) - search.fit(X, y) - result = search.predict(X) - - assert isinstance(result, np.ndarray) - - -def test_predict_log_proba(): - """Test predict_log_proba method behavior.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - with pytest.raises(AttributeError): - search.predict_log_proba(X) - - search = HyperactiveSearchCV(nb, nb_params, opt) - search.fit(X, y) - result = search.predict_log_proba(X) - - assert isinstance(result, np.ndarray) - - -def test_decision_function(): - """Test decision_function method.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - result = search.decision_function(X) - - assert isinstance(result, np.ndarray) - - -def test_transform(): - """Test transform method behavior.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - with pytest.raises(AttributeError): - search.transform(X) - - search = HyperactiveSearchCV(pca, pca_params, opt) - search.fit(X, y) - result = search.transform(X) - - assert isinstance(result, np.ndarray) - - -def test_inverse_transform(): - """Test inverse_transform method behavior.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - with pytest.raises(AttributeError): - search.inverse_transform(X) - - search = HyperactiveSearchCV(pca, pca_params, opt) - search.fit(X, y) - result = search.inverse_transform(search.transform(X)) - - assert isinstance(result, np.ndarray) - - -def test_best_params_and_score(): - """Test access to best parameters and score.""" - search = HyperactiveSearchCV(svc, svc_params, opt) - search.fit(X, y) - - best_params = search.best_params_ - best_score = search.best_score_ - - assert "kernel" in best_params and "C" in best_params - assert isinstance(best_score, float) - - -def test_search_data(): - """Test access to search data after optimization.""" - n_iter = 50 - search = HyperactiveSearchCV(svc, svc_params, opt, n_iter=n_iter) - search.fit(X, y) - - search_data = search.search_data_ - columns = search_data.columns - - assert len(search_data) == n_iter - assert "kernel" in columns and "C" in columns diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py deleted file mode 100644 index b66f7249..00000000 --- a/tests/test_callbacks.py +++ /dev/null @@ -1,105 +0,0 @@ -"""Test module for callback functionality.""" - -import numpy as np - -from hyperactive import Hyperactive - -search_space = { - "x1": list(np.arange(-100, 100, 1)), -} - - -def test_callback_0(): - """Test callbacks executed before objective function.""" - - def callback_1(access): - access.stuff1 = 1 - - def callback_2(access): - access.stuff2 = 2 - - def objective_function(access): - assert access.stuff1 == 1 - assert access.stuff2 == 2 - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - callbacks={"before": [callback_1, callback_2]}, - ) - hyper.run() - - -def test_callback_1(): - """Test callbacks executed before and after objective function.""" - - def callback_1(access): - access.stuff1 = 1 - - def callback_2(access): - access.stuff1 = 2 - - def objective_function(access): - assert access.stuff1 == 1 - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - callbacks={"before": [callback_1], "after": [callback_2]}, - ) - hyper.run() - - -def test_callback_2(): - """Test callbacks with pass_through parameter.""" - - def callback_1(access): - access.pass_through["stuff1"] = 1 - - def objective_function(access): - assert access.pass_through["stuff1"] == 1 - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - callbacks={"before": [callback_1]}, - pass_through={"stuff1": 0}, - ) - hyper.run() - - -def test_callback_3(): - """Test callbacks executed after objective function with pass_through.""" - - def callback_1(access): - access.pass_through["stuff1"] = 1 - - def objective_function(access): - if access.nth_iter == 0: - assert access.pass_through["stuff1"] == 0 - else: - assert access.pass_through["stuff1"] == 1 - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - callbacks={"after": [callback_1]}, - pass_through={"stuff1": 0}, - ) - hyper.run() diff --git a/tests/test_catch.py b/tests/test_catch.py deleted file mode 100644 index 377b8bd0..00000000 --- a/tests/test_catch.py +++ /dev/null @@ -1,125 +0,0 @@ -"""Test module for exception catching functionality.""" - -import math - -import numpy as np - -from hyperactive import Hyperactive - -search_space = { - "x1": list(np.arange(-100, 100, 1)), -} - - -def test_catch_1(): - """Test catching TypeError exceptions in objective function.""" - - def objective_function(access): - 1 + "str" # Intentional TypeError for testing - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - catch={TypeError: np.nan}, - ) - hyper.run() - - -def test_catch_2(): - """Test catching ValueError exceptions in objective function.""" - - def objective_function(access): - math.sqrt(-10) - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - catch={ValueError: np.nan}, - ) - hyper.run() - - -def test_catch_3(): - """Test catching ZeroDivisionError exceptions in objective function.""" - - def objective_function(access): - 1 / 0 # Intentional ZeroDivisionError for testing - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - catch={ZeroDivisionError: np.nan}, - ) - hyper.run() - - -def test_catch_all_0(): - """Test catching multiple exception types returning NaN values.""" - - def objective_function(access): - 1 + "str" # Intentional TypeError for testing - math.sqrt(-10) # Intentional ValueError for testing - 1 / 0 # Intentional ZeroDivisionError for testing - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - catch={ - TypeError: np.nan, - ValueError: np.nan, - ZeroDivisionError: np.nan, - }, - ) - hyper.run() - - nan_ = hyper.search_data(objective_function)["score"].values[0] - - assert math.isnan(nan_) - - -def test_catch_all_1(): - """Test catching multiple exception types returning tuple values.""" - - def objective_function(access): - 1 + "str" # Intentional TypeError for testing - math.sqrt(-10) # Intentional ValueError for testing - 1 / 0 # Intentional ZeroDivisionError for testing - - return 0, {"error": False} - - catch_return = (np.nan, {"error": True}) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - catch={ - TypeError: catch_return, - ValueError: catch_return, - ZeroDivisionError: catch_return, - }, - ) - hyper.run() - - nan_ = hyper.search_data(objective_function)["score"].values[0] - error_ = hyper.search_data(objective_function)["error"].values[0] - - assert math.isnan(nan_) - assert error_ diff --git a/tests/test_constr_opt.py b/tests/test_constr_opt.py deleted file mode 100644 index 64c4ea38..00000000 --- a/tests/test_constr_opt.py +++ /dev/null @@ -1,148 +0,0 @@ -"""Test module for constraint optimization functionality.""" - -import numpy as np - -from hyperactive import Hyperactive - - -def test_constr_opt_0(): - """Test constraint optimization with single constraint.""" - - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": list(np.arange(-15, 15, 1)), - } - - def constraint_1(para): - print(" para", para) - - return para["x1"] > -5 - - constraints_list = [constraint_1] - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=50, - constraints=constraints_list, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - x0_values = search_data["x1"].values - - print("\n search_data \n", search_data, "\n") - - assert np.all(x0_values > -5) - - -def test_constr_opt_1(): - """Test constraint optimization with single constraint on 2D space.""" - - def objective_function(para): - score = -(para["x1"] * para["x1"] + para["x2"] * para["x2"]) - return score - - search_space = { - "x1": list(np.arange(-10, 10, 1)), - "x2": list(np.arange(-10, 10, 1)), - } - - def constraint_1(para): - return para["x1"] > -5 - - constraints_list = [constraint_1] - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=50, - constraints=constraints_list, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - x0_values = search_data["x1"].values - - print("\n search_data \n", search_data, "\n") - - assert np.all(x0_values > -5) - - -def test_constr_opt_2(): - """Test constraint optimization with multiple constraints.""" - n_iter = 50 - - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": list(np.arange(-10, 10, 0.1)), - } - - def constraint_1(para): - return para["x1"] > -5 - - def constraint_2(para): - return para["x1"] < 5 - - constraints_list = [constraint_1, constraint_2] - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=50, - constraints=constraints_list, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - x0_values = search_data["x1"].values - - print("\n search_data \n", search_data, "\n") - - assert np.all(x0_values > -5) - assert np.all(x0_values < 5) - - n_new_positions = 0 - n_new_scores = 0 - - n_current_positions = 0 - n_current_scores = 0 - - n_best_positions = 0 - n_best_scores = 0 - - for hyper_optimizer in hyper.opt_pros.values(): - optimizer = hyper_optimizer.gfo_optimizer - - n_new_positions = n_new_positions + len(optimizer.pos_new_list) - n_new_scores = n_new_scores + len(optimizer.score_new_list) - - n_current_positions = n_current_positions + len(optimizer.pos_current_list) - n_current_scores = n_current_scores + len(optimizer.score_current_list) - - n_best_positions = n_best_positions + len(optimizer.pos_best_list) - n_best_scores = n_best_scores + len(optimizer.score_best_list) - - print("\n optimizer", optimizer) - print(" n_new_positions", optimizer.pos_new_list) - print(" n_new_scores", optimizer.score_new_list) - - assert n_new_positions == n_iter - assert n_new_scores == n_iter - - assert n_current_positions == n_current_scores - assert n_current_positions <= n_new_positions - - assert n_best_positions == n_best_scores - assert n_best_positions <= n_new_positions - - assert n_new_positions == n_new_scores diff --git a/tests/test_distribution.py b/tests/test_distribution.py deleted file mode 100644 index 69e54163..00000000 --- a/tests/test_distribution.py +++ /dev/null @@ -1,198 +0,0 @@ -"""Test module for distribution functionality.""" - -import sys - -import numpy as np -import pytest -from tqdm import tqdm - -from hyperactive import Hyperactive - -if sys.platform.startswith("win"): - pytest.skip("skip these tests for windows", allow_module_level=True) - - -def objective_function(opt): - """Return simple quadratic objective function for testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -search_space = { - "x1": list(np.arange(-100, 101, 1)), -} - - -def test_n_jobs_0(): - """Test basic n_jobs functionality with 2 parallel jobs.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - assert len(hyper.results_list) == 2 - - -def test_n_jobs_1(): - """Test n_jobs functionality with 4 parallel jobs.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=4) - hyper.run() - - assert len(hyper.results_list) == 4 - - -def test_n_jobs_2(): - """Test n_jobs functionality with 8 parallel jobs.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=8) - hyper.run() - - assert len(hyper.results_list) == 8 - - -def test_n_jobs_3(): - """Test default n_jobs behavior (single job).""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15) - hyper.run() - - -def test_n_jobs_5(): - """Test multiple searches with n_jobs=2 each.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - - hyper.run() - - assert len(hyper.results_list) == 4 - - -def test_n_jobs_6(): - """Test four searches with n_jobs=2 each.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - - hyper.run() - - assert len(hyper.results_list) == 8 - - -def test_n_jobs_7(): - """Test n_jobs=-1 (use all available cores).""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=-1) - hyper.run() - - -def test_multiprocessing_0(): - """Test multiprocessing distribution backend.""" - hyper = Hyperactive(distribution="multiprocessing") - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - -def test_multiprocessing_1(): - """Test multiprocessing with custom initializer configuration.""" - hyper = Hyperactive( - distribution={ - "multiprocessing": { - "initializer": tqdm.set_lock, - "initargs": (tqdm.get_lock(),), - } - }, - ) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - -def test_joblib_0(): - """Test joblib distribution backend.""" - hyper = Hyperactive(distribution="joblib") - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - -def test_joblib_1(): - """Test custom joblib wrapper function.""" - from joblib import Parallel, delayed - - def joblib_wrapper(process_func, search_processes_paras, n_jobs, **kwargs): - n_jobs = len(search_processes_paras) - - jobs = [ - delayed(process_func)(*info_dict) for info_dict in search_processes_paras - ] - results = Parallel(n_jobs=n_jobs, *kwargs)(jobs) - - return results - - hyper = Hyperactive(distribution=joblib_wrapper) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - - hyper.run() - - -def test_pathos_0(): - """Test pathos distribution backend.""" - hyper = Hyperactive(distribution="pathos") - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - -def test_n_processes_0(): - """Test n_processes=1 with n_jobs=2.""" - hyper = Hyperactive(n_processes=1) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - assert len(hyper.results_list) == 2 - - -def test_n_processes_1(): - """Test n_processes=2 with n_jobs=2.""" - hyper = Hyperactive(n_processes=2) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - assert len(hyper.results_list) == 2 - - -def test_n_processes_2(): - """Test n_processes=4 with n_jobs=2.""" - hyper = Hyperactive(n_processes=4) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper.run() - - assert len(hyper.results_list) == 2 - - -def test_n_processes_3(): - """Test n_processes=4 with n_jobs=3.""" - hyper = Hyperactive(n_processes=4) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=3) - hyper.run() - - assert len(hyper.results_list) == 3 - - -def test_n_processes_4(): - """Test n_processes=1 with n_jobs=4.""" - hyper = Hyperactive(n_processes=1) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=4) - hyper.run() - - assert len(hyper.results_list) == 4 - - -def test_n_processes_5(): - """Test n_processes=1 with multiple searches having n_jobs=4.""" - hyper = Hyperactive(n_processes=1) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=4) - hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=4) - hyper.run() - - assert len(hyper.results_list) == 8 diff --git a/tests/test_early_stop.py b/tests/test_early_stop.py deleted file mode 100644 index 157a2988..00000000 --- a/tests/test_early_stop.py +++ /dev/null @@ -1,344 +0,0 @@ -"""Test module for early stopping functionality.""" - -import numpy as np - -from hyperactive import Hyperactive - - -def objective_function(para): - """Return simple quadratic objective function for testing.""" - score = -para["x1"] * para["x1"] - return score - - -search_space = { - "x1": list(np.arange(0, 100000, 0.1)), -} - - -def test_early_stop_0(): - """Test early stopping with both absolute and relative tolerance.""" - early_stopping = { - "n_iter_no_change": 5, - "tol_abs": 0.1, - "tol_rel": 0.1, - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1000, - initialize={"warm_start": [{"x1": 0}]}, - early_stopping=early_stopping, - ) - hyper.run() - - -def test_early_stop_1(): - """Test early stopping with relative tolerance only.""" - early_stopping = { - "n_iter_no_change": 5, - "tol_abs": None, - "tol_rel": 5, - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1000, - initialize={"warm_start": [{"x1": 0}]}, - early_stopping=early_stopping, - ) - hyper.run() - - -def test_early_stop_2(): - """Test early stopping with absolute tolerance only.""" - early_stopping = { - "n_iter_no_change": 5, - "tol_abs": 0.1, - "tol_rel": None, - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1000, - initialize={"warm_start": [{"x1": 0}]}, - early_stopping=early_stopping, - ) - hyper.run() - - -def test_early_stop_3(): - """Test early stopping without tolerance and verify iteration count.""" - - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 100, 0.1)), - } - - n_iter_no_change = 5 - early_stopping = { - "n_iter_no_change": n_iter_no_change, - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100000, - initialize={"warm_start": [{"x1": 0}]}, - early_stopping=early_stopping, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - n_performed_iter = len(search_data) - - print("\n n_performed_iter \n", n_performed_iter) - print("\n n_iter_no_change \n", n_iter_no_change) - - assert n_performed_iter == (n_iter_no_change + 1) - - -def test_early_stop_4(): - """Test early stopping with absolute tolerance where no early stop occurs.""" - - def objective_function(para): - return para["x1"] - - search_space = { - "x1": list(np.arange(0, 100, 0.1)), - } - - n_iter_no_change = 5 - early_stopping = { - "n_iter_no_change": 5, - "tol_abs": 0.1, - "tol_rel": None, - } - - start1 = {"x1": 0} - start2 = {"x1": 0.1} - start3 = {"x1": 0.2} - start4 = {"x1": 0.3} - start5 = {"x1": 0.4} - - warm_start_l = [ - start1, - start1, - start1, - start1, - start1, - start2, - start2, - start2, - start3, - start3, - start3, - start4, - start4, - start4, - start5, - start5, - start5, - ] - n_iter = len(warm_start_l) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=n_iter, - initialize={"warm_start": warm_start_l}, - early_stopping=early_stopping, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - n_performed_iter = len(search_data) - - print("\n n_performed_iter \n", n_performed_iter) - print("\n n_iter_no_change \n", n_iter_no_change) - - assert n_performed_iter == n_iter - - -def test_early_stop_5(): - """Test early stopping with absolute tolerance where early stop occurs.""" - - def objective_function(para): - return para["x1"] - - search_space = { - "x1": list(np.arange(0, 100, 0.01)), - } - - n_iter_no_change = 5 - early_stopping = { - "n_iter_no_change": n_iter_no_change, - "tol_abs": 0.1, - "tol_rel": None, - } - - start1 = {"x1": 0} - start2 = {"x1": 0.09} - start3 = {"x1": 0.20} - - warm_start_l = [ - start1, - start1, - start1, - start1, - start1, - start2, - start2, - start2, - start3, - start3, - start3, - ] - n_iter = len(warm_start_l) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=n_iter, - initialize={"warm_start": warm_start_l}, - early_stopping=early_stopping, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - n_performed_iter = len(search_data) - - print("\n n_performed_iter \n", n_performed_iter) - print("\n n_iter_no_change \n", n_iter_no_change) - - assert n_performed_iter == (n_iter_no_change + 1) - - -def test_early_stop_6(): - """Test early stopping with relative tolerance where no early stop occurs.""" - - def objective_function(para): - return para["x1"] - - search_space = { - "x1": list(np.arange(0, 100, 0.01)), - } - - n_iter_no_change = 5 - early_stopping = { - "n_iter_no_change": 5, - "tol_abs": None, - "tol_rel": 10, - } - - start1 = {"x1": 1} - start2 = {"x1": 1.1} - start3 = {"x1": 1.22} - start4 = {"x1": 1.35} - start5 = {"x1": 1.48} - - warm_start_l = [ - start1, - start1, - start1, - start1, - start1, - start2, - start2, - start2, - start3, - start3, - start3, - start4, - start4, - start4, - start5, - start5, - start5, - ] - n_iter = len(warm_start_l) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=n_iter, - initialize={"warm_start": warm_start_l}, - early_stopping=early_stopping, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - n_performed_iter = len(search_data) - - print("\n n_performed_iter \n", n_performed_iter) - print("\n n_iter_no_change \n", n_iter_no_change) - - assert n_performed_iter == n_iter - - -def test_early_stop_7(): - """Test early stopping with relative tolerance where early stop occurs.""" - - def objective_function(para): - return para["x1"] - - search_space = { - "x1": list(np.arange(0, 100, 0.01)), - } - - n_iter_no_change = 5 - early_stopping = { - "n_iter_no_change": n_iter_no_change, - "tol_abs": None, - "tol_rel": 10, - } - - start1 = {"x1": 1} - start2 = {"x1": 1.09} - start3 = {"x1": 1.20} - - warm_start_l = [ - start1, - start1, - start1, - start1, - start1, - start2, - start2, - start2, - start3, - start3, - start3, - ] - n_iter = len(warm_start_l) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=n_iter, - initialize={"warm_start": warm_start_l}, - early_stopping=early_stopping, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - n_performed_iter = len(search_data) - - print("\n n_performed_iter \n", n_performed_iter) - print("\n n_iter_no_change \n", n_iter_no_change) - - assert n_performed_iter == (n_iter_no_change + 1) diff --git a/tests/test_empty_output/__init__.py b/tests/test_empty_output/__init__.py deleted file mode 100644 index 215da704..00000000 --- a/tests/test_empty_output/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test package for empty output functionality tests.""" diff --git a/tests/test_empty_output/non_verbose.py b/tests/test_empty_output/non_verbose.py deleted file mode 100644 index b0c6634b..00000000 --- a/tests/test_empty_output/non_verbose.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Test module for non-verbose output functionality.""" - -import numpy as np - -from hyperactive import Hyperactive - - -def ackley_function(para): - """Ackley optimization function for testing.""" - x, y = para["x"], para["y"] - - loss = ( - -20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y))) - - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) - + np.exp(1) - + 20 - ) - - return -loss - - -search_space = { - "x": list(np.arange(-10, 10, 0.01)), - "y": list(np.arange(-10, 10, 0.01)), -} - - -hyper = Hyperactive(verbosity=False) -hyper.add_search(ackley_function, search_space, n_iter=30, memory=True) -hyper.run() diff --git a/tests/test_empty_output/test_empty_output.py b/tests/test_empty_output/test_empty_output.py deleted file mode 100644 index 65bae03a..00000000 --- a/tests/test_empty_output/test_empty_output.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Test module for empty output functionality.""" - -import os -import subprocess -import sys - -import pytest - -if sys.platform.startswith("win"): - pytest.skip("skip these tests for windows", allow_module_level=True) - - -here = os.path.dirname(os.path.abspath(__file__)) - -verbose_file = os.path.join(here, "verbose.py") -non_verbose_file = os.path.join(here, "non_verbose.py") - - -def _run_subprocess(script): - output = [] - process = subprocess.Popen( # noqa: S603 - [sys.executable, "-u", script], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - bufsize=1, # Line buffered - env={**os.environ, "PYTHONUNBUFFERED": "1"}, - ) - # Read output line by line - while True: - line = process.stdout.readline() - if line: - output.append(line) - if not line and process.poll() is not None: - break - - return "".join(output), process.stderr.read() - - -def test_empty_output(): - """Test that verbose and non-verbose modes produce expected output.""" - stdout_verb, stderr_verb = _run_subprocess(verbose_file) - stdout_non_verb, stderr_non_verb = _run_subprocess(non_verbose_file) - - print("\n stdout_verb \n", stdout_verb, "\n") - print("\n stderr_verb \n", stderr_verb, "\n") - - print("\n stdout_non_verb \n", stdout_non_verb, "\n") - print("\n stderr_non_verb \n", stderr_non_verb, "\n") - - assert "Results:" in stdout_verb - assert not stdout_non_verb diff --git a/tests/test_empty_output/verbose.py b/tests/test_empty_output/verbose.py deleted file mode 100644 index a74fb749..00000000 --- a/tests/test_empty_output/verbose.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Test module for verbose output functionality.""" - -import sys - -import numpy as np - -from hyperactive import Hyperactive - - -def ackley_function(para): - """Ackley optimization function for testing.""" - x, y = para["x"], para["y"] - - loss = ( - -20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y))) - - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) - + np.exp(1) - + 20 - ) - - return -loss - - -search_space = { - "x": list(np.arange(-10, 10, 0.01)), - "y": list(np.arange(-10, 10, 0.01)), -} - - -hyper = Hyperactive() -hyper.add_search(ackley_function, search_space, n_iter=30, memory=True) -hyper.run() - -sys.stdout.flush() diff --git a/tests/test_hyper_gradient_trafo.py b/tests/test_hyper_gradient_trafo.py deleted file mode 100644 index 5d9d2672..00000000 --- a/tests/test_hyper_gradient_trafo.py +++ /dev/null @@ -1,222 +0,0 @@ -"""Test module for hyper gradient transformation functionality.""" - -import time - -import numpy as np -import pandas as pd -import pytest - -from hyperactive import Hyperactive - - -def objective_function_0(opt): - """Return simple quadratic objective function for testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -search_space_0 = { - "x1": list(np.arange(-5, 6, 1)), -} -search_space_1 = { - "x1": list(np.arange(0, 6, 1)), -} -search_space_2 = { - "x1": list(np.arange(-5, 1, 1)), -} - - -search_space_3 = { - "x1": list(np.arange(-1, 1, 0.1)), -} -search_space_4 = { - "x1": list(np.arange(-1, 0, 0.1)), -} -search_space_5 = { - "x1": list(np.arange(0, 1, 0.1)), -} - - -search_space_para_0 = [ - (search_space_0), - (search_space_1), - (search_space_2), - (search_space_3), - (search_space_4), - (search_space_5), -] - - -@pytest.mark.parametrize("search_space", search_space_para_0) -def test_trafo_0(search_space): - """Test search space transformations with various ranges.""" - hyper = Hyperactive() - hyper.add_search(objective_function_0, search_space, n_iter=25) - hyper.run() - - for value in hyper.search_data(objective_function_0)["x1"].values: - if value not in search_space["x1"]: - assert False - - -# ----------------- # Test if memory warm starts do work as intended - - -from sklearn.datasets import load_breast_cancer -from sklearn.model_selection import cross_val_score -from sklearn.tree import DecisionTreeClassifier - -data = load_breast_cancer() -X, y = data.data, data.target - - -def objective_function_1(opt): - """Decision tree objective function for testing with sklearn.""" - dtc = DecisionTreeClassifier(min_samples_split=opt["min_samples_split"]) - scores = cross_val_score(dtc, X, y, cv=10) - time.sleep(0.1) - - return scores.mean() - - -search_space_0 = { - "min_samples_split": list(np.arange(2, 12)), -} - -search_space_1 = { - "min_samples_split": list(np.arange(12, 22)), -} - -search_space_2 = { - "min_samples_split": list(np.arange(22, 32)), -} - -memory_dict = {"min_samples_split": range(2, 12), "score": range(2, 12)} -memory_warm_start_0 = pd.DataFrame(memory_dict) - -memory_dict = {"min_samples_split": range(12, 22), "score": range(12, 22)} -memory_warm_start_1 = pd.DataFrame(memory_dict) - -memory_dict = {"min_samples_split": range(22, 32), "score": range(22, 32)} -memory_warm_start_2 = pd.DataFrame(memory_dict) - -search_space_para_1 = [ - (search_space_0, memory_warm_start_0), - (search_space_1, memory_warm_start_1), - (search_space_2, memory_warm_start_2), -] - -random_state_para_0 = [ - (0), - (1), - (2), - (3), - (4), -] - -# ----------------- # Test if wrong memory warm starts do not work as intended -""" test is possible in future gfo versions -@pytest.mark.parametrize("random_state", random_state_para_0) -@pytest.mark.parametrize("search_space, memory_warm_start", search_space_para_1) -def test_trafo_1(random_state, search_space, memory_warm_start): - search_space = search_space - memory_warm_start = memory_warm_start - - c_time_0 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search( - objective_function_1, - search_space, - n_iter=10, - random_state=random_state, - initialize={"random": 1}, - ) - hyper.run() - d_time_0 = time.perf_counter() - c_time_0 - - c_time_1 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search( - objective_function_1, - search_space, - n_iter=10, - random_state=random_state, - initialize={"random": 1}, - memory_warm_start=memory_warm_start, - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time_1 - - assert d_time_1 < d_time_0 * 0.5 - - - -search_space_0 = { - "min_samples_split": list(np.arange(2, 12)), -} - -search_space_1 = { - "min_samples_split": list(np.arange(12, 22)), -} - -search_space_2 = { - "min_samples_split": list(np.arange(22, 32)), -} - -memory_dict = {"min_samples_split": range(12, 22), "score": range(2, 12)} -memory_warm_start_0 = pd.DataFrame(memory_dict) - -memory_dict = {"min_samples_split": range(22, 32), "score": range(12, 22)} -memory_warm_start_1 = pd.DataFrame(memory_dict) - -memory_dict = {"min_samples_split": range(2, 12), "score": range(22, 32)} -memory_warm_start_2 = pd.DataFrame(memory_dict) - -search_space_para_2 = [ - (search_space_0, memory_warm_start_0), - (search_space_1, memory_warm_start_1), - (search_space_2, memory_warm_start_2), -] - -random_state_para_0 = [ - (0), - (1), - (2), - (3), - (4), -] - - -@pytest.mark.parametrize("random_state", random_state_para_0) -@pytest.mark.parametrize("search_space, memory_warm_start", search_space_para_2) -def test_trafo_2(random_state, search_space, memory_warm_start): - search_space = search_space - memory_warm_start = memory_warm_start - - c_time_0 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search( - objective_function_1, - search_space, - n_iter=25, - random_state=random_state, - initialize={"random": 1}, - ) - hyper.run() - d_time_0 = time.perf_counter() - c_time_0 - - c_time_1 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search( - objective_function_1, - search_space, - n_iter=25, - random_state=random_state, - initialize={"random": 1}, - memory_warm_start=memory_warm_start, - ) - hyper.run() - d_time_1 = time.perf_counter() - c_time_1 - - assert not (d_time_1 < d_time_0 * 0.8) -""" diff --git a/tests/test_initializers.py b/tests/test_initializers.py deleted file mode 100644 index 2498961f..00000000 --- a/tests/test_initializers.py +++ /dev/null @@ -1,132 +0,0 @@ -"""Test module for initializer functionality.""" - -import numpy as np - -from hyperactive import Hyperactive - - -def objective_function(opt): - """Return simple quadratic objective function for testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -search_space = { - "x1": list(np.arange(-100, 101, 1)), -} - - -def test_initialize_warm_start_0(): - """Test warm start initialization with optimal point.""" - init = { - "x1": 0, - } - - initialize = {"warm_start": [init]} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1, - initialize=initialize, - ) - hyper.run() - - assert abs(hyper.best_score(objective_function)) < 0.001 - - -def test_initialize_warm_start_1(): - """Test warm start initialization with boundary point.""" - search_space = { - "x1": list(np.arange(-10, 10, 1)), - } - init = { - "x1": -10, - } - - initialize = {"warm_start": [init]} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1, - initialize=initialize, - ) - hyper.run() - - assert hyper.best_para(objective_function) == init - - -def test_initialize_vertices(): - """Test vertices initialization strategy.""" - initialize = {"vertices": 2} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=2, - initialize=initialize, - ) - hyper.run() - - assert abs(hyper.best_score(objective_function)) - 10000 < 0.001 - - -def test_initialize_grid_0(): - """Test grid initialization with optimal center point.""" - search_space = { - "x1": list(np.arange(-1, 2, 1)), - } - initialize = {"grid": 1} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1, - initialize=initialize, - ) - hyper.run() - - assert abs(hyper.best_score(objective_function)) < 0.001 - - -def test_initialize_grid_1(): - """Test grid initialization with off-center optimal point.""" - search_space = { - "x1": list(np.arange(-2, 3, 1)), - } - - initialize = {"grid": 1} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1, - initialize=initialize, - ) - hyper.run() - - assert abs(hyper.best_score(objective_function)) - 1 < 0.001 - - -def test_initialize_all_0(): - """Test combination of all initialization strategies.""" - search_space = { - "x1": list(np.arange(-2, 3, 1)), - } - - initialize = {"grid": 100, "vertices": 100, "random": 100} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=300, - initialize=initialize, - ) - hyper.run() diff --git a/tests/test_issues/__init__.py b/tests/test_issues/__init__.py deleted file mode 100644 index 6ef6a259..00000000 --- a/tests/test_issues/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test package for issue reproduction tests.""" diff --git a/tests/test_issues/test_issue_25.py b/tests/test_issues/test_issue_25.py deleted file mode 100644 index 4a898d95..00000000 --- a/tests/test_issues/test_issue_25.py +++ /dev/null @@ -1,61 +0,0 @@ -"""Test module for issue #25 reproduction.""" - -import numpy as np -import pandas as pd - -from hyperactive import Hyperactive - - -def test_issue_25(): - """Test issue 25 - memory warm start with CSV file persistence.""" - # set a path to save the dataframe - path = "./search_data.csv" - search_space = { - "n_neighbors": list(range(1, 50)), - } - - # get para names from search space + the score - para_names = list(search_space.keys()) + ["score"] - - # init empty pandas dataframe - search_data = pd.DataFrame(columns=para_names) - search_data.to_csv(path, index=False) - - def objective_function(para): - # score = random.choice([1.2, 2.3, np.nan]) - score = np.nan - - # you can access the entire dictionary from "para" - parameter_dict = para.para_dict - - # save the score in the copy of the dictionary - parameter_dict["score"] = score - - # append parameter dictionary to pandas dataframe - search_data = pd.read_csv(path, na_values="nan") - search_data_new = pd.DataFrame(parameter_dict, columns=para_names, index=[0]) - - # search_data = search_data.append(search_data_new) - search_data = pd.concat([search_data, search_data_new], ignore_index=True) - - search_data.to_csv(path, index=False, na_rep="nan") - - return score - - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=50) - hyper0.run() - - search_data_0 = pd.read_csv(path, na_values="nan") - """ - the second run should be much faster than before, - because Hyperactive already knows most parameters/scores - """ - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=50, - memory_warm_start=search_data_0, - ) - hyper1.run() diff --git a/tests/test_issues/test_issue_29.py b/tests/test_issues/test_issue_29.py deleted file mode 100644 index 8a55db1f..00000000 --- a/tests/test_issues/test_issue_29.py +++ /dev/null @@ -1,38 +0,0 @@ -"""Test module for issue #29 reproduction.""" - -from sklearn.datasets import load_diabetes -from sklearn.model_selection import cross_val_score -from sklearn.tree import DecisionTreeRegressor - -from hyperactive import Hyperactive - - -def test_issue_29(): - """Test issue 29 - accessing optimizer attributes during optimization.""" - data = load_diabetes() - X, y = data.data, data.target - - def model(para): - dtr = DecisionTreeRegressor( - min_samples_split=para["min_samples_split"], - max_depth=para["max_depth"], - ) - scores = cross_val_score(dtr, X, y, cv=3) - - print( - "Iteration:", - para.optimizer.nth_iter, - " Best score", - para.optimizer.best_score, - ) - - return scores.mean() - - search_space = { - "min_samples_split": list(range(2, 12)), - "max_depth": list(range(2, 12)), - } - - hyper = Hyperactive() - hyper.add_search(model, search_space, n_iter=20) - hyper.run() diff --git a/tests/test_issues/test_issue_34.py b/tests/test_issues/test_issue_34.py deleted file mode 100644 index 6dcc6de4..00000000 --- a/tests/test_issues/test_issue_34.py +++ /dev/null @@ -1,84 +0,0 @@ -"""Test module for issue #34 reproduction.""" - -import numpy as np - -from hyperactive import Hyperactive - -""" --- test search spaces with mixed int/float types --- """ -n_iter = 100 - - -def test_mixed_type_search_space_0(): - """Test search space with integer type validation.""" - - def objective_function(para): - assert isinstance(para["x1"], int) - - return 1 - - search_space = { - "x1": list(range(10, 20)), - } - - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=n_iter) - hyper.run() - - -def test_mixed_type_search_space_1(): - """Test search space with float type validation.""" - - def objective_function(para): - assert isinstance(para["x2"], float) - - return 1 - - search_space = { - "x2": list(np.arange(1, 2, 0.1)), - } - - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=n_iter) - hyper.run() - - -def test_mixed_type_search_space_2(): - """Test search space with mixed integer and float type validation.""" - - def objective_function(para): - assert isinstance(para["x1"], int) - assert isinstance(para["x2"], float) - - return 1 - - search_space = { - "x1": list(range(10, 20)), - "x2": list(np.arange(1, 2, 0.1)), - } - - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=n_iter) - hyper.run() - - -def test_mixed_type_search_space_3(): - """Test search space with mixed integer, float, and string type validation.""" - - def objective_function(para): - assert isinstance(para["x1"], int) - assert isinstance(para["x2"], float) - assert isinstance(para["x3"], float) - assert isinstance(para["x4"], str) - - return 1 - - search_space = { - "x1": list(range(10, 20)), - "x2": list(np.arange(1, 2, 0.1)), - "x3": list(np.arange(1, 2, 0.1)), - "x4": ["str1", "str2", "str3"], - } - - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=n_iter) - hyper.run() diff --git a/tests/test_max_score.py b/tests/test_max_score.py deleted file mode 100644 index 15f5f31d..00000000 --- a/tests/test_max_score.py +++ /dev/null @@ -1,92 +0,0 @@ -"""Test module for max score functionality.""" - -import time - -import numpy as np - -from hyperactive import Hyperactive -from hyperactive.optimizers import ( - HillClimbingOptimizer, -) - - -def objective_function(para): - """Return simple quadratic objective function for testing.""" - score = -para["x1"] * para["x1"] - return score - - -search_space = { - "x1": list(np.arange(0, 100000, 0.1)), -} - - -def test_max_score_0(): - """Test max_score termination with hill climbing optimizer.""" - - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 100, 0.1)), - } - - max_score = -9999 - - opt = HillClimbingOptimizer( - epsilon=0.01, - rand_rest_p=0, - ) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt, - n_iter=100000, - initialize={"warm_start": [{"x1": 99}]}, - max_score=max_score, - ) - hyper.run() - - print("\n Results head \n", hyper.search_data(objective_function).head()) - print("\n Results tail \n", hyper.search_data(objective_function).tail()) - - print("\nN iter:", len(hyper.search_data(objective_function))) - - assert -100 > hyper.best_score(objective_function) > max_score - - -def test_max_score_1(): - """Test max_score termination with time constraint.""" - - def objective_function(para): - score = -para["x1"] * para["x1"] - time.sleep(0.01) - return score - - search_space = { - "x1": list(np.arange(0, 100, 0.1)), - } - - max_score = -9999 - - c_time = time.perf_counter() - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100000, - initialize={"warm_start": [{"x1": 99}]}, - max_score=max_score, - ) - hyper.run() - diff_time = time.perf_counter() - c_time - - print("\n Results head \n", hyper.search_data(objective_function).head()) - print("\n Results tail \n", hyper.search_data(objective_function).tail()) - - print("\nN iter:", len(hyper.search_data(objective_function))) - - assert diff_time < 1 diff --git a/tests/test_max_time.py b/tests/test_max_time.py deleted file mode 100644 index 154af6ec..00000000 --- a/tests/test_max_time.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Test module for max time functionality.""" - -import time - -import numpy as np - -from hyperactive import Hyperactive - - -def objective_function(para): - """Objective function for max time testing.""" - score = -para["x1"] * para["x1"] - return score - - -search_space = { - "x1": list(np.arange(0, 100000, 1)), -} - - -def test_max_time_0(): - """Test max time constraint with short duration.""" - c_time1 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=1000000) - hyper.run(max_time=0.1) - diff_time1 = time.perf_counter() - c_time1 - - assert diff_time1 < 1 - - -def test_max_time_1(): - """Test max time constraint with longer duration.""" - c_time1 = time.perf_counter() - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=1000000) - hyper.run(max_time=1) - diff_time1 = time.perf_counter() - c_time1 - - assert 0.3 < diff_time1 < 2 diff --git a/tests/test_obj_func_arg.py b/tests/test_obj_func_arg.py deleted file mode 100644 index 7cf08625..00000000 --- a/tests/test_obj_func_arg.py +++ /dev/null @@ -1,33 +0,0 @@ -"""Test module for objective function argument functionality.""" - -import numpy as np - -from hyperactive import Hyperactive - -search_space = { - "x1": list(np.arange(0, 100, 1)), -} - - -def test_argument_0(): - """Test objective function arguments with pass_through parameter.""" - - def objective_function(para): - print("\npara.nth_iter", para.nth_iter) - print("nth_iter_local", para.pass_through["nth_iter_local"]) - - assert para.nth_iter == para.pass_through["nth_iter_local"] - - para.pass_through["nth_iter_local"] += 1 - - return 0 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - pass_through={"nth_iter_local": 0}, - memory=False, - ) - hyper.run() diff --git a/tests/test_optimization_strategies/__init__.py b/tests/test_optimization_strategies/__init__.py deleted file mode 100644 index e78ef282..00000000 --- a/tests/test_optimization_strategies/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test package for optimization strategy tests.""" diff --git a/tests/test_optimization_strategies/_parametrize.py b/tests/test_optimization_strategies/_parametrize.py deleted file mode 100644 index 0ce5d050..00000000 --- a/tests/test_optimization_strategies/_parametrize.py +++ /dev/null @@ -1,109 +0,0 @@ -from hyperactive.optimizers import ( - BayesianOptimizer, - DirectAlgorithm, - DownhillSimplexOptimizer, - EvolutionStrategyOptimizer, - ForestOptimizer, - GridSearchOptimizer, - HillClimbingOptimizer, - LipschitzOptimizer, - ParallelTemperingOptimizer, - ParticleSwarmOptimizer, - PatternSearch, - PowellsMethod, - RandomAnnealingOptimizer, - RandomRestartHillClimbingOptimizer, - RandomSearchOptimizer, - RepulsingHillClimbingOptimizer, - SimulatedAnnealingOptimizer, - SpiralOptimization, - StochasticHillClimbingOptimizer, - TreeStructuredParzenEstimators, -) - -optimizers = ( - "Optimizer", - [ - (HillClimbingOptimizer), - (StochasticHillClimbingOptimizer), - (RepulsingHillClimbingOptimizer), - (SimulatedAnnealingOptimizer), - (DownhillSimplexOptimizer), - (RandomSearchOptimizer), - (GridSearchOptimizer), - (RandomRestartHillClimbingOptimizer), - (RandomAnnealingOptimizer), - (PowellsMethod), - (PatternSearch), - (ParallelTemperingOptimizer), - (ParticleSwarmOptimizer), - (SpiralOptimization), - (EvolutionStrategyOptimizer), - (BayesianOptimizer), - (LipschitzOptimizer), - (DirectAlgorithm), - (TreeStructuredParzenEstimators), - (ForestOptimizer), - ], -) - - -optimizers_strat = ( - "Optimizer_strat", - [ - (HillClimbingOptimizer), - (StochasticHillClimbingOptimizer), - (RepulsingHillClimbingOptimizer), - (SimulatedAnnealingOptimizer), - (DownhillSimplexOptimizer), - (RandomSearchOptimizer), - (GridSearchOptimizer), - (RandomRestartHillClimbingOptimizer), - (RandomAnnealingOptimizer), - (PowellsMethod), - (PatternSearch), - (ParallelTemperingOptimizer), - (ParticleSwarmOptimizer), - (SpiralOptimization), - (EvolutionStrategyOptimizer), - (BayesianOptimizer), - (LipschitzOptimizer), - (DirectAlgorithm), - (TreeStructuredParzenEstimators), - (ForestOptimizer), - ], -) - - -optimizers_non_smbo = ( - "Optimizer_non_smbo", - [ - (HillClimbingOptimizer), - (StochasticHillClimbingOptimizer), - (RepulsingHillClimbingOptimizer), - (SimulatedAnnealingOptimizer), - (DownhillSimplexOptimizer), - (RandomSearchOptimizer), - (GridSearchOptimizer), - (RandomRestartHillClimbingOptimizer), - (RandomAnnealingOptimizer), - (PowellsMethod), - (PatternSearch), - (ParallelTemperingOptimizer), - (ParticleSwarmOptimizer), - (SpiralOptimization), - (EvolutionStrategyOptimizer), - ], -) - - -optimizers_smbo = ( - "Optimizer_smbo", - [ - (BayesianOptimizer), - (LipschitzOptimizer), - (DirectAlgorithm), - (TreeStructuredParzenEstimators), - (ForestOptimizer), - ], -) diff --git a/tests/test_optimization_strategies/test_constr_opt.py b/tests/test_optimization_strategies/test_constr_opt.py deleted file mode 100644 index ec34e92d..00000000 --- a/tests/test_optimization_strategies/test_constr_opt.py +++ /dev/null @@ -1,165 +0,0 @@ -"""Test module for constraint optimization strategy.""" - -import numpy as np - -from hyperactive import Hyperactive -from hyperactive.optimizers import HillClimbingOptimizer, RandomSearchOptimizer -from hyperactive.optimizers.strategies import CustomOptimizationStrategy - - -def test_constr_opt_0(): - """Test constrained optimization with single constraint.""" - - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": list(np.arange(-15, 15, 1)), - } - - def constraint_1(para): - print(" para", para) - - return para["x1"] > -5 - - constraints_list = [constraint_1] - - optimizer1 = RandomSearchOptimizer() - optimizer2 = HillClimbingOptimizer() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.7) - opt_strat.add_optimizer(optimizer2, duration=0.3) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=50, - constraints=constraints_list, - optimizer=opt_strat, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - x0_values = search_data["x1"].values - - print("\n search_data \n", search_data, "\n") - - assert np.all(x0_values > -5) - - -def test_constr_opt_1(): - """Test constrained optimization with multi-dimensional search space.""" - - def objective_function(para): - score = -(para["x1"] * para["x1"] + para["x2"] * para["x2"]) - return score - - search_space = { - "x1": list(np.arange(-10, 10, 1)), - "x2": list(np.arange(-10, 10, 1)), - } - - def constraint_1(para): - return para["x1"] > -5 - - constraints_list = [constraint_1] - - optimizer1 = RandomSearchOptimizer() - optimizer2 = HillClimbingOptimizer() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.7) - opt_strat.add_optimizer(optimizer2, duration=0.3) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=50, - constraints=constraints_list, - optimizer=opt_strat, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - x0_values = search_data["x1"].values - - print("\n search_data \n", search_data, "\n") - - assert np.all(x0_values > -5) - - -def test_constr_opt_2(): - """Test constrained optimization with multiple constraints.""" - n_iter = 50 - - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": list(np.arange(-10, 10, 0.1)), - } - - def constraint_1(para): - return para["x1"] > -5 - - def constraint_2(para): - return para["x1"] < 5 - - constraints_list = [constraint_1, constraint_2] - - optimizer1 = RandomSearchOptimizer() - optimizer2 = HillClimbingOptimizer() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.7) - opt_strat.add_optimizer(optimizer2, duration=0.3) - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=50, - constraints=constraints_list, - optimizer=opt_strat, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - x0_values = search_data["x1"].values - - print("\n search_data \n", search_data, "\n") - - assert np.all(x0_values > -5) - assert np.all(x0_values < 5) - - n_current_positions = 0 - n_current_scores = 0 - - n_best_positions = 0 - n_best_scores = 0 - - for optimizer_setup in list(hyper.opt_pros.values())[0].optimizer_setup_l: - optimizer = optimizer_setup["optimizer"].gfo_optimizer - duration = optimizer_setup["duration"] - - duration_sum = 1 - n_iter_expected = round(n_iter * duration / duration_sum) - - n_current_positions = n_current_positions + len(optimizer.pos_current_list) - n_current_scores = n_current_scores + len(optimizer.score_current_list) - - n_best_positions = n_best_positions + len(optimizer.pos_best_list) - n_best_scores = n_best_scores + len(optimizer.score_best_list) - - print("\n optimizer", optimizer) - print(" n_new_positions", optimizer.pos_new_list, len(optimizer.pos_new_list)) - print(" n_new_scores", optimizer.score_new_list, len(optimizer.score_new_list)) - print(" n_iter_expected", n_iter_expected) - - assert len(optimizer.pos_new_list) == n_iter_expected - assert len(optimizer.score_new_list) == n_iter_expected diff --git a/tests/test_optimization_strategies/test_early_stopping.py b/tests/test_optimization_strategies/test_early_stopping.py deleted file mode 100644 index 38a5559d..00000000 --- a/tests/test_optimization_strategies/test_early_stopping.py +++ /dev/null @@ -1,68 +0,0 @@ -"""Test module for early stopping optimization strategy.""" - -import numpy as np -import pytest - -from hyperactive import Hyperactive -from hyperactive.optimizers import RandomSearchOptimizer -from hyperactive.optimizers.strategies import CustomOptimizationStrategy - -from ._parametrize import optimizers - -n_iter_no_change_parametr = ( - "n_iter_no_change", - [ - (5), - (10), - (15), - ], -) - - -@pytest.mark.parametrize(*n_iter_no_change_parametr) -@pytest.mark.parametrize(*optimizers) -def test_strategy_early_stopping_0(Optimizer, n_iter_no_change): - """Test early stopping strategy with different optimizers and iteration limits.""" - - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 100, 0.1)), - } - - # n_iter_no_change = 5 - early_stopping = { - "n_iter_no_change": n_iter_no_change, - } - - optimizer1 = Optimizer() - optimizer2 = RandomSearchOptimizer() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.9, early_stopping=early_stopping) - opt_strat.add_optimizer(optimizer2, duration=0.1) - - n_iter = 30 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - initialize={"warm_start": [{"x1": 0}]}, - ) - hyper.run() - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - search_data = optimizer1.search_data - n_performed_iter = len(search_data) - - print("\n n_performed_iter \n", n_performed_iter) - print("\n n_iter_no_change \n", n_iter_no_change) - - assert n_performed_iter == (n_iter_no_change + 1) diff --git a/tests/test_optimization_strategies/test_search_space_pruning.py b/tests/test_optimization_strategies/test_search_space_pruning.py deleted file mode 100644 index 339ac6e4..00000000 --- a/tests/test_optimization_strategies/test_search_space_pruning.py +++ /dev/null @@ -1,61 +0,0 @@ -"""Test module for search space pruning optimization strategy.""" - -import time - -import numpy as np -import pytest - -from hyperactive import Hyperactive -from hyperactive.optimizers import GridSearchOptimizer -from hyperactive.optimizers.strategies import CustomOptimizationStrategy - -from ._parametrize import optimizers_smbo - - -@pytest.mark.parametrize(*optimizers_smbo) -def test_memory_Warm_start_smbo_0(Optimizer_smbo): - """Test memory warm start with SMBO optimizers and custom optimization strategy.""" - - def objective_function(opt): - time.sleep(0.01) - score = -(opt["x1"] * opt["x1"]) - return score - - search_space = { - "x1": list(np.arange(0, 100, 1)), - } - - optimizer1 = GridSearchOptimizer() - optimizer2 = Optimizer_smbo() - - opt_strat = CustomOptimizationStrategy() - - duration_1 = 0.8 - duration_2 = 0.2 - - opt_strat.add_optimizer(optimizer1, duration=duration_1) - opt_strat.add_optimizer(optimizer2, duration=duration_2) - - n_iter = 20 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=True, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == int(n_iter * duration_1) - assert len(optimizer2.search_data) == int(n_iter * duration_2) - - assert optimizer1.best_score <= optimizer2.best_score diff --git a/tests/test_optimizers/__init__.py b/tests/test_optimizers/__init__.py deleted file mode 100644 index f623cb62..00000000 --- a/tests/test_optimizers/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test package for optimizer tests.""" diff --git a/tests/test_optimizers/_parametrize.py b/tests/test_optimizers/_parametrize.py deleted file mode 100644 index ea8d7a9e..00000000 --- a/tests/test_optimizers/_parametrize.py +++ /dev/null @@ -1,52 +0,0 @@ -from hyperactive.optimizers import ( - BayesianOptimizer, - DifferentialEvolutionOptimizer, - DirectAlgorithm, - DownhillSimplexOptimizer, - EvolutionStrategyOptimizer, - ForestOptimizer, - GeneticAlgorithmOptimizer, - GridSearchOptimizer, - HillClimbingOptimizer, - LipschitzOptimizer, - ParallelTemperingOptimizer, - ParticleSwarmOptimizer, - PatternSearch, - PowellsMethod, - RandomAnnealingOptimizer, - RandomRestartHillClimbingOptimizer, - RandomSearchOptimizer, - RepulsingHillClimbingOptimizer, - SimulatedAnnealingOptimizer, - SpiralOptimization, - StochasticHillClimbingOptimizer, - TreeStructuredParzenEstimators, -) - -optimizers = ( - "Optimizer", - [ - (HillClimbingOptimizer), - (StochasticHillClimbingOptimizer), - (RepulsingHillClimbingOptimizer), - (SimulatedAnnealingOptimizer), - (DownhillSimplexOptimizer), - (RandomSearchOptimizer), - (GridSearchOptimizer), - (RandomRestartHillClimbingOptimizer), - (RandomAnnealingOptimizer), - (PowellsMethod), - (PatternSearch), - (ParallelTemperingOptimizer), - (ParticleSwarmOptimizer), - (SpiralOptimization), - (GeneticAlgorithmOptimizer), - (EvolutionStrategyOptimizer), - (DifferentialEvolutionOptimizer), - (BayesianOptimizer), - (LipschitzOptimizer), - (DirectAlgorithm), - (TreeStructuredParzenEstimators), - (ForestOptimizer), - ], -) diff --git a/tests/test_optimizers/test_best_results.py b/tests/test_optimizers/test_best_results.py deleted file mode 100644 index 7972641e..00000000 --- a/tests/test_optimizers/test_best_results.py +++ /dev/null @@ -1,115 +0,0 @@ -"""Test module for best results optimizer functionality.""" - -import numpy as np -import pytest - -from hyperactive import Hyperactive - -from ._parametrize import optimizers - - -def objective_function(opt): - """Return standard quadratic objective function.""" - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_m5(opt): - """Quadratic objective function shifted by -5.""" - score = -(opt["x1"] - 5) * (opt["x1"] - 5) - return score - - -def objective_function_p5(opt): - """Quadratic objective function shifted by +5.""" - score = -(opt["x1"] + 5) * (opt["x1"] + 5) - return score - - -search_space_0 = {"x1": list(np.arange(-100, 101, 1))} -search_space_1 = {"x1": list(np.arange(0, 101, 1))} -search_space_2 = {"x1": list(np.arange(-100, 1, 1))} - -search_space_3 = {"x1": list(np.arange(-10, 11, 0.1))} -search_space_4 = {"x1": list(np.arange(0, 11, 0.1))} -search_space_5 = {"x1": list(np.arange(-10, 1, 0.1))} - -search_space_6 = {"x1": list(np.arange(-0.0000000003, 0.0000000003, 0.0000000001))} -search_space_7 = {"x1": list(np.arange(0, 0.0000000003, 0.0000000001))} -search_space_8 = {"x1": list(np.arange(-0.0000000003, 0, 0.0000000001))} - -objective_para = ( - "objective", - [ - (objective_function), - (objective_function_m5), - (objective_function_p5), - ], -) - -search_space_para = ( - "search_space", - [ - (search_space_0), - (search_space_1), - (search_space_2), - (search_space_3), - (search_space_4), - (search_space_5), - (search_space_6), - (search_space_7), - (search_space_8), - ], -) - - -@pytest.mark.parametrize(*objective_para) -@pytest.mark.parametrize(*search_space_para) -@pytest.mark.parametrize(*optimizers) -def test_best_results_0(Optimizer, search_space, objective): - """Test best score consistency with best parameters.""" - search_space = search_space - objective_function = objective - - initialize = {"vertices": 2} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=Optimizer(), - n_iter=10, - memory=False, - initialize=initialize, - ) - hyper.run() - - assert hyper.best_score(objective_function) == objective_function( - hyper.best_para(objective_function) - ) - - -@pytest.mark.parametrize(*objective_para) -@pytest.mark.parametrize(*search_space_para) -@pytest.mark.parametrize(*optimizers) -def test_best_results_1(Optimizer, search_space, objective): - """Test best parameters are present in search data.""" - search_space = search_space - objective_function = objective - - initialize = {"vertices": 2} - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=Optimizer(), - n_iter=10, - memory=False, - initialize=initialize, - ) - hyper.run() - - assert hyper.best_para(objective_function)["x1"] in list( - hyper.search_data(objective_function)["x1"] - ) diff --git a/tests/test_optimizers/test_gfo_wrapper.py b/tests/test_optimizers/test_gfo_wrapper.py deleted file mode 100644 index 8a54d75d..00000000 --- a/tests/test_optimizers/test_gfo_wrapper.py +++ /dev/null @@ -1,111 +0,0 @@ -"""Test module for GFO wrapper optimizer functionality.""" - -import numpy as np -import pytest -from tqdm import tqdm - -from hyperactive.search_space import SearchSpace - -from ._parametrize import optimizers - - -def objective_function(opt): - """Return standard quadratic objective function.""" - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_m5(opt): - """Quadratic objective function shifted by -5.""" - score = -(opt["x1"] - 5) * (opt["x1"] - 5) - return score - - -def objective_function_p5(opt): - """Quadratic objective function shifted by +5.""" - score = -(opt["x1"] + 5) * (opt["x1"] + 5) - return score - - -search_space_0 = {"x1": list(np.arange(-100, 101, 1))} -search_space_1 = {"x1": list(np.arange(0, 101, 1))} -search_space_2 = {"x1": list(np.arange(-100, 1, 1))} - -search_space_3 = {"x1": list(np.arange(-10, 11, 0.1))} -search_space_4 = {"x1": list(np.arange(0, 11, 0.1))} -search_space_5 = {"x1": list(np.arange(-10, 1, 0.1))} - -search_space_6 = {"x1": list(np.arange(-0.0000000003, 0.0000000003, 0.0000000001))} -search_space_7 = {"x1": list(np.arange(0, 0.0000000003, 0.0000000001))} -search_space_8 = {"x1": list(np.arange(-0.0000000003, 0, 0.0000000001))} - -objective_para = ( - "objective", - [ - (objective_function), - (objective_function_m5), - (objective_function_p5), - ], -) - -search_space_para = ( - "search_space", - [ - (search_space_0), - (search_space_1), - (search_space_2), - (search_space_3), - (search_space_4), - (search_space_5), - (search_space_6), - (search_space_7), - (search_space_8), - ], -) - - -@pytest.mark.parametrize(*objective_para) -@pytest.mark.parametrize(*search_space_para) -@pytest.mark.parametrize(*optimizers) -def test_gfo_opt_wrapper_0(Optimizer, search_space, objective): - """Test GFO optimizer wrapper functionality with various configurations.""" - search_space = search_space - objective_function = objective - - n_iter = 10 - s_space = SearchSpace(search_space) - - initialize = {"vertices": 2} - constraints = [] - pass_through = {} - callbacks = None - catch = None - max_score = None - early_stopping = None - random_state = None - memory = None - memory_warm_start = None - verbosity = ["progress_bar", "print_results", "print_times"] - - opt = Optimizer() - - opt.setup_search( - objective_function=objective_function, - s_space=s_space, - n_iter=n_iter, - initialize=initialize, - constraints=constraints, - pass_through=pass_through, - callbacks=callbacks, - catch=catch, - max_score=max_score, - early_stopping=early_stopping, - random_state=random_state, - memory=memory, - memory_warm_start=memory_warm_start, - verbosity=verbosity, - ) - opt.max_time = None - opt.search(nth_process=0, p_bar=tqdm(total=n_iter)) - - assert opt.best_score == objective_function(opt.best_para) diff --git a/tests/test_optimizers/test_memory.py b/tests/test_optimizers/test_memory.py deleted file mode 100644 index 5ee443ce..00000000 --- a/tests/test_optimizers/test_memory.py +++ /dev/null @@ -1,42 +0,0 @@ -"""Test module for optimizer memory functionality.""" - -import numpy as np -import pytest - -from hyperactive import Hyperactive - -from ._parametrize import optimizers - - -def objective_function(opt): - """Return simple quadratic objective function for memory testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -search_space = {"x1": list(np.arange(-10, 11, 1))} - - -@pytest.mark.parametrize(*optimizers) -def test_memory_0(Optimizer): - """Test memory functionality with multiple search runs.""" - optimizer = Optimizer() - - n_iter = 30 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=optimizer, - n_iter=n_iter, - n_jobs=2, - ) - hyper.add_search( - objective_function, - search_space, - optimizer=optimizer, - n_iter=n_iter, - n_jobs=2, - ) - hyper.run() diff --git a/tests/test_optimizers/test_optimization_strategies.py b/tests/test_optimizers/test_optimization_strategies.py deleted file mode 100644 index 7b4baab6..00000000 --- a/tests/test_optimizers/test_optimization_strategies.py +++ /dev/null @@ -1,58 +0,0 @@ -"""Test module for optimizer optimization strategy functionality.""" - -import numpy as np -import pytest - -from hyperactive import Hyperactive -from hyperactive.optimizers import HillClimbingOptimizer -from hyperactive.optimizers.strategies import CustomOptimizationStrategy - -from ._parametrize import optimizers - - -def objective_function(opt): - """Two-dimensional quadratic objective function.""" - score = -(opt["x1"] * opt["x1"] + opt["x2"] * opt["x2"]) - return score - - -search_space = { - "x1": list(np.arange(-3, 3, 1)), - "x2": list(np.arange(-3, 3, 1)), -} - - -@pytest.mark.parametrize(*optimizers) -def test_strategy_combinations_0(Optimizer): - """Test custom optimization strategy with multiple optimizers.""" - optimizer1 = Optimizer() - optimizer2 = HillClimbingOptimizer() - - opt_strat = CustomOptimizationStrategy() - opt_strat.add_optimizer(optimizer1, duration=0.5) - opt_strat.add_optimizer(optimizer2, duration=0.5) - - n_iter = 4 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - optimizer=opt_strat, - n_iter=n_iter, - memory=False, - initialize={"random": 1}, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - - optimizer1 = hyper.opt_pros[0].optimizer_setup_l[0]["optimizer"] - optimizer2 = hyper.opt_pros[0].optimizer_setup_l[1]["optimizer"] - - assert len(search_data) == n_iter - - assert len(optimizer1.search_data) == 2 - assert len(optimizer2.search_data) == 2 - - assert optimizer1.best_score <= optimizer2.best_score diff --git a/tests/test_pass_through.py b/tests/test_pass_through.py deleted file mode 100644 index 7e2ba2af..00000000 --- a/tests/test_pass_through.py +++ /dev/null @@ -1,210 +0,0 @@ -"""Test module for pass through functionality.""" - -import copy - -import numpy as np -import pytest - -from hyperactive import Hyperactive - -search_space = { - "x1": list(np.arange(0, 100, 1)), -} - - -def _test_func(): - pass - - -def _test_func_1(): - pass - - -def objective_function_0(opt): - """Test objective function for integer pass_through values.""" - if opt.pass_through["stuff"] != 1: - print("\n pass_through:", opt.pass_through["stuff"]) - assert False - - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_1(opt): - """Test objective function for float pass_through values.""" - if opt.pass_through["stuff"] != 0.001: - print("\n pass_through:", opt.pass_through["stuff"]) - assert False - - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_2(opt): - """Test objective function for list pass_through values.""" - if opt.pass_through["stuff"] != [1, 2, 3]: - print("\n pass_through:", opt.pass_through["stuff"]) - assert False - - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_3(opt): - """Test objective function for function pass_through values.""" - if opt.pass_through["stuff"] != _test_func: - print("\n pass_through:", opt.pass_through["stuff"]) - assert False - - score = -opt["x1"] * opt["x1"] - return score - - -pass_through_0 = {"stuff": 1} -pass_through_1 = {"stuff": 0.001} -pass_through_2 = {"stuff": [1, 2, 3]} -pass_through_3 = {"stuff": _test_func} - - -pass_through_setup_0 = (objective_function_0, pass_through_0) -pass_through_setup_1 = (objective_function_1, pass_through_1) -pass_through_setup_2 = (objective_function_2, pass_through_2) -pass_through_setup_3 = (objective_function_3, pass_through_3) - -pass_through_setups = ( - "pass_through_setup", - [ - (pass_through_setup_0), - (pass_through_setup_1), - (pass_through_setup_2), - (pass_through_setup_3), - ], -) - - -@pytest.mark.parametrize(*pass_through_setups) -def test_pass_through_0(pass_through_setup): - """Test basic pass_through functionality with different data types.""" - objective_function = pass_through_setup[0] - pass_through = pass_through_setup[1] - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - pass_through=pass_through, - ) - hyper.run() - - -def objective_function_0(opt): - """Test objective function for modifying integer pass_through values.""" - if opt.nth_iter > 1: - assert opt.pass_through["stuff"] == 2 - opt.pass_through["stuff"] = 2 - - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_1(opt): - """Test objective function for modifying float pass_through values.""" - if opt.nth_iter > 1: - assert opt.pass_through["stuff"] == 0.002 - opt.pass_through["stuff"] = 0.002 - - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_2(opt): - """Test objective function for modifying list pass_through values.""" - if opt.nth_iter > 1: - assert 4 in opt.pass_through["stuff"] - opt.pass_through["stuff"].append(4) - - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function_3(opt): - """Test objective function for modifying function pass_through values.""" - if opt.nth_iter > 1: - assert opt.pass_through["stuff"] == _test_func_1 - opt.pass_through["stuff"] = _test_func_1 - - score = -opt["x1"] * opt["x1"] - return score - - -pass_through_setup_0 = (objective_function_0, pass_through_0) -pass_through_setup_1 = (objective_function_1, pass_through_1) -pass_through_setup_2 = (objective_function_2, pass_through_2) -pass_through_setup_3 = (objective_function_3, pass_through_3) - -pass_through_setups = ( - "pass_through_setup", - [ - (pass_through_setup_0), - (pass_through_setup_1), - (pass_through_setup_2), - (pass_through_setup_3), - ], -) - - -@pytest.mark.parametrize(*pass_through_setups) -def test_pass_through_1(pass_through_setup): - """Test pass_through modification during optimization.""" - objective_function = pass_through_setup[0] - pass_through = pass_through_setup[1] - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - pass_through=pass_through, - ) - pass_through = copy.deepcopy(pass_through) - - hyper.run() - - -@pytest.mark.parametrize(*pass_through_setups) -def test_pass_through_2(pass_through_setup): - """Test pass_through modification with 2 parallel jobs.""" - objective_function = pass_through_setup[0] - pass_through = pass_through_setup[1] - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - n_jobs=2, - pass_through=pass_through, - ) - pass_through = copy.deepcopy(pass_through) - - hyper.run() - - -@pytest.mark.parametrize(*pass_through_setups) -def test_pass_through_3(pass_through_setup): - """Test pass_through modification with 4 parallel jobs.""" - objective_function = pass_through_setup[0] - pass_through = pass_through_setup[1] - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=100, - n_jobs=4, - pass_through=pass_through, - ) - pass_through = copy.deepcopy(pass_through) - - hyper.run() diff --git a/tests/test_random_state.py b/tests/test_random_state.py deleted file mode 100644 index 987f7ee0..00000000 --- a/tests/test_random_state.py +++ /dev/null @@ -1,205 +0,0 @@ -"""Test module for random state functionality.""" - -import numpy as np - -from hyperactive import Hyperactive - - -def objective_function(opt): - """Two-dimensional quadratic objective function for random state testing.""" - score = -(opt["x1"] * opt["x1"] + opt["x2"] * opt["x2"]) - return score - - -search_space = { - "x1": list(np.arange(-1000, 1000, 0.1)), - "x2": list(np.arange(-1000, 1000, 0.1)), -} - - -err = 0.001 - - -def test_random_state_n_jobs_0(): - """Test random state behavior with n_jobs=2.""" - n_jobs = 2 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=5, - initialize={"random": 1}, - random_state=1, - n_jobs=n_jobs, - ) - hyper.run() - - results = hyper.search_data(objective_function) - - no_dup = results.drop_duplicates(subset=list(search_space.keys())) - print("no_dup", no_dup) - print("results", results) - - print(int(len(results) / n_jobs)) - print(len(no_dup)) - - assert int(len(results) / n_jobs) != len(no_dup) - - -def test_random_state_n_jobs_1(): - """Test random state behavior with n_jobs=3.""" - n_jobs = 3 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=5, - initialize={"random": 1}, - random_state=1, - n_jobs=n_jobs, - ) - hyper.run() - - results = hyper.search_data(objective_function) - - no_dup = results.drop_duplicates(subset=list(search_space.keys())) - print("no_dup", no_dup) - print("results", results) - - assert int(len(results) / n_jobs) != len(no_dup) - - -def test_random_state_n_jobs_2(): - """Test random state behavior with n_jobs=4.""" - n_jobs = 4 - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=5, - initialize={"random": 1}, - random_state=1, - n_jobs=n_jobs, - ) - hyper.run() - - results = hyper.search_data(objective_function) - - no_dup = results.drop_duplicates(subset=list(search_space.keys())) - print("no_dup", no_dup) - print("results", results) - - assert int(len(results) / n_jobs) != len(no_dup) - - -def test_random_state_0(): - """Test reproducibility with same random state.""" - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - random_state=1, - ) - hyper0.run() - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - random_state=1, - ) - hyper1.run() - - best_score0 = hyper0.best_score(objective_function) - best_score1 = hyper1.best_score(objective_function) - - assert abs(best_score0 - best_score1) < err - - -def test_random_state_1(): - """Test reproducibility with same random state (different value).""" - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - random_state=10, - ) - hyper0.run() - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - random_state=10, - ) - hyper1.run() - - best_score0 = hyper0.best_score(objective_function) - best_score1 = hyper1.best_score(objective_function) - - assert abs(best_score0 - best_score1) < err - - -def test_random_state_2(): - """Test different results with different random states.""" - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - random_state=1, - ) - hyper0.run() - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - random_state=10, - ) - hyper1.run() - - best_score0 = hyper0.best_score(objective_function) - best_score1 = hyper1.best_score(objective_function) - - assert abs(best_score0 - best_score1) > err - - -def test_no_random_state_0(): - """Test non-reproducibility without fixed random state.""" - hyper0 = Hyperactive() - hyper0.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - ) - hyper0.run() - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=10, - initialize={"random": 1}, - ) - hyper1.run() - - best_score0 = hyper0.best_score(objective_function) - best_score1 = hyper1.best_score(objective_function) - - assert abs(best_score0 - best_score1) > err diff --git a/tests/test_results.py b/tests/test_results.py deleted file mode 100644 index ac387169..00000000 --- a/tests/test_results.py +++ /dev/null @@ -1,238 +0,0 @@ -"""Test module for results functionality.""" - -import numpy as np -import pandas as pd -import pytest - -from hyperactive import Hyperactive - - -def objective_function(opt): - """Return simple quadratic objective function for results testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -search_space = { - "x1": list(np.arange(0, 100, 1)), -} - - -def test_attributes_results_0(): - """Test search data returns pandas DataFrame.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=100) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - - -def test_attributes_results_1(): - """Test search data contains search space columns.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=100) - hyper.run() - - assert set(search_space.keys()) < set(hyper.search_data(objective_function).columns) - - -def test_attributes_results_2(): - """Test search data contains x1 column.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=100) - hyper.run() - - assert "x1" in list(hyper.search_data(objective_function).columns) - - -def test_attributes_results_3(): - """Test search data contains score column.""" - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=100) - hyper.run() - - assert "score" in list(hyper.search_data(objective_function).columns) - - -def test_attributes_results_4(): - """Test warm start initialization with specific value.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1, - initialize={"warm_start": [{"x1": 0}]}, - ) - hyper.run() - - assert 0 in list(hyper.search_data(objective_function)["x1"].values) - - -def test_attributes_results_5(): - """Test warm start initialization with different value.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=1, - initialize={"warm_start": [{"x1": 10}]}, - ) - hyper.run() - - print( - "\n x1_results \n", - list(hyper.search_data(objective_function)["x1"].values), - ) - - assert 10 in list(hyper.search_data(objective_function)["x1"].values) - - -def test_attributes_results_6(): - """Test memory disabled allows duplicate search space points.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 10, 1)), - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=20, - initialize={"random": 1}, - memory=False, - ) - hyper.run() - - x1_results = list(hyper.search_data(objective_function)["x1"].values) - - print("\n x1_results \n", x1_results) - - assert len(set(x1_results)) < len(x1_results) - - -def test_attributes_results_7(): - """Test search data without times parameter excludes timing columns.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 10, 1)), - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=20, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - with pytest.raises(Exception): - search_data["eval_times"] - - -def test_attributes_results_8(): - """Test search data without times parameter excludes iteration timing.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 10, 1)), - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=20, - ) - hyper.run() - - search_data = hyper.search_data(objective_function) - with pytest.raises(Exception): - search_data["iter_times"] - - -def test_attributes_results_9(): - """Test search data with times parameter includes timing columns.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 10, 1)), - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=20, - ) - hyper.run() - - search_data = hyper.search_data(objective_function, times=True) - search_data["iter_times"] - search_data["eval_times"] - - -""" -def test_attributes_results_7(): - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": np.arange(0, 10, 1), - } - - opt = RandomSearchOptimizer(search_space) - opt.search( - objective_function, n_iter=20, initialize={"random": 1}, memory=True - ) - - x1_results = list(opt.results["x1"].values) - - print("\n x1_results \n", x1_results) - - assert len(set(x1_results)) == len(x1_results) - - -def test_attributes_results_8(): - def objective_function(para): - score = -para["x1"] * para["x1"] - return score - - search_space = { - "x1": np.arange(-10, 11, 1), - } - - results = pd.DataFrame(np.arange(-10, 10, 1), columns=["x1"]) - results["score"] = 0 - - opt = RandomSearchOptimizer(search_space) - opt.search( - objective_function, - n_iter=100, - initialize={}, - memory=True, - memory_warm_start=results, - ) - - print("\n opt.results \n", opt.results) - - x1_results = list(opt.results["x1"].values) - - assert 10 == x1_results[0] -""" diff --git a/tests/test_results_methods.py b/tests/test_results_methods.py deleted file mode 100644 index d810c3e0..00000000 --- a/tests/test_results_methods.py +++ /dev/null @@ -1,257 +0,0 @@ -"""Test module for results methods functionality.""" - -import numbers - -import numpy as np -import pandas as pd -import pytest - -from hyperactive import Hyperactive - - -def objective_function(opt): - """Primary objective function for results testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -def objective_function1(opt): - """Secondary objective function for results testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -search_space = { - "x1": list(np.arange(0, 100, 1)), -} - - -def test_attributes_best_score_objective_function_0(): - """Test best score returns numeric value.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_score(objective_function), numbers.Number) - - -def test_attributes_best_score_objective_function_1(): - """Test best score with multiple objective functions.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.add_search( - objective_function1, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_score(objective_function), numbers.Number) - - -""" -def test_attributes_best_score_search_id_0(): - # Test best score with search ID. - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - search_id="1", - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_score(objective_function), numbers.Number) - - -def test_attributes_best_score_search_id_1(): - # Test best score with multiple search IDs. - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - search_id="1", - n_iter=15, - ) - hyper.add_search( - objective_function1, - search_space, - search_id="2", - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_score(objective_function), numbers.Number) -""" - - -def test_attributes_best_para_objective_function_0(): - """Test best parameters returns dictionary.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_para(objective_function), dict) - - -def test_attributes_best_para_objective_function_1(): - """Test best parameters with multiple objective functions.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.add_search( - objective_function1, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_para(objective_function), dict) - - -""" -def test_attributes_best_para_search_id_0(): - # Test best parameters with search ID. - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - search_id="1", - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_para("1"), dict) - - -def test_attributes_best_para_search_id_1(): - # Test best parameters with multiple search IDs. - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - search_id="1", - n_iter=15, - ) - hyper.add_search( - objective_function1, - search_space, - search_id="2", - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.best_para("1"), dict) -""" - - -def test_attributes_results_objective_function_0(): - """Test search results returns DataFrame.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - - -def test_attributes_results_objective_function_1(): - """Test search results with multiple objective functions.""" - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.add_search( - objective_function1, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - - -""" -def test_attributes_results_search_id_0(): - # Test search results with search ID. - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - search_id="1", - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data("1"), pd.DataFrame) - - -def test_attributes_results_search_id_1(): - # Test search results with multiple search IDs. - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - search_id="1", - n_iter=15, - ) - hyper.add_search( - objective_function1, - search_space, - search_id="2", - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data("1"), pd.DataFrame) -""" - - -def test_attributes_result_errors_0(): - """Test error handling with no search runs.""" - with pytest.raises(ValueError): - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15) - hyper.run() - - hyper.best_para(objective_function1) - - -def test_attributes_result_errors_1(): - """Test error handling with unknown objective function.""" - with pytest.raises(ValueError): - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15) - hyper.run() - - hyper.best_score(objective_function1) - - -def test_attributes_result_errors_2(): - """Test error handling with unknown search ID.""" - with pytest.raises(ValueError): - hyper = Hyperactive() - hyper.add_search(objective_function, search_space, n_iter=15) - hyper.run() - - hyper.search_data(objective_function1) diff --git a/tests/test_search_spaces.py b/tests/test_search_spaces.py deleted file mode 100644 index 2727c8b6..00000000 --- a/tests/test_search_spaces.py +++ /dev/null @@ -1,223 +0,0 @@ -"""Test module for search space functionality.""" - -import sys - -import numpy as np -import pandas as pd -import pytest - -from hyperactive import Hyperactive - -if sys.platform.startswith("win"): - pytest.skip("skip these tests for windows", allow_module_level=True) - - -def test_search_space_0(): - """Test search space with integer range.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(range(0, 3, 1)), - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - assert hyper.best_para(objective_function)["x1"] in search_space["x1"] - - -def test_search_space_1(): - """Test search space with float range.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(np.arange(0, 0.003, 0.001)), - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - assert hyper.best_para(objective_function)["x1"] in search_space["x1"] - - -def test_search_space_2(): - """Test search space with numpy float range.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(range(0, 100, 1)), - "str1": ["0", "1", "2"], - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - assert hyper.best_para(objective_function)["str1"] in search_space["str1"] - - -def test_search_space_3(): - """Test search space with function objects.""" - - def func1(): - pass - - def func2(): - pass - - def func3(): - pass - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(range(0, 100, 1)), - "func1": [func1, func2, func3], - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - assert hyper.best_para(objective_function)["func1"] in search_space["func1"] - - -def test_search_space_4(): - """Test search space with class objects.""" - - class class1: - pass - - class class2: - pass - - class class3: - pass - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(range(0, 100, 1)), - "class1": [class1, class2, class3], - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - assert hyper.best_para(objective_function)["class1"] in search_space["class1"] - - -def test_search_space_5(): - """Test search space with initialized class instances.""" - - class class1: - def __init__(self): - pass - - class class2: - def __init__(self): - pass - - class class3: - def __init__(self): - pass - - def class_f1(): - return class1 - - def class_f2(): - return class2 - - def class_f3(): - return class3 - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - search_space = { - "x1": list(range(0, 100, 1)), - "class1": [class_f1, class_f2, class_f3], - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - assert hyper.best_para(objective_function)["class1"] in search_space["class1"] - - -def test_search_space_6(): - """Test search space with mixed parameter types.""" - - def objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - def list_f1(): - return [0, 1] - - def list_f2(): - return [1, 0] - - search_space = { - "x1": list(range(0, 100, 1)), - "list1": [list_f1, list_f2], - } - - hyper = Hyperactive() - hyper.add_search( - objective_function, - search_space, - n_iter=15, - ) - hyper.run() - - assert isinstance(hyper.search_data(objective_function), pd.DataFrame) - assert hyper.best_para(objective_function)["list1"] in search_space["list1"] diff --git a/tests/test_warm_starts/__init__.py b/tests/test_warm_starts/__init__.py deleted file mode 100644 index 98ac7f85..00000000 --- a/tests/test_warm_starts/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test package for warm start functionality tests.""" diff --git a/tests/test_warm_starts/test_memory_warm_start.py b/tests/test_warm_starts/test_memory_warm_start.py deleted file mode 100644 index b1c43e0f..00000000 --- a/tests/test_warm_starts/test_memory_warm_start.py +++ /dev/null @@ -1,145 +0,0 @@ -"""Test module for memory warm start functionality.""" - -import sys - -import numpy as np -import pytest - -from hyperactive import Hyperactive - -if sys.platform.startswith("win"): - pytest.skip("skip these tests for windows", allow_module_level=True) - - -def func1(): - """Test function 1 for search space.""" - pass - - -def func2(): - """Test function 2 for search space.""" - pass - - -class class1: - """Test class for search space functionality.""" - - def __init__(self): - pass - - -class class2: - """Test class for search space functionality.""" - - def __init__(self): - pass - - -def class_f1(): - """Return class1 for search space.""" - return class1 - - -def class_f2(): - """Return class2 for search space.""" - return class2 - - -def numpy_f1(): - """Return numpy array [0, 1] for search space.""" - return np.array([0, 1]) - - -def numpy_f2(): - """Return numpy array [1, 0] for search space.""" - return np.array([1, 0]) - - -search_space = { - "x0": list(range(-3, 3)), - "x1": list(np.arange(-1, 1, 0.001)), - "string0": ["str0", "str1"], - "function0": [func1, func2], - "class0": [class_f1, class_f2], - "numpy0": [numpy_f1, numpy_f2], -} - - -def objective_function(opt): - """Return simple quadratic objective function for testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -def test_memory_warm_start_0(): - """Test memory warm start from single job to single job.""" - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=15) - hyper0.run() - - search_data0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - memory_warm_start=search_data0, - ) - hyper1.run() - - -def test_memory_warm_start_1(): - """Test memory warm start from multi-job to single job.""" - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper0.run() - - search_data0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - memory_warm_start=search_data0, - ) - hyper1.run() - - -def test_memory_warm_start_2(): - """Test memory warm start from single job to multi-job.""" - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=15) - hyper0.run() - - search_data0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive(distribution="pathos") - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - n_jobs=2, - memory_warm_start=search_data0, - ) - hyper1.run() - - -def test_memory_warm_start_3(): - """Test memory warm start from multi-job to multi-job.""" - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper0.run() - - search_data0 = hyper0.search_data(objective_function) - - hyper1 = Hyperactive(distribution="pathos") - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - n_jobs=2, - memory_warm_start=search_data0, - ) - hyper1.run() diff --git a/tests/test_warm_starts/test_warm_start.py b/tests/test_warm_starts/test_warm_start.py deleted file mode 100644 index 60c1a7ac..00000000 --- a/tests/test_warm_starts/test_warm_start.py +++ /dev/null @@ -1,145 +0,0 @@ -"""Test module for warm start functionality.""" - -import sys - -import numpy as np -import pytest - -from hyperactive import Hyperactive - -if sys.platform.startswith("win"): - pytest.skip("skip these tests for windows", allow_module_level=True) - - -def func1(): - """Test function 1 for search space.""" - pass - - -def func2(): - """Test function 2 for search space.""" - pass - - -class class1: - """Test class for search space functionality.""" - - def __init__(self): - pass - - -class class2: - """Test class for search space functionality.""" - - def __init__(self): - pass - - -def class_f1(): - """Return class1 for search space.""" - return class1 - - -def class_f2(): - """Return class2 for search space.""" - return class2 - - -def numpy_f1(): - """Return numpy array [0, 1] for search space.""" - return np.array([0, 1]) - - -def numpy_f2(): - """Return numpy array [1, 0] for search space.""" - return np.array([1, 0]) - - -search_space = { - "x0": list(range(-3, 3)), - "x1": list(np.arange(-1, 1, 0.001)), - "string0": ["str0", "str1"], - "function0": [func1, func2], - "class0": [class_f1, class_f2], - "numpy0": [numpy_f1, numpy_f2], -} - - -def objective_function(opt): - """Return simple quadratic objective function for testing.""" - score = -opt["x1"] * opt["x1"] - return score - - -def test_warm_start_0(): - """Test warm start from single job to single job.""" - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=15) - hyper0.run() - - best_para0 = hyper0.best_para(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - initialize={"warm_start": [best_para0]}, - ) - hyper1.run() - - -def test_warm_start_1(): - """Test warm start from multi-job to single job.""" - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper0.run() - - best_para0 = hyper0.best_para(objective_function) - - hyper1 = Hyperactive() - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - initialize={"warm_start": [best_para0]}, - ) - hyper1.run() - - -def test_warm_start_2(): - """Test warm start from single job to multi-job.""" - hyper0 = Hyperactive() - hyper0.add_search(objective_function, search_space, n_iter=15) - hyper0.run() - - best_para0 = hyper0.best_para(objective_function) - - hyper1 = Hyperactive(distribution="pathos") - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - n_jobs=2, - initialize={"warm_start": [best_para0]}, - ) - hyper1.run() - - -def test_warm_start_3(): - """Test warm start from multi-job to multi-job.""" - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search(objective_function, search_space, n_iter=15, n_jobs=2) - hyper0.run() - - best_para0 = hyper0.best_para(objective_function) - - hyper1 = Hyperactive(distribution="pathos") - hyper1.add_search( - objective_function, - search_space, - n_iter=15, - n_jobs=2, - initialize={"warm_start": [best_para0]}, - ) - hyper1.run() diff --git a/tests/test_warm_starts/test_warm_start_smbo.py b/tests/test_warm_starts/test_warm_start_smbo.py deleted file mode 100644 index 03c43944..00000000 --- a/tests/test_warm_starts/test_warm_start_smbo.py +++ /dev/null @@ -1,160 +0,0 @@ -# ruff: noqa: D100, D103 -import sys - -import numpy as np -import pytest - -from hyperactive import ( - Hyperactive, -) -from hyperactive.optimizers import ( - BayesianOptimizer, - ForestOptimizer, - TreeStructuredParzenEstimators, -) - -if sys.platform.startswith("win"): - pytest.skip("skip these tests for windows", allow_module_level=True) - - -def _func1(): - pass - - -def _func2(): - pass - - -class _class1: - def __init__(self): - pass - - -class _class2: - def __init__(self): - pass - - -def _class_f1(): - return _class1 - - -def _class_f2(): - return _class2 - - -def _numpy_f1(): - return np.array([0, 1]) - - -def _numpy_f2(): - return np.array([1, 0]) - - -search_space = { - "x0": list(range(-3, 3)), - "x1": list(np.arange(-1, 1, 0.001)), - "string0": ["str0", "str1"], - "function0": [_func1, _func2], - "class0": [_class_f1, _class_f2], - "numpy0": [_numpy_f1, _numpy_f2], -} - - -def _objective_function(opt): - score = -opt["x1"] * opt["x1"] - return score - - -smbo_opts = [ - BayesianOptimizer, - TreeStructuredParzenEstimators, - ForestOptimizer, -] - -initialize = {"random": 1} -n_iter = 3 - - -@pytest.mark.parametrize("smbo_opt", smbo_opts) -def test_warm_start_smbo_0(smbo_opt): - hyper0 = Hyperactive() - hyper0.add_search(_objective_function, search_space, n_iter=n_iter) - hyper0.run() - - search_data0 = hyper0.search_data(_objective_function) - smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) - - hyper1 = Hyperactive() - hyper1.add_search( - _objective_function, - search_space, - n_iter=n_iter, - optimizer=smbo_opt_, - initialize=initialize, - ) - hyper1.run() - - -@pytest.mark.parametrize("smbo_opt", smbo_opts) -def test_warm_start_smbo_1(smbo_opt): - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search( - _objective_function, - search_space, - n_iter=n_iter, - n_jobs=2, - initialize=initialize, - ) - hyper0.run() - - search_data0 = hyper0.search_data(_objective_function) - smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) - - hyper1 = Hyperactive() - hyper1.add_search( - _objective_function, search_space, n_iter=n_iter, optimizer=smbo_opt_ - ) - hyper1.run() - - -@pytest.mark.parametrize("smbo_opt", smbo_opts) -def test_warm_start_smbo_2(smbo_opt): - hyper0 = Hyperactive() - hyper0.add_search(_objective_function, search_space, n_iter=n_iter) - hyper0.run() - - search_data0 = hyper0.search_data(_objective_function) - smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) - - hyper1 = Hyperactive(distribution="joblib") - hyper1.add_search( - _objective_function, - search_space, - n_iter=n_iter, - n_jobs=2, - optimizer=smbo_opt_, - initialize=initialize, - ) - hyper1.run() - - -@pytest.mark.parametrize("smbo_opt", smbo_opts) -def test_warm_start_smbo_3(smbo_opt): - hyper0 = Hyperactive(distribution="pathos") - hyper0.add_search(_objective_function, search_space, n_iter=n_iter, n_jobs=2) - hyper0.run() - - search_data0 = hyper0.search_data(_objective_function) - smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) - - hyper1 = Hyperactive(distribution="joblib") - hyper1.add_search( - _objective_function, - search_space, - n_iter=n_iter, - n_jobs=2, - optimizer=smbo_opt_, - initialize=initialize, - ) - hyper1.run()