
    &`in?                     &   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
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 d dlmZ d dlmZ d d	lmZmZ 	 d dlZ n# e!$ r dZ Y nw xY werd d
l"m#Z#  ej$        e%          Z&d Z' G d de          Z(dS )    N)defaultdict)TYPE_CHECKINGAnyDictListOptionalTuple)DEFAULT_METRIC)UNDEFINED_METRIC_MODEUNDEFINED_SEARCH_SPACEUNRESOLVED_SEARCH_SPACESearcher)DomainFloat	QuantizedUniform)parse_spec_vars)flatten_dict)is_nan_or_infunflatten_dict)ExperimentAnalysisc                     t          |           }|                                D ]4\  }}t          |t                    rd                    ||          ||<   5t          j        |dt                    }|S )Nz{:.{digits}f})digitsT)	sort_keysdefault)r   items
isinstancefloatformatjsondumpsstr)config	precision
flatconfigparamvaluehasheds         |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/bayesopt/bayesopt_search.py
_dict_hashr*      s|    f%%J"((** P PueU## 	P / 6 6uY 6 O OJuZ
dC@@@FM    c                       e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 d+dee         d	ee         d
ee         deee                  dee         de	de	de	de	de
ded         f fdZd Zd	ee         d
ee         dede
fdZdedee         fdZd,dZ	 d-dedee         de
fdZd ee         defd!Zdeeef         fd"Zd#eeef         fd$Zd%efd&Zd%efd'Zed.d(ed)e
defd*            Z xZS )/BayesOptSearchaU  Uses bayesian-optimization/BayesianOptimization to optimize hyperparameters.

    bayesian-optimization/BayesianOptimization is a library for Bayesian Optimization. More
    info can be found here: https://github.com/bayesian-optimization/BayesianOptimization.

    This searcher will automatically filter out any NaN, inf or -inf
    results.

    You will need to install bayesian-optimization/BayesianOptimization via the following:

    .. code-block:: bash

        pip install bayesian-optimization==1.4.3

    Initializing this search algorithm with a ``space`` requires that it's
    in the ``BayesianOptimization`` search space format. Otherwise, you
    should instead pass in a Tune search space into ``Tuner(param_space=...)``,
    and the search space will be automatically converted for you.

    See this ``BayesianOptimization`` example notebook
    <https://github.com/bayesian-optimization/BayesianOptimization/blob/33b99ec0a4fc51239e1a2fca3eaa37ad6debac5d/examples/advanced-tour.ipynb>`_
    for an example.

    Args:
        space: Continuous search space. Parameters will be sampled from
            this space which will be used to run trials.
        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.
        utility_kwargs: Parameters to define the utility function.
            The default value is a dictionary with three keys:
            - kind: ucb (Upper Confidence Bound)
            - kappa: 2.576
            - xi: 0.0
        random_state: Used to initialize BayesOpt.
        random_search_steps: Number of initial random searches.
            This is necessary to avoid initial local overfitting
            of the Bayesian process.
        verbose: Sets verbosity level for BayesOpt packages.
        patience: If patience is set and we've repeated a trial numerous times,
            we terminate the experiment.
        skip_duplicate: skip duplicate config
        analysis: Optionally, the previous analysis to integrate.

    Tune automatically converts search spaces to BayesOptSearch's format:

    .. code-block:: python

        from ray import tune
        from ray.tune.search.bayesopt import BayesOptSearch

        config = {
            "width": tune.uniform(0, 20),
            "height": tune.uniform(-100, 100)
        }

        bayesopt = BayesOptSearch(metric="mean_loss", mode="min")
        tuner = tune.Tuner(
            my_func,
            tune_config=tune.TuneConfig(
                search_alg=baysopt,
            ),
            param_space=config,
        )
        tuner.fit()

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

    .. code-block:: python

        from ray import tune
        from ray.tune.search.bayesopt import BayesOptSearch

        space = {
            'width': (0, 20),
            'height': (-100, 100),
        }
        bayesopt = BayesOptSearch(space, metric="mean_loss", mode="min")
        tuner = tune.Tuner(
            my_func,
            tune_config=tune.TuneConfig(
                search_alg=bayesopt,
            ),
        )
        tuner.fit()

    N*   
   r      Tspacemetricmodepoints_to_evaluateutility_kwargsrandom_staterandom_search_stepsverbosepatienceskip_duplicateanalysisr   c                 t   t           
