code: def main():
      parser = argparse.ArgumentParser()
  
      # training parameters
      parser.add_argument(
          "--env",
          type=str,
          default="LunarLander-v2",
          help="OpenAI Gym Environment ID.",
      )
      parser.add_argument(
          "--outdir",
          type=str,
          default="results",
          help=(
              "Directory path to save output files."
              " If it does not exist, it will be created."
          ),
      )
      parser.add_argument(
          "--seed",
          type=int,
          default=0,
          help="Random seed for randomizer.",
      )
      parser.add_argument(
          "--monitor",
          action="store_true",
          default=False,
          help=(
              "Monitor env. Videos and additional information are saved as output files."
          ),
      )
      parser.add_argument(
          "--gpu",
          type=int,
          default=0,
          help="GPU to use, set to -1 if no GPU.",
      )
      parser.add_argument(
          "--steps",
          type=int,
          default=4 * 10**5,
          help="Total number of timesteps to train the agent for each trial",
      )
      parser.add_argument(
          "--train-max-episode-len",
          type=int,
          default=1000,
          help="Maximum episode length during training.",
      )
      parser.add_argument(
          "--eval-n-episodes",
          type=int,
          default=10,
          help="Number of episodes at each evaluation phase.",
      )
      parser.add_argument(
          "--eval-interval",
          type=int,
          default=10**4,
          help="Frequency (in timesteps) of evaluation phase.",
      )
      parser.add_argument(
          "--batch-size",
          type=int,
          default=64,
          help="Training batch size.",
      )
  
      # Optuna related args
      parser.add_argument(
          "--optuna-study-name",
          type=str,
          default="optuna-pfrl-quickstart",
          help="Name for Optuna Study.",
      )
      parser.add_argument(
          "--optuna-storage",
          type=str,
          default="sqlite:///example.db",
          help=(
              "DB URL for Optuna Study. Be sure to create one beforehand: "
              "optuna create-study --study-name <name> --storage <storage> --direction maximize"  # noqa
          ),
      )
      parser.add_argument(
          "--optuna-training-steps-budget",
          type=int,
          default=4 * 10**7,
          help=(
              "Total training steps thoughout the optimization. If the pruner works "
              "well, this limited training steps can be allocated to promissing trials "
              "efficiently, and thus the tuned hyperparameter should get better."
          ),
      )
      parser.add_argument(
          "--optuna-pruner",
          type=str,
          default="NopPruner",
          choices=[
              "NopPruner",
              "ThresholdPruner",
              "PercentilePruner",
              "HyperbandPruner",
          ],
          help=(
              "Optuna pruner. For more details see: "
              "https://optuna.readthedocs.io/en/stable/reference/pruners.html"
          ),
      )
      # add pruner specific arguments...
      _tmp_args, _unknown = parser.parse_known_args()
      n_warmup_steps_help_msg = (
          "Don't prune for first `n_warmup_steps` steps for each trial (pruning check "
          "will be invoked every `eval_interval` step). Note that `step` for the pruner "
          "is the training step, not the number of evaluations so far."
      )
      if _tmp_args.optuna_pruner == "NopPruner":
          pass
      elif _tmp_args.optuna_pruner == "ThresholdPruner":
          parser.add_argument(
              "--lower",
              type=float,
              required=True,
              help=(
                  "Lower side threshold score for pruning trials. "
                  "Please set the appropriate value for your specified env."
              ),
          )
          parser.add_argument(
              "--n-warmup-steps",
              type=int,
              default=5 * _tmp_args.eval_interval,
              help=n_warmup_steps_help_msg,
          )
      elif _tmp_args.optuna_pruner == "PercentilePruner":
          parser.add_argument(
              "--percentile",
              type=float,
              default=50.0,
              help="Setting percentile == 50.0 is equivalent to the MedianPruner.",
          )
          parser.add_argument(
              "--n-startup-trials",
              type=int,
              default=5,
          )
          parser.add_argument(
              "--n-warmup-steps",
              type=int,
              default=5 * _tmp_args.eval_interval,
              help=n_warmup_steps_help_msg,
          )
      elif _tmp_args.optuna_pruner == "HyperbandPruner":
          pass
  
      args = parser.parse_args()
  
      rootdir = experiments.prepare_output_dir(
          args=args, basedir=args.outdir
      )
      file_handler = logging.FileHandler(
          filename=os.path.join(rootdir, "console.log")
      )
      console_handler = logging.StreamHandler()
      logging.basicConfig(
          level=logging.INFO, handlers=[file_handler, console_handler]
      )
  
      randomizer = random.Random(args.seed)
  
      def objective(trial):
          # suggest parameters from Optuna
          hyperparams = suggest(trial, args.steps)
  
          # seed is generated for each objective
          seed = randomizer.randint(0, 2**31 - 1)
          additional_args = dict(seed=seed, **hyperparams)
  
          outdir = experiments.prepare_output_dir(
              args=additional_args, basedir=rootdir
          )
          print("Output files are saved in {}".format(outdir))
  
          return _objective_core(
              # optuna parameters
              trial=trial,
              # training parameters
              env_id=args.env,
              outdir=outdir,
              seed=seed,
              monitor=args.monitor,
              gpu=args.gpu,
              steps=args.steps,
              train_max_episode_len=args.train_max_episode_len,
              eval_n_episodes=args.eval_n_episodes,
              eval_interval=args.eval_interval,
              batch_size=args.batch_size,
              # hyperparameters
              hyperparams=hyperparams,
          )
  
      sampler = optuna.samplers.TPESampler(seed=args.seed)
  
      # pruner
      if args.optuna_pruner == "NopPruner":
          pruner = optuna.pruners.NopPruner()
      elif args.optuna_pruner == "ThresholdPruner":
          pruner = optuna.pruners.ThresholdPruner(
              lower=args.lower,
              n_warmup_steps=args.n_warmup_steps,
          )
      elif args.optuna_pruner == "PercentilePruner":
          pruner = optuna.pruners.PercentilePruner(
              percentile=args.percentile,
              n_startup_trials=args.n_startup_trials,
              n_warmup_steps=args.n_warmup_steps,
          )
      elif args.optuna_pruner == "HyperbandPruner":
          pruner = optuna.pruners.HyperbandPruner(
              min_resource=args.eval_interval
          )
  
      study = optuna.load_study(
          study_name=args.optuna_study_name,
          storage=args.optuna_storage,
          sampler=sampler,
          pruner=pruner,
      )
  
      class OptunaTrainingStepsBudgetCallback:
          def __init__(self, training_steps_budget, logger=None):
              self.training_steps_budget = training_steps_budget
              self.logger = logger or logging.getLogger(__name__)
  
          def __call__(self, study, trial):
              training_steps = sum(
                  trial.last_step
                  for trial in study.get_trials()
                  if trial.last_step is not None
              )
              self.logger.info(
                  "{} / {} (sum of training steps / budget)".format(
                      training_steps, self.training_steps_budget
                  )
              )
              if training_steps >= self.training_steps_budget:
                  study.stop()
  
      callbacks = [
          OptunaTrainingStepsBudgetCallback(
              training_steps_budget=args.optuna_training_steps_budget,
          ),
      ]
      study.optimize(objective, callbacks=callbacks)
  
file_path: pfrl/examples/optuna/optuna_dqn_obs1d.py
imports: import argparse
  import logging
  import os
  import random
  import gym
  import torch.optim
  import pfrl
  from pfrl import experiments, explorers, q_functions, replay_buffers, utils
  from pfrl.agents.dqn import DQN
  from pfrl.experiments.evaluation_hooks import OptunaPrunerHook
  from pfrl.nn.mlp import MLP
label_desc: Use image node_id 4 for calling a global function with example usage: main() and returns: _objective_core