
    &`iA                        d dl Z d dlZd dlmZmZmZm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 d dlmZ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 	 d dl Z d dl!Z!n# e"$ r dZ Y nw xY w e j#        e$          Z%dZ& G d	 d
e          Z'dS )    N)DictListOptionalUnion)DEFAULT_METRIC)UNDEFINED_METRIC_MODEUNDEFINED_SEARCH_SPACEUNRESOLVED_SEARCH_SPACESearcher)CategoricalDomainFloatInteger
LogUniform	QuantizedUniform)parse_spec_vars)is_nan_or_infunflatten_dictvalidate_warmstartz[Space must be either a HEBO DesignSpace objector a dictionary with ONLY tune search spaces.c                       e Zd ZdZ	 	 	 	 	 	 	 d%deeedf                  dee         dee         deee                  d	ee         d
ee	         de	f fdZ
de	de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edee         defdZdedefdZ	 	 	 d'dededededeee                  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 ))
HEBOSearcha  Uses HEBO (Heteroscedastic Evolutionary Bayesian Optimization)
    to optimize hyperparameters.

    HEBO is a cutting edge black-box optimization framework created
    by Huawei's Noah Ark. More info can be found here:
    https://github.com/huawei-noah/HEBO/tree/master/HEBO.

    `space` can either be a HEBO's `DesignSpace` object or a dict of Tune
    search spaces.

    Please note that the first few trials will be random and used
    to kickstart the search process. In order to achieve good results,
    we recommend setting the number of trials to at least 16.

    Maximum number of concurrent trials is determined by ``max_concurrent``
    argument. Trials will be done in batches of ``max_concurrent`` trials.
    If this Searcher is used in a ``ConcurrencyLimiter``, the
    ``max_concurrent`` value passed to it will override the value passed
    here.

    Args:
        space: A dict mapping parameter names to Tune search spaces or a
            HEBO DesignSpace object.
        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.
        evaluated_rewards: If you have previously evaluated the
            parameters passed in as points_to_evaluate you can avoid
            re-running those trials by passing in the reward attributes
            as a list so the optimiser can be told the results without
            needing to re-compute the trial. Must be the same length as
            points_to_evaluate.
        random_state_seed: Seed for reproducible
            results. Defaults to None. Please note that setting this to a value
            will change global random states for `numpy` and `torch`
            on initalization and loading from checkpoint.
        max_concurrent: Number of maximum concurrent trials.
            If this Searcher is used in a ``ConcurrencyLimiter``, the
            ``max_concurrent`` value passed to it will override the
            value passed here.
        **kwargs: The keyword arguments will be passed to `HEBO()``.

    Tune automatically converts search spaces to HEBO's format:

    .. code-block:: python

        from ray import tune
        from ray.tune.search.hebo import HEBOSearch

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

        hebo = HEBOSearch(metric="mean_loss", mode="min")
        tuner = tune.Tuner(
            trainable_function,
            tune_config=tune.TuneConfig(
                search_alg=hebo
            ),
            param_space=config
        )
        tuner.fit()

    Alternatively, you can pass a HEBO `DesignSpace` object manually to the
    Searcher:

    .. code-block:: python

        from ray import tune
        from ray.tune.search.hebo import HEBOSearch
        from hebo.design_space.design_space import DesignSpace

        space_config = [
            {'name' : 'width', 'type' : 'num', 'lb' : 0, 'ub' : 20},
            {'name' : 'height', 'type' : 'num', 'lb' : -100, 'ub' : 100},
        ]
        space = DesignSpace().parse(space_config)

        hebo = HEBOSearch(space, metric="mean_loss", mode="min")
        tuner = tune.Tuner(
            trainable_function,
            tune_config=tune.TuneConfig(
                search_alg=hebo
            )
        )
        tuner.fit()

    N   spacez*hebo.design_space.design_space.DesignSpacemetricmodepoints_to_evaluateevaluated_rewardsrandom_state_seedmax_concurrentc                    t           
J d            |r|dv s
J d            t          |t                    r|dk    s
J d            |?t          |t                    s*J d                    t	          |                                t          t          |                               ||           t          |t                    r|r~t          |          \  }	}
}|	rt          t                    |
s|rPt                              t          j        dt	          |           	                     |                     |          }n]|[t          |t           j        j        j                  s7t          t          d
                    t	          |                    z             || _        || _        || _        || _        || _        g | _        i | _        || _        g | _        d| _        d | _        |r|                                  d S d S )NzHEBO must be installed! You can install HEBO with the command: `pip install 'HEBO>=0.2.0'`.This error may also be caused if HEBO dependencies have bad versions. Try updating HEBO first.)minmaxz`mode` must be 'min' or 'max'.   z3`max_concurrent` must be an integer and at least 1.z0random_state_seed must be None or int, got '{}'.)r   r   r   )parclsz Got {}.F)hebo
