o
    $i                     @   s   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 e!Z"dd Z#eG d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                 C   s   ddl m} || | d S )Nr   )_warn_num_samples)ray.tune.search.repeaterr   )searchertotal_samplesr    r   ]/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/search/search_generator.py_warn_on_repeater   s   r   c                   @   s&  e Zd ZdZdZdefddZedd Zde	e
 d	e	e
 d
edefddZedd Zdeeee ee
ef f fddZdd Zdede	e fddZde
defddZ	d0de
de	e defdd Zdefd!d"Zdefd#d$Zd%efd&d'Zd(e
fd)d*Zd(e
d+e
fd,d-Zd(e
fd.d/ZdS )1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                 C   s@   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   r   r   r   __init__,   s   
zSearchGenerator.__init__c                 C   s   | j jS N)r   metricr%   r   r   r   r(   7   s   zSearchGenerator.metricr(   modeconfigreturnc                 K   s   t | jj|||fi |S r'   )r   r   set_search_properties)r%   r(   r*   r+   specr   r   r   r-   ;   s
   z%SearchGenerator.set_search_propertiesc                 C   s   | j S r'   )r#   r)   r   r   r   r   B   s   zSearchGenerator.total_samplesexperimentsc                 C   s|   | 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	   lenr.   getr#   r   r   r   format)r%   r/   experiment_listexperiment_specr   r   r   add_configurationsF   s   


z"SearchGenerator.add_configurationsc                 C   s   |   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!   r.   r)   r   r   r   
next_trial\   s   zSearchGenerator.next_trialr9   c                 C   s   t d t }| j|}|tjkrd| _t d d S |d u r$d S t	
|}t|d t	
||d< t|d }|  jd7  _dt| jt|}t|| jt|||d}|S )Nzcreating trialTzSearcher has finished.r+   r0   z{0}_{1})evaluated_paramsexperiment_tagtrial_id)r3   r4   r   generate_idr   suggestr   FINISHEDr$   copydeepcopyr   r   r"   r7   strr   r
   r    r   )r%   r9   r@   suggested_configr.   flattened_configtagtrialr   r   r   r<   f   s,   



z(SearchGenerator.create_trial_if_possibler@   resultc                 C   s   | j || dS )z!Notifies the underlying searcher.N)r   on_trial_result)r%   r@   rK   r   r   r   rL      s   zSearchGenerator.on_trial_resultNFerrorc                 C   s   | j j|||d d S )N)r@   rK   rM   )r   on_trial_complete)r%   r@   rK   rM   r   r   r   rN      s   z!SearchGenerator.on_trial_completec                 C   s   | j | jkp| jS r'   )r"   r#   r$   r)   r   r   r   r;      s   zSearchGenerator.is_finishedc                 C   s   | j | j| j| jdS )N)counterr   finished
experimentr"   r#   r$   r!   r)   r   r   r   	get_state   s
   zSearchGenerator.get_statestatec                 C   s,   |d | _ |d | _|d | _|d | _d S )NrO   r   rP   rQ   rR   )r%   rT   r   r   r   	set_state   s   


zSearchGenerator.set_statedirpathc                 C   s   t t|| jdS )N*)boolr   CKPT_FILE_TMPLr7   )r%   rV   r   r   r   has_checkpoint   s   zSearchGenerator.has_checkpointsession_strc                 C   s   | j }|  }t|dr.t|j}||v rtd| n| |d| < |j }t|ds|}||| t	||| j
|d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)rT   checkpoint_dir	file_nametmp_file_nameN)r   rS   hasattrr   __name__r3   warningr7   save_to_dirr   rY   )r%   rV   r[   r   search_alg_statesearcher_namebase_searcherr   r   r   rc      s(   





zSearchGenerator.save_to_dirc                 C   s   | j }t|| jd}|std|t|drFdt|j }||vr6dd |D }t	d|| n|
|| |j }t|ds|}td|  || | 
| d	S )
z8Restores self + searcher + search wrappers from dirpath.rW   z Unable to find checkpoint in {}.r   r\   c                 S   s$   g | ]}| d r|d d qS )r\   r0   )
startswithsplit).0keyr   r   r   
<listcomp>   s    z4SearchGenerator.restore_from_dir.<locals>.<listcomp>zB{} was not found in the experiment state when restoring. Found {}.zsearching base N)r   r   rY   r7   RuntimeErrorr`   r   ra   r3   rb   rU   popr4   restore_from_dir)r%   rV   r   rd   re   namesrf   r   r   r   rn      s.   


z SearchGenerator.restore_from_dir)NF)ra   
__module____qualname____doc__rY   r   r&   propertyr(   r   rF   r   rX   r-   r   r   r   r   r:   r=   r   r<   rL   rN   r;   rS   rU   rZ   rc   rn   r   r   r   r   r      sJ    





$r   )%rD   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   	getLoggerra   r3   r   r   r   r   r   r   <module>   s     
