Ë
    þÍ:j3K  ã                  óÊ   — d dl mZ d dl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 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Z G d„ dej.                  «      Zy)é    )ÚannotationsN)Ú	Container)ÚSequence)ÚAny)Úcast)ÚJSONSerializable)ÚBaseDistribution)ÚUpdateFinishedTrialError)ÚFrozenStudy)ÚStudyDirection)ÚFrozenTrial)Ú
TrialStatezno-name-c                  ót  — e Zd ZdZej
                  	 d	 	 	 	 	 d d„«       Zej
                  d!d„«       Zej
                  d"d„«       Zej
                  d#d„«       Z	ej
                  d$d„«       Z
ej
                  d%d„«       Zej
                  d&d	„«       Zej
                  d'd
„«       Zej
                  d'd„«       Zej
                  d(d„«       Zej
                  dd)d„«       Zej
                  	 	 	 	 	 	 	 	 	 	 d*d„«       Zd+d„Zd,d„Zd-d„Zej
                  	 d	 	 	 	 	 	 	 d.d„«       Zej
                  	 	 	 	 	 	 	 	 d/d„«       Zej
                  d0d„«       Zej
                  d1d„«       Zej
                  d2d„«       Zej
                  	 	 d3	 	 	 	 	 	 	 d4d„«       Z	 d	 	 	 	 	 d5d„Zd6d„Zd7d„Zd7d„Zd7d„Zd8d„Z d9d„Z!y):ÚBaseStoragea¶  Base class for storages.

    This class is not supposed to be directly accessed by library users.

    This class abstracts a backend database and provides internal interfaces to
    read/write histories of studies and trials.

    A storage class implementing this class must meet the following requirements.

    **Thread safety**

    A storage class instance can be shared among multiple threads, and must therefore be
    thread-safe. It must guarantee that a data instance read from the storage must not be modified
    by subsequent writes. For example, `FrozenTrial` instance returned by `get_trial`
    should not be updated by the subsequent `set_trial_xxx`. This is usually achieved by replacing
    the old data with a copy on `set_trial_xxx`.

    A storage class can also assume that a data instance returned are never modified by its user.
    When a user modifies a return value from a storage class, the internal state of the storage
    may become inconsistent. Consequences are undefined.

    **Ownership of RUNNING trials**

    Trials in finished states are not allowed to be modified.
    Trials in the WAITING state are not allowed to be modified except for the `state` field.
    Nc                ó   — t         ‚)a¹  Create a new study from a name.

        If no name is specified, the storage class generates a name.
        The returned study ID is unique among all current and deleted studies.

        Args:
            directions:
                 A sequence of direction whose element is either
                 :obj:`~optuna.study.StudyDirection.MAXIMIZE` or
                 :obj:`~optuna.study.StudyDirection.MINIMIZE`.
            study_name:
                Name of the new study to create.

        Returns:
            ID of the created study.

        Raises:
            :exc:`optuna.exceptions.DuplicatedStudyError`:
                If a study with the same ``study_name`` already exists.
        ©ÚNotImplementedError)ÚselfÚ
