L idddlmZddlmZddlmZerddlmZddlm Z ddlm Z GddZ y ) ) annotations) TYPE_CHECKING) TrialState) Container)Study) FrozenTrialcDeZdZdZej ff ddZddZy)MaxTrialsCallbacka"Set a maximum number of trials before ending the study. While the ``n_trials`` argument of :meth:`optuna.study.Study.optimize` sets the number of trials that will be run, you may want to continue running until you have a certain number of successfully completed trials or stop the study when you have a certain number of trials that fail. This ``MaxTrialsCallback`` class allows you to set a maximum number of trials for a particular :class:`~optuna.trial.TrialState` before stopping the study. Example: .. testcode:: import optuna from optuna.study import MaxTrialsCallback from optuna.trial import TrialState def objective(trial): x = trial.suggest_float("x", -1, 1) return x**2 study = optuna.create_study() study.optimize( objective, callbacks=[MaxTrialsCallback(10, states=(TrialState.COMPLETE,))], ) Args: n_trials: The max number of trials. Must be set to an integer. states: Tuple of the :class:`~optuna.trial.TrialState` to be counted towards the max trials limit. Default value is ``(TrialState.COMPLETE,)``. If :obj:`None`, count all states. c ||_||_y)N) _n_trials_states)selfn_trialsstatess W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/_callbacks.py__init__zMaxTrialsCallback.__init__5s" c|jd|j}t|}||jk\r|j yy)NF)deepcopyr) get_trialsr lenr stop)rstudytrialtrials n_completes r__call__zMaxTrialsCallback.__call__;s>!!5!F[  ' JJL (rN)rintrzContainer[TrialState] | NonereturnNone)rrrrrr )__name__ __module__ __qualname____doc__rCOMPLETErrrrr r s:#LFPEXEXDZ%A  rr N) __future__rtypingr optuna.trialrcollections.abcr optuna.studyrrr r&rrr,s&" #)"(00r