isinstanceintformattypesuperr   __init__dictr   	TypeErrorSPACE_ERROR_MESSAGEloggerwarningr
   convert_search_spacedesign_spaceDesignSpace_hebo_config_random_state_seed_space_points_to_evaluate_evaluated_rewards_initial_points_live_trial_mapping_max_concurrent_suggestions_cache_batch_filled_opt_setup_optimizer)selfr   r   r   r   r   r   r    kwargsresolved_varsdomain_vars	grid_vars	__class__s               t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/search/hebo/hebo_search.pyr-   zHEBOSearch.__init__   sO        	L>)))+K)))~s++	A0>!0C0C0C@ 1D0CC(!3   AHH&''   
 	j$((T(BBBeT"" 	Ru 	R4CE4J4J1M;	 5 3444 9i 9+2wDJJOOO   11%88z4$1=(
 (
 /*2C2CDKK2P2PPQQQ""3#5 "3!#% -"$"	 	$!!#####	$ 	$    returnc                     || _         dS )NT)r=   )rB   r    s     rH   set_max_concurrencyzHEBOSearch.set_max_concurrency   s    -trI   c                 |   | j         dk    rd| _        n| j         dk    rd| _        | j        | j         rt          | _        t	          | j        t          j        j        j                  s%t          dt          | j                   d          | j        j        dk    rt          d          | j        Ht          j                            | j                   t           j                            | j                   t          j        j        j        d
d	| j        i| j        | _        | j        rt/          | j        j        | j        | j                   | j        rS| j                            t7          j        | j                  t          j        | j                  | j        z             d S | j        | _        d S d S )Nr#   g      r"   g      ?zInvalid search space: zp. Either pass a valid search space to the `HEBOSearch` class or pass a `param_space` parameter to `tune.Tuner()`r   zqGot empty search space. Please make sure to pass a valid search space with at least one parameter to `HEBOSearch`r    )_mode
_metric_op_metricr   r(   r8   r'   r4   r5   
ValueErrorr+   	num_parasr7   nprandomseedtorchmanual_seed
optimizersHEBOr6   r@   r9   r   
para_namesr:   observepd	DataFramearrayr;   )rB   s    rH   rA   zHEBOSearch._setup_optimizer   s   :"DOOZ5  !DO<DJ)DL$+t'8'E'QRR 	?dk):): ? ? ?   ; A%%   ".INN42333L$$T%<===O(-UUDKU4CTUU	# 	@&('  
 & @	!!L!9::HT455G    
 (,'?$$$	@ 	@rI   configc                     | j         rdS |                     |          }|| _        |r|| _        |r|| _        |                                  dS )NFT)r@   r3   r8   rQ   rO   rA   )rB   r   r   r`   specr   s         rH   set_search_propertiesz HEBOSearch.set_search_properties   sb     9 	5))&11 	"!DL 	DJtrI   trial_idc                 2   | j         s-t          t          j        | j        j        d                    | j        r| j        s8t          t          j        | j        j        | j        | j                            | j	        sd| _
        | j        r3| j                            d          }t          j        |gdg          }n| j
        st          | j	                  | j        k    rd S | j        s:| j                             | j                  }|                    d          | _        | j                            d          }t          j        |gdg          }|| j	        |<   t          | j	                  | j        k    rd	| _
        t)          |          S )
Nr   )r&   r   )r&   r   r   Fr   )index)n_suggestionsrecordsT)r@   RuntimeErrorr	   r*   rG   __name__rQ   rO   r   r<   r?   r;   popr]   r^   lenr=   r>   suggestto_dictr   )rB   rd   params
suggestions       rH   rm   zHEBOSearch.suggest  s   y 	&-/w     | 	4: 	%,/4:     ' 	'!&D 	;)--a00Fvhqc:::JJ "t/00D4HHHt* H!Y..T=Q.RR
*4*<*<Y*G*G',0033Fvhqc:::J-7 *t'((D,@@@!%Df%%%rI   Fresulterrorc                 j    |r|                      ||           | j                            |           dS )zINotification for the completion of trial.

        HEBO always minimizes.N)_process_resultr<   rk   )rB   rd   rq   rr   s       rH   on_trial_completezHEBOSearch.on_trial_complete0  s?      	3  6222 $$X.....rI   c                     | j         |         }|r]t          || j                           sE| j                            |t          j        | j        || j                 z  g                     d S d S d S )N)r<   r   rQ   r@   r\   rT   r_   rP   )rB   rd   rq   
trial_infos       rH   rt   zHEBOSearch._process_result;  s    -h7
 	-t|(<== 	IBHdot|8L&L%MNN    	 	 	 	rI   
parametersvalueprunedintermediate_valuesc                 N    |rt                               d           |sk|si j                            t	          j         fd|                                D             g          t          j        |g           j	        z             d S t                               d           d S )Nz/HEBO doesn't use intermediate_values. Ignoring.c                 B    i | ]\  }}|j         j        j        v ||S rN   )r@   r   r[   ).0kvrB   s      rH   
<dictcomp>z2HEBOSearch.add_evaluated_point.<locals>.<dictcomp>P  s;        $1 DIO$>>> q>>>rI   z<Only non errored and non pruned points can be added to HEBO.)
r1   r2   r@   r\   r]   r^   itemsrT   r_   rP   )rB   rx   ry   rr   rz   r{   s   `     rH   add_evaluated_pointzHEBOSearch.add_evaluated_pointB  s      	NNNLMMM 	V 	I   (2(8(8(:(:    %!!DO3     NNN    rI   checkpoint_pathc                 B   | j         2t          j                                        }t	          j                    }nd}d}| j                                        }||d<   ||d<   t          |d          5 }t          j
        ||           ddd           dS # 1 swxY w Y   dS )z Storing current optimizer state.N__numpy_random_state__torch_random_statewb)r7   rT   rU   	get_staterW   get_rng_state__dict__copyopenpickledump)rB   r   numpy_random_statetorch_random_statesave_objectfs         rH   savezHEBOSearch.save^  s    ".!#!4!4!6!6!&!4!6!6!%!%m((**.@*+.@*+/4(( 	(AKQ'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   1BBBc                 (   t          |d          5 }t          j        |          }ddd           n# 1 swxY w Y   t          |t                    rG|                    dd          }|                    dd          }| j                            |           n5|\
  | _        | _	        }}| _
        | _        | _        | _        | _        | _        |t           j                            |           |!t&          j                            |           dS dS )z"Restoring current optimizer state.rbNr   r   )r   r   loadr(   r.   rk   r   updater@   r;   r<   r=   r>   r8   r6   r?   rT   rU   	set_staterW   set_rng_state)rB   r   r   r   r   r   s         rH   restorezHEBOSearch.restorel  sN   /4(( 	)A +a..K	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) k4(( 	!,1G!N!N!,1G!N!NM  ---- 	$""($'!")I 2333)L&&'9::::: *)s   266 rb   prefixc                    t          |           \  }}}g }|s|si S |rt          d          dt          dt          fd}|D ]M\  }}d                    d |r|f|z   n|D                       }	 ||	|          }
|                    |
           Nt          j        j                                        	                    |          S )NzPGrid search parameters cannot be automatically converted to a HEBO search space.r%   domainc                 6   |                                 }t          |t                    r.t                              d           |                                 }t          |t
                    rMt          |t                    r| d|j        |j        dS t          |t                    r| d|j        |j        dS nt          |t                    rSt          |t                    r| d|j        |j        dz
  dS t          |t                    r| d|j        |j        dz
  dS n-t          |t                    r| dt          |j                  d	S t          d
                    t!          |          j        t!          |j                  j                            )Nz@HEBO search does not support quantization. Dropped quantization.pow)namer+   lbubnumpow_intr$   r)   cat)r   r+   
categorieszHHEBO does not support parameters of type `{}` with samplers of type `{}`)get_samplerr(   r   r1   r2   r   r   lowerupperr   r   r   listr   rR   r*   r+   rj   sampler)r%   r   r   s      rH   resolve_valuez6HEBOSearch.convert_search_space.<locals>.resolve_value  s   ((**G'9-- 0,   "--//&%(( $gz22  # %$l$l	    11  # %$l$l	   FG,, gz22  # )$l$lQ.	    11  # %$l$lQ.	   FK00 !"&v'8"9"9   228&LL)4+?+?+H3 3  rI   /c                 ,    g | ]}t          |          S rN   )str)r~   ps     rH   
<listcomp>z3HEBOSearch.convert_search_space.<locals>.<listcomp>  s    SSSqCFFSSSrI   )
r   rR   r   r   joinappendr'   r4   r5   parse)rb   r   rD   rE   rF   ro   r   pathr   r%   ry   s              rH   r3   zHEBOSearch.convert_search_space  s   0?0E0E-{I 	9 	I 	*  
4	s 4	F 4	 4	 4	 4	l ( 	! 	!LD&((SS-QfY-=-=TSSSTTC!M#v..EMM%     -99;;AA&IIIrI   )NNNNNNr   )NF)FFN)r   )rj   
__module____qualname____doc__r   r   r   r   r   r)   r-   boolrL   rA   rc   rm   ru   rt   floatr   r   r   staticmethodr3   __classcell__)rG   s   @rH   r   r   )   s       _ _J  $"37,0+/=$ =$$DDE
=$
 =$ sm=$ %T$Z0=$ $D>=$ $C==$ =$ =$ =$ =$ =$ =$~# $    +@ +@ +@Zsm+3C=BF	    #& #& #& #& #& #&L KP	/ 	/	/%-d^	/CG	/ 	/ 	/ 	/ T     59   	
  &d5k2   8(C ( ( ( (;s ; ; ; ;8 IJ IJ4 IJ IJd IJ IJ IJ \IJ IJ IJ IJ IJrI   r   )(loggingr   typingr   r   r   r   numpyrT   pandasr]   ray.tune.resultr   ray.tune.searchr   r	   r
   r   ray.tune.search.sampler   r   r   r   r   r   r   !ray.tune.search.variant_generatorr   ray.tune.utils.utilr   r   r   r'   rW   ImportError	getLoggerrj   r1   r0   r   rN   rI   rH   <module>r      s     . . . . . . . . . . . .         * * * * * *                             > = = = = = Q Q Q Q Q Q Q Q Q QKKKLLLL   DDD 
	8	$	$4 iJ iJ iJ iJ iJ iJ iJ iJ iJ iJs   A A%$A%