J d            |r|dv s
J d            t          t                    | _        |	| _        d| _        | j        dk    rt          d          |
| _        t          t          |           
                    ||           |t          dd	d
          }|dk    rd| _        n|dk    rd| _        || _        i | _        g | _        || _        d| _        t          j        di || _        || _        t+          |t                    rk|rit-          |          \  }}}|s|rRt.                              t3          j        dt7          |                                |                     |d          }|| _        || _        || _        d | _         |r| !                                 d S d S )NzsBayesOpt must be installed!. You can install BayesOpt with the command: `pip install bayesian-optimization==1.4.3`.)minmaxz`mode` must be 'min' or 'max'.r0   r   z/patience must be set to a value greater than 0!r2   r3   ucbgS㥛@g        )kindkappaxir>         ?r=         r1   )parclsTjoin )"byor   int_config_counter	_patiencerepeat_float_precision
ValueError_skip_duplicatesuperr-   __init__dict
_metric_op_points_to_evaluate_live_trial_mapping_buffered_trial_resultsrandom_search_trials_total_random_search_trialsUtilityFunctionutility	_analysisr   r   loggerwarningr   r   typeconvert_search_space_space_verbose_random_state	optimizer_setup_optimizer)selfr1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   resolved_varsdomain_vars	grid_vars	__class__s                  r)   rS   zBayesOptSearch.__init__   s    H   	L>)))+K)))*3//!&'#>QNOOO-nd##,, 	- 	
 	
 	

 ! "  N 5==!DOOU]]"DO#5 #% ')$$7!+,(*<<^<<!eT"" 	Du 	D4CE4J4J1M;	 Di D+2wDJJOOO   11%d1CC) 	$!!#####	$ 	$r+   c                     | j         | j        rt          | _         t          j        d | j        | j        | j                  | _        | j	        | 
                    | j	                   d S d S )N)fpboundsr8   r6   )_metric_moder
   rK   BayesianOptimizationrb   rc   rd   re   r]   register_analysis)rg   s    r)   rf   zBayesOptSearch._setup_optimizer   sr    <DJ)DL1KM+	
 
 
 >%""4>22222 &%r+   r#   returnc                     | j         rdS |                     |          }|| _        |r|| _        |r|| _        | j        dk    rd| _        n| j        dk    rd| _        |                                  dS )NFr>   rD   r=   rE   T)re   ra   rb   ro   rp   rU   rf   )rg   r2   r3   r#   specr1   s         r)   set_search_propertiesz$BayesOptSearch.set_search_properties   s     > 	5))&11 	"!DL 	DJ:!DOOZ5  "DOtr+   trial_idc                    | j         s-t          t          j        | j        j        d                    | j        r| j        s8t          t          j        | j        j        | j        | j                            | j	        r| j	        
                    d          }n| j                             | j                  }t          || j                  }|| j        v }| j        |xx         dz  cc<   t!          | j                                                  }| j        || j        k    rt&          j        S |r6| j        r/t,                              d                    |                     dS t1          | j                  | j        k     r$| j        | j        k    rdS |r| xj        dz  c_        || j        |<   t;          |          S )aO  Return new point to be explored by black box function.

        Args:
            trial_id: Id of the trial.
                This is a short alphanumerical string.

        Returns:
            Either a dictionary describing the new point to explore or
            None, when no new point is to be explored for the time being.
        r1   )rG   r1   )rG   r2   r3   r      NzSkipping duplicated config: {}.)re   RuntimeErrorr   r   rk   __name__ro   rp   r   rV   popsuggestr\   r*   rO   rM   r>   valuesrN   r   FINISHEDrQ   r^   infolenrX   rY   rZ   rW   r   )rg   rw   r#   config_hashalready_seentop_repeatss         r)   r}   zBayesOptSearch.suggest   s    ~ 	&-/w     | 	4: 	%,/4:     # 	:-11!44FF ^++DL99F )DEE"d&::[)))Q.)))$.557788 >%+*F*F$$ 	D0 	KK9@@HHIII4 t+,,t/HHH /43LLLt 600A500 .4 * f%%%r+   c                    t          |                    | j        | j                                                  |                                                                          D ]\  \  }}}|                     ||           dS )zIntegrate the given analysis into the gaussian process.

        Args:
            analysis: Optionally, the previous analysis
                to integrate.
        r?   N)zip	dataframero   rp   iterrowsget_all_configsr~   _register_result)rg   r;   _reportparamss        r)   rr   z BayesOptSearch.register_analysis:  s     $'dlDDMMOO$$&&--//$
 $
 	2 	2KQ !!&&1111	2 	2r+   Fresulterrorc                 r   | j                             |d          }|||rdS t          | j                  | j        k    r|                     ||           dS | j                            ||f           t          | j                  | j        k    r#| j        D ]\  }}|                     ||           dS dS )a{  Notification for the completion of trial.

        Args:
            trial_id: Id of the trial.
                This is a short alphanumerical string.
            result: Dictionary of result.
                May be none when some error occurs.
            error: Boolean representing a previous error state.
                The result should be None when error is True.
        N)rW   r|   r   rX   rY   r   append)rg   rw   r   r   r   s        r)   on_trial_completez BayesOptSearch.on_trial_completeI  s     )--h== >V^u^F t+,,0III!!&&111F 	$++VV,<=== t+,,0III"&"> 6 6%%ff5555 JI6 6r+   r   c                     t          || j                           rdS | j                            || j        || j                 z             dS )z+Register given tuple of params and results.N)r   r2   re   registerrU   )rg   r   r   s      r)   r   zBayesOptSearch._register_resultp  sJ    ,-- 	F&:M(MNNNNNr+   c                 8    | j                                         }|S N)__dict__copyrg   states     r)   	get_statezBayesOptSearch.get_statev  s    ""$$r+   r   c                 :    | j                             |           d S r   )r   updater   s     r)   	set_statezBayesOptSearch.set_statez  s    U#####r+   checkpoint_pathc                     |                                  }t          |d          5 }t          j        ||           ddd           dS # 1 swxY w Y   dS )z Storing current optimizer state.wbN)r   openpickledump)rg   r   save_objectrm   s       r)   savezBayesOptSearch.save}  s    nn&&/4(( 	(AKQ'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   AAAc                    t          |d          5 }t          j        |          }ddd           n# 1 swxY w Y   t          |t                    r|                     |           dS |\  | _        | _        | _        | _	        | _
        dS )z"Restoring current optimizer state.rbN)r   r   loadr   rT   r   re   rX   rZ   rM   rV   )rg   r   rm   r   s       r)   restorezBayesOptSearch.restore  s    /4(( 	)A +a..K	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) k4(( 
	NN;''''' ,0$(((s   266ru   rI   c                 .   t          |           \  }}}|rt          d          t          | d          } t          |           \  }}}dt          dt          t
          t
          f         fdfd|D             }|r|                     |           | }|S )NzTGrid search parameters cannot be automatically converted to a BayesOpt search space.T)prevent_delimiterdomainrs   c                    |                                  }t          |t                    r.t                              d           |                                 }t          | t
                    r\| j        Gt          | j        t                    s-t                              d                    |                     | j	        | j
        fS t          d                    t          |           j                            )NzDBayesOpt search does not support quantization. Dropped quantization.zTBayesOpt does not support specific sampling methods. The {} sampler will be dropped.z1BayesOpt does not support parameters of type `{}`)get_samplerr   r   r^   r_   r   samplerr   r   lowerupperrP   r`   r{   )r   r   s     r)   resolve_valuez:BayesOptSearch.convert_search_space.<locals>.resolve_value  s    ((**G'9-- 0,   "--//&%(( 4>-jNG7 7- NN::@&//   fl33vd6ll344  r+   c                 T    i | ]$\  }}d                      |           |          %S )/rH   ).0pathr   r   s      r)   
<dictcomp>z7BayesOptSearch.convert_search_space.<locals>.<dictcomp>  s3    XXXLD&#((4..--"7"7XXXr+   )r   rP   r   r   r	   r   r   )ru   rI   rh   ri   rj   boundsr   s         @r)   ra   z#BayesOptSearch.convert_search_space  s    0?0E0E-{I 	.   DD9990?0E0E-{I	& 	U5%<-@ 	 	 	 	2 YXXXKXXX 	KKFr+   )NNNNNr.   r/   r   r0   TN)r;   r   )NF)F)r{   
__module____qualname____doc__re   r   r   r"   r   rL   boolrS   rf   rv   r}   rr   r   r	   r   r   r   r   r   r   staticmethodra   __classcell__)rk   s   @r)   r-   r-   )   s       ^ ^B I !% $"37)-#%#37G$ G$~G$ G$ sm	G$
 %T$Z0G$ !G$ G$ !G$ G$ G$ G$ /0G$ G$ G$ G$ G$ G$R3 3 3 sm+3C=BF	   (>& >& >& >& >& >&@2 2 2 2  KP%6 %6%6%-d^%6CG%6 %6 %6 %6NOuSz O4 O O O O4S>    $tCH~ $ $ $ $(C ( ( ( (s    " , ,4 ,t , , , , \, , , , ,r+   r-   ))r    loggingr   collectionsr   typingr   r   r   r   r   r	   ray.tune.resultr
   ray.tune.searchr   r   r   r   ray.tune.search.sampler   r   r   r   !ray.tune.search.variant_generatorr   ray.tune.utilsr   ray.tune.utils.utilr   r   	bayes_optrK   ImportErrorray.tuner   	getLoggerr{   r^   r*   r-   rJ   r+   r)   <module>r      s      # # # # # # B B B B B B B B B B B B B B B B * * * * * *            E D D D D D D D D D D D = = = = = = ' ' ' ' ' ' = = = = = = = =   
CCC  ,++++++		8	$	$  X X X X XX X X X X Xs   A A%$A%