
    -jiK                        S SK Jr  S SKrS SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJr  Sr " S S\R.                  5      rg)    )annotationsN)	Container)Sequence)Any)cast)JSONSerializable)BaseDistribution)UpdateFinishedTrialError)FrozenStudy)StudyDirection)FrozenTrial)
TrialStatezno-name-c                     \ rS rSrSr\R                   S!     S"S jj5       r\R                  S#S j5       r\R                  S$S j5       r	\R                  S%S j5       r
\R                  S&S j5       r\R                  S'S	 j5       r\R                  S(S
 j5       r\R                  S)S j5       r\R                  S)S j5       r\R                  S*S j5       r\R                  S!S+S jj5       r\R                            S,S j5       rS-S jrS.S jrS/S jr\R                   S!       S0S jj5       r\R                          S1S j5       r\R                  S2S j5       r\R                  S3S j5       r\R                  S4S j5       r\R                    S5       S6S jj5       r S!     S7S jjrS8S jrS9S jrS9S jrS9S jr S:S jr!S;S jr"S r#g)<BaseStorage   ar  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                    [         e)aA  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      O/home/james-whalen/.local/lib/python3.13/site-packages/optuna/storages/_base.pycreate_new_studyBaseStorage.create_new_study3   s
    0 "!    c                    [         e)zDelete 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_studyBaseStorage.delete_studyM   s
     "!r   c                    [         e)aI  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_attrBaseStorage.set_study_user_attr[   
    $ "!r   c                    [         e)aM  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_attr!BaseStorage.set_study_system_attro   r'   r   c                    [         e)zRead 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_name"BaseStorage.get_study_id_from_name   
     "!r   c                    [         e)zRead 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_id"BaseStorage.get_study_name_from_id   r.   r   c                    [         e)zRead 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_directions BaseStorage.get_study_directions   r.   r   c                    [         e)zRead 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_attrs BaseStorage.get_study_user_attrs   r.   r   c                    [         e)a  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_attrs"BaseStorage.get_study_system_attrs   r.   r   c                    [         e)zRead 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_studiesBaseStorage.get_all_studies   s
     "!r   c                    [         e)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_trialBaseStorage.create_new_trial   
    ( "!r   c                    [         e)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_paramBaseStorage.set_trial_param   s
    4 "!r   c                    U R                  USS9n[        U5      U::  a  [        SR                  X!5      5      eX2   R                  $ )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 {} exists in study with study_id {}.)get_all_trialslenKeyErrorformat	_trial_id)r   r   trial_numbertrialss       r   'get_trial_id_from_study_id_trial_number3BaseStorage.get_trial_id_from_study_id_trial_number  sT      $$X$>v;,&QXX  
 #---r   c                8    U R                  U5      R                  $ )a  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   rE   s     r   get_trial_number_from_id$BaseStorage.get_trial_number_from_id1  s    $ ~~h'...r   c                z    U R                  U5      nUR                  U   R                  UR                  U   5      $ )a2  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.
)rW   distributionsto_internal_reprparams)r   rE   rF   trials       r   get_trial_paramBaseStorage.get_trial_paramE  s7    " x("":.??Z@XYYr   c                    [         e)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   rE   statevaluess       r   set_trial_state_values"BaseStorage.set_trial_state_valuesY  s
    < "!r   c                    [         e)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   rE   stepintermediate_values       r   set_trial_intermediate_value(BaseStorage.set_trial_intermediate_valuey  s
    , "!r   c                    [         e)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   rE   r#   r$   s       r   set_trial_user_attrBaseStorage.set_trial_user_attr  rC   r   c                    [         e)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   rn   s       r   set_trial_system_attr!BaseStorage.set_trial_system_attr  rC   r   c                    [         e)zRead 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   rY   s     r   rW   BaseStorage.get_trial  r.   r   c                    [         e)a  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   rL   statess       r   rM   BaseStorage.get_all_trials  s
    2 "!r   c                f    [        U[        5      (       a  U4n[        U R                  USUS95      $ )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rL   rw   )
isinstancer   rN   rM   )r   r   rd   s      r   get_n_trialsBaseStorage.get_n_trials  s5    ( eZ((HE4&&x%&NOOr   c                2   U R                  US[        R                  /S9n[        U5      S:X  a  [	        S5      eU R                  U5      n[        U5      S:  a  [        S5      eUS   nU[        R                  :X  a  [        US S9nU$ [        US	 S9nU$ )
a  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.
Frz   r   zNo trials are completed yet.   zBBest trial can be obtained only for single-objective optimization.c                6    [        [        U R                  5      $ Nr   floatr$   ts    r   <lambda>,BaseStorage.get_best_trial.<locals>.<lambda>$      tE1777Kr   )r#   c                6    [        [        U R                  5      $ r   r   r   s    r   r   r   &  r   r   )rM   r   COMPLETErN   
ValueErrorr3   RuntimeErrorr   MAXIMIZEmaxmin)r   r   
all_trialsr   	direction
best_trials         r   get_best_trialBaseStorage.get_best_trial  s    ( ((E:K^K^J_(`
z?a;<<..x8
z?QT  qM	///Z-KLJ  Z-KLJr   c                8    U R                  U5      R                  $ )a9  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.
)rW   r_   rY   s     r   get_trial_paramsBaseStorage.get_trial_params*  s     ~~h'...r   c                8    U R                  U5      R                  $ )zRead 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.
)rW   
user_attrsrY   s     r   get_trial_user_attrs BaseStorage.get_trial_user_attrs;  s     ~~h'222r   c                8    U R                  U5      R                  $ )a  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.
)rW   system_attrsrY   s     r   get_trial_system_attrs"BaseStorage.get_trial_system_attrsK  s     ~~h'444r   c                    g)z'Clean up all connections to a database.N r<   s    r   remove_sessionBaseStorage.remove_session[  s    r   c                    UR                  5       (       a5  U R                  U5      n[        SR                  UR                  5      5      eg)a  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.
z5Trial#{} has already finished and can not be updated.N)is_finishedrW   r
   rP   rX   )r   rE   trial_stater`   s       r   check_trial_is_updatable$BaseStorage.check_trial_is_updatable_  sE     ""$$NN8,E*GNNu||\  %r   r   r   )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   r@   zFrozenTrial | Noner   r   )
rE   r   rF   r   rG   r   rH   r	   r   r   )r   r   rR   r   r   r   )rE   r   r   r   )rE   r   rF   r   r   r   )rE   r   rd   r   re   zSequence[float] | Noner   bool)rE   r   ri   r   rj   r   r   r   )rE   r   r#   r   r$   r   r   r   )rE   r   r#   r   r$   r   r   r   )rE   r   r   r   )TN)r   r   rL   r   rw   zContainer[TrialState] | Noner   zlist[FrozenTrial])r   r   rd   z*tuple[TrialState, ...] | TrialState | Noner   r   )r   r   r   r   )rE   r   r   r   )r   r   )rE   r   r   r   r   r   )$__name__
__module____qualname____firstlineno____doc__abcabstractmethodr   r   r%   r)   r,   r0   r3   r6   r9   r=   rA   rI   rT   rZ   ra   rf   rk   ro   rr   rW   rM   r|   r   r   r   r   r   r   __static_attributes__r   r   r   r   r      s   : 	MQ"2"@J"	" "2 	" " 	" "& 	" "* 	" "  	" "  	" "  	" "  	" "  	" " 	" "* 	"" " $	"
 '" 
" "6.2/(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 $ # ' \	#'' \	r   