
    &`i!                     P   d dl Z d dl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 ZddZedk    rj e j                    Ze                    d	d
dd           e                                \  ZZej        r ej        d            eej                   dS dS )    N)tune)
Checkpoint)PopulationBasedTrainingc                 ,   | d         }|                      dd          }d}d}t          j                    }|r|                                5 }t	          t
          j                            |d          d          5 }t          j	        |          }ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |d         }|d	         }	|	dz   }d
}
d}d}	 ||
k     r	d|z  |
z  }ndd||
z
  z  |
z  z
  }t          dt          d|                    }t          ||          t          ||          z  }||k     r|d|z  t          j                    z  z  }n"||k    r|||z
  t          j                    z  z  }||t          j                                        z  z  }t          d|          }||||||
dz  k    d}||z  dk    rt          j                    5 }t	          t
          j                            |d          d          5 }||d}t          j        ||           ddd           n# 1 swxY w Y   t          j        |t'          j        |                     ddd           n# 1 swxY w Y   nt          j        |           |dz  })a  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.
    lrcheckpoint_interval   g        zcheckpoint.jsonrNaccstepd         Tg{Gz?gMbP?g      ?r   )mean_accuracycur_lr
optimal_lrq_errdonew)r   r   )
checkpoint)getr   get_checkpointas_directoryopenospathjoinjsonloadminmaxrandomnpnormaltempfileTemporaryDirectorydumpreportr   from_directory)configr   r   accuracyr   r   checkpoint_dirfcheckpoint_dict	last_stepmidpointq_tolerancenoise_levelr   r   metricstempdirs                    r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/examples/pbt_function.pypbt_functionr6      s   * 
B **%:A>>H D$&&J $$&& 	/.bgll>3DEEsKK /q"&)A,,/ / / / / / / / / / / / / / /	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ #5)#F+	1} HKK&h83JJ8(; <x GGJs5*5566
 B
##c"j&9&99;u77HH*__,??HK")"2"2"4"444q(## &$x!|+
 
 %%** ,.. T'"',,w0ABBCHH 2A.6&E&EOIoq1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 G
0I'0R0RSSSS	T T T T T T T T T T T T T T T K   	M&sl   /B3;BB3B 	 B3#B 	$B33B7:B7/I/	H0$I/0H4	4I/7H4	8+I//I36I3Fc                    d}t          d|t          j        dd          ddgd          }t          j        t          t          j        d	d
d| rdnddt          j        d          t          j        dd                    t          j        |dddd          dd|d          }|	                                }t          d|                                j                   d S )N   training_iterationg-C6?g{Gz?r	   r   )r   some_other_factor)	time_attrperturbation_intervalhyperparam_mutationspbt_function_api_exampleFT
   i  )r   r9   )	fail_fastr   )checkpoint_score_attributenum_to_keep)nameverbosestopfailure_configcheckpoint_configr!      )	schedulermetricmodenum_samplesreuse_actors)r   r:   r   )
run_configtune_configparam_spacez!Best hyperparameters found were: )r   r   uniformTunerr6   	RunConfigFailureConfigCheckpointConfig
TuneConfigfitprintget_best_resultr*   )
smoke_testr<   pbttunerresultss        r5   run_tune_pbtr^   j   s?   
!&3 ,vt,,"#Q	
 
	 	 	C J>+ ,6&@bbD	   -   #3+:  
 
 
" O"
 
 
  "# $9!
 
5, , ,EZ iikkG	
-w/F/F/H/H/OPPPPP    __main__z--smoke-test
store_truezFinish quickly for testing)actiondefaulthelpr   )num_cpus)rZ   )F)argparser   r   r"   r%   numpyr#   rayr   ray.tuner   ray.tune.schedulersr   r6   r^   __name__ArgumentParserparseradd_argumentparse_known_argsargs_rZ   init r_   r5   <module>rt      sJ     				       



             7 7 7 7 7 7V V Vr<Q <Q <Q <Q~ z$X$&&F
)	     %%''GD! !LDO,,,,,, r_   