
    !#hJ                    p    d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
  ed       G d de             Zy)	    )annotationsN)experimental_class)
BasePruner)StudyDirectionz2.8.0c                  2    e Zd ZdZ	 d	 	 	 	 	 	 	 ddZddZy)PatientPruneraW  Pruner which wraps another pruner with tolerance.

    Example:

        .. testcode::

            import numpy as np
            from sklearn.datasets import load_iris
            from sklearn.linear_model import SGDClassifier
            from sklearn.model_selection import train_test_split

            import optuna

            X, y = load_iris(return_X_y=True)
            X_train, X_valid, y_train, y_valid = train_test_split(X, y)
            classes = np.unique(y)


            def objective(trial):
                alpha = trial.suggest_float("alpha", 0.0, 1.0)
                clf = SGDClassifier(alpha=alpha)
                n_train_iter = 100

                for step in range(n_train_iter):
                    clf.partial_fit(X_train, y_train, classes=classes)

                    intermediate_value = clf.score(X_valid, y_valid)
                    trial.report(intermediate_value, step)

                    if trial.should_prune():
                        raise optuna.TrialPruned()

                return clf.score(X_valid, y_valid)


            study = optuna.create_study(
                direction="maximize",
                pruner=optuna.pruners.PatientPruner(optuna.pruners.MedianPruner(), patience=1),
            )
            study.optimize(objective, n_trials=20)

    Args:
        wrapped_pruner:
            Wrapped pruner to perform pruning when :class:`~optuna.pruners.PatientPruner` allows a
            trial to be pruned. If it is :obj:`None`, this pruner is equivalent to
            early-stopping taken the intermediate values in the individual trial.
        patience:
            Pruning is disabled until the objective doesn't improve for
            ``patience`` consecutive steps.
        min_delta:
            Tolerance value to check whether or not the objective improves.
            This value should be non-negative.

    c                ~    |dk  rt        d| d      |dk  rt        d| d      || _        || _        || _        y )Nr   z$patience cannot be negative but got .z%min_delta cannot be negative but got )
ValueError_wrapped_pruner	_patience
_min_delta)selfwrapped_prunerpatience	min_deltas       T/var/www/html/sandstorm/venv/lib/python3.12/site-packages/optuna/pruners/_patient.py__init__zPatientPruner.__init__D   sR     a<CH:QOPPq=DYKqQRR-!#    c                L   |j                   }|y|j                  t        j                  t	        j                                     }|j                  | j                  dz   k  ry|j                          |d | j                   dz
   }t        j                  t	        fd|D                    }|| j                   dz
  d  }t        j                  t	        fd|D                    }|j                  }	|	t        j                  k(  r9t        j                  |      | j                  z   t        j                  |      k  }
n8t        j                  |      | j                  z
  t        j                  |      kD  }
|
r)| j                  | j                  j!                  ||      S yy)NF   c              3  (   K   | ]	  }|     y wN .0stepintermediate_valuess     r   	<genexpr>z&PatientPruner.prune.<locals>.<genexpr>a   s     Mt$T*M   c              3  (   K   | ]	  }|     y wr   r   r   s     r   r   z&PatientPruner.prune.<locals>.<genexpr>f   s     Lt$T*Lr    T)	last_stepr   npasarraylistkeyssizer   sort	directionr   MINIMIZEnanminr   nanmaxr   prune)r   studytrialr   stepssteps_before_patiencescores_before_patiencesteps_after_patiencescores_after_patiencer)   maybe_pruner   s              @r   r-   zPatientPruner.pruneQ   sy   <#77

4 3 8 8 :;< ::!++

 %&;!(; <!#M7LMM"
  %dnn_q%8%:; "

L7KLL!
 OO	///))$:;dooMPRPYPY%Q K ))$:;dooMPRPYPY%Q K ##/++11%??r   N)g        )r   zBasePruner | Noner   intr   floatreturnNone)r.   z'optuna.study.Study'r/   z'optuna.trial.FrozenTrial'r8   bool)__name__
__module____qualname____doc__r   r-   r   r   r   r   r      s9    5p TW$/$;>$KP$	$(r   r   )
__future__r   numpyr#   optunaoptuna._experimentalr   optuna.prunersr   optuna.study._study_directionr   r   r   r   r   <module>rE      s:    "   3 % 8 GmJ m mr   