
    &`i<                     R   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ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mZ d dlmZ d d	lmZmZmZmZm Z m!Z! d d
l"m#Z#m$Z$ d dl%m&Z& erd dl'm(Z( dZ) G d d          Z* G d d          Z+e& G d de                      Z,dS )    N)Path)TYPE_CHECKINGDictListOptionalUnion)tag_searcher)	TuneError)_create_trial_from_spec_make_parser)_BackwardsCompatibleNumpyRngnp_random_generator)SearchAlgorithm)_count_spec_samples_count_variants_flatten_resolved_vars_get_preset_variantsformat_varsgenerate_variants)_atomic_save_load_newest_checkpoint)	PublicAPI)
Experimentg    .Ac                   ,    e Zd ZdZddZd Zd Zd ZdS )	_VariantIteratorzIterates over generated variants from the search space.

    This object also toggles between lazy evaluation and
    eager evaluation of samples. If lazy evaluation is enabled,
    this object cannot be serialized.
    Fc                     || _         || _        d| _        |r|                                  d S t	          |          | _        t          | j                  | _        d S )NT)	lazy_evaliterable	_has_next_load_valuelistbool)selfr   r   s      q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/basic_variant.py__init__z_VariantIterator.__init__)   sY    "  	1 NNDM!$-00DNNN    c                 j    	 t          | j                  | _        d S # t          $ r d| _        Y d S w xY wNF)nextr   
next_valueStopIterationr   r#   s    r$   r    z_VariantIterator._load_value3   sD    	#"4=11DOOO 	# 	# 	#"DNNNN	#s    22c                     | j         S N)r   r,   s    r$   has_nextz_VariantIterator.has_next9   s
    ~r&   c                     | j         r| j        }|                                  |S | j                            d          }t          | j                  | _        |S )Nr   )r   r*   r    r   popr"   r   )r#   current_values     r$   __next__z_VariantIterator.__next__<   sW    > 	! OM  ))!,,dm,,r&   N)F)__name__
__module____qualname____doc__r%   r    r/   r3    r&   r$   r   r   !   s_         1 1 1 1# # #      r&   r   c                       e Zd ZdZ	 	 	 	 	 ddedededed	ee	         d
ededee
edej        j        f                  fdZd Zd Zd ZdS )_TrialIteratoraG  Generates trials from the spec.

    Args:
        uuid_prefix: Used in creating the trial name.
        num_samples: Number of samples from distribution
             (same as tune.TuneConfig).
        unresolved_spec: Experiment specification
            that might have unresolved distributions.
        constant_grid_search: Should random variables be sampled
            first before iterating over grid variants (True) or not (False).
        points_to_evaluate: Configurations that will be tried out without sampling.
        lazy_eval: Whether variants should be generated
            lazily or eagerly. This is toggled depending
            on the size of the grid search.
        start: index at which to start counting trials.
        random_state (int | np.random.Generator | np.random.RandomState):
            Seed or numpy random generator to use for reproducible results.
            If None (default), will use the global numpy random generator
            (``np.random``). Please note that full reproducibility cannot
            be guaranteed in a distributed environment.
    FNr   uuid_prefixnum_samplesunresolved_specconstant_grid_searchpoints_to_evaluater   startrandom_stater   c	                     t                      | _        || _        || _        || _        || _        || _        |pg | _        t          | j                  | _	        || _
        || _        d | _        || _        d S r.   )r   parserr<   r;   num_samples_leftr=   r>   r?   lennum_points_to_evaluatecounterr   variantsrA   )	r#   r;   r<   r=   r>   r?   r   r@   rA   s	            r$   r%   z_TrialIterator.__init__]   sz     #nn&& +.$8!"4":&)$*A&B&B#"(r&   c                    | j         d| j        z  z   }t          | j                  }|r%|d                    t	          |                    z  }| xj        dz  c_        t          || j        t          |          ||          S )Nz%05dz_{}   )evaluated_paramstrial_idexperiment_tag)r;   rG   strformatr   r   rC   r   )r#   resolved_varsspecrL   rM   s        r$   create_trialz_TrialIterator.create_trialw   s    #v'<=T\** 	Gell;}+E+EFFFN&K3MBB)
 
 
 	
r&   c                 @   d| j         vr't          d                    | j                             | j        rF| j                                        r-t          | j                  \  }}|                     ||          S | j        r| j                            d          }| xj	        dz  c_	        t          t          | j         || j        | j                  | j                  | _        t          | j                  \  }}|                     ||          S | j	        dk    rwt          t          | j         | j        | j                  | j                  | _        | xj	        dz  c_	        t          | j                  \  }}|                     ||          S t           )a  Generates Trial objects with the variant generation process.

        Uses a fixed point iteration to resolve variants. All trials
        should be able to be generated at once.

        See also: `ray.tune.search.variant_generator`.

        Returns:
            Trial object
        runzMust specify `run` in {}r   rJ   )r>   rA   r   )r=   r
   rO   rH   r/   r)   rR   r?   r1   rD   r   r   r>   rA   r   r   r+   )r#   rP   rQ   configs       r$   r3   z_TrialIterator.__next__   s    ,,,6==d>RSSTTT= 	:T]3355 	: #'t}"5"5M4$$]D999" 	 ,0033F!!Q&!!,$()-)B!%!2	   .  DM #'t}"5"5M4$$]D999"Q&&,!()-)B!%!2  
 .  DM !!Q&!!"&t}"5"5M4$$]D999r&   c                     | S r.   r8   r,   s    r$   __iter__z_TrialIterator.__iter__   s    r&   )FNFr   N)r4   r5   r6   r7   rN   intdictr"   r   r   r   nprandomRandomStater%   rR   r3   rX   r8   r&   r$   r:   r:   F   s         6 &+-1 ) )) ) 	)
 #) %TN) ) ) #,bi.CCD
) ) ) )4
 
 
0  0  0 d    r&   r:   c                   &   e Zd ZdZdZ	 	 	 	 ddeee                  dede	d	ee
ed
ej        j        f                  fdZed             Zde
ded         eeef         f         fdZd Z	 ddedee         de	fdZd Zd Zd ZdefdZdefdZdS )BasicVariantGeneratora  Uses Tune's variant generation for resolving variables.

    This is the default search algorithm used if no other search algorithm
    is specified.


    Args:
        points_to_evaluate: Initial parameter suggestions to be run
            first. This is for when you already have some good parameters
            you want to run first to help the algorithm make better suggestions
            for future parameters. Needs to be a list of dicts containing the
            configurations.
        max_concurrent: Maximum number of concurrently running trials.
            If 0 (default), no maximum is enforced.
        constant_grid_search: If this is set to ``True``, Ray Tune will
            *first* try to sample random values and keep them constant over
            grid search parameters. If this is set to ``False`` (default),
            Ray Tune will sample new random parameters in each grid search
            condition.
        random_state:
            Seed or numpy random generator to use for reproducible results.
            If None (default), will use the global numpy random generator
            (``np.random``). Please note that full reproducibility cannot
            be guaranteed in a distributed environment.


    Example:

    .. code-block:: python

        from ray import tune

        # This will automatically use the `BasicVariantGenerator`
        tuner = tune.Tuner(
            lambda config: config["a"] + config["b"],
            tune_config=tune.TuneConfig(
                num_samples=4
            ),
            param_space={
                "a": tune.grid_search([1, 2]),
                "b": tune.randint(0, 3)
            },
        )
        tuner.fit()

    In the example above, 8 trials will be generated: For each sample
    (``4``), each of the grid search variants for ``a`` will be sampled
    once. The ``b`` parameter will be sampled randomly.

    The generator accepts a pre-set list of points that should be evaluated.
    The points will replace the first samples of each experiment passed to
    the ``BasicVariantGenerator``.

    Each point will replace one sample of the specified ``num_samples``. If
    grid search variables are overwritten with the values specified in the
    presets, the number of samples will thus be reduced.

    Example:

    .. code-block:: python

        from ray import tune
        from ray.tune.search.basic_variant import BasicVariantGenerator

        tuner = tune.Tuner(
            lambda config: config["a"] + config["b"],
            tune_config=tune.TuneConfig(
                search_alg=BasicVariantGenerator(points_to_evaluate=[
                    {"a": 2, "b": 2},
                    {"a": 1},
                    {"b": 2}
                ]),
                num_samples=4
            ),
            param_space={
                "a": tune.grid_search([1, 2]),
                "b": tune.randint(0, 3)
            },
        )
        tuner.fit()

    The example above will produce six trials via four samples:

    - The first sample will produce one trial with ``a=2`` and ``b=2``.
    - The second sample will produce one trial with ``a=1`` and ``b`` sampled
      randomly
    - The third sample will produce two trials, one for each grid search
      value of ``a``. It will be ``b=2`` for both of these trials.
    - The fourth sample will produce two trials, one for each grid search
      value of ``a``. ``b`` will be sampled randomly and independently for
      both of these trials.

    zbasic-variant-state-{}.jsonNr   Fr?   max_concurrentr>   rA   r   c                    t          |            g | _        g | _        d | _        d| _        t          |          | _        |pg | _        t          j	        
                    d          }|r|dz   | _        n5t          t          j                    j                  d d         dz   | _        d| _        || _        || _        t'                      | _        d S )NF_TEST_TUNE_TRIAL_UUID_   r   )r	   _trial_generator
_iterators_trial_iter	_finishedr   _random_state_points_to_evaluateosenvironget_uuid_prefixrN   uuiduuid1hex_total_samplesr`   _constant_grid_searchset_live_trials)r#   r?   r`   r>   rA   force_test_uuids         r$   r%   zBasicVariantGenerator.__init__  s     	T "9,GG#5#;  *..)@AA 	@ /# 5D #DJLL$4 5 5bqb 9C ?D,%9"EEr&   c                     | j         S r.   )rr   r,   s    r$   total_samplesz#BasicVariantGenerator.total_samples=  s    ""r&   experimentsr   c                 V   ddl m}  ||          }|D ]}t          |j        d          }|t          k    }|r-t          j        d| dt          t                     d           | j        }t          j
        | j                  }| xj        t          |j        |          z  c_        t          | j        |j                            dd          |j        | j        |||| j        	          }	| j                            |	           t)          j        | j        |	          | _        d
