o
    }oi                     @  s^   d dl mZ d dlm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	 d	Z
dS )
    )annotations)AnyN)experimental_class)experimental_func)FrozenTrial2.8.0c                   @  sT   e Zd ZdZ	ddd
dZdddZeeddddZeeddddZ	dS )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`.
    NF	max_retry
int | Noneinherit_intermediate_valuesboolreturnNonec                 C  s   || _ || _d S )N)
_max_retry_inherit_intermediate_values)selfr	   r    r   N/home/ubuntu/.local/lib/python3.10/site-packages/optuna/storages/_callbacks.py__init__;   s   
z!RetryFailedTrialCallback.__init__study'optuna.study.Study'trialr   c              
   C  sz   |j g d|j}|d |j  | jd ur!| jt|d k r!d S |tjtjj	j
|j|j|j|| jr6|jnd d d S )N)failed_trialretry_historyr   )stateparamsdistributions
user_attrssystem_attrsintermediate_values)numberr   appendr   len	add_trialoptunacreate_trialr   
TrialStateWAITINGr   r   r   r   r   )r   r   r   r   r   r   r   __call__A   s&   
z!RetryFailedTrialCallback.__call__r   c                 C  s   | j ddS )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`.
        r   Nr   getr   r   r   r   retried_trial_numberY   s   z-RetryFailedTrialCallback.retried_trial_numberz3.0.0	list[int]c                 C  s   | j dg S )al  Return 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.
        r   r)   r+   r   r   r   r   i   s   z&RetryFailedTrialCallback.retry_history)NF)r	   r
   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r
   )r   r   r   r-   )
__name__
__module____qualname____doc__r   r(   staticmethodr   r,   r   r   r   r   r   r      s    /
r   )
__future__r   typingr   r$   optuna._experimentalr   r   optuna.trialr   r   r   r   r   r   <module>   s    