L iO ddlmZddlmZddlmZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddl Z dd l mZdd l mZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!ejDe#Z$dZ%GddeZ&e!jNdddddddd ddZ(y)) annotations)Mapping)SequenceN)Any)cast)Dict)overload) distributions)logging)convert_positional_args)deprecated_func)JSONSerializable)-_convert_old_distribution_to_new_distribution)BaseDistribution)CategoricalChoiceType)CategoricalDistribution)FloatDistribution)IntDistribution)_SUGGEST_INT_POSITIONAL_ARGS) BaseTrial) TrialStatez Use suggest_float{args} instead.cNeZdZdZdd ddZd?d Z dd d  d@d Z e d de jddAdZe d de jddAdZe d de jddBdZeedddd d  dCdZedDdZedEdZedFdZedGdZedHd Ze dId!Z dId"ZdJd#ZdKd$ZdLd%Ze d&ddLd'ZdMd(ZdNd)Zed>d*Zej<dOd+ZedPd,Zej<dQd-ZdRd.Z dSd/Z!ee e!Z"edTd0Z#e#j<dUd1Z#edVd2Z$e$j<dWd3Z$edXd4Z%e%j<dYd5Z%edVd6Z&e&j<dZd7Z&edVd8Z'e'j<d[d9Z'ed\d:Z(ed]d;Z)y)^ FrozenTrialaStatus and results of a :class:`~optuna.trial.Trial`. An object of this class has the same methods as :class:`~optuna.trial.Trial`, but is not associated with, nor has any references to a :class:`~optuna.study.Study`. It is therefore not possible to make persistent changes to a storage from this object by itself, for instance by using :func:`~optuna.trial.FrozenTrial.set_user_attr`. It will suggest the parameter values stored in :attr:`params` and will not sample values from any distributions. It can be passed to objective functions (see :func:`~optuna.study.Study.optimize`) and is useful for deploying optimization results. Example: Re-evaluate an objective function with parameter values optimized study. .. testcode:: import optuna def objective(trial): x = trial.suggest_float("x", -1, 1) return x**2 study = optuna.create_study() study.optimize(objective, n_trials=3) assert objective(study.best_trial) == study.best_value .. note:: Instances are mutable, despite the name. For instance, :func:`~optuna.trial.FrozenTrial.set_user_attr` will update user attributes of objects in-place. Example: Overwritten attributes. .. testcode:: import copy import datetime import optuna def objective(trial): x = trial.suggest_float("x", -1, 1) # this user attribute always differs trial.set_user_attr("evaluation time", datetime.datetime.now()) return x**2 study = optuna.create_study() study.optimize(objective, n_trials=3) best_trial = study.best_trial best_trial_copy = copy.deepcopy(best_trial) # re-evaluate objective(best_trial) # the user attribute is overwritten by re-evaluation assert best_trial.user_attrs != best_trial_copy.user_attrs .. note:: Please refer to :class:`~optuna.trial.Trial` for details of methods and properties. Attributes: number: Unique and consecutive number of :class:`~optuna.trial.Trial` for each :class:`~optuna.study.Study`. Note that this field uses zero-based numbering. state: :class:`TrialState` of the :class:`~optuna.trial.Trial`. value: Objective value of the :class:`~optuna.trial.Trial`. ``value`` and ``values`` must not be specified at the same time. values: Sequence of objective values of the :class:`~optuna.trial.Trial`. The length is greater than 1 if the problem is multi-objective optimization. ``value`` and ``values`` must not be specified at the same time. datetime_start: Datetime where the :class:`~optuna.trial.Trial` started. datetime_complete: Datetime where the :class:`~optuna.trial.Trial` finished. params: Dictionary that contains suggested parameters. distributions: Dictionary that contains the distributions of :attr:`params`. user_attrs: Dictionary that contains the attributes of the :class:`~optuna.trial.Trial` set with :func:`optuna.trial.Trial.set_user_attr`. system_attrs: Dictionary that contains the attributes of the :class:`~optuna.trial.Trial` set with :func:`optuna.trial.Trial.set_system_attr`. intermediate_values: Intermediate objective values set with :func:`optuna.trial.Trial.report`. N)valuesc ||_||_d|_| | td| |g|_n| t | |_||_||_||_||_| |_ | |_ ||_ | |_ y)Nz)Specify only one of `value` and `values`.) _numberstate_values ValueErrorlist_datetime_startdatetime_complete_params _user_attrs _system_attrsintermediate_values_distributions _trial_id) selfnumberrvaluedatetime_startr"paramsr user_attrs system_attrsr&trial_idrs Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/trial/_frozen.py__init__zFrozenTrial.__init__s   +/  !3HI I  !7DL  z'FrozenTrial.__hash__..sJ5'$.Js)hashtupler8r)s`r1__hash__zFrozenTrial.__hash__sEJDMMJJKKr3cdjjjdjfdjDdzS)Nz{cls}({kwargs})z, c 3K|]@}dj|jds|n|ddtt|Byw)z{field}={value}_N)rFr+)format startswithreprrCrDs r1rGz'FrozenTrial.__repr__..sR  "((','7'7'<%%)wtU34)sAA z , value=None)clskwargs)rP __class____name__joinr8rJs`r1__repr__zFrozenTrial.__repr__sQ ''''99 "]] (  r3F)steplogc @|j|t||||SN)rZrY)_suggestrr)namelowhighrYrZs r1 suggest_floatzFrozenTrial.suggest_floats!}}T#4S$Cd#STTr3z3.0.0z6.0.0)args)textc(|j|||Sr5rbr)r_r`ras r1suggest_uniformzFrozenTrial.suggest_uniforms!!$T22r3z(..., log=True)c,|j|||dS)NT)rZrgrhs r1suggest_loguniformzFrozenTrial.suggest_loguniforms!!$Tt!<|j t dtd|jDr t dt|jjt|jjk7r]t d jt|jjt|jj|jj!D]R\}}|j|}|j#|}|j%|r8t d j|||y) NzK`datetime_start` is supposed to be set when the trial state is not waiting.z?`datetime_complete` is supposed to be set for a finished trial.zC`datetime_complete` is supposed to be None for an unfinished trial.z2values should be None for a failed trial, but got .z*values should be set for a complete trial.c3FK|]}tj|ywr5)mathisnan)rExs r1rGz(FrozenTrial._validate..Hs9qTZZ]9s!zvalues should not contain NaN.z0Inconsistent parameters {} and distributions {}.zFThe value {} of parameter '{}' isn't contained in the distribution {}.)rrWAITINGr,r is_finishedr"FAILrCOMPLETEanysetr-keysr rPitemsto_internal_repr _contains)r) param_name param_value distributionparam_value_in_internal_reprs r1 _validatezFrozenTrial._validate4s ::++ +0C0C0K]  :: ! ! #%%- !bcc%%1 Y :: (T\\-EQRVR^R^Q__`ab b ::,, ,||# !MNN9DLL99 !ABB t{{! "c$*<*<*A*A*C&D DBII ((*+S1C1C1H1H1J-K  (,{{'8'8':  #J --j9L+7+H+H+U ())*FG  &j,G  r3c||jvrtdj||j|}|j|}|j |s&t j dj|||||jvr#tj|j||||j|<|S)NzjThe value of the parameter '{}' is not found. Please set it at the construction of the FrozenTrial object.zNThe value {} of the parameter '{}' is out of the range of the distribution {}.) r#rrPrrwarningswarnr'r check_distribution_compatibility)r)r_rr+rs r1r]zFrozenTrial._suggest\s t|| #>>DfTl   T"'3'D'DU'K$%%&BC MM44:F5$ 4U  4&& &  : :4;N;Nt;TVb c$0D! r3c|jSr5rrJs r1r*zFrozenTrial.numberr ||r3c||_yr5rr)r+s r1r*zFrozenTrial.numbervs  r3c|j2t|jdkDr td|jdSy)NrODThis attribute is not available during multi-objective optimization.rrlen RuntimeErrorrJs r1r+zFrozenTrial.valuezs@ << #4<< 1$"Z<<? "r3c|j#t|jdkDr td| |g|_yd|_y)NrOrrr)vs r1r+zFrozenTrial.valuesD << #4<< 1$"Z =3DLDLr3c|jSr5)rrJs r1 _get_valueszFrozenTrial._get_valuess ||r3c8|t||_yd|_yr5)r rrs r1 _set_valueszFrozenTrial._set_valuess =7DLDLr3c|jSr5r!rJs r1r,zFrozenTrial.datetime_starts###r3c||_yr5rrs r1r,zFrozenTrial.datetime_starts $r3c|jSr5r#rJs r1r-zFrozenTrial.paramsrr3c||_yr5r)r)r-s r1r-zFrozenTrial.paramss  r3c|jSr5r'rJs r1r zFrozenTrial.distributionss"""r3c||_yr5rrs r1r zFrozenTrial.distributionss #r3c|jSr5rrJs r1r.zFrozenTrial.user_attrssr3c||_yr5rrs r1r.zFrozenTrial.user_attrss  r3c|jSr5rrJs r1r/zFrozenTrial.system_attrss!!!r3cHttttf||_yr5)rrstrrr%rs r1r/zFrozenTrial.system_attrss!$sCx.%8r3czt|jdk(ryt|jjS)zReturn the maximum step of :attr:`intermediate_values` in the trial. Returns: The maximum step of intermediates. rN)rr&maxrrJs r1 last_stepzFrozenTrial.last_steps4 t'' (A -t//4467 7r3cf|jr%|jr|j|jz Sy)ziReturn the elapsed time taken to complete the trial. Returns: The duration. N)r,r"rJs r1durationzFrozenTrial.durations/   4#9#9))D,?,?? ?r3)r*rsrrr+ float | Noner,datetime.datetime | Noner"rr-dict[str, Any]r dict[str, BaseDistribution]r.rr/rr&zdict[int, float]r0rsrSequence[float] | NonereturnNone)r:rrbool)rrs)rr) r_rr`floatrarrYrrZrrr)r_rr`rrarrr) r_rr`rrarrmrrr) r_rr`rsrarsrYrsrZrrrs)r_rrxzSequence[None]rr)r_rrxzSequence[bool]rr)r_rrxz Sequence[int]rrs)r_rrxzSequence[float]rr)r_rrxz Sequence[str]rr)r_rrxzSequence[CategoricalChoiceType]rr)r+rrYrsrr)rr)rrr+rrr)rr)r_rrrrr)r+rsrr)rr)rrrr)rzlist[float] | None)rrrr)rr)r+rrr)rr)r-rrr)rr)r+rrr)r+rrr)r+zMapping[str, JSONSerializable]rr)rz int | None)rzdatetime.timedelta | None)*rV __module__ __qualname____doc__r2r;r>r@rKrXrbr _suggest_deprecated_msgrPrirkrnr rrtr ryrrrrrr]propertyr*setterr+rrrr,r-r r.r/rrrvr3r1rr!s)ir*. " " " " 1 " 4 " "3 "# "% ". " "' "  "D/ * + L  &" U U U U  U U  UWg,C,J,JPR,J,ST3U3Wg,C,J,JPa,J,bc=d=Wg,C,J,JPa,J,bc;d;&B" >?EXX!X),X7:XEIX X  X RR RR PP TT PP $$"A$ $$MM"AM M  * &Wg&('(&P, ]] \\     k; /F $$%% ]]##$$  !!""99 8 8  r3r)rr+rr-r r.r/r&c|xsi}|xsi}|jD cic]\}} |t| }}} |xsi}|xsi}|xsi}|tjk(rd} ntjj } |j r| } nd} tdd|||| | ||||| } | j| Scc} }w)aCreate a new :class:`~optuna.trial.FrozenTrial`. Example: .. testcode:: import optuna from optuna.distributions import CategoricalDistribution from optuna.distributions import FloatDistribution trial = optuna.trial.create_trial( params={"x": 1.0, "y": 0}, distributions={ "x": FloatDistribution(0, 10), "y": CategoricalDistribution([-1, 0, 1]), }, value=5.0, ) assert isinstance(trial, optuna.trial.FrozenTrial) assert trial.value == 5.0 assert trial.params == {"x": 1.0, "y": 0} .. seealso:: See :func:`~optuna.study.Study.add_trial` for how this function can be used to create a study from existing trials. .. note:: Please note that this is a low-level API. In general, trials that are passed to objective functions are created inside :func:`~optuna.study.Study.optimize`. .. note:: When ``state`` is :class:`TrialState.COMPLETE`, the following parameters are required: * ``params`` * ``distributions`` * ``value`` or ``values`` Args: state: Trial state. value: Trial objective value. Must be specified if ``state`` is :class:`TrialState.COMPLETE`. ``value`` and ``values`` must not be specified at the same time. values: Sequence of the trial objective values. The length is greater than 1 if the problem is multi-objective optimization. Must be specified if ``state`` is :class:`TrialState.COMPLETE`. ``value`` and ``values`` must not be specified at the same time. params: Dictionary with suggested parameters of the trial. distributions: Dictionary with parameter distributions of the trial. user_attrs: Dictionary with user attributes. system_attrs: Dictionary with system attributes. Should not have to be used for most users. intermediate_values: Dictionary with intermediate objective values of the trial. Returns: Created trial. N) r*r0rr+rr,r"r-r r.r/r&) rrrrdatetimenowrrr) rr+rr-r r.r/r&rdistr,r"trials r1 create_trialrs\\rF!'RM-,,. C :4 @@M!rJ%2L-3 """!**..0 6D  %+#!/ E OO LGsB>)rrr+rrrr-dict[str, Any] | Noner z"dict[str, BaseDistribution] | Noner.rr/rr&zdict[int, float] | Nonerr)) __future__rcollections.abcrrrrtypingrrrr roptunar r optuna._convert_positional_argsr optuna._deprecatedr optuna._typingroptuna.distributionsrrrrrroptuna.trial._baserroptuna.trial._stater get_loggerrV_loggerrrrrrvr3r1rs"#$  C.+N16820;(* '  X &<~)~F#++%)$(8<(,*.37s s s # s " s 6 s&s(s1ssr3