
    &`iR                        d dl Z d dlZd dlmZ d dl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 d dlmZmZmZmZ d dlmZmZ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j$        d          Z%e%&                    ej'                   d dl(Z)d dl*m+Z+ n# e,$ r dZ)dZ+Y nw xY w ej$        e-          Z.dZ/ G d de          Z0dS )    N)partial)AnyDictListOptional)cloudpickle)	TuneError)DEFAULT_METRIC)UNDEFINED_METRIC_MODEUNDEFINED_SEARCH_SPACEUNRESOLVED_SEARCH_SPACESearcher)CategoricalDomainFloatInteger
LogUniformNormal	QuantizedUniform)assign_valueparse_spec_vars)flatten_dicthyperopt)Applyz This issue can also come up with HyperOpt if your search space only contains constant variables, which is not supported by HyperOpt. In that case, don't pass any searcher or add sample variables to the search space.c                       e Zd ZdZ	 	 	 	 	 	 	 d&dee         dee         dee         deee                  d	ed
ee         de	f fdZ
d'dZd'dZdee         dee         dedefdZdedee         fdZdededdfdZ	 d(dedee         deddfdZdededdfdZdedefdZdedee         fdZdefdZdeddfdZdeddfd Zdeddfd!Zed)d#ed$edefd%            Z xZS )*HyperOptSearcha  A wrapper around HyperOpt to provide trial suggestions.

    HyperOpt a Python library for serial and parallel optimization
    over awkward search spaces, which may include real-valued, discrete,
    and conditional dimensions. More info can be found at
    http://hyperopt.github.io/hyperopt.

    HyperOptSearch uses the Tree-structured Parzen Estimators algorithm,
    though it can be trivially extended to support any algorithm HyperOpt
    supports.

    To use this search algorithm, you will need to install HyperOpt:

    .. code-block:: bash

        pip install -U hyperopt


    Parameters:
        space: HyperOpt configuration. Parameters will be sampled
            from this configuration and will be used to override
            parameters generated in the variant generation process.
        metric: The training result objective value attribute. If None
            but a mode was passed, the anonymous metric `_metric` will be used
            per default.
        mode: One of {min, max}. Determines whether objective is
            minimizing or maximizing the metric attribute.
        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.
        n_initial_points: number of random evaluations of the
            objective function before starting to approximate it with
            tree parzen estimators. Defaults to 20.
        random_state_seed: seed for reproducible
            results. Defaults to None.
        gamma: parameter governing the tree parzen
            estimators suggestion algorithm. Defaults to 0.25.

    Tune automatically converts search spaces to HyperOpt's format:

    .. code-block:: python

        config = {
            'width': tune.uniform(0, 20),
            'height': tune.uniform(-100, 100),
            'activation': tune.choice(["relu", "tanh"])
        }

        current_best_params = [{
            'width': 10,
            'height': 0,
            'activation': "relu",
        }]

        hyperopt_search = HyperOptSearch(
            metric="mean_loss", mode="min",
            points_to_evaluate=current_best_params)

        tuner = tune.Tuner(
            trainable,
            tune_config=tune.TuneConfig(
                search_alg=hyperopt_search
            ),
            param_space=config
        )
        tuner.fit()

    If you would like to pass the search space manually, the code would
    look like this:

    .. code-block:: python

        space = {
            'width': hp.uniform('width', 0, 20),
            'height': hp.uniform('height', -100, 100),
            'activation': hp.choice("activation", ["relu", "tanh"])
        }

        current_best_params = [{
            'width': 10,
            'height': 0,
            'activation': "relu",
        }]

        hyperopt_search = HyperOptSearch(
            space, metric="mean_loss", mode="min",
            points_to_evaluate=current_best_params)

        tuner = tune.Tuner(
            trainable,
            tune_config=tune.TuneConfig(
                search_alg=hyperopt_search
            ),
        )
        tuner.fit()

    N         ?spacemetricmodepoints_to_evaluaten_initial_pointsrandom_state_seedgammac                 Z   t           
J d            |r|dv s
J d            t          t          |                               ||           |dk    rd| _        n|dk    rd| _        |t           j        j        | _        n%t          t           j        j        |	          | _        |t          | j        |
          | _        t          j
        |          | _        i | _        t          j                            |          | _        d | _        t%          |t&                    r|rt)          |          \  }}	}
|	s|
rPt*                              t/          j        dt3          |                                |                     |          }|| _        |                                  d S d S d S )Nz7HyperOpt must be installed! Run `pip install hyperopt`.)minmaxz`mode` must be 'min' or 'max'.)r!   r"   r)         r(         ?)n_startup_jobs)r&   r    )parcls)hposuperr   __init__	metric_optpesuggestalgor   copydeepcopy_points_to_evaluate_live_trial_mappingnprandomRandomStaterstatedomain
isinstancedictr   loggerwarningr   formattypeconvert_search_space_space_setup_hyperopt)selfr    r!   r"   r#   r$   r%   r&   resolved_varsdomain_vars	grid_vars	__class__s              |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/hyperopt/hyperopt_search.pyr1   zHyperOptSearch.__init__   s    OOD OO 	L>)))+K)))nd##,, 	- 	
 	
 	

 5==!DNNU]] DN#DII@PQQQDI	777DI#'=1C#D#D #% i++,=>>eT"" 	#u 	#4CE4J4J1M;	 9i 9+2wDJJOOO   11%88DK  """""	# 	# 	# 	#    returnc                    ddl m} | j        s5t          t	          j        | j        j        d          t          z             | j	        | j
        rt          | _	        | j         t          j                    | _        d| _        nt!          | j        t"          t$          f          sJ t'          t)          | j                            D ]$}| j        |         }|                     |           %t#          t-          | j                            | _         || j                  | _        | j                                         t)          | j                  | _        t          j        d | j                  | _        d S )Nr   )generate_trials_to_calculater    r.   r    c                     | S )N )spcs    rM   <lambda>z0HyperOptSearch._setup_hyperopt.<locals>.<lambda>   s    S rN   )hyperopt.fminrQ   rF   RuntimeErrorr   rC   rL   __name__HYPEROPT_UNDEFINED_DETAILS_metric_moder
   r8   r/   Trials_hpopt_trialsr?   listtuplerangelen_convert_categories_to_indicesreversedrefreshr   r>   )rH   rQ   iconfigs       rM   rG   zHyperOptSearch._setup_hyperopt   sj   >>>>>>{ 	&-/w   --   <DJ)DL#+!$D'(D$$d6uFFFFF3t78899 < <1!433F;;;;'+HT5M,N,N'O'OD$!=!=d>V!W!WD&&((('*4+C'D'DD$j$+>>rN   c                 @    fd|D ]} || j         |           dS )zConvert config parameters for categories into hyperopt-compatible
        representations where instead the index of the category is expected.c                    t          | |         t                    r&| |         D ]} | |         ||         |           d S ||v rt          ||         t          j        j        j                  r||         j        dk    rt          ||         j                  dk    rd ||         j        dd          D             }	 |	                    | |                   }nV# t          $ rI}d| |          d| d}t          | |         t                    r|dz  }|d	z  }t          |          |d }~ww xY w|| |<   d S d S d S d S d S )
Nswitchr   c                 2    g | ]}|j         d k    |j        S )literal)nameobj).0as     rM   
<listcomp>zRHyperOptSearch._convert_categories_to_indices.<locals>._lookup.<locals>.<listcomp>   s1     & & & ! v22 E222rN      z"Did not find category with value `z` in hyperopt parameter `z`. zIn previous versions, a numerical index was expected for categorical values of `points_to_evaluate`, but in ray>=1.2.0, the categorical value is expected to be directly provided. z1Please make sure the specified category is valid.)r?   r@   r/   basepyllr   rm   rb   pos_argsindex
ValueErrorint)	config_dict
space_dictkeyk
categoriesidxexcmsg_lookups	           rM   r   z>HyperOptSearch._convert_categories_to_indices.<locals>._lookup   s   +c*D11 $/$S) B BAGK,joqAAAAB B :%%":c?CHM4GHH &"3,88:c?344q88& &%/_%=abb%A& & &

;","2"2;s3C"D"DCC) ; ; ;!@$/$4!@ !@7:!@ !@ !@    *+c*:C@@ " #%1!"  #VVC",S//s:%;& ,/C(((? &%%%8888s    C 
D/&AD**D/N)rF   )rH   rg   r|   r   s      @rM   rc   z-HyperOptSearch._convert_categories_to_indices   sQ    %	/ %	/ %	/ %	/ %	/N  	, 	,AGFDK++++	, 	,rN   rg   c                     | j         rdS |                     |          }|| _        |r|| _        |r|| _        | j        dk    rdnd| _        |                                  dS )NFr)   r*   r+   T)r>   rE   rF   r[   r\   r2   rG   )rH   r!   r"   rg   specr    s         rM   set_search_propertiesz$HyperOptSearch.set_search_properties  sy     ; 	5))&11 	"!DL 	DJ!%u!4!4#trN   trial_idc                    | j         s5t          t          j        | j        j        d          t          z             | j        r| j        s8t          t          j        | j        j        | j        | j                            | j
        dk    r-d}| j        j        | j
        dz
           }| xj
        dz  c_
        nd}| j                            d          }| j                                         |                     || j         | j        | j                            d                    }| j                            |           | j                                         |d         }|d	         |f| j        |<   t(          j                            |d
                   }t/          |d          }t(          j                            | j        |          }| j                             |          }t(          j                            | j         j        |t(          j        j        |           	 t(          j                            | j         j        || j         j                  }	n^# t@          tB          f$ rJ}
|rAtE          |
t@                    sdtG          |
          v rtI          d| d| j%                   |
|
d }
~
ww xY wtM          j'        |	          S )Nr    rR   )r.   r!   r"   r   Trr   Fitidmisc)flatten_list)current_trial)memoprint_node_on_errorGarbageCollecteda  HyperOpt encountered a GarbageCollected switch argument. Usually this is caused by a config in `points_to_evaluate` missing a key present in `space`. Ensure that `points_to_evaluate` contains all non-constant keys from `space`.
Config from `points_to_evaluate`: z
HyperOpt search space: )(r>   rX   r   rC   rL   rY   rZ   r[   r\   r   r8   r^   trialsnew_trial_idsre   r5   r=   randintinsert_trial_docsr9   r/   rs   spec_from_miscr   Ctrlmemo_from_configutilsuse_obj_for_literal_in_memoexprrt   rec_evalrec_eval_print_node_on_errorAssertionError	TypeErrorr?   strrw   rF   r6   r7   )rH   r   using_point_to_evaluate	new_trialnew_ids
new_trialsrg   ctrlr   suggested_configes              rM   r4   zHyperOptSearch.suggest(  s   { 	&-/w   --   | 	4: 	%,/4:     #a''&*#*1$2JQ2NOI$$)$$$&+#(66q99G&&((( "##I..	 J 00<<<&&((("1I.7.>	-J * ((6):;; f4888x}}T/y}II{++F33	--KdCHM4	
 	
 	
	"x00 $(K$L  1    
 	* 	 	 	& 1n--1Cs1vv1M1M 	% 	% 	% {	% 	%   G!	" }-...s   )6I   J;1AJ66J;resultc                     |                      |          }|d S t          j                                        }||d<   ||d<   d S )N	book_timerefresh_time)_get_hyperopt_trialr/   r   coarse_utcnow)rH   r   r   ho_trialnows        rM   on_trial_resultzHyperOptSearch.on_trial_resultq  sM    ++H55Fi%%'' ##&   rN   Ferrorc                 X   |                      |          }|dS t          j                                        |d<   |rMt          j        j        |d<   t          t                    df|d         d<   | j        	                                 n|r| 
                    ||           | j        |= dS )zNotification for the completion of trial.

        The result is internally negated when interacting with HyperOpt
        so that HyperOpt can "maximize" this value, as it minimizes on default.
        Nr   statez
Tune Errorr   r   )r   r/   r   r   rs   JOB_STATE_ERRORr   r	   r^   re   _process_resultr9   )rH   r   r   r   r   s        rM   on_trial_completez HyperOptSearch.on_trial_completey  s     ++H55F#&9#:#:#<#<  	3 # 8HW),Y(FHVW%&&(((( 	3  6222$X...rN   c                    |                      |          }|sd S t          j                                        |d<   t          j        j        |d<   |                     |          }||d<   | j                                         d S )Nr   r   r   )	r   r/   r   r   rs   JOB_STATE_DONE_to_hyperopt_resultr^   re   )rH   r   r   r   	hp_results        rM   r   zHyperOptSearch._process_result  s    ++H55 	F#&9#:#:#<#< H3,,V44	&""$$$$$rN   c                     	 | j         || j                 z  ddS # t          $ r}t          d| j         d          |d }~ww xY w)Nok)lossstatusz%Hyperopt expected to see the metric `z` in the last result, but it was not found. To fix this, make sure your call to `tune.report` or your return value of your trainable class `step()` contains the above metric as a key.)r2   r!   KeyErrorrX   )rH   r   r   s      rM   r   z"HyperOptSearch._to_hyperopt_result  sq    		 NVDK-@@DQQQ 	 	 	     	s    
A=Ac                 |    || j         vrd S | j         |         d         fd| j        j        D             d         S )Nr   c                 ,    g | ]}|d          k    |S )r   rT   )ro   thyperopt_tids     rM   rq   z6HyperOptSearch._get_hyperopt_trial.<locals>.<listcomp>  s'    QQQa%L8P8P8P8P8PrN   )r9   r^   r   )rH   r   r   s     @rM   r   z"HyperOptSearch._get_hyperopt_trial  sN    4333F/9!<QQQQ4-4QQQRSTTrN   c                 D    | j         | j                                        dS )N)hyperopt_trialsr=   )r^   r=   	get_state)rH   s    rM   r   zHyperOptSearch.get_state  s(    #1k++--
 
 	
rN   r   c                 `    |d         | _         | j                            |d                    d S )Nr   r=   )r^   r=   	set_state)rH   r   s     rM   r   zHyperOptSearch.set_state  s0    "#45eHo.....rN   checkpoint_pathc                     | j                                         }| j                                        |d<   t	          |d          5 }t          j        ||           d d d            d S # 1 swxY w Y   d S )N__rstatewb)__dict__r6   r=   r   openr   dump)rH   r   save_objectfs       rM   savezHyperOptSearch.save  s    m((**"&+"7"7"9"9J/4(( 	-A[!,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   A))A-0A-c                 B   t          |d          5 }t          j        |          }d d d            n# 1 swxY w Y   d|vr|                     |           d S | j                            |                    d                     | j                            |           d S )Nrbr   )r   r   loadr   r=   popr   update)rH   r   r   r   s       rM   restorezHyperOptSearch.restore  s    /4(( 	.A%*1--K	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. [((NN;'''''K!!+//*"="=>>>M  -----s   266 r   prefixc                 R  	 t          j        |           } t          |           \  }}}|s|si S |rt          d          dt          dt
          dt          f	fd	|D ]I\  }}d                    d |r|f|z   n|D                       } 	||          }t          | ||           J| S )NzTGrid search parameters cannot be automatically converted to a HyperOpt search space.r-   r>   rO   c           
          d }|                                 }t          |t                    r|j        }|j        }t          |t
                    rt          |t                    r|rPt          j        	                     t          j        |j                  t          j        |j                  |          S t          j                             t          j        |j                  t          j        |j                            S t          |t                    rY|r,t          j                             |j        |j        |          S t          j                             |j        |j                  S t          |t$                    rY|r,t          j                             |j        |j        |          S t          j                             |j        |j                  S n$t          |t.                    rt          |t                    r|rwt          j        j        j                            t          j        	                     t          j        |j                  t          j        |j                  |                    S t          j        j        j                            t          j        	                     t          j        |j                  t          j        |j        dz
            d                    S t          |t                    r|rVt          j        j        j                            t          j                             |j        |j        dz
  |                    S t          j                             |j        |j        dz
            S nit          |t:                    rTt          |t                    r?t          j                              fdt?          |j                   D                       S tC          d"                    tG          |          j$        tG          |j                  j$                            )Nrr   r+   )highc           	         g | ]\  }}t          |t                    rt                              |           nt          |t                    rit          |          dk    rVt          |d         t                    r;t                              t          t          |                     d|            n't          |t                    r  d| |          n|S ))r   r   /)r?   r@   r   rE   r_   rb   r   	enumerate)ro   rf   categoryr-   resolve_values      rM   rq   zNHyperOptSearch.convert_search_space.<locals>.resolve_value.<locals>.<listcomp>  s      ( !,8 $.h#=#=!" C C$,S !D !" !" !" (2(D'A'A
