
    &`i                         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
 d dlmZ erd dlmZ  e j        e          Ze G d d	e                      ZdS )
    N)TYPE_CHECKINGDictOptional)Trial)HyperBandScheduler)TrialScheduler)	PublicAPI)TuneControllerc                   n    e Zd ZdZdddefdZdddededefdZdddefd	Z		 dddde
dee         fdZdS )HyperBandForBOHBa  Extends HyperBand early stopping algorithm for BOHB.

    This implementation removes the ``HyperBandScheduler`` pipelining. This
    class introduces key changes:

    1. Trials are now placed so that the bracket with the largest size is
    filled first.

    2. Trials will be paused even if the bracket is not filled. This allows
    BOHB to insert new trials into the training.

    See ray.tune.schedulers.HyperBandScheduler for parameter docstring.
    tune_controllerr
   trialc                 8   | j         r| j        s8t          d                    | j        j        | j         | j                            | j        d         }| j        | j        d                  }||	                                rd}|r| 
                                r1g }| j                            |           | j        dxx         dz  cc<   | j        t          |          z
  dz
  }|dk    s
J d            |                     |          dk    rt                              d	           d}nd
}|                     |          }|                    |           || j        d<   || j        d                             |           || j        d         f| j        |<   dS )a  Adds new trial.

        On a new trial add, if current bracket is not filled, add to current
        bracket. Else, if current band is not filled, create new bracket, add
        to current bracket. Else, create new iteration, create new bracket,
        add to bracket.
        z{} 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()`bracketband_idxNT   r   zCurrent band is filled!z%BOHB: Bracket too small - Retrying...F)_metric
_metric_op
ValueErrorformat	__class____name___mode_state_hyperbandsfilled_cur_band_filledappend_s_max_1len_get_r0loggerdebug_create_bracket	add_trial_trial_info)selfr   r   cur_bracketcur_bandretryss          o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/schedulers/hb_bohb.pyon_trial_addzHyperBandForBOHB.on_trial_add   s    | 	4? 	) *0N+T\4:* *	   k),#DK
$;<+"4"4"6"6E 5((** 1!H$++H555K
+++q0+++ MCMM1A5Avvv8vvv<<??a''LL!HIII"&KK!E"&"6"6q"9"9K,,,)4I&%  5( 	I((///"-t{:/F"F    resultreturnc                 p   i |d<   | j                  \  }}|                    |           |                              rt          j        S |j        |d         d<   d |j        D             }|                                rt          fd|D                       r0|j	        j
                            j                   t          j        S t                              d d           |                     ||          }|t          j        k    r$|j	        j
                            j                   |S )at  If bracket is finished, all trials will be stopped.

        If a given trial finishes and bracket iteration is not done,
        the trial will be paused and resources will be given up.

        This scheduler will not start trials but will stop trials.
        The current running trial will not be handled,
        as the trialrunner will be given control to handle it.hyperband_infobudgetc                      g | ]}||j         fS  )status.0ts     r,   
<listcomp>z4HyperBandForBOHB.on_trial_result.<locals>.<listcomp>b   s    @@@aQM@@@r.   c              3   F   K   | ]\  }}|u	|t           j        k    V  d S N)r   PAUSED)r8   r9   r6   r   s      r,   	<genexpr>z3HyperBandForBOHB.on_trial_result.<locals>.<genexpr>c   s;       '
 '
'0q&Fel"'
 '
r.   zProcessing bracket after trial z result)r&   update_trial_statscontinue_trialr   CONTINUE_cumul_r_live_trialsr   any
search_algsearcheron_pausetrial_idPAUSEr"   r#   _process_bracket)r'   r   r   r/   r   _statusesactions     `     r,   on_trial_resultz HyperBandForBOHB.on_trial_resultL   sT    $& %e,
""5&111!!%(( 	+!**-4-= * A@7+?@@@~~ 	(3 '
 '
 '
 '
4<'
 '
 '
 $
 $
 	( &/88HHH!''EuEEEFFF&&@@^)))&/88HHHr.   c                 N    |j         j                            |j                   d S r<   )rE   rF   
on_unpauserH   )r'   r   r   s      r,   _unpause_trialzHyperBandForBOHB._unpause_trial{   s$    "+66u~FFFFFr.   Tallow_recursec                    | j         D ]e}d |D             }|D ]T                                D ]=}|j        t          j        k    r	|j        v s|j        t          j        k    r|c c c S >Uft          d |                                D                       s| j         D ]}|D ]rt          d                                 D                       rzt          
                    d           |                     |           |rHt          fd                                D                       r|                     |d          c c S dS )	zFair scheduling within iteration by completion percentage.

        List of trials not used since all trials are tracked as state
        of scheduler. If iteration is occupied (ie, no trials to run),
        then look into next iteration.
        c                     g | ]}||S r<   r5   )r8   bs     r,   r:   z8HyperBandForBOHB.choose_trial_to_run.<locals>.<listcomp>   s    >>>ar.   c              3   @   K   | ]}|j         t          j        k    V  d S r<   )r6   r   RUNNINGr7   s     r,   r>   z7HyperBandForBOHB.choose_trial_to_run.<locals>.<genexpr>   s+      SS18u},SSSSSSr.   c              3   @   K   | ]}|j         t          j        k    V  d S r<   )r6   r   r=   )r8   r   s     r,   r>   z7HyperBandForBOHB.choose_trial_to_run.<locals>.<genexpr>   s?       ' '! 4' ' ' ' ' 'r.   z-Processing bracket since no trial is running.c              3   ~   K   | ]7}|j         t          j        k    o|j        v p|j         t          j        k    V  8d S r<   )r6   r   r=   trials_to_unpausePENDING)r8   r   r   s     r,   r>   z7HyperBandForBOHB.choose_trial_to_run.<locals>.<genexpr>   sf       1 1 !&	 !& < !G$)W-F$F=  %|u}<1 1 1 1 1 1r.   F)rR   N)r   current_trialsr6   r   r=   rZ   r[   rD   
get_trialsr"   r#   rJ   choose_trial_to_run)r'   r   rR   	hyperbandscrubbedr   r   s         @r,   r^   z$HyperBandForBOHB.choose_trial_to_run   s    ) 
	% 
	%I ?>9>>>H# % %$3355 % %E44!W%>>>66$ 7	%% SSo6P6P6R6RSSSSS 	!-  	(  G 3 ' '%,%;%;%=%=' ' ' $ $ 
 %TUUU--owGGG ) 
S 1 1 1 1 *1)?)?)A)A1 1 1 . . 
 $(#;#; /u $< $ $     +2 tr.   N)T)r   
__module____qualname____doc__r   r-   r   strrN   rQ   boolr   r^   r5   r.   r,   r   r      s         +G,< +GU +G +G +G +GZ-/-8=-GK-	- - - -^G.> Gu G G G G
 HL1 1/1@D1	%1 1 1 1 1 1r.   r   )loggingtypingr   r   r   ray.tune.experimentr   ray.tune.schedulers.hyperbandr   #ray.tune.schedulers.trial_schedulerr   ray.utilr	   "ray.tune.execution.tune_controllerr
   	getLoggerr   r"   r   r5   r.   r,   <module>rn      s     0 0 0 0 0 0 0 0 0 0 % % % % % % < < < < < < > > > > > >       BAAAAAA		8	$	$ ` ` ` ` `) ` ` ` ` `r.   