
    &`iS                        d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZmZ d dlmZ erd dlmZ d dlmZ  ej        e          Ze G d	 d
                      Ze G d de                      ZdS )    N)TYPE_CHECKINGAnyDictListOptionalUnion)tag_searcher)+_set_search_properties_backwards_compatible)DeveloperAPI	PublicAPI)log_onceExperimentAnalysisTrialc                      e Zd ZdZdZdZ	 	 d*dee         dee         fdZdee         dee         de	d	e
fd
Zdede	d	dfdZ	 d+dedee	         de
d	dfdZded	ee	         fdZ	 	 	 d,de	dede
de
deee                  f
dZdeded         df         defdZdefdZdefdZded	e
fdZd	e	fd Zd!e	fd"Zd-d$ed%efd&Zd$efd'Zed	efd(            Zed	efd)            ZdS ).Searchera  Abstract class for wrapping suggesting algorithms.

    Custom algorithms can extend this class easily by overriding the
    `suggest` method provide generated parameters for the trials.

    Any subclass that implements ``__init__`` must also call the
    constructor of this class: ``super(Subclass, self).__init__(...)``.

    To track suggestions and their corresponding evaluations, the method
    `suggest` will be passed a trial_id, which will be used in
    subsequent notifications.

    Not all implementations support multi objectives.

    Note to Tune developers: If a new searcher is added, please update
    `air/_internal/usage.py`.

    Args:
        metric: The training result objective value attribute. If
            list then list of training result objective value attributes
        mode: If string One of {min, max}. If list then
            list of max and min, determines whether objective is minimizing
            or maximizing the metric attribute. Must match type of metric.

    .. code-block:: python

        class ExampleSearch(Searcher):
            def __init__(self, metric="mean_loss", mode="min", **kwargs):
                super(ExampleSearch, self).__init__(
                    metric=metric, mode=mode, **kwargs)
                self.optimizer = Optimizer()
                self.configurations = {}

            def suggest(self, trial_id):
                configuration = self.optimizer.query()
                self.configurations[trial_id] = configuration

            def on_trial_complete(self, trial_id, result, **kwargs):
                configuration = self.configurations[trial_id]
                if result and self.metric in result:
                    self.optimizer.update(configuration, result[self.metric])

        tuner = tune.Tuner(
            trainable_function,
            tune_config=tune.TuneConfig(
                search_alg=ExampleSearch()
            )
        )
        tuner.fit()


    FINISHEDzsearcher-state-{}.pklNmetricmodec                    t          |            || _        || _        |r|sd S t          |t	          |                    s
J d            t          |t
                    r|dv s
J d            d S t          |t                    rOt          |          t          |          k    s
J d            t          d |D                       s
J d            d S t          d          )Nz(metric and mode must be of the same type)minmaxz*if `mode` is a str must be 'min' or 'max'!z'Metric and mode must be the same lengthc              3      K   | ]}|d v V  	dS ))r   r   obsN ).0mods     l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/searcher.py	<genexpr>z$Searcher.__init__.<locals>.<genexpr>b   s9        14,,         z,All of mode must be 'min' or 'max' or 'obs'!z$Mode most either be a list or string)
r	   _metric_mode
isinstancetypestrlistlenall
ValueError)selfr   r   s      r   __init__zSearcher.__init__N   s6   
 	T
 	6 	FDJJ
 
 	6 	65	6 	6 
 dC   	E>)))+W)))))d## 	Et99F+++-V+++  8<     > >=> >  > > CDDDr!   configreturnc                     dS )a  Pass search properties to searcher.

        This method acts as an alternative to instantiating search algorithms
        with their own specific search spaces. Instead they can accept a
        Tune config through this method. A searcher should return ``True``
        if setting the config was successful, or ``False`` if it was
        unsuccessful, e.g. when the search space has already been set.

        Args:
            metric: Metric to optimize
            mode: One of ["min", "max"]. Direction to optimize.
            config: Tune config dict.
            **spec: Any kwargs for forward compatiblity.
                Info like Experiment.PUBLIC_KEYS is provided through here.
        Fr   r+   r   r   r-   specs        r   set_search_propertieszSearcher.set_search_propertiesh   s	    $ ur!   trial_idresultc                     dS )a  Optional notification for result during training.

        Note that by default, the result dict may include NaNs or
        may not include the optimization metric. It is up to the
        subclass implementation to preprocess the result to
        avoid breaking the optimization process.

        Args:
            trial_id: A unique string ID for the trial.
            result: Dictionary of metrics for current training progress.
                Note that the result dict may include NaNs or
                may not include the optimization metric. It is up to the
                subclass implementation to preprocess the result to
                avoid breaking the optimization process.
        Nr   r+   r3   r4   s      r   on_trial_resultzSearcher.on_trial_result|   s	      	r!   Ferrorc                     t           )a  Notification for the completion of trial.

        Typically, this method is used for notifying the underlying
        optimizer of the result.

        Args:
            trial_id: A unique string ID for the trial.
            result: Dictionary of metrics for current training progress.
                Note that the result dict may include NaNs or
                may not include the optimization metric. It is up to the
                subclass implementation to preprocess the result to
                avoid breaking the optimization process. Upon errors, this
                may also be None.
            error: True if the training process raised an error.

        NotImplementedErrorr+   r3   r4   r8   s       r   on_trial_completezSearcher.on_trial_complete   s
    & "!r!   c                     t           )a  Queries the algorithm to retrieve the next set of parameters.

        Arguments:
            trial_id: Trial ID used for subsequent notifications.

        Returns:
            dict | FINISHED | None: Configuration for a trial, if possible.
                If FINISHED is returned, Tune will be notified that
                no more suggestions/configurations will be provided.
                If None is returned, Tune will skip the querying of the
                searcher for this step.

        r:   )r+   r3   s     r   suggestzSearcher.suggest   s
     "!r!   
parametersvalueprunedintermediate_valuesc                     t           )a  Pass results from a point that has been evaluated separately.

        This method allows for information from outside the
        suggest - on_trial_complete loop to be passed to the search
        algorithm.
        This functionality depends on the underlying search algorithm
        and may not be always available.

        Args:
            parameters: Parameters used for the trial.
            value: Metric value obtained in the trial.
            error: True if the training process raised an error.
            pruned: True if trial was pruned.
            intermediate_values: List of metric values for
                intermediate iterations of the result. None if not
                applicable.

        r:   r+   r@   rA   r8   rB   rC   s         r   add_evaluated_pointzSearcher.add_evaluated_point   s
    4 "!r!   trials_or_analysisr   r   c                   	
 | j         t          j         k    rt          ddlm} ddlm	 ddlm t          |t          t          f          r|}n>t          |	          r|g}n*t          ||          r|j        }nt          d|           d
d	dt          t          t          f         f	
fd	}|D ]} ||          }|r | j         di | 
st!          j        d
           dS dS )a5  Pass results from trials that have been evaluated separately.

        This method allows for information from outside the
        suggest - on_trial_complete loop to be passed to the search
        algorithm.
        This functionality depends on the underlying search algorithm
        and may not be always available (same as ``add_evaluated_point``.)

        Args:
            trials_or_analysis: Trials to pass results form to the searcher.
            metric: Metric name reported by trials used for
                determining the objective value.

        r   r   r   )DONEzRExpected input to be a `Trial`, a list of `Trial`s, or `ExperimentAnalysis`, got: Ftrialr.   c                 p   | j         j        k    o| j                            d           }| j         j        k    o| j                            d          }s| j        v o|j        r| j        vrd S t	          | j        | j                            d           | j         j        k    |d           S )NF)r@   rA   r8   rB   rC   )status
TERMINATEDlast_resultgetdictr-   ERROR)rJ   has_trial_been_prunedhas_trial_finishedrI   r   any_trial_had_metricr   s      r   trial_to_pointsz6Searcher.add_evaluated_trials.<locals>.trial_to_points   s      00 ;)--dE::: "
  00WU5F5J5J4QV5W5W  ( e//F4F %  F%2C$C$Ct <'++FD99lek1,$(   r!   zaNo completed trial returned the specified metric. Make sure the name you have passed is correct. Nr   )rF   r   r;   ray.tune.analysisr   ray.tune.experimentr   ray.tune.resultrI   r$   r'   tupletrialsr   r&   r   warningswarn)r+   rG   r   r   rZ   rU   rJ   kwargsrI   r   rT   s     `     @@@r   add_evaluated_trialszSearcher.add_evaluated_trials   s   & #x'CCC%% 	988888------(((((((4-88 
	'FF*E22 	()FF*,>?? 	'.FF%C.@C C  
  %	5 	T#s(^ 	 	 	 	 	 	 	 	 	.  	3 	3E$_U++F 3((226222# 	MB    	 	r!   checkpoint_pathc                     t           )aw  Save state to path for this search algorithm.

        Args:
            checkpoint_path: File where the search algorithm
                state is saved. This path should be used later when
                restoring from file.

        Example:

        .. code-block:: python

            search_alg = Searcher(...)

            tuner = tune.Tuner(
                cost,
                tune_config=tune.TuneConfig(
                    search_alg=search_alg,
                    num_samples=5
                ),
                param_space=config
            )
            results = tuner.fit()

            search_alg.save("./my_favorite_path.pkl")

        .. versionchanged:: 0.8.7
            Save is automatically called by `Tuner().fit()`. You can use
            `Tuner().restore()` to restore from an experiment directory
            such as `~/ray_results/trainable`.

        r:   r+   r_   s     r   savezSearcher.save  s    @ "!r!   c                     t           )a  Restore state for this search algorithm


        Args:
            checkpoint_path: File where the search algorithm
                state is saved. This path should be the same
                as the one provided to "save".

        Example:

        .. code-block:: python

            search_alg.save("./my_favorite_path.pkl")

            search_alg2 = Searcher(...)
            search_alg2 = ConcurrencyLimiter(search_alg2, 1)
            search_alg2.restore(checkpoint_path)
            tuner = tune.Tuner(
                cost,
                tune_config=tune.TuneConfig(
                    search_alg=search_alg2,
                    num_samples=5
                ),
            )
            tuner.fit()

        r:   ra   s     r   restorezSearcher.restore<  s
    8 "!r!   max_concurrentc                     dS )a  Set max concurrent trials this searcher can run.

        This method will be called on the wrapped searcher by the
        ``ConcurrencyLimiter``. It is intended to allow for searchers
        which have custom, internal logic handling max concurrent trials
        to inherit the value passed to ``ConcurrencyLimiter``.

        If this method returns False, it signifies that no special
        logic for handling this case is present in the searcher.

        Args:
            max_concurrent: Number of maximum concurrent trials.
        Fr   r+   re   s     r   set_max_concurrencyzSearcher.set_max_concurrencyZ  s	     ur!   c                     t           Nr:   r+   s    r   	get_statezSearcher.get_statej      !!r!   statec                     t           rj   r:   r+   rn   s     r   	set_statezSearcher.set_statem  rm   r!   defaultcheckpoint_dirsession_strc           	         t           j                            |d          }d}	 |                     |           n;# t          $ r. t          d          rt                              d           d}Y nw xY w|rlt           j                            |          rOt          j	        |t           j                            || j
                            |                               dS dS dS )a,  Automatically saves the given searcher to the checkpoint_dir.

        This is automatically used by Tuner().fit() during a Tune job.

        Args:
            checkpoint_dir: Filepath to experiment dir.
            session_str: Unique identifier of the current run
                session.
        z.tmp_searcher_ckptTzsuggest:save_to_dirz1save not implemented for Searcher. Skipping save.FN)ospathjoinrb   r;   r   loggerwarningexistsreplaceCKPT_FILE_TMPLformat)r+   rs   rt   tmp_search_ckpt_pathsuccesss        r   save_to_dirzSearcher.save_to_dirp  s      "w||N<PQQ	II*++++" 	 	 	-.. TRSSSGGG	
  	rw~~&:;; 	J$^T-@-G-G-T-TUU    	 	 	 	s   : 5A21A2c                 .   | j                             d          }t          j        t          j                            ||                    }|s"t          d                    |                    t          |          }|                     |           dS )a(  Restores the state of a searcher from a given checkpoint_dir.

        Typically, you should use this function to restore from an
        experiment directory such as `~/ray_results/trainable`.

        .. code-block:: python

            tuner = tune.Tuner(
                cost,
                run_config=tune.RunConfig(
                    name=self.experiment_name,
                    storage_path="~/my_results",
                ),
                tune_config=tune.TuneConfig(
                    search_alg=search_alg,
                    num_samples=5
                ),
                param_space=config
            )
            tuner.fit()

            search_alg2 = Searcher()
            search_alg2.restore_from_dir(
                os.path.join("~/my_results", self.experiment_name)
        *z(Searcher unable to find checkpoint in {}N)	r}   r~   globrv   rw   rx   RuntimeErrorr   rd   )r+   rs   pattern