%& ),H(9(9(28A;(G(G ):	 %3$G$G(,Yx-@-@(A(AS,,ST,, %H %& %& %& ,6h+G+G)6llqllH(M(M(M-5!  rN   zLHyperOpt does not support parameters of type `{}` with samplers of type `{}`)%get_samplerr?   r   qsamplerr   r   r/   hpqloguniformr:   loglowerupper
loguniformr   quniformuniformr   qnormalmeansdnormalr   rs   rt   scoperx   
uniformintr   choicer   r}   rw   rC   rD   rY   )r-   r>   quantizer   r   s   `   rM   r   z:HyperOptSearch.convert_search_space.<locals>.resolve_value  s   H((**G'9-- *"9!/&%(( Fgz22 H "v11!5!5rvfl7K7KX     6,,RVFL1126&,3G3G    11 	H "vv|X     6>>#v|V\JJJ00 H W"v~~c7<XVVV6==glGJGGGH
 FG,, 2gz22 W "x}266F.. # "v| 4 4 "v| 4 4 (	      8=.22**!5!5rvflQ>N7O7OQT   
  11 W "x}266FOO #V\6<!3CX     
 6,,S&,V\TUEU,VVVW FK00 gw// 6==    ( 099J/K/K)    4 228&LL)4+?+?+H3 3  rN   r   c                 ,    g | ]}t          |          S rT   )r   )ro   ps     rM   rq   z7HyperOptSearch.convert_search_space.<locals>.<listcomp>+  s    SSSqCFFSSSrN   )	r6   r7   r   rw   r   r   r   joinr   )
r   r   rI   rJ   rK   pathr>   r-   valuer   s
            @rM   rE   z#HyperOptSearch.convert_search_space  s   }T""0?0E0E-{I 	9 	I 	.  
U	s U	F U	s U	 U	 U	 U	 U	 U	n ( 	, 	,LD&((SS-QfY-=-=TSSSTTC!M#v..EtU++++rN   )NNNNr   Nr   )rO   N)NF)r   )rY   
__module____qualname____doc__r   r   r   r   rx   floatr1   rG   rc   boolr   r4   r   r   r   r   r   r   r   r   r   staticmethodrE   __classcell__)rL   s   @rM   r   r   3   s       b bL !% $"37 "+/.# .#~.# .# sm	.#
 %T$Z0.# .# $C=.# .# .# .# .# .# .#`? ? ? ?@,, ,, ,, ,,\sm+3C=BF	   $G/ G/ G/ G/ G/ G/R' 'T 'd ' ' ' ' KP/ //%-d^/CG/	/ / / /(	% 	%T 	%d 	% 	% 	% 	%
$ 
4 
 
 
 
UC UHTN U U U U
4 
 
 
 
/t / / / / /-C -D - - - -	.s 	.t 	. 	. 	. 	. i i4 i id i i i \i i i i irN   r   )1r6   logging	functoolsr   typingr   r   r   r   numpyr:   rayr   ray.tune.errorr	   ray.tune.resultr
   ray.tune.searchr   r   r   r   ray.tune.search.sampler   r   r   r   r   r   r   r   !ray.tune.search.variant_generatorr   r   ray.tune.utilsr   	getLoggerhyperopt_loggersetLevelWARNINGr   r/   hyperopt.pyllr   ImportErrorrY   rA   rZ   r   rT   rN   rM   <module>r     s2           , , , , , , , , , , , ,           $ $ $ $ $ $ * * * * * *           	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 L K K K K K K K ' ' ' ' ' ''g'
33OW_---#######   
CEEE
 
	8	$	$K | | | | |X | | | | |s    4B 	B! B!