code: def optimize(
      self,
      algorithm,
      data_type,
      X_train,
      y_train,
      sample_weight,
      X_validation,
      y_validation,
      sample_weight_validation,
      learner_params,
  ):
      # only tune models with original data type
      if data_type != "original":
          return learner_params
  
      key = f"{data_type}_{algorithm}"
      if key in self.tuning:
          return self.update_learner_params(
              learner_params, self.tuning[key]
          )
  
      if self.verbose:
          print(
              f"Optuna optimizes {algorithm} with time budget {self.time_budget} seconds "
              f"eval_metric {self.eval_metric.name} ({self.direction})"
          )
  
      self.cat_features_indices = []
      for i in range(X_train.shape[1]):
          if PreprocessingUtils.is_categorical(X_train.iloc[:, i]):
              self.cat_features_indices += [i]
  
      study = optuna.create_study(
          direction=self.direction,
          sampler=optuna.samplers.TPESampler(seed=self.random_state),
          pruner=optuna.pruners.MedianPruner(
              n_warmup_steps=self.n_warmup_steps
          ),
      )
      obejctive = None
      if algorithm == "LightGBM":
          objective = LightgbmObjective(
              self.ml_task,
              X_train,
              y_train,
              sample_weight,
              X_validation,
              y_validation,
              sample_weight_validation,
              self.eval_metric,
              self.cat_features_indices,
              self.n_jobs,
              self.random_state,
          )
      elif algorithm == "Xgboost":
          objective = XgboostObjective(
              self.ml_task,
              X_train,
              y_train,
              sample_weight,
              X_validation,
              y_validation,
              sample_weight_validation,
              self.eval_metric,
              self.n_jobs,
              self.random_state,
          )
      elif algorithm == "CatBoost":
          objective = CatBoostObjective(
              self.ml_task,
              X_train,
              y_train,
              sample_weight,
              X_validation,
              y_validation,
              sample_weight_validation,
              self.eval_metric,
              self.cat_features_indices,
              self.n_jobs,
              self.random_state,
          )
      elif algorithm == "Random Forest":
          objective = RandomForestObjective(
              self.ml_task,
              X_train,
              y_train,
              sample_weight,
              X_validation,
              y_validation,
              sample_weight_validation,
              self.eval_metric,
              self.n_jobs,
              self.random_state,
          )
      elif algorithm == "Extra Trees":
          objective = ExtraTreesObjective(
              self.ml_task,
              X_train,
              y_train,
              sample_weight,
              X_validation,
              y_validation,
              sample_weight_validation,
              self.eval_metric,
              self.n_jobs,
              self.random_state,
          )
      elif algorithm == "Nearest Neighbors":
          objective = KNNObjective(
              self.ml_task,
              X_train,
              y_train,
              sample_weight,
              X_validation,
              y_validation,
              sample_weight_validation,
              self.eval_metric,
              self.n_jobs,
              self.random_state,
          )
      elif algorithm == "Neural Network":
          objective = NeuralNetworkObjective(
              self.ml_task,
              X_train,
              y_train,
              sample_weight,
              X_validation,
              y_validation,
              sample_weight_validation,
              self.eval_metric,
              self.n_jobs,
              self.random_state,
          )
  
      study.optimize(
          objective,
          n_trials=5000,
          timeout=self.time_budget,
          gc_after_trial=True,
      )
  
      self.plot_study(algorithm, data_type, study)
  
      joblib.dump(study, os.path.join(self.study_dir, key + ".joblib"))
  
      best = study.best_params
  
      if algorithm == "LightGBM":
          best["metric"] = objective.eval_metric_name
          best[
              "custom_eval_metric_name"
          ] = objective.custom_eval_metric_name
          best["num_boost_round"] = objective.rounds
          best[
              "early_stopping_rounds"
          ] = objective.early_stopping_rounds
          # best["learning_rate"] = objective.learning_rate
          best["cat_feature"] = self.cat_features_indices
          best["feature_pre_filter"] = False
          best["seed"] = objective.seed
      elif algorithm == "CatBoost":
          best["eval_metric"] = objective.eval_metric_name
          best["num_boost_round"] = objective.rounds
          best[
              "early_stopping_rounds"
          ] = objective.early_stopping_rounds
          # best["bootstrap_type"] = "Bernoulli"
          # best["learning_rate"] = objective.learning_rate
          best["seed"] = objective.seed
      elif algorithm == "Xgboost":
          best["objective"] = objective.objective
          best["eval_metric"] = objective.eval_metric_name
          # best["eta"] = objective.learning_rate
          best["max_rounds"] = objective.rounds
          best[
              "early_stopping_rounds"
          ] = objective.early_stopping_rounds
          best["seed"] = objective.seed
      elif algorithm == "Extra Trees":
          # Extra Trees are not using early stopping
          best[
              "max_steps"
          ] = objective.max_steps  # each step has 100 trees
          best["seed"] = objective.seed
          best["eval_metric_name"] = self.eval_metric.name
      elif algorithm == "Random Forest":
          # Random Forest is not using early stopping
          best[
              "max_steps"
          ] = objective.max_steps  # each step has 100 trees
          best["seed"] = objective.seed
          best["eval_metric_name"] = self.eval_metric.name
      elif algorithm == "Nearest Neighbors":
          best["rows_limit"] = 100000
      elif algorithm == "Neural Network":
          pass
  
      self.tuning[key] = best
      self.save()
  
      return self.update_learner_params(learner_params, best)
  
imports: import json
  import os
  import warnings
  import joblib
  import matplotlib
  import optuna
  from matplotlib import pyplot
  from supervised.exceptions import AutoMLException
  from supervised.preprocessing.preprocessing_utils import PreprocessingUtils
  from supervised.tuner.optuna.catboost import CatBoostObjective
  from supervised.tuner.optuna.extra_trees import ExtraTreesObjective
  from supervised.tuner.optuna.knn import KNNObjective
  from supervised.tuner.optuna.lightgbm import LightgbmObjective
  from supervised.tuner.optuna.nn import NeuralNetworkObjective
  from supervised.tuner.optuna.random_forest import RandomForestObjective
  from supervised.tuner.optuna.xgboost import XgboostObjective
  from supervised.utils.metric import Metric
  from supervised.utils.jsonencoder import MLJSONEncoder
label_desc: Use image node_id 3 for calling the OptunaTuner obj's underlying member method code with example usage: obj.optimize(algorithm, data_type, X_train, y_train, sample_weight, X_validation, y_validation, sample_weight_validation, learner_params) and returns: self, learner_params, self