full_pathsmost_recent_checkpoints        r   restore_from_dirzSearcher.restore_from_dir  s    6 %,,S11Yrw||NGDDEE
 	:AA.QQ   "%Z+,,,,,r!   c                     | j         S )z.The training result objective value attribute.)r"   rk   s    r   r   zSearcher.metric  s     |r!   c                     | j         S )z1Specifies if minimizing or maximizing the metric.)r#   rk   s    r   r   zSearcher.mode  s     zr!   )NNNFFFN)rr   )__name__
__module____qualname____doc__r   r}   r   r&   r,   r   boolr2   r7   r=   r?   floatr   rF   r   r^   rb   rd   intrh   rl   rq   r   r   propertyr   r   r   r!   r   r   r      s       3 3j H,N !%"E EE smE E E E4sm+3C=BF	   ( T d    & KP" ""%-d^"CG"	" " " "*" " " " " "( 59" "" " 	"
 " &d5k2" " " "8I!'4=:N"NOI I I I IV "C  "  "  "  "D"s " " " "<# $     "4 " " " ""t " " " " # C    2"-s "- "- "- "-H     X c    X  r!   r   c                   8    e Zd ZdZd dededef fdZd Zdedefd	Z	d
e
e         de
e         dedefdZdede
e         fdZ	 d!dede
e         defdZdededdfdZ	 	 	 d"dedededede
ee                  f
dZdefdZdefdZdefdZdefdZ xZS )#ConcurrencyLimitera  A wrapper algorithm for limiting the number of concurrent trials.

    Certain Searchers have their own internal logic for limiting
    the number of concurrent trials. If such a Searcher is passed to a
    ``ConcurrencyLimiter``, the ``max_concurrent`` of the
    ``ConcurrencyLimiter`` will override the ``max_concurrent`` value
    of the Searcher. The ``ConcurrencyLimiter`` will then let the
    Searcher's internal logic take over.

    Args:
        searcher: Searcher object that the
            ConcurrencyLimiter will manage.
        max_concurrent: Maximum concurrent samples from the underlying
            searcher.
        batch: Whether to wait for all concurrent samples
            to finish before updating the underlying searcher.

    Example:

    .. code-block:: python

        from ray.tune.search import ConcurrencyLimiter
        search_alg = HyperOptSearch(metric="accuracy")
        search_alg = ConcurrencyLimiter(search_alg, max_concurrent=2)
        tuner = tune.Tuner(
            trainable_function,
            tune_config=tune.TuneConfig(
                search_alg=search_alg
            ),
        )
        tuner.fit()

    Fsearcherre   batchc                    t          |          t          u r|dk    sJ || _        || _        || _        t                      | _        d| _        i | _        d| _	        t          |t                    s t          dt          |           d          |                                  t          t          |                               | j        j        | j        j                   d S )Nr   TzAThe `ConcurrencyLimiter` only works with `Searcher` objects (got zH). Please try to pass `max_concurrent` to the search generator directly.)r   r   )r%   r   r   re   r   setlive_trialsnum_unfinished_live_trialscached_results_limit_concurrencyr$   r   r   _set_searcher_max_concurrencysuperr   r,   r   r   )r+   r   re   r   	__class__s       r   r,   zConcurrencyLimiter.__init__  s    N##s**~/A/A/AA ,
