code: def tune_xgboost(use_class_trainable=True):
      search_space = {
          # You can mix constants with search space objects.
          "objective": "binary:logistic",
          "eval_metric": ["logloss", "error"],
          "max_depth": 9,
          "learning_rate": 1,
          "min_child_weight": tune.grid_search([2, 3]),
          "subsample": tune.grid_search([0.8, 0.9]),
          "colsample_bynode": tune.grid_search([0.8, 0.9]),
          "random_state": 1,
          "num_parallel_tree": 2000,
      }
      # This will enable aggressive early stopping of bad trials.
      base_scheduler = ASHAScheduler(
          max_t=16,
          grace_period=1,
          reduction_factor=2,  # 16 training iterations
      )
  
      def example_resources_allocation_function(
          tune_controller: "TuneController",
          trial: Trial,
          result: Dict[str, Any],
          scheduler: "ResourceChangingScheduler",
      ) -> Optional[PlacementGroupFactory]:
          """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.
          """
  
          # Get base trial resources as defined in
          # ``tune.with_resources``
          base_trial_resource = scheduler._base_trial_resources
  
          # Don't bother if this is just the first iteration
          if result["training_iteration"] < 1:
              return None
  
          # default values if resources_per_trial is unspecified
          if base_trial_resource is None:
              base_trial_resource = PlacementGroupFactory(
                  [{"CPU": 1, "GPU": 0}]
              )
  
          # Assume that the number of CPUs cannot go below what was
          # specified in ``Tuner.fit()``.
          min_cpu = base_trial_resource.required_resources.get("CPU", 0)
  
          # Get the number of CPUs available in total (not just free)
          total_available_cpus = (
              tune_controller._resource_updater.get_num_cpus()
          )
  
          # Divide the free CPUs among all live trials
          cpu_to_use = max(
              min_cpu,
              total_available_cpus
              // len(tune_controller.get_live_trials()),
          )
  
          # Assign new CPUs to the trial in a PlacementGroupFactory
          return PlacementGroupFactory([{"CPU": cpu_to_use, "GPU": 0}])
  
      # You can either define your own resources_allocation_function, or
      # use the default one - DistributeResources
  
      # from ray.tune.schedulers.resource_changing_scheduler import \
      #    DistributeResources
  
      scheduler = ResourceChangingScheduler(
          base_scheduler=base_scheduler,
          resources_allocation_function=example_resources_allocation_function
          # resources_allocation_function=DistributeResources()  # default
      )
  
      if use_class_trainable:
          fn = BreastCancerTrainable
      else:
          fn = train_breast_cancer
  
      tuner = tune.Tuner(
          tune.with_resources(
              fn,
              resources=PlacementGroupFactory([{"CPU": 1, "GPU": 0}]),
          ),
          tune_config=tune.TuneConfig(
              metric="eval-logloss",
              mode="min",
              num_samples=1,
              scheduler=scheduler,
          ),
          run_config=train.RunConfig(
              checkpoint_config=train.CheckpointConfig(
                  checkpoint_at_end=use_class_trainable,
              )
          ),
          param_space=search_space,
      )
      results = tuner.fit()
  
      if use_class_trainable:
          assert results.get_dataframe()["nthread"].max() > 1
  
      return results.get_best_result()
  
imports: from typing import Dict, Any, Optional, TYPE_CHECKING
  import sklearn.datasets
  import sklearn.metrics
  import os
  from sklearn.model_selection import train_test_split
  import xgboost
  from xgboost.core import Booster
  import pickle
  import ray
  from ray import train, tune
  from ray.tune.schedulers import ResourceChangingScheduler, ASHAScheduler
  from ray.tune import Trainable
  from ray.tune.execution.placement_groups import PlacementGroupFactory
  from ray.tune.experiment import Trial
  from ray.tune.integration.xgboost import TuneReportCheckpointCallback
label_desc: Use image node_id 3 for calling a global function with example usage: tune_xgboost(use_class_trainable) and returns: results, PlacementGroupFactory, None