
    &`i                         d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZ  ej        e          Ze G d de                      ZdS )    N)DictListOptional)Searcher)+_set_search_properties_backwards_compatible)	PublicAPIc                   P    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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,
            tune_config=tune.TuneConfig(
                search_alg=search_alg
            ),
        )
        tuner.fit()
    Fsearchermax_concurrent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.)metricmode)typeintr   r   r   setlive_trialsnum_unfinished_live_trialscached_results_limit_concurrency
isinstancer   RuntimeError_set_searcher_max_concurrencysuperr
   __init__r   r   )selfr   r   r   	__class__s       w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/concurrency_limiter.pyr   zConcurrencyLimiter.__init__/   s    N##s**~/A/A/AA ,
55*+' "&(H-- 	F $XF F F   	**,,, $''00='dm.@ 	1 	
 	
 	
 	
 	
    c                 P    | j                             | j                   | _        d S N)r   set_max_concurrencyr   r   )r   s    r   r   z0ConcurrencyLimiter._set_searcher_max_concurrencyF   s/     '+m&G&G'
 '
 #
r    returnc                     || _         dS )NT)r   )r   r   s     r   r#   z&ConcurrencyLimiter.set_max_concurrencyM   s     -tr    r   r   configc                 ^    |                                   t          | j        j        |||fi |S r"   )r   r   r   set_search_properties)r   r   r   r&   specs        r   r(   z(ConcurrencyLimiter.set_search_propertiesS   sC     	**,,,:M/v
 
IM
 
 	
r    trial_idc                    | 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   suggestr   lenr   loggerdebugr   FINISHEDaddr   )r   r*   
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    Nresulterrorc                 $   | 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   r5   r,   r   )	r   r   on_trial_completer   r   r   r   itemsremove)r   r*   r4   r5   s       r   r7   z$ConcurrencyLimiter.on_trial_completeq   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 r"   )r   on_trial_result)r   r*   r4   s      r   r;   z"ConcurrencyLimiter.on_trial_result   s     %%h77777r    
parametersvalueprunedintermediate_valuesc                 >    | j                             |||||          S r"   )r   add_evaluated_point)r   r<   r=   r5   r>   r?   s         r   rA   z&ConcurrencyLimiter.add_evaluated_point   s*     }00uf.A
 
 	
r    c                 b    | j                                         }|d= t          j        |          S )Nr   )__dict__copydeepcopyr   states     r   	get_statezConcurrencyLimiter.get_state   s-    ""$$*}U###r    rG   c                 :    | j                             |           d S r"   )rC   updaterF   s     r   	set_statezConcurrencyLimiter.set_state   s    U#####r    checkpoint_pathc                 :    | j                             |           d S r"   )r   saver   rL   s     r   rN   zConcurrencyLimiter.save   s    ?+++++r    c                 :    | j                             |           d S r"   )r   restorerO   s     r   rQ   zConcurrencyLimiter.restore   s    o.....r    c                 :    | j                             |           d S r"   )r   on_pauser   r*   s     r   rS   zConcurrencyLimiter.on_pause   s    x(((((r    c                 :    | j                             |           d S r"   )r   
on_unpauserT   s     r   rV   zConcurrencyLimiter.on_unpause   s      *****r    )F)NF)FFN)__name__
__module____qualname____doc__r   r   boolr   r   r#   r   strr   r(   r-   r7   r;   floatr   rA   rH   rK   rN   rQ   rS   rV   __classcell__)r   s   @r   r
   r
      sY        B
 
 
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 / / / /
) ) ) ) )+3 + + + + + + + +r    r
   )rD   loggingtypingr   r   r   ray.tune.search.searcherr   ray.tune.search.utilr   ray.util.annotationsr   	getLoggerrW   r/   r
    r    r   <module>rf      s      ' ' ' ' ' ' ' ' ' ' - - - - - - L L L L L L * * * * * *		8	$	$ c+ c+ c+ c+ c+ c+ c+ c+ c+ c+r    