
    &`i                        d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z
mZmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZmZmZmZ d dlmZ  ej         e!          Z"d Z#e G d de                      Z$dS )    N)DictListOptionalUnion)	TuneError)
ExperimentTrial_convert_to_experiment_list)_create_trial_from_spec_make_parser)SearchAlgorithm)Searcher)+_set_search_properties_backwards_compatible)_resolve_nested_dictformat_vars)_atomic_save_load_newest_checkpointflatten_dictmerge_dicts)DeveloperAPIc                 *    ddl m}  || |           d S )Nr   )_warn_num_samples)ray.tune.search.repeaterr   )searchertotal_samplesr   s      t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/search_generator.py_warn_on_repeaterr      s-    ::::::h.....    c                   Z   e Zd ZdZdZdefdZed             Zde	e
         de	e
         ded	efd
Zed             Zdeeee         ee
ef         f         fdZd Zded	e	e         fdZde
defdZ	 d!de
de	e         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
de
fdZde
fd ZdS )"SearchGeneratora  Generates trials to be passed to the TrialRunner.

    Uses the provided ``searcher`` object to generate trials. This class
    transparently handles repeating trials with score aggregation
    without embedding logic into the Searcher.

    Args:
        searcher: Search object that subclasses the Searcher base class. This
            is then used for generating new hyperparameter samples.
    zsearch_gen_state-{}.jsonr   c                     t          t          |          t                    s
J d            || _        t	                      | _        d | _        d| _        d| _        d| _	        d S )Nz(Searcher should be subclassing Searcher.r   F)

issubclasstyper   r   r   _parser_experiment_counter_total_samples	_finished)selfr   s     r   __init__zSearchGenerator.__init__,   sl    NNH
 
 	6 	65	6 	6 
 !#~~r   c                     | j         j        S N)r   metricr)   s    r   r-   zSearchGenerator.metric7   s    }##r   r-   modeconfigreturnc                 6    t          | j        j        |||fi |S r,   )r   r   set_search_properties)r)   r-   r/   r0   specs        r   r3   z%SearchGenerator.set_search_properties;   s1     ;M/v
 
IM
 
 	
r   c                     | j         S r,   )r'   r.   s    r   r   zSearchGenerator.total_samplesB   s    ""r   experimentsc                    | j         rJ t                              d           t          |          }t	          |          dk    s
J d            |d         | _         | j         j        }| j         j                            dd          | _        t          | j	        | j                   d|vr"t          d                    |                    dS )	znRegisters experiment specifications.

        Arguments:
            experiments: Experiments to run.
        zadded configurations   z9SearchAlgorithms can only support 1 experiment at a time.r   num_samplesrunzMust specify `run` in {}N)r%   loggerdebugr
   lenr4   getr'   r   r   r   format)r)   r6   experiment_listexperiment_specs       r   add_configurationsz"SearchGenerator.add_configurationsF   s     ####+,,,5kBB  A%%%F &%%*1-*/".377qII$-)<===''6==oNNOOO ('r   c                 l    |                                  s|                     | j        j                  S dS )zProvides one Trial object to be queued into the TrialRunner.

        Returns:
            Trial: Returns a single trial.
        N)is_finishedcreate_trial_if_possibler%   r4   r.   s    r   
next_trialzSearchGenerator.next_trial\   s8     !! 	H001A1FGGGtr   rA   c                    t                               d           t          j                    }| j                            |          }|t          j        k    r#d| _        t                               d           d S |d S t          j
        |          }t          |d         t          j
        |                    |d<   t          |d                   }| xj        dz  c_        d                    t          | j                  t!          |                    }t#          || j        t'          |          ||          }|S )Nzcreating trialTzSearcher has finished.r0   r8   z{0}_{1})evaluated_paramsexperiment_tagtrial_id)r;   r<   r	   generate_idr   suggestr   FINISHEDr(   copydeepcopyr   r   r&   r?   strr   r   r$   r   )r)   rA   rJ   suggested_configr4   flattened_configtagtrials           r   rE   z(SearchGenerator.create_trial_if_possiblef   s#   %&&&$&&=00::x000!DNLL1222F#F}_--$T(^T]CS5T5TUUX 0X??s4=11;?O3P3PQQ'L)*:;;
 
 
 r   rJ   resultc                 <    | j                             ||           dS )z!Notifies the underlying searcher.N)r   on_trial_result)r)   rJ   rU   s      r   rW   zSearchGenerator.on_trial_result   s     %%h77777r   NFerrorc                 @    | j                             |||           d S )N)rJ   rU   rX   )r   on_trial_complete)r)   rJ   rU   rX   s       r   rZ   z!SearchGenerator.on_trial_complete   s(     	''&PU'VVVVVr   c                 0    | j         | j        k    p| j        S r,   )r&   r'   r(   r.   s    r   rD   zSearchGenerator.is_finished   s    } 33Et~Er   c                 8    | j         | j        | j        | j        dS )N)counterr   finished
experimentr&   r'   r(   r%   r.   s    r   	get_statezSearchGenerator.get_state   s'    }!0*	
 
 	
r   statec                 n    |d         | _         |d         | _        |d         | _        |d         | _        d S )Nr]   r   r^   r_   r`   )r)   rb   s     r   	set_statezSearchGenerator.set_state   s:    i(#O4z* .r   dirpathc                 l    t          t          || j                            d                              S )N*)boolr   CKPT_FILE_TMPLr?   )r)   re   s     r   has_checkpointzSearchGenerator.has_checkpoint   s-    +GT5H5O5OPS5T5TUUVVVr   session_strc                    | j         }|                                 }t          |d          rwt          |          j        }||v r.t
                              d                    |                     n|                                |d|z   <   |j         }t          |d          w|}|                    ||           t          ||| j
                            |          d           dS )a  Saves self + searcher to dir.

        Separates the "searcher" from its wrappers (concurrency, repeating).
        This allows the user to easily restore a given searcher.

        The save operation is atomic (write/swap).

        Args:
            dirpath: Filepath to experiment dir.
            session_str: Unique identifier of the current run
                session.
        r   zDThere was a duplicate when saving {}. Restore may not work properly.name:z.tmp_search_generator_ckpt)rb   checkpoint_dir	file_nametmp_file_nameN)r   ra   hasattrr#   __name__r;   warningr?   save_to_dirr   ri   )r)   re   rk   r   search_alg_statesearcher_namebase_searchers          r   rt   zSearchGenerator.save_to_dir   s    =>>++h