directionsÚ
study_names      új/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/optuna/storages/_base.pyÚcreate_new_studyzBaseStorage.create_new_study3   s
   € ô0 "Ð!ó    c                ó   — t         ‚)zÏDelete a study.

        Args:
            study_id:
                ID of the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   ©r   Ústudy_ids     r   Údelete_studyzBaseStorage.delete_studyM   s
   € ô "Ð!r   c                ó   — t         ‚)a©  Register a user-defined attribute to a study.

        This method overwrites any existing attribute.

        Args:
            study_id:
                ID of the study.
            key:
                Attribute key.
            value:
                Attribute value. It should be JSON serializable.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   ©r   r   ÚkeyÚvalues       r   Úset_study_user_attrzBaseStorage.set_study_user_attr[   ó
   € ô$ "Ð!r   c                ó   — t         ‚)a­  Register an optuna-internal attribute to a study.

        This method overwrites any existing attribute.

        Args:
            study_id:
                ID of the study.
            key:
                Attribute key.
            value:
                Attribute value. It should be JSON serializable.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   r   s       r   Úset_study_system_attrz!BaseStorage.set_study_system_attro   r#   r   c                ó   — t         ‚)a  Read the ID of a study.

        Args:
            study_name:
                Name of the study.

        Returns:
            ID of the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_name`` exists.
        r   )r   r   s     r   Úget_study_id_from_namez"BaseStorage.get_study_id_from_name…   ó
   € ô "Ð!r   c                ó   — t         ‚)a  Read the study name of a study.

        Args:
            study_id:
                ID of the study.

        Returns:
            Name of the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   r   s     r   Úget_study_name_from_idz"BaseStorage.get_study_name_from_id–   r(   r   c                ó   — t         ‚)a@  Read whether a study maximizes or minimizes an objective.

        Args:
            study_id:
                ID of a study.

        Returns:
            Optimization directions list of the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   r   s     r   Úget_study_directionsz BaseStorage.get_study_directions§   r(   r   c                ó   — t         ‚)a<  Read the user-defined attributes of a study.

        Args:
            study_id:
                ID of the study.

        Returns:
            Dictionary with the user attributes of the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   r   s     r   Úget_study_user_attrsz BaseStorage.get_study_user_attrs¸   r(   r   c                ó   — t         ‚)aJ  Read the optuna-internal attributes of a study.

        Args:
            study_id:
                ID of the study.

        Returns:
            Dictionary with the optuna-internal attributes of the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   r   s     r   Úget_study_system_attrsz"BaseStorage.get_study_system_attrsÉ   r(   r   c                ó   — t         ‚)z°Read a list of :class:`~optuna.study.FrozenStudy` objects.

        Returns:
            A list of :class:`~optuna.study.FrozenStudy` objects, sorted by ``study_id``.

        r   ©r   s    r   Úget_all_studieszBaseStorage.get_all_studiesÚ   s
   € ô "Ð!r   c                ó   — t         ‚)a'  Create and add a new trial to a study.

        The returned trial ID is unique among all current and deleted trials.

        Args:
            study_id:
                ID of the study.
            template_trial:
                Template :class:`~optuna.trial.FrozenTrial` with default user-attributes,
                system-attributes, intermediate-values, and a state.

        Returns:
            ID of the created trial.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   )r   r   Útemplate_trials      r   Úcreate_new_trialzBaseStorage.create_new_trialæ   ó
   € ô( "Ð!r   c                ó   — t         ‚)a?  Set a parameter to a trial.

        Args:
            trial_id:
                ID of the trial.
            param_name:
                Name of the parameter.
            param_value_internal:
                Internal representation of the parameter value.
            distribution:
                Sampled distribution of the parameter.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
            :exc:`~optuna.exceptions.UpdateFinishedTrialError`:
                If the trial is already finished.
        r   )r   Útrial_idÚ