55*+' "&(H-- 	F $XF F F   	**,,, $''00='dm.@ 	1 	
 	
 	
 	
 	
r!   c                 P    | j                             | j                   | _        d S rj   )r   rh   re   r   rk   s    r   r   z0ConcurrencyLimiter._set_searcher_max_concurrency  s/     '+m&G&G'
 '
 #
r!   r.   c                     || _         dS )NT)re   rg   s     r   rh   z&ConcurrencyLimiter.set_max_concurrency  s     -tr!   r   r   r-   c                 ^    |                                   t          | j        j        |||fi |S rj   )r   r
   r   r2   r0   s        r   r2   z(ConcurrencyLimiter.set_search_properties   sC     	**,,,:M/v
 
IM
 
 	
r!   r3   c                    | j         s| j                            |          S || j        vsJ d| d            t	          | j                  | j        k    r9t                              d| dt	          | j                  | j                   d S | j                            |          }|d t          j	        fvr*| j        
                    |           | xj        dz  c_        |S )Nz	Trial ID z& must be unique: already found in set.zNot providing a suggestion for z! due to concurrency limit: %s/%s.   )r   r   r?   r   r(   re   ry   debugr   r   addr   )r+   r3   
suggestions      r   r?   zConcurrencyLimiter.suggest  s   & 	3=((222 D,,,,GxGGG -,,t  D$777LL,( , , ,D$%%#	   F]**844
dH$5666  ***++q0++r!   Nr4   r8   c                 $   | j         s| j                            |||          S || j        vrd S | j        r||f| j        |<   | xj        dz  c_        | j        dk    ri| j                                        D ]?\  }\  }}| j                            |||           | j                            |           @i | _        d| _        d S d S | j                            |||           | j                            |           | xj        dz  c_        d S )N)r4   r8   r   r   )	r   r   r=   r   r   r   r   itemsremover<   s       r   r=   z$ConcurrencyLimiter.on_trial_complete  sR    & 	Y=228FRW2XXX4+++FZ 	1-3UOD)++q0++.!33 261D1J1J1L1L 6 6-HovuM33 u 4    $++H5555&(#23///M++HV5+QQQ##H---++q0++++r!   c                 <    | j                             ||           d S rj   )r   r7   r6   s      r   r7   z"ConcurrencyLimiter.on_trial_result:  s     %%h77777r!   r@   rA   rB   rC   c                 >    | j                             |||||          S rj   )r   rF   rE   s         r   rF   z&ConcurrencyLimiter.add_evaluated_point=  s*     }00uf.A
 
 	
r!   c                 b    | j                                         }|d= t          j        |          S )Nr   )__dict__copydeepcopyrp   s     r   rl   zConcurrencyLimiter.get_stateI  s-    ""$$*}U###r!   rn   c                 :    | j                             |           d S rj   )r   updaterp   s     r   rq   zConcurrencyLimiter.set_stateN  s    U#####r!   r_   c                 :    | j                             |           d S rj   )r   rb   ra   s     r   rb   zConcurrencyLimiter.saveQ  s    ?+++++r!   c                 :    | j                             |           d S rj   )r   rd   ra   s     r   rd   zConcurrencyLimiter.restoreT  s    o.....r!   )Fr   r   )r   r   r   r   r   r   r   r,   r   rh   r   r&   r   r2   r?   r=   r7   r   r   rF   rl   rq   rb   rd   __classcell__)r   s   @r   r   r     s#          D
 
 
3 
t 
 
 
 
 
 
.
 
 
# $    
sm
+3C=
BF
	
 
 
 
     . KP1 11%-d^1CG1 1 1 188 8T 8d 8 8 8 8 59

 



 

 	


 

 &d5k2

 

 

 

$4 $ $ $ $
$t $ $ $ $,C , , , ,/s / / / / / / / /r!   r   )r   r   loggingrv   r[   typingr   r   r   r   r   r   ray.air._internal.usager	   ray.tune.search.utilr
   ray.util.annotationsr   r   ray.util.debugr   rV   r   rW   r   	getLoggerr   ry   r   r   r   r!   r   <module>r      su      				  B B B B B B B B B B B B B B B B 0 0 0 0 0 0 L L L L L L 8 8 8 8 8 8 8 8 # # # # # # *444444))))))		8	$	$ ` ` ` ` ` ` ` `F \/ \/ \/ \/ \/ \/ \/ \/ \/ \/r!   