
    &`i'                        d dl Z d dlZd dlmZmZmZmZ d dl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  e j        e          Ze G d d	e                      Z G d
 d          ZeZedk    rk eddd          Z ee                                           ej        d          Z ee                    d  e d          D                                  dS dS )    N)TYPE_CHECKINGDictOptionalUnion)Trial)DEFAULT_METRIC)FIFOSchedulerTrialScheduler)	PublicAPI)TuneControllerc                   
    e Zd ZdZ	 	 	 	 	 	 	 	 dded	ee         d
ee         dededededef fdZ	d	ee         d
ee         defdZ
dddefdZdddededefdZdddedefdZdddefdZdefdZdefdZdefdZ xZS ) AsyncHyperBandScheduleraY  Implements the Async Successive Halving.

    This should provide similar theoretical performance as HyperBand but
    avoid straggler issues that HyperBand faces. One implementation detail
    is when using multiple brackets, trial allocation to bracket is done
    randomly with over a softmax probability.

    See https://arxiv.org/abs/1810.05934

    Args:
        time_attr: A training result attr to use for comparing time.
            Note that you can pass in something non-temporal such as
            `training_iteration` as a measure of progress, the only requirement
            is that the attribute should increase monotonically.
        metric: The training result objective value attribute. Stopping
            procedures will use this attribute. If None but a mode was passed,
            the `ray.tune.result.DEFAULT_METRIC` will be used per default.
        mode: One of {min, max}. Determines whether objective is
            minimizing or maximizing the metric attribute.
        max_t: max time units per trial. Trials will be stopped after
            max_t time units (determined by time_attr) have passed.
        grace_period: Only stop trials at least this old in time.
            The units are the same as the attribute named by `time_attr`.
        reduction_factor: Used to set halving rate and amount. This
            is simply a unit-less scalar.
        brackets: Number of brackets. Each bracket has a different
            halving rate, specified by the reduction factor.
        stop_last_trials: Whether to terminate the trials after
            reaching max_t. Defaults to True.
    training_iterationNd         T	time_attrmetricmodemax_tgrace_periodreduction_factorbracketsstop_last_trialsc	                 ,   dk    s
J d            k    s
J d            dk    s
J d            dk    s
J d            |dk    s
J d            |r|dv s
J d	            t                                                       | _        | _        i | _        fd
t          |          D             | _        d| _        d| _        || _	        || _
        d | _        | j
        dk    rd| _        n| j
        dk    rd| _        || _        | _        d S )Nr   zMax (time_attr) not valid!zgrace_period must be <= max_t!zgrace_period must be positive!r   zReduction Factor not valid!zbrackets must be positive!)minmaxz`mode` must be 'min' or 'max'!c           	      8    g | ]}t          |           S ))r   )_Bracket).0sr   r   r   r   s     w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/schedulers/async_hyperband.py
<listcomp>z4AsyncHyperBandScheduler.__init__.<locals>.<listcomp>M   sI     	
 	
 	
   !1  	
 	
 	
    r         ?r         )super__init___reduction_factor_max_t_trial_inforange	_brackets_counter_num_stopped_metric_mode
_metric_op
_time_attr_stop_last_trials)
selfr   r   r   r   r   r   r   r   	__class__s
       ``` `r"   r(   z AsyncHyperBandScheduler.__init__3   su    qyyy6yyy$$$&F$$$a!A!###%B###!|||9||| 	L>)))+K)))!1	
 	
 	
 	
 	
 	
 	
 8__	
 	
 	
 
:!DOOZ5  "DO#!1r$   returnc                     | j         r|rdS | j        r|rdS |r|| _         |r|| _        | j        dk    rd| _        n| j        dk    rd| _        | j         | j        rt          | _         dS )NFr   r%   r   r&   T)r0   r1   r2   r   )r5   r   r   specs       r"   set_search_propertiesz-AsyncHyperBandScheduler.set_search_propertiesc   s     < 	F 	5: 	$ 	5 	"!DL 	DJ:!DOOZ5  "DO<DJ)DLtr$   tune_controllerr   trialc                    | j         r| j        s8t          d                    | j        j        | j         | j                            t          j        d | j	        D                       }t          j
        ||                                z
  z  }||                                z  }t          j                            t          | j	                  |          }| j	        |         | j        |j        <   d S )Nz{} has been instantiated without a valid `metric` ({}) or `mode` ({}) parameter. Either pass these parameters when instantiating the scheduler, or pass them as parameters to `tune.TuneConfig()`c                 6    g | ]}t          |j                  S  )len_rungsr    bs     r"   r#   z8AsyncHyperBandScheduler.on_trial_add.<locals>.<listcomp>   s     @@@A#ah--@@@r$   )p)r0   r2   
ValueErrorformatr6   __name__r1   nparrayr-   er   sumrandomchoicer@   r+   trial_id)r5   r;   r<   sizesprobs
normalizedidxs          r"   on_trial_addz$AsyncHyperBandScheduler.on_trial_add{   s    | 	4? 	) *0N+T\4:* *	   @@@@@AA,-UYY[[(
is4>22jAA+/>#+>(((r$   resultc                 h   t           j        }| j        |vs	| j        |vr|S || j                 | j        k    r| j        rt           j        }nG| j        |j                 }|	                    ||| j                 | j
        || j                 z            }|t           j        k    r| xj        dz  c_        |S )Nr   )r
   CONTINUEr3   r0   r*   r4   STOPr+   rN   	on_resultr2   r/   )r5   r;   r<   rT   actionbrackets         r"   on_trial_resultz'AsyncHyperBandScheduler.on_trial_result   s      (?&((DL,F,FM$/"dk11d6L1#(FF&u~6G&&vdo.&BV0V F ^((("r$   c                     | j         |vs	| j        |vrd S | j        |j                 }|                    ||| j                  | j        || j                 z             | j        |j        = d S N)r3   r0   r+   rN   rX   r2   )r5   r;   r<   rT   rZ   s        r"   on_trial_completez)AsyncHyperBandScheduler.on_trial_complete   sz     ?&((DL,F,FF"5>26$/*DOfT\>R,R	
 	
 	
 U^,,,r$   c                      | j         |j        = d S r]   )r+   rN   )r5   r;   r<   s      r"   on_trial_removez'AsyncHyperBandScheduler.on_trial_remove   s    U^,,,r$   c                     d                     | j                  }|dd                    d | j        D                       z   z  }|S )Nz$Using AsyncHyperBand: num_stopped={}
c                 6    g | ]}|                                 S r?   )	debug_strrB   s     r"   r#   z8AsyncHyperBandScheduler.debug_string.<locals>.<listcomp>   s      G G G1 G G Gr$   )rF   r/   joinr-   )r5   outs     r"   debug_stringz$AsyncHyperBandScheduler.debug_string   sI    4;;D<MNNtdii G G G G GHHHH
r$   checkpoint_pathc                     | j         }t          |d          5 }t          j        ||           d d d            d S # 1 swxY w Y   d S )Nwb)__dict__openpickledump)r5   rh   save_object
outputFiles       r"   savezAsyncHyperBandScheduler.save   s    m/4(( 	1JKZ000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   ;??c                     t          |d          5 }t          j        |          }d d d            n# 1 swxY w Y   | j                            |           d S )Nrb)rl   rm   loadrk   update)r5   rh   	inputFilero   s       r"   restorezAsyncHyperBandScheduler.restore   s    /4(( 	1I +i00K	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1[)))))s   266)r   NNr   r   r   r   T)rG   
__module____qualname____doc__strr   intfloatboolr(   r:   r   rS   r   r[   r^   r`   rg   rq   rw   __classcell__)r6   s   @r"   r   r      s        B . $""#!%.2 .2.2 .2 sm	.2
 .2 .2  .2 .2 .2 .2 .2 .2 .2 .2`sm+3C=	   0?,< ?U ? ? ? ?"/8=GK	   "	-/	-8=	-GK	- 	- 	- 	--/? - - - - -c    
1C 1 1 1 1
*s * * * * * * * *r$   r   c                       e Zd ZdZ	 ddededededef
dZd	ee	eee
ej        f                  fd
Zdededee         d	efdZd	efdZdS )r   a  Bookkeeping system to track the cutoffs.

    Rungs are created in reversed order so that we can more easily find
    the correct rung corresponding to the current iteration of the result.

    Example:
        >>> trial1, trial2, trial3 = ... # doctest: +SKIP
        >>> b = _Bracket(1, 10, 2, 0) # doctest: +SKIP
        >>> # CONTINUE
        >>> b.on_result(trial1, 1, 2) # doctest: +SKIP
        >>> # CONTINUE
        >>> b.on_result(trial2, 1, 4) # doctest: +SKIP
        >>> # rungs are reversed
        >>> b.cutoff(b._rungs[-1][1]) == 3.0 # doctest: +SKIP
         # STOP
        >>> b.on_result(trial3, 1, 1) # doctest: +SKIP
        >>> b.cutoff(b._rungs[3][1]) == 2.0 # doctest: +SKIP
    Tmin_tr   r   r!   r   c                     | _         t          t          j        |z            t          j         j                   z  z
  dz             } fdt	          t          |                    D              _        | _        d S )Nr   c                 4    g | ]}j         |z   z  z  i fS r?   )rf)r    kr   r!   r5   s     r"   r#   z%_Bracket.__init__.<locals>.<listcomp>   s;     
 
 
12UTWQ'',
 
 
r$   )r   r|   rH   logreversedr,   rA   r4   )r5   r   r   r   r!   r   	MAX_RUNGSs   ``  `  r"   r(   z_Bracket.__init__   s     #uu}--tw?!CaGHH	
 
 
 
 
 