param_nameÚparam_value_internalÚdistributions        r   Úset_trial_paramzBaseStorage.set_trial_paramü   s
   € ô4 "Ð!r   c                ó†   — | j                  |d¬«      }t        |«      |k  rt        d|›d|›d�«      ‚||   j                  S )a`  Read the trial ID of a trial.

        Args:
            study_id:
                ID of the study.
            trial_number:
                Number of the trial.

        Returns:
            ID of the trial.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``study_id`` and ``trial_number`` exists.
        F)ÚdeepcopyzNo trial with trial_number=z exists in study with study_id=ú.)Úget_all_trialsÚlenÚKeyErrorÚ	_trial_id)r   r   Útrial_numberÚtrialss       r   Ú'get_trial_id_from_study_id_trial_numberz3BaseStorage.get_trial_id_from_study_id_trial_number  sU   € ð  ×$Ñ$ X¸Ð$Ó>ˆÜˆv‹;˜,Ò&ÜÐ9¨L¨?Ð:ZÐQYÐP[Ð[\Ð]Ó^Ð^Ø�lÑ#×-Ñ-Ð-r   c                ó8   — | j                  |«      j                  S )aw  Read the trial number of a trial.

        .. note::

            The trial number is only unique within a study, and is sequential.

        Args:
            trial_id:
                ID of the trial.

        Returns:
            Number of the trial.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
        )Ú	get_trialÚnumber©r   r9   s     r   Úget_trial_number_from_idz$BaseStorage.get_trial_number_from_id-  s   € ð$ �~‰~˜hÓ'×.Ñ.Ð.r   c                óz   — | j                  |«      }|j                  |   j                  |j                  |   «      S )a’  Read the parameter of a trial.

        Args:
            trial_id:
                ID of the trial.
            param_name:
                Name of the parameter.

        Returns:
            Internal representation of the parameter.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
                If no such parameter exists.
        )rI   ÚdistributionsÚto_internal_reprÚparams)r   r9   r:   Útrials       r   Úget_trial_paramzBaseStorage.get_trial_paramA  s7   € ð" —‘˜xÓ(ˆØ×"Ñ" :Ñ.×?Ñ?ÀÇÁÈZÑ@XÓYÐYr   c                ó   — t         ‚)aÊ  Update the state and values of a trial.

        Set return values of an objective function to values argument.
        If values argument is not :obj:`None`, this method overwrites any existing trial values.

        Args:
            trial_id:
                ID of the trial.
            state:
                New state of the trial.
            values:
                Values of the objective function.

        Returns:
            :obj:`True` if the state is successfully updated.
            :obj:`False` if the state is kept the same.
            The latter happens when this method tries to update the state of
            :obj:`~optuna.trial.TrialState.RUNNING` trial to
            :obj:`~optuna.trial.TrialState.RUNNING`.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
            :exc:`~optuna.exceptions.UpdateFinishedTrialError`:
                If the trial is already finished.
        r   )r   r9   ÚstateÚvaluess       r   Úset_trial_state_valuesz"BaseStorage.set_trial_state_valuesU  s
   € ô< "Ð!r   c                ó   — t         ‚)aŒ  Report an intermediate value of an objective function.

        This method overwrites any existing intermediate value associated with the given step.

        Args:
            trial_id:
                ID of the trial.
            step:
                Step of the trial (e.g., the epoch when training a neural network).
            intermediate_value:
                Intermediate value corresponding to the step.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
            :exc:`~optuna.exceptions.UpdateFinishedTrialError`:
                If the trial is already finished.
        r   )r   r9   ÚstepÚintermediate_values       r   Úset_trial_intermediate_valuez(BaseStorage.set_trial_intermediate_valueu  s
   € ô, "Ð!r   c                ó   — t         ‚)a  Set a user-defined attribute to a trial.

        This method overwrites any existing attribute.

        Args:
            trial_id:
                ID of the trial.
            key:
                Attribute key.
            value:
                Attribute value. It should be JSON serializable.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
            :exc:`~optuna.exceptions.UpdateFinishedTrialError`:
                If the trial is already finished.
        r   ©r   r9   r    r!   s       r   Úset_trial_user_attrzBaseStorage.set_trial_user_attr�  r7   r   c                ó   — t         ‚)a  Set an optuna-internal attribute to a trial.

        This method overwrites any existing attribute.

        Args:
            trial_id:
                ID of the trial.
            key:
                Attribute key.
            value:
                Attribute value. It should be JSON serializable.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
            :exc:`~optuna.exceptions.UpdateFinishedTrialError`:
                If the trial is already finished.
        r   r\   s       r   Úset_trial_system_attrz!BaseStorage.set_trial_system_attr£  r7   r   c                ó   — t         ‚)a  Read a trial.

        Args:
            trial_id:
                ID of the trial.

        Returns:
            Trial with a matching trial ID.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
        r   rK   s     r   rI   zBaseStorage.get_trial»  r(   r   c                ó   — t         ‚)aI  Read all trials in a study.

        Args:
            study_id:
                ID of the study.
            deepcopy:
                Whether to copy the list of trials before returning.
                Set to :obj:`True` if you intend to update the list or elements of the list.
            states:
                Trial states to filter on. If :obj:`None`, include all states.

        Returns:
            List of trials in the study, sorted by ``trial_id``.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        r   )r   r   r?   Ústatess       r   rA   zBaseStorage.get_all_trialsÌ  s
   € ô2 "Ð!r   c                ób   — t        |t        «      r|f}t        | j                  |d|¬«      «      S )a…  Count the number of trials in a study.

        Args:
            study_id:
                ID of the study.
            state:
                Trial states to filter on. If :obj:`None`, include all states.

        Returns:
            Number of trials in the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
        F©r?   rb   )Ú
