
    &`i_                         d dl Z d dlZd dl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  ej        e          ZdZ	 de	defd	Z G d
 d          Ze G d de	                      ZdS )    N)DictListOptional)Searcher)+_set_search_properties_backwards_compatible)	PublicAPI__trial_index__searchernum_samplesc                     t          | t                    r&|| j        z  rt                              d           d S d S d S )Na  `num_samples` is now expected to be the total number of trials, including the repeat trials. For example, set num_samples=15 if you intend to obtain 3 search algorithm suggestions and repeat each suggestion 5 times. Any leftover trials (num_samples mod repeat) will be ignored.)
isinstanceRepeaterrepeatloggerwarning)r
   r   s     l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/repeater.py_warn_num_samplesr      s[    (H%% 
+*G 
8	
 	
 	
 	
 	

 
 
 
    c                       e Zd ZdZddede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ee                  fdZd	efdZdS )_TrialGroupa  Internal class for grouping trials of same parameters.

    This is used when repeating trials for reducing training variance.

    Args:
        primary_trial_id: Trial ID of the "primary trial".
            This trial is the one that the Searcher is aware of.
        config: Suggested configuration shared across all trials
            in the trial group.
        max_trials: Max number of trials to execute within this group.

       primary_trial_idconfig
max_trialsc                     t          |          t          u sJ d                    |                      || _        || _        |d i| _        || _        d S )Nzconfig is not a dict, got {})typedictformatr   r   _trialsr   )selfr   r   r   s       r   __init__z_TrialGroup.__init__*   sV    F||t###%C%J%J6%R%R### 0($/$r   trial_idc                 z    t          | j                  | j        k     sJ | j                            |d            d S N)lenr   r   
setdefault)r    r"   s     r   addz_TrialGroup.add1   s=    4<  4?2222$/////r   returnc                 <    t          | j                  | j        k    S r$   )r%   r   r   r    s    r   fullz_TrialGroup.full5   s    4<  DO33r   scorec                 R    || j         v sJ |t          d          || j         |<   d S )Nz%Internal Error: Score cannot be None.)r   
ValueError)r    r"   r,   s      r   reportz_TrialGroup.report8   s;    4<''''=DEEE!&Xr   c                 r    d | j                                         vot          | j                   | j        k    S r$   )r   valuesr%   r   r*   s    r   finished_reportingz_TrialGroup.finished_reporting>   s3    ++---V#dl2C2Ct2V	
r   c                 N    t          | j                                                  S r$   )listr   r1   r*   s    r   scoresz_TrialGroup.scoresC   s    DL''))***r   c                 *    t          | j                  S r$   )r%   r   r*   s    r   countz_TrialGroup.countF   s    4<   r   N)r   )__name__
__module____qualname____doc__strr   intr!   r'   boolr+   floatr/   r2   r   r   r5   r7    r   r   r   r      s         % % %d % % % % %0C 0 0 0 04d 4 4 4 4's '5 ' ' ' '
D 
 
 
 

+Xe_- + + + +!s ! ! ! ! ! !r   r   c                        e Zd ZdZddededef fdZded	e	e
         fd
Zdded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	e         de	e         de
d	efdZ xZS )r   a  A wrapper algorithm for repeating trials of same parameters.

    Set tune.TuneConfig(num_samples=...) to be a multiple of `repeat`. For example,
    set num_samples=15 if you intend to obtain 3 search algorithm suggestions
    and repeat each suggestion 5 times. Any leftover trials
    (num_samples mod repeat) will be ignored.

    It is recommended that you do not run an early-stopping TrialScheduler
    simultaneously.

    Args:
        searcher: Searcher object that the
            Repeater will optimize. Note that the Searcher
            will only see 1 trial among multiple repeated trials.
            The result/metric passed to the Searcher upon
            trial completion will be averaged among all repeats.
        repeat: Number of times to generate a trial with a repeated
            configuration. Defaults to 1.
        set_index: Sets a tune.search.repeater.TRIAL_INDEX in
            Trainable/Function config which corresponds to the index of the
            repeated trial. This can be used for seeds. Defaults to True.

    Example:

    .. code-block:: python

        from ray.tune.search import Repeater

        search_alg = BayesOptSearch(...)
        re_search_alg = Repeater(search_alg, repeat=10)

        # Repeat 2 samples 10 times each.
        tuner = tune.Tuner(
            trainable,
            tune_config=tune.TuneConfig(
                search_alg=re_search_alg,
                num_samples=20,
            ),
        )
        tuner.fit()

    r   Tr
   r   	set_indexc                     || _         || _        || _        g | _        i | _        d | _        t          t          |                               | j         j	        | j         j
                   d S )N)metricmode)r
   r   
_set_index_groups_trial_id_to_group_current_groupsuperr   r!   rD   rE   )r    r
   r   rB   	__class__s       r   r!   zRepeater.__init__w   so     #"$"h&&='dm.@ 	' 	
 	
 	
 	
 	
r   r"   r(   c                    | j         | j                                         rn| j                            |          }||S t	          |t          j        |          | j                  | _         | j        	                    | j                    d}n3| j         
                                }| j                             |           | j         j                                        }| j        r
||t          <   | j         | j        |<   |S )N)r   r   )rI   r+   r
   suggestr   copydeepcopyr   rG   appendr7   r'   r   rF   TRIAL_INDEXrH   )r    r"   r   index_in_groups       r   rM   zRepeater.suggest   s    &$*=*B*B*D*D&]**844F~"-$-//DK# # #D L 3444NN!06688N##H---$+0022? 	1"0F;,0,?)r   Nresultc                     || j         vr@t                              d                    |t	          | j                                        | j         |         }|r| j        j        |vrt          j        }n|| j        j                 }|	                    ||           |
                                rN|                                } | j        j        |j        fd| j        j        t          j        |          ii| dS dS )a  Stores the score for and keeps track of a completed trial.

        Stores the metric of a trial as nan if any of the following conditions
        are met:

        1. ``result`` is empty or not provided.
        2. ``result`` is provided but no metric was provided.

        z;Trial {} not in group; cannot report score. Seen trials: {}rS   N)rH   r   errorr   r4   r
   rD   npnanr/   r2   r5   on_trial_completer   nanmean)r    r"   rS   kwargstrial_groupr,   r5   s          r   rX   zRepeater.on_trial_complete   s    4222LL""(&48O3P3P"Q"Q   -h7 	1-V;;FEE4=/0E8U+++))++ 	 ''))F+DM+, ,bj.@.@A     	 	r   c                 >    | j                                         }|d= |S )Nr
   )__dict__rN   )r    
self_states     r   	get_statezRepeater.get_state   s#    ]''))
z"r   statec                 :    | j                             |           d S r$   )r]   update)r    r`   s     r   	set_statezRepeater.set_state   s    U#####r   checkpoint_pathc                 :    | j                             |           d S r$   )r
   saver    rd   s     r   rf   zRepeater.save   s    ?+++++r   c                 :    | j                             |           d S r$   )r
   restorerg   s     r   ri   zRepeater.restore   s    o.....r   rD   rE   r   c                 6    t          | j        j        |||fi |S r$   )r   r
   set_search_properties)r    rD   rE   r   specs        r   rk   zRepeater.set_search_properties   s1     ;M/v
 
IM
 
 	
r   )r   Tr$   )r8   r9   r:   r;   r   r=   r>   r!   r<   r   r   rM   rX   r_   rc   rf   ri   rk   __classcell__)rK   s   @r   r   r   J   s[       ) )V	
 	
 	
3 	
t 	
 	
 	
 	
 	
 	
     ( # x~    <4    
$t $ $ $ $,C , , , ,/s / / / /
sm
+3C=
BF
	
 
 
 
 
 
 
 
r   r   )rN   loggingtypingr   r   r   numpyrV   ray.tune.search.searcherr   ray.tune.search.utilr   ray.utilr   	getLoggerr8   r   rQ   r=   r   r   r   r@   r   r   <module>ru      s&     ' ' ' ' ' ' ' ' ' '     - - - - - - L L L L L L      		8	$	$ G
 
s 
 
 
 
+! +! +! +! +! +! +! +!\ |
 |
 |
 |
 |
x |
 |
 |
 |
 |
r   