diff --git a/pyproject.toml b/pyproject.toml index 55c62122..90b5ed5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ dependencies = [ "psutil", "pymoo>=0.6.0", "pyyaml", + "pydantic>=2.10.6", ] requires-python = ">=3.10" authors = [ diff --git a/src/deephyper/hpo/_search.py b/src/deephyper/hpo/_search.py index a8e35bd9..b31626e8 100644 --- a/src/deephyper/hpo/_search.py +++ b/src/deephyper/hpo/_search.py @@ -6,12 +6,14 @@ import os import pathlib import time -from typing import Dict, List +from typing import Dict, List, Union, Optional, Tuple import numpy as np import pandas as pd import yaml +from pydantic import BaseModel + from deephyper.evaluator import Evaluator, HPOJob, MaximumJobsSpawnReached from deephyper.evaluator.callback import TqdmCallback from deephyper.skopt.moo import non_dominated_set @@ -47,6 +49,14 @@ def get_init_params_as_json(obj): return params +class Evaluation(BaseModel): + """Represents the evaluation of parameters.""" + + id: int + parameters: Dict[str, Optional[Union[str, int, float]]] + objective: Optional[Union[Union[float, str], Tuple[float, str], List[float, str]]] + + class Search(abc.ABC): """Abstract class which represents a search algorithm. @@ -375,7 +385,7 @@ def ask(self, n: int = 1) -> List[Dict]: n (int, optional): The number of configurations to ask. Defaults to 1. Returns: - List[Dict]: a list of hyperparameter configurations to evaluate. + List[Evaluation]: a list of hyperparameter configurations to evaluate. """ logging.info(f"Asking {n} configuration(s)...") t1 = time.time() @@ -387,32 +397,32 @@ def ask(self, n: int = 1) -> List[Dict]: return new_samples @abc.abstractmethod - def _ask(self, n: int = 1) -> List[Dict]: + def _ask(self, n: int = 1) -> List[Evaluation]: """Ask the search for new configurations to evaluate. Args: n (int, optional): The number of configurations to ask. Defaults to 1. Returns: - List[Dict]: a list of hyperparameter configurations to evaluate. + List[Evaluation]: a list of hyperparameter configurations to evaluate. """ - def tell(self, results: List[HPOJob]): + def tell(self, results: List[Evaluation]): """Tell the search the results of the evaluations. Args: - results (List[HPOJob]): a list of HPOJobs from which hyperparameters and objectives can - be retrieved. + results (List[Evaluation]): a list of Evaluations from which hyperparameters and + objectives can be retrieved. """ self._tell(results) @abc.abstractmethod - def _tell(self, results: List[HPOJob]): + def _tell(self, results: List[Evaluation]): """Tell the search the results of the evaluations. Args: - results (List[HPOJob]): a list of HPOJobs from which hyperparameters and objectives can - be retrieved. + results (List[Evaluation]): a list of Evaluation from which hyperparameters and + objectives can be retrieved. """ def dump_jobs_done_to_csv(self, flush: bool = False):