isinstancer   rB   rA   )r   r   rT   s      r   Úget_n_trialszBaseStorage.get_n_trialsç  s2   € ô( �eœZÔ(Ø�HˆEÜ�4×&Ñ& x¸%ÈÐ&ÓNÓOÐOr   c                ó>  — | j                  |dt        j                  g¬«      }t        |«      dk(  rt	        d«      ‚| j                  |«      }t        |«      dkD  rt        d«      ‚|d   }|t        j                  k(  rt        |d„ ¬«      }|S t        |d	„ ¬«      }|S )
aS  Return the trial with the best value in a study.

        This method is valid only during single-objective optimization.

        Args:
            study_id:
                ID of the study.

        Returns:
            The trial with the best objective value among all finished trials in the study.

        Raises:
            :exc:`KeyError`:
                If no study with the matching ``study_id`` exists.
            :exc:`RuntimeError`:
                If the study has more than one direction.
            :exc:`ValueError`:
                If no trials have been completed.
        Frd   r   zNo trials are completed yet.é   zBBest trial can be obtained only for single-objective optimization.c                ó6   — t        t        | j                  «      S ©N©r   Úfloatr!   ©Úts    r   ú<lambda>z,BaseStorage.get_best_trial.<locals>.<lambda>   ó   € ´t¼EÀ1Ç7Á7Ó7K€ r   )r    c                ó6   — t        t        | j                  «      S rj   rk   rm   s    r   ro   z,BaseStorage.get_best_trial.<locals>.<lambda>"  rp   r   )rA   r   ÚCOMPLETErB   Ú