++ 		) NN3M 00055;VM5J5J   
 =E<N<N<P<P =!89(H h
++ 		) ! 	!!';777"")00==6		
 	
 	
 	
 	
 	
r   c                    | j         }t          || j                            d                    }|s"t	          d                    |                    t          |d          rdt          |          j        z   }||vr;d |D             }t          	                    d                    ||                     n(|
                    |                    |                     |j         }t          |d          |}t                              d|            |                    |           | 
                    |           dS )	z8Restores self + searcher + search wrappers from dirpath.rg   z Unable to find checkpoint in {}.r   rm   c                 n    g | ]2}|                     d           |                    d           d         3S )rm   r8   )
startswithsplit).0keys     r   
<listcomp>z4SearchGenerator.restore_from_dir.<locals>.<listcomp>   sK       ~~g..IIg&&q)  r   zB{} was not found in the experiment state when restoring. Found {}.zsearching base N)r   r   ri   r?   RuntimeErrorrq   r#   rr   r;   rs   rd   popr<   restore_from_dir)r)   re   r   ru   rv   namesrw   s          r   r   z SearchGenerator.restore_from_dir   si    =2T(//44
 
   	SAHHQQRRRh
++ 	)#d8nn&==M$444 /  
 66<f]E6R6R   
 ""#3#7#7#F#FGGG(H h
++ 	) !6}66777&&w///'(((((r   )NF)rr   
__module____qualname____doc__ri   r   r*   propertyr-   r   rP   r   rh   r3   r   r   r   r   rB   rF   r	   rE   rW   rZ   rD   ra   rd   rj   rt   r    r   r   r    r       s;       	 	 0N	 	 	 	 	 $ $ X$
sm
+3C=
BF
	
 
 
 
 # # X#P T*-=tCI!NOP P P P,   %    68 8T 8 8 8 8
 KPW WW%-d^WCGW W W W
FT F F F F
4 
 
 
 
/t / / / /Wc W W W W"
3 "
S "
 "
 "
 "
H) ) ) ) ) ) )r   r    )%rN   loggingtypingr   r   r   r   ray.tune.errorr   ray.tune.experimentr   r	   r
   !ray.tune.experiment.config_parserr   r    ray.tune.search.search_algorithmr   ray.tune.search.searcherr   ray.tune.search.utilr   !ray.tune.search.variant_generatorr   r   ray.tune.utils.utilr   r   r   r   ray.util.annotationsr   	getLoggerrr   r;   r   r    r   r   r   <module>r      s     . . . . . . . . . . . . $ $ $ $ $ $ N N N N N N N N N N S S S S S S S S < < < < < < - - - - - - L L L L L L O O O O O O O O            . - - - - -		8	$	$/ / / @) @) @) @) @)o @) @) @) @) @)r   