
    &`i                     l   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  G d dej                  Z	e
dk    r e j                    Ze                    ddd	
           e                                \  ZZej        r ej        d           dZ eded ddgd          Z ej        e	 ej        ddej        rdnddd  ej        edd                     ej        edddd          ddd           Ze                                Z ed!e                                j                   dS dS )"    N)tune)PopulationBasedTrainingc                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )PBTBenchmarkExamplea  Toy PBT problem for benchmarking adaptive learning rate.

    The goal is to optimize this trainable's accuracy. The accuracy increases
    fastest at the optimal lr, which is a function of the current accuracy.

    The optimal lr schedule for this problem is the triangle wave as follows.
    Note that many lr schedules for real models also follow this shape:

     best lr
      ^
      |    /      |   /        |  /          | /            ------------> accuracy

    In this problem, using PBT with a population of 2-4 is sufficient to
    roughly approximate this lr schedule. Higher population sizes will yield
    faster convergence. Training will not converge without PBT.
    c                 .    |d         | _         d| _        d S )Nlrg        )r   accuracy)selfconfigs     q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/examples/pbt_example.pysetupzPBTBenchmarkExample.setup#   s    ,    c                    d}d}d}| j         |k     rd| j         z  |z  }ndd| j         |z
  z  |z  z
  }t          dt          d|                    }t          | j        |          t          | j        |          z  }||k     r(| xj         d|z  t	          j                    z  z  c_         n2| j        |k    r'| xj         ||z
  t	          j                    z  z  c_         | xj         |t
          j                                        z  z  c_         t          d| j                   | _         | j         | j        ||| j         |dz  k    dS )	Nd         g{Gz?gMbP?g      ?r   )mean_accuracycur_lr
optimal_lrq_errdone)r	   minmaxr   randomnpnormal)r
   midpointq_tolerancenoise_levelr   r   s         r   stepzPBTBenchmarkExample.step'   sI   
 =8##-8JJ(@ AH LLJs5*5566
 DGZ((3tw
+C+CC;MMcEkV]__<<MMMWz!!MMek1V]__DDMMry'7'7'9'999At}-- "]g$MHqL0
 
 	
r   c                      | j         | j        dS )Nr	   r   r"   )r
   checkpoint_dirs     r   save_checkpointz#PBTBenchmarkExample.save_checkpointF   s    '
 
 	
r   c                      |d         | _         d S )Nr	   )r	   )r
   
checkpoints     r   load_checkpointz#PBTBenchmarkExample.load_checkpointL   s    ":.r   c                 .    |d         | _         || _        dS )Nr   T)r   r   )r
   
new_configs     r   reset_configz PBTBenchmarkExample.reset_configO   s    T" tr   N)	__name__
__module____qualname____doc__r   r    r$   r'   r*    r   r   r   r      si         *  
 
 
>
 
 
/ / /    r   r   __main__z--smoke-test
store_truezFinish quickly for testing)actionhelpr   )num_cpus   training_iterationc                  ,    t          j        dd          S )N-C6?g{Gz?)r   uniformr/   r   r   <lambda>r:   e   s    &.66 r      )r   some_other_factor)	time_attrperturbation_intervalhyperparam_mutationspbt_class_api_exampleT
   i  )r   r6   r      )checkpoint_frequencycheckpoint_score_attributenum_to_keep)namestopverbosecheckpoint_configr      )	schedulermetricmodereuse_actorsnum_samplesr8   )
run_configtune_configparam_spacez!Best hyperparameters found were: )argparser   numpyr   rayr   ray.tune.schedulersr   	Trainabler   r+   ArgumentParserparseradd_argumentparse_known_argsargs_
smoke_testinitr>   pbtTuner	RunConfigCheckpointConfig
TuneConfigtunerfitresultsprintget_best_resultr   r/   r   r   <module>rj      s1         



       7 7 7 7 7 7E E E E E$. E E EP z$X$&&F
|2N     %%''GD! !
!
!&3 76"#Q	
 
	 	 	C DJ!4>( ,0O&Ebb   4d3%:+:  
 
 
* $DO"
 
 
  "#	
 
=$ $ $EJ iikkG	E
-w/F/F/H/H/OPPPPP{ r   