L iKddlmZddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z dd l mZdd lmZdd lmZdd lmZdd lmZdZGddej.Zy)) annotationsN) Container)Sequence)Any)cast)JSONSerializable)BaseDistribution)UpdateFinishedTrialError) FrozenStudy)StudyDirection) FrozenTrial) TrialStatezno-name-cteZdZdZej d d dZej d!dZej d"dZej d#dZ ej d$dZ ej d%dZ ej d&d Z ej d'd Z ej d'd Zej d(d Zej dd)d Zej d*dZd+dZd,dZd-dZej d d.dZej d/dZej d0dZej d1dZej d2dZej d3 d4dZ d d5dZd6dZd7dZd7dZd7dZd8dZ d9dZ!y): BaseStorageaBase 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. Nct)aCreate 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 [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/storages/_base.pycreate_new_studyzBaseStorage.create_new_study3s 0"!ct)zDelete a study. Args: study_id: ID of the study. Raises: :exc:`KeyError`: If no study with the matching ``study_id`` exists. rrstudy_ids r delete_studyzBaseStorage.delete_studyMs "!rct)aRegister 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. rrrkeyvalues rset_study_user_attrzBaseStorage.set_study_user_attr[ $"!rct)aRegister 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. rrs rset_study_system_attrz!BaseStorage.set_study_system_attror#rct)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)rrs rget_study_id_from_namez"BaseStorage.get_study_id_from_name "!rct)aRead 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. rrs rget_study_name_from_idz"BaseStorage.get_study_name_from_idr(rct)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. rrs rget_study_directionsz BaseStorage.get_study_directionsr(rct)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. rrs rget_study_user_attrsz BaseStorage.get_study_user_attrsr(rct)aJRead 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. rrs rget_study_system_attrsz"BaseStorage.get_study_system_attrsr(rct)zRead a list of :class:`~optuna.study.FrozenStudy` objects. Returns: A list of :class:`~optuna.study.FrozenStudy` objects, sorted by ``study_id``. rrs rget_all_studieszBaseStorage.get_all_studiess "!rct)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)rrtemplate_trials rcreate_new_trialzBaseStorage.create_new_trial ("!rct)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)rtrial_id param_nameparam_value_internal distributions rset_trial_paramzBaseStorage.set_trial_params 4"!rc|j|d}t||krtdj||||jS)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_trialslenKeyErrorformat _trial_id)rr trial_numbertrialss r'get_trial_id_from_study_id_trial_numberz3BaseStorage.get_trial_id_from_study_id_trial_numbersX $$X$> v;, &QXX (  l#---rc8|j|jS)awRead 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_trialnumberrr9s rget_trial_number_from_idz$BaseStorage.get_trial_number_from_id1s$~~h'...rcz|j|}|j|j|j|S)aRead 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 distributionsto_internal_reprparams)rr9r:trials rget_trial_paramzBaseStorage.get_trial_paramEs7"x("":.?? Z@XYYrct)aUpdate 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)rr9statevaluess rset_trial_state_valuesz"BaseStorage.set_trial_state_valuesYs <"!rct)aReport 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)rr9stepintermediate_values rset_trial_intermediate_valuez(BaseStorage.set_trial_intermediate_valueys ,"!rct)aSet 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. rrr9r r!s rset_trial_user_attrzBaseStorage.set_trial_user_attrr7rct)aSet 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. rr\s rset_trial_system_attrz!BaseStorage.set_trial_system_attrr7rct)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. rrKs rrIzBaseStorage.get_trialr(rct)aIRead 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)rrr?statess rr@zBaseStorage.get_all_trialss 2"!rcbt|tr|f}t|j|d|S)aCount 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. Fr?rb) isinstancerrAr@)rrrTs r get_n_trialszBaseStorage.get_n_trialss2( eZ (HE4&&x%&NOOrc>|j|dtjg}t|dk(r t d|j |}t|dkDr t d|d}|tjk(rt|d}|St|d }|S) aSReturn 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. FrdrzNo trials are completed yet.zBBest trial can be obtained only for single-objective optimization.c6tt|jSNrfloatr!ts rz,BaseStorage.get_best_trial..$tE1777Kr)r c6tt|jSrjrkrms rroz,BaseStorage.get_best_trial..&rpr) r@rCOMPLETErA ValueErrorr, RuntimeErrorr MAXIMIZEmaxmin)rr all_trialsr direction best_trials rget_best_trialzBaseStorage.get_best_trials(((E:K^K^J_(` z?a ;< <..x8 z?Q T qM // /Z-KLJZ-KLJrc8|j|jS)aRead 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. )rIrPrKs rget_trial_paramszBaseStorage.get_trial_params*s~~h'...rc8|j|jS)aDRead 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_attrsrKs rget_trial_user_attrsz BaseStorage.get_trial_user_attrs;s~~h'222rc8|j|jS)aJRead 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_attrsrKs rget_trial_system_attrsz"BaseStorage.get_trial_system_attrsKs~~h'444rcy)z'Clean up all connections to a database.Nr2s rremove_sessionzBaseStorage.remove_session[s rc|jr5|j|}tdj|jy)ajCheck 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_finishedrIr rCrJ)rr9 trial_staterQs rcheck_trial_is_updatablez$BaseStorage.check_trial_is_updatable_sB  " " $NN8,E*GNNu||\  %rrj)rzSequence[StudyDirection]rz str | Nonereturnint)rrrNone)rrr strr!rrr)rrr rr!rrr)rrrr)rrrr)rrrzlist[StudyDirection])rrrdict[str, Any])rzlist[FrozenStudy])rrr5zFrozenTrial | Nonerr) r9rr:rr;rlr<r rr)rrrErrr)r9rrr)r9rr:rrrl)r9rrTrrUzSequence[float] | Nonerbool)r9rrXrrYrlrr)r9rr rr!rrr)r9rr rr!rrr)r9rrr )TN)rrr?rrbzContainer[TrialState] | Nonerzlist[FrozenTrial])rrrTz*tuple[TrialState, ...] | TrialState | Nonerr)rrrr )r9rrr)rr)r9rrrrr)"__name__ __module__ __qualname____doc__abcabstractmethodrrr"r%r'r*r,r.r0r3r6r=rGrLrRrVrZr]r_rIr@rfr{r}rrrrrrrrrs: MQ"2"@J" ""2  " " ""& ""* ""  ""  ""  ""  ""  "" ""* """$ " ' "  ""6.2/(Z( QU""$."8N" ""> ""#&"rsF" %$+16,8$#'\ #''\ r