S )z{Chains generator given experiment specifications.

        Arguments:
            experiments: Experiments to run.
        r   )_convert_to_experiment_listrJ   )r<   z%The number of pre-generated samples (z') exceeds the serialization threshold (zm). Resume ability is disabled. To fix this, reduce the number of dimensions/size of the provided grid search.r<   )r;   r<   r=   r>   r?   r   r@   rA   N)ray.tune.experimentr{   r   rQ   SERIALIZATION_THRESHOLDwarningswarnrY   rr   copydeepcopyrj   r   r:   rn   rm   rs   ri   rf   append	itertoolschainre   )
r#   ry   r{   experiment_list
experiment	grid_valsr   previous_samplesr?   iterators
             r$   add_configurationsz(BasicVariantGenerator.add_configurationsA  sk    	DCCCCC55kBB) 	U 	UJ+JOKKKI!$;;I CI C C344C C C    $2!%t/G!H!H?:?DV#W#WW% -&O//qAA *%)%?#5#&!/	 	 	H O""8,,,$-OD4I8$T$TD!!5	U 	Ur&   c                    |                                  rdS | j        dk    rt          | j                  | j        k    rdS | j        st          | j                  | _        	 t          | j                  }| j                            |j	                   |S # t          $ r& g | _        d| _        |                                  Y dS w xY w)zProvides one Trial object to be queued into the TrialRunner.

        Returns:
            Trial: Returns a single trial.
        Nr   )is_finishedr`   rE   ru   rg   iterre   r)   addrL   r+   set_finished)r#   trials     r$   
next_trialz BasicVariantGenerator.next_triali  s      	4""s4+<'='=AT'T'T4 	;#D$9::D	)**E!!%.111L 	 	 	$&D!#D44		s   "4B ,CCrL   resulterrorc                 P    || j         v r| j                             |           d S d S r.   )ru   remove)r#   rL   r   r   s       r$   on_trial_completez'BasicVariantGenerator.on_trial_complete  s7     t((($$X..... )(r&   c                 ~    t          d | j        D                       rdS | j                                        }|d= |S )Nc              3   $   K   | ]}|j         V  d S r.   rU   .0r   s     r$   	<genexpr>z2BasicVariantGenerator.get_state.<locals>.<genexpr>  %      BBhx!BBBBBBr&   Fre   )anyrf   __dict__r   )r#   states     r$   	get_statezBasicVariantGenerator.get_state  sI    BB$/BBBBB 	5""$$$%r&   c                     | j                             |           | j        D ]!}t          j        | j        |          | _        "d S r.   )r   updaterf   r   r   re   )r#   r   r   s      r$   	set_statezBasicVariantGenerator.set_state  sQ    U### 	U 	UH$-OD4I8$T$TD!!	U 	Ur&   c                     t          d | j        D                       rdS |                                 }t          ||| j                            |          d           d S )Nc              3   $   K   | ]}|j         V  d S r.   rU   r   s     r$   r   z4BasicVariantGenerator.save_to_dir.<locals>.<genexpr>  r   r&   Fz.tmp_generator)r   checkpoint_dir	file_nametmp_file_name)r   rf   r   r   CKPT_FILE_TMPLrO   )r#   dirpathsession_str
state_dicts       r$   save_to_dirz!BasicVariantGenerator.save_to_dir  su    BB$/BBBBB 	5^^%%
")00==*		
 	
 	
 	
 	
 	
r&   r   c                     t          t          |                              | j                            d                              S )z0Whether a checkpoint file exists within dirpath.*)r   r   globr   rO   )r#   r   s     r$   has_checkpointz$BasicVariantGenerator.has_checkpoint  s5    4==%%d&9&@&@&E&EFFGGGr&   c                     t          || j                            d                    }|s"t          d                    |                    |                     |           dS )z8Restores self + searcher + search wrappers from dirpath.r   z Unable to find checkpoint in {}.N)r   r   rO   RuntimeErrorr   )r#   r   r   s      r$   restore_from_dirz&BasicVariantGenerator.restore_from_dir  sa    ,Wd6I6P6PQT6U6UVV
 	SAHHQQRRRz"""""r&   )Nr   FNr(   )r4   r5   r6   r7   r   r   r   r   rY   r"   r   r[   r\   r]   r%   propertyrx   rN   r   r   r   r   r   r   r   r   r8   r&   r$   r_   r_      s       \ \| 3N 48%* " "$T$Z0" " #	"
 #,bi.CCD
" " " "> # # X#&U tL/A4T	?!RS&U &U &U &UP  . KP/ //%-d^/CG/ / / /  U U U
	
 	
 	
Hc H H H H# # # # # # #r&   r_   )-r   r   rk   ro   r~   pathlibr   typingr   r   r   r   r   numpyr[   ray.air._internal.usager	   ray.tune.errorr
   !ray.tune.experiment.config_parserr   r   ray.tune.search.sampler   r    ray.tune.search.search_algorithmr   !ray.tune.search.variant_generatorr   r   r   r   r   r   ray.tune.utils.utilr   r   ray.utilr   r|   r   r}   r   r:   r_   r8   r&   r$   <module>r      s$        				         = = = = = = = = = = = = = =     0 0 0 0 0 0 $ $ $ $ $ $ S S S S S S S S T T T T T T T T < < < < < <                F E E E E E E E       /...... " " " " " " " "Js s s s s s s sl h# h# h# h# h#O h# h# h# h# h#r&   