ValueErrorr,   ÚRuntimeErrorr   ÚMAXIMIZEÚmaxÚmin)r   r   Ú
all_trialsr   Ú	directionÚ
best_trials         r   Úget_best_trialzBaseStorage.get_best_trialÿ  s«   € ð( ×(Ñ(¨¸EÌ:×K^ÑK^ÐJ_Ð(Ó`ˆ
äˆz‹?˜aÒÜÐ;Ó<Ð<à×.Ñ.¨xÓ8ˆ
Üˆz‹?˜QÒÜØTóð ð ˜q‘Mˆ	àœ×/Ñ/Ò/Ü˜ZÑ-KÔLˆJð Ðô ˜ZÑ-KÔLˆJàÐr   c                ó8   — | j                  |«      j                  S )a‰  Read the parameter dictionary of a trial.

        Args:
            trial_id:
                ID of the trial.

        Returns:
            Dictionary of a parameters. Keys are parameter names and values are external
            representations of the parameter values.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
        )rI   rP   rK   s     r   Úget_trial_paramszBaseStorage.get_trial_params&  s   € ð �~‰~˜hÓ'×.Ñ.Ð.r   c                ó8   — | j                  |«      j                  S )aD  Read the user-defined attributes of a trial.

        Args:
            trial_id:
                ID of the trial.

        Returns:
            Dictionary with the user-defined attributes of the trial.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
        )rI   Ú
user_attrsrK   s     r   Úget_trial_user_attrsz BaseStorage.get_trial_user_attrs7  s   € ð �~‰~˜hÓ'×2Ñ2Ð2r   c                ó8   — | j                  |«      j                  S )aJ  Read the optuna-internal attributes of a trial.

        Args:
            trial_id:
                ID of the trial.

        Returns:
            Dictionary with the optuna-internal attributes of the trial.

        Raises:
            :exc:`KeyError`:
                If no trial with the matching ``trial_id`` exists.
        )rI   Úsystem_attrsrK   s     r   Úget_trial_system_attrsz"BaseStorage.get_trial_system_attrsG  s   € ð �~‰~˜hÓ'×4Ñ4Ð4r   c                 ó   — y)z'Clean up all connections to a database.N© r2   s    r   Úremove_sessionzBaseStorage.remove_sessionW  s   € àr   c                óx   — |j                  «       r*| j                  |«      }t        d|j                  › d�«      ‚y)aj  Check whether a trial state is updatable.

        Args:
            trial_id:
                ID of the trial.
                Only used for an error message.
            trial_state:
                Trial state to check.

        Raises:
            :exc:`~optuna.exceptions.UpdateFinishedTrialError`:
                If the trial is already finished.
        zTrial#z- has already finished and can not be updated.N)Úis_finishedrI   r
   rJ   )r   r9   Útrial_staterQ   s       r   Úcheck_trial_is_updatablez$BaseStorage.check_trial_is_updatable[  sA   € ð ×"Ñ"Ô$Ø—N‘N 8Ó,ˆEÜ*Ø˜Ÿ™˜Ð&SÐTóð ð %r   rj   )r   zSequence[StudyDirection]r   z
str | NoneÚreturnÚint)r   rŒ   r‹   ÚNone)r   rŒ   r    Ústrr!   r   r‹   r�   )r   rŒ   r    rŽ   r!   r   r‹   r�   )r   rŽ   r‹   rŒ   )r   rŒ   r‹   rŽ   )r   rŒ   r‹   zlist[StudyDirection])r   rŒ   r‹   údict[str, Any])r‹   zlist[FrozenStudy])r   rŒ   r5   zFrozenTrial | Noner‹   rŒ   )
r9   rŒ   r:   rŽ   r;   rl   r<   r	   r‹   r�   )r   rŒ   rE   rŒ   r‹   rŒ   )r9   rŒ   r‹   rŒ   )r9   rŒ   r:   rŽ   r‹   rl   )r9   rŒ   rT   r   rU   zSequence[float] | Noner‹   Úbool)r9   rŒ   rX   rŒ   rY   rl   r‹   r�   )r9   rŒ   r    rŽ   r!   r   r‹   r�   )r9   rŒ   r    rŽ   r!   r   r‹   r�   )r9   rŒ   r‹   r   )TN)r   rŒ   r?   r�   rb   zContainer[TrialState] | Noner‹   zlist[FrozenTrial])r   rŒ   rT   z*tuple[TrialState, ...] | TrialState | Noner‹   rŒ   )r   rŒ   r‹   r   )r9   rŒ   r‹   r�   )r‹   r�   )r9   rŒ   r‰   r   r‹   r�   )"Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚabcÚabstractmethodr   r   r"   r%   r'   r*   r,   r.   r0   r3   r6   r=   rG   rL   rR   rV   rZ   r]   r_   rI   rA   rf   r{   r}   r€   rƒ   r†   rŠ   r…   r   r   r   r      sî  „ ñð: 	×ÑàMQð"Ø2ð"Ø@Jð"à	ò"ó ð"ð2 	×Ñò"ó ð"ð 	×Ñò"ó ð"ð& 	×Ñò"ó ð"ð* 	×Ñò"ó ð"ð  	×Ñò"ó ð"ð  	×Ñò"ó ð"ð  	×Ñò"ó ð"ð  	×Ñò"ó ð"ð  	×Ñò"ó ð"ð 	×Ñó"ó ð"ð* 	×Ñð"àð"ð ð"ð $ð	"ð
 'ð"ð 
ò"ó ð"ó6.ó*/ó(Zð( 	×ÑàQUð"Øð"Ø$.ð"Ø8Nð"à	ò"ó ð"ð> 	×Ñð"Øð"Ø#&ð"Ø<Að"à	ò"ó ð"ð. 	×Ñò"ó ð"ð* 	×Ñò"ó ð"ð. 	×Ñò"ó ð"ð  	×Ñð Ø/3ð	"àð"ð ð"ð -ð	"ð
 
ò"ó ð"ð6 RVðPØðPØ$NðPà	óPó0%óN/ó"3ó 5ó ôr   r   )Ú
__future__r   r•   Úcollections.abcr   r   Útypingr   r   Úoptuna._typingr   Úoptuna.distributionsr	   Úoptuna.exceptionsr
   Úoptuna.study._frozenr   Úoptuna.study._study_directionr   Úoptuna.trialr   r   ÚDEFAULT_STUDY_NAME_PREFIXÚABCr   r…   r   r   ú<module>r¢      sF   ðÝ "ã 
Ý %Ý $Ý Ý å +Ý 1Ý 6Ý ,Ý 8Ý $Ý #ð 'Ð ôX	�#—'‘'õ X	r   