6>uY?O?O6P6P
 
 
 "2r$   r7   c                     |sd S t          j        t          |                                          dd| j        z  z
  dz            S )Nr   r   )rH   nanpercentilelistvaluesr   )r5   recordeds     r"   cutoffz_Bracket.cutoff   sC     	4X__%6%6 7 7!a$'k/S9PQQQr$   r<   cur_itercur_rewc                 (   t           j        }| j        D ]}\  }}||k    r|j        |v r	| j        s na||k     s	|j        |v r-|                     |          }|||k     rt           j        }|t                              d           n
|||j        <    |S )NzEReward attribute is None! Consider reporting using a different field.)	r
   rV   rA   rN   r4   r   rW   loggerwarning)r5   r<   r   r   rY   	milestoner   r   s           r"   rX   z_Bracket.on_result   s    (#'; 	 	IxI%%Nh... / )##u~'A'AX..%'F*:*:+0F?NN>   
 07HU^,r$   c                 Z     d                      fd j        D                       }d|z   S )Nz | c                 h    g | ].\  }}d                      |                    |                    /S )zIter {:.3f}: {})rF   r   )r    r   r   r5   s      r"   r#   z&_Bracket.debug_str.<locals>.<listcomp>  sI       'Ix "((DKK4I4IJJ  r$   z	Bracket: )re   rA   )r5   iterss   ` r"   rd   z_Bracket.debug_str   sK    

   +/;  
 
 U""r$   N)T)rG   rx   ry   rz   r|   r}   r~   r(   r   r   complexrH   ndarrayr   r   r{   rX   rd   r?   r$   r"   r   r      s         2 "&2 22 2  	2
 2 2 2 2 2R(5eWbj1P+Q"R R R R R
u  huo RU    :#3 # # # # # #r$   r   __main__r   
      )r   r   r   c                 .    i | ]}t          |          |S r?   )r{   )r    is     r"   
<dictcomp>r     s     777#a&&!777r$      )!loggingrm   typingr   r   r   r   numpyrH   ray.tune.experimentr   ray.tune.resultr   #ray.tune.schedulers.trial_schedulerr	   r
   ray.utilr   "ray.tune.execution.tune_controllerr   	getLoggerrG   r   r   r   ASHASchedulerschedprintrg   r-   rZ   r   r,   r?   r$   r"   <module>r      s     7 7 7 7 7 7 7 7 7 7 7 7     % % % % % % * * * * * * M M M M M M M M       BAAAAAA		8	$	$ e* e* e* e* e*m e* e* e*PM# M# M# M# M# M# M# M#` (z##"qQQQE	E%



oa G	E'..77UU2YY777
8
899999	 r$   