
    ,YHh
                        d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ  ee      Z ed       G d	 d
             Zy)    )annotations)experimental_class)
get_logger)Study)BaseTerminator)
Terminator)FrozenTrialz3.2.0c                  "    e Zd ZdZdddZddZy)TerminatorCallbackax  A callback that terminates the optimization using Terminator.

    This class implements a callback which wraps :class:`~optuna.terminator.Terminator`
    so that it can be used with the :func:`~optuna.study.Study.optimize` method.

    Args:
        terminator:
            A terminator object which determines whether to terminate the optimization by
            assessing the room for optimization and statistical error. Defaults to a
            :class:`~optuna.terminator.Terminator` object with default
            ``improvement_evaluator`` and ``error_evaluator``.

    Example:

        .. testcode::

            from sklearn.datasets import load_wine
            from sklearn.ensemble import RandomForestClassifier
            from sklearn.model_selection import cross_val_score
            from sklearn.model_selection import KFold

            import optuna
            from optuna.terminator import TerminatorCallback
            from optuna.terminator import report_cross_validation_scores


            def objective(trial):
                X, y = load_wine(return_X_y=True)

                clf = RandomForestClassifier(
                    max_depth=trial.suggest_int("max_depth", 2, 32),
                    min_samples_split=trial.suggest_float("min_samples_split", 0, 1),
                    criterion=trial.suggest_categorical("criterion", ("gini", "entropy")),
                )

                scores = cross_val_score(clf, X, y, cv=KFold(n_splits=5, shuffle=True))
                report_cross_validation_scores(trial, scores)
                return scores.mean()


            study = optuna.create_study(direction="maximize")
            terminator = TerminatorCallback()
            study.optimize(objective, n_trials=50, callbacks=[terminator])

    .. seealso::
        Please refer to :class:`~optuna.terminator.Terminator` for the details of
        the terminator mechanism.
    Nc                *    |xs
 t               | _        y N)r   _terminator)self
terminators     S/var/www/html/planif/env/lib/python3.12/site-packages/optuna/terminator/callback.py__init__zTerminatorCallback.__init__A   s    %5    c                    | j                   j                  |      }|r&t        j                  d       |j	                          y y )N)studyz-The study has been stopped by the terminator.)r   should_terminate_loggerinfostop)r   r   trialr   s       r   __call__zTerminatorCallback.__call__D   s:    ++<<5<ILLHIJJL r   r   )r   zBaseTerminator | NonereturnNone)r   r   r   r	   r   r   )__name__
__module____qualname____doc__r   r    r   r   r   r      s    /b6r   r   N)
__future__r   optuna._experimentalr   optuna.loggingr   optuna.study.studyr   optuna.terminator.terminatorr   r   optuna.trialr	   r   r   r   r"   r   r   <module>r)      sD    " 3 % $ 7 3 $ X
 G: : :r   