o
    ciu                     @   s   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rJd d	lmZ d
ZdddZdefddZdd Zedkroejd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.train.Resultc                 C   sB   t j| jtd}d| jd  }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accuracy r   g/home/ubuntu/.local/lib/python3.10/site-packages/ray/tune/examples/xgboost_dynamic_resources_example.pyget_best_model_checkpoint   s   r   r   c              
   C   s   t jjdd\}}t||dd\}}}}tj||d}tj||d}d }	t }
|
r1tj	|
t
d}	tt  j| d< td| d  d	|	  tj| ||d
fgd|	tdt
dgd 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   r   r   r   train_breast_cancer"   s0   
r<   c                  C   s   dddgddt ddgt dd	gt dd	gdd
d	} tdddd}dddtdtttf dddtt f
dd}t	||d}t j
t jttdddgdt jdd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                 S   sl   |j }|d dk rdS |du rtdddg}|jdd}| j }t||t|   }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GPUrV   )	_base_trial_resourcesr   required_resourcesget_resource_updaterget_num_cpusmaxlenget_live_trials)rO   rP   rQ   rR   base_trial_resourcemin_cputotal_available_cpus
cpu_to_user   r   r   %example_resources_allocation_function]   s   
z;tune_xgboost.<locals>.example_resources_allocation_function)base_schedulerresources_allocation_functionr   rU   )	resourcesr   min)metricmodenum_samplesrR   )tune_configparam_space)r   grid_searchr   r	   r   strr   r   r   r   Tunerwith_resourcesr<   
TuneConfigfitget_best_result)search_spacere   rd   rR   tunerresultsr   r   r   tune_xgboostK   sT   

<rx   __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<   rx   __name__initr   r   r   r   r   r   <module>   s,    
)e