L irddlmZddlmZddlZddlmZddlmZddlm Z edGdd Z y) ) annotations)AnyN)experimental_class)experimental_func) FrozenTrial2.8.0c~eZdZdZ d d dZd dZeedd dZeedd dZ y)RetryFailedTrialCallbacka:Retry a failed trial up to a maximum number of times. When a trial fails, this callback can be used with a class in :mod:`optuna.storages` to recreate the trial in ``TrialState.WAITING`` to queue up the trial to be run again. The failed trial can be identified by the :func:`~optuna.storages.RetryFailedTrialCallback.retried_trial_number` function. Even if repetitive failure occurs (a retried trial fails again), this method returns the number of the original trial. To get a full list including the numbers of the retried trials as well as their original trial, call the :func:`~optuna.storages.RetryFailedTrialCallback.retry_history` function. This callback is helpful in environments where trials may fail due to external conditions, such as being preempted by other processes. Usage: .. testcode:: import optuna from optuna.storages import RetryFailedTrialCallback storage = optuna.storages.RDBStorage( url="sqlite:///:memory:", heartbeat_interval=60, grace_period=120, failed_trial_callback=RetryFailedTrialCallback(max_retry=3), ) study = optuna.create_study( storage=storage, ) .. seealso:: See :class:`~optuna.storages.RDBStorage`. Args: max_retry: The max number of times a trial can be retried. Must be set to :obj:`None` or an integer. If set to the default value of :obj:`None` will retry indefinitely. If set to an integer, will only retry that many times. inherit_intermediate_values: Option to inherit `trial.intermediate_values` reported by :func:`optuna.trial.Trial.report` from the failed trial. Default is :obj:`False`. Nc ||_||_y)N) _max_retry_inherit_intermediate_values)self max_retryinherit_intermediate_valuess `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/storages/_callbacks.py__init__z!RetryFailedTrialCallback.__init__;s$,G)c |jgd|j}|dj|j|j|jt |dkry|j t jt jjj|j|j|j||jr |jndy)N) failed_trial retry_historyr)stateparams distributions user_attrs system_attrsintermediate_values)numberrappendr len add_trialoptuna create_trialtrial TrialStateWAITINGrrrr r)rstudyr#rs r__call__z!RetryFailedTrialCallback.__call__As!LL(   ( _%,,U\\: ?? &\/%B!CC    ll--55||#11 ++)151R1RE--X\  rrc:|jjddS)a Return the number of the original trial being retried. Args: trial: The trial object. Returns: The number of the first failed trial. If not retry of a previous trial, returns :obj:`None`. rNrgetr#s rretried_trial_numberz-RetryFailedTrialCallback.retried_trial_numberYs!!%%nd;;rz3.0.0c:|jjdgS)alReturn the list of retried trial numbers with respect to the specified trial. Args: trial: The trial object. Returns: A list of trial numbers in ascending order of the series of retried trials. The first item of the list indicates the original trial which is identical to the :func:`~optuna.storages.RetryFailedTrialCallback.retried_trial_number`, and the last item is the one right before the specified trial in the retry series. If the specified trial is not a retry of any trial, returns an empty list. rr)r+s rrz&RetryFailedTrialCallback.retry_historyis !!%%or::r)NF)r int | NonerboolreturnNone)r&z'optuna.study.Study'r#rr0r1)r#rr0r.)r#rr0z list[int]) __name__ __module__ __qualname____doc__rr' staticmethodrr,rrrr r sw,^QVH#HIMH H  0w <  <w; ;rr ) __future__rtypingrr!optuna._experimentalrr optuna.trialrr r7rrr<s8" 32$Gm;m;m;r