
    &`it                        d dl mZmZmZmZ d dlZd dlZd dlZ	d dl
m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 d dlmZmZ erd d	lmZ d
ZddZdefdZd Zedk    r( ej        d            e            Z  ee           Z!dS dS )    )TYPE_CHECKINGAnyDictOptionalN)train_test_split)tune)PlacementGroupFactory)Trial)TuneReportCheckpointCallback)ASHASchedulerResourceChangingScheduler)TuneControllerzbooster-checkpoint.jsonbest_resultray.tune.Resultc                     t          j        | j        t                    }d| j        d         z
  }t          d| j                    t          d|d           |S )Nfilenameg      ?eval-loglosszBest model parameters: zBest model total accuracy: z.4f)r   	get_model
checkpointCHECKPOINT_FILENAMEmetricsprintconfig)r   best_bstaccuracys      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/examples/xgboost_dynamic_resources_example.pyget_best_model_checkpointr      so    +5)<  H [(88H	
8K$6
8
8999	
6
6
6
6777O    r   c                 D   t           j                            d          \  }}t          ||d          \  }}}}t	          j        ||          }t	          j        ||          }d }	t          j                    }
|
rt          j	        |
t                    }	t          t          j                                                    j                  | d<   t          d| d          d	|	            t	          j        | ||d
fgd|	t          dt                    g           d S )NT)
return_X_yg      ?)	test_size)labelr   nthreadz
nthreads: z xgb_model: evalF   )	frequencyr   )evalsverbose_eval	xgb_model	callbacks)sklearndatasetsload_breast_cancerr   xgbDMatrixr   get_checkpointr   r   r   intget_contextget_trial_resources	head_cpusr   train)r   datalabelstrain_xtest_xtrain_ytest_y	train_settest_setr*   r   s              r   train_breast_cancerr?   "   sJ    #66$6GGLD&'7fPT'U'U'U$GVWfG7333I{6000H I$&&J 
0:!4
 
 
	 D,..BBDDNOOF9	
Avi(
A
Ai
A
ABBBI&!"( ,	  
     r   c                  N   dddgddt          j        ddg          t          j        dd	g          t          j        dd	g          dd
d	} t          ddd          }dddt          dt          t
          t          f         dddt          t                   f
d}t          ||          }t          j
        t          j        t          t          dddg                    t          j        ddd|          |           }|                                }|                                S )Nzbinary:logisticloglosserror	   r&         g?g?i  )		objectiveeval_metric	max_depthlearning_ratemin_child_weight	subsamplecolsample_bynoderandom_statenum_parallel_tree   )max_tgrace_periodreduction_factortune_controllerr   trialresult	schedulerr   returnc                 H   |j         }|d         dk     rdS |t          dddg          }|j                            dd          }| j                                        }t          ||t          |                                           z            }t          |ddg          S )aO  This is a basic example of a resource allocating function.

        The function naively balances available CPUs over live trials.

        This function returns a new ``PlacementGroupFactory`` with updated
        resource requirements, or None. If the returned
        ``PlacementGroupFactory`` is equal by value to the one the
        trial has currently, the scheduler will skip the update process
        internally (same with None).

        See :class:`DistributeResources` for a more complex,
        robust approach.

        Args:
            tune_controller: Trial runner for this Tune run.
                Can be used to obtain information about other trials.
            trial: The trial to allocate new resources to.
            result: The latest results of trial.
            scheduler: The scheduler calling the function.
        training_iterationr&   Nr   CPUGPUr[   )	_base_trial_resourcesr	   required_resourcesget_resource_updaterget_num_cpusmaxlenget_live_trials)rS   rT   rU   rV   base_trial_resourcemin_cputotal_available_cpus
cpu_to_uses           r   %example_resources_allocation_functionz;tune_xgboost.<locals>.example_resources_allocation_function]   s    : (= &'!++4 &"719M9M8N"O"O &8<<UAFF  /@MMOO )S1P1P1R1R-S-SS
 


 %j&C&C%DEEEr   )base_schedulerresources_allocation_functionr   rZ   )	resourcesr   min)metricmodenum_samplesrV   )tune_configparam_space)r   grid_searchr   r
   r   strr   r   r	   r   Tunerwith_resourcesr?   
TuneConfigfitget_best_result)search_spacerj   ri   rV   tunerresultss         r   tune_xgboostr}   K   s    '!7+ ,aV44%sCj11 ,c3Z88! L #q1  N4F)4F4F S#X4F /	4F
 
'	(4F 4F 4F 4Fx *%&K  I J+@!TUBVBVAW+X+X	
 	
 	
 O!	
 
 
 !  E iikkG""$$$r   __main__   )num_cpus)r   r   )"typingr   r   r   r   sklearn.datasetsr,   sklearn.metricsxgboostr/   sklearn.model_selectionr   rayr   #ray.tune.execution.placement_groupsr	   ray.tune.experimentr
   ray.tune.integration.xgboostr   ray.tune.schedulersr   r   "ray.tune.execution.tune_controllerr   r   r   dictr?   r}   __name__initr   r    r   r   <module>r      su   5 5 5 5 5 5 5 5 5 5 5 5             4 4 4 4 4 4 



       E E E E E E % % % % % % E E E E E E H H H H H H H H BAAAAAA/    & & & & &Rb% b% b%J zCHa,..K((55HHH	 r   