o
    ci                  
   @   s>  d Z 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	 ddl
mZ ddlmZ G dd de	Zed	krddlZejd
krHed ejdd deddeddeddgdZdZededddZe ZejjeddZejeejddeidejddeedded Ze  Z!e"d!e!# j dS dS )"zThis example demonstrates the usage of BOHB with Ray Tune.

Requires the HpBandSter and ConfigSpace libraries to be installed
(`pip install hpbandster ConfigSpace`).
    N)tune)	Trainable)HyperBandForBOHB)TuneBOHBc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )MyTrainableClasszExample agent whose learning curve is a random sigmoid.

    The dummy hyperparameters "width" and "height" determine the slope and
    maximum reward value reached.
    c                 C   s
   d| _ d S )Nr   )timestep)selfconfig r
   R/home/ubuntu/.local/lib/python3.10/site-packages/ray/tune/examples/bohb_example.pysetup   s   
zMyTrainableClass.setupc                 C   sP   |  j d7  _ tt| j | jdd }|| jdd9 }td d|iS )N   widthheightg?episode_reward_mean)r   nptanhfloatr	   gettimesleep)r   vr
   r
   r   step    s
   
zMyTrainableClass.stepc                 C   sT   t j|d}t|d}|td| ji W d    d S 1 s#w   Y  d S )N
checkpointwr   )ospathjoinopenwritejsondumpsr   r   checkpoint_dirr   fr
   r
   r   save_checkpoint)   s   "z MyTrainableClass.save_checkpointc                 C   sR   t j|d}t|d}t| d | _W d    d S 1 s"w   Y  d S )Nr   rr   )r   r   r   r   r    loadsreadr   r"   r
   r
   r   load_checkpoint.   s   "z MyTrainableClass.load_checkpointN)__name__
__module____qualname____doc__r   r   r%   r)   r
   r
   r
   r   r      s    	r   __main__)         )num_cpusd      irelur   )
iterationsr   r   
activation
   training_iteration   F)	time_attrmax_treduction_factorstop_last_trials   )max_concurrent	bohb_test)namestopr   max    )metricmode	scheduler
search_algnum_samples)
run_configtune_configparam_spacez!Best hyperparameters found were: )$r-   r    r   r   numpyr   rayr   ray.tuner   ray.tune.schedulers.hb_bohbr   ray.tune.search.bohbr   r   r*   sysversion_infoexitinituniformchoicer	   max_iterationsbohb_hyperbandbohb_searchsearchConcurrencyLimiterTuner	RunConfig
TuneConfigtunerfitresultsprintget_best_resultr
   r
   r
   r   <module>   s^   



