code: def _objective_core(
      # optuna parameters
      trial,
      # training parameters
      env_id,
      outdir,
      seed,
      monitor,
      gpu,
      steps,
      train_max_episode_len,
      eval_n_episodes,
      eval_interval,
      batch_size,
      # hyperparameters
      hyperparams,
  ):
      # Set a random seed used in PFRL
      utils.set_random_seed(seed)
  
      # Set different random seeds for train and test envs.
      train_seed = seed
      test_seed = 2**31 - 1 - seed
  
      def make_env(test=False):
          env = gym.make(env_id)
  
          if not isinstance(env.observation_space, gym.spaces.Box):
              raise ValueError(
                  "Supported only Box observation environments, but given: {}".format(
                      env.observation_space
                  )
              )
          if len(env.observation_space.shape) != 1:
              raise ValueError(
                  "Supported only observation spaces with ndim==1, but given: {}".format(
                      env.observation_space.shape
                  )
              )
          if not isinstance(env.action_space, gym.spaces.Discrete):
              raise ValueError(
                  "Supported only discrete action environments, but given: {}".format(
                      env.action_space
                  )
              )
  
          env_seed = test_seed if test else train_seed
          env.seed(env_seed)
          # Cast observations to float32 because our model uses float32
          env = pfrl.wrappers.CastObservationToFloat32(env)
          if monitor:
              env = pfrl.wrappers.Monitor(env, outdir)
          if not test:
              # Scale rewards (and thus returns) to a reasonable range so that
              # training is easier
              env = pfrl.wrappers.ScaleReward(
                  env, hyperparams["reward_scale_factor"]
              )
          return env
  
      env = make_env(test=False)
      obs_space = env.observation_space
      obs_size = obs_space.low.size
      action_space = env.action_space
      n_actions = action_space.n
  
      # create model & q_function
      model = MLP(
          in_size=obs_size,
          out_size=n_actions,
          hidden_sizes=hyperparams["hidden_sizes"],
      )
      q_func = q_functions.SingleModelStateQFunctionWithDiscreteAction(
          model=model
      )
  
      # Use epsilon-greedy for exploration
      start_epsilon = 1
      explorer = explorers.LinearDecayEpsilonGreedy(
          start_epsilon=start_epsilon,
          end_epsilon=hyperparams["end_epsilon"],
          decay_steps=hyperparams["decay_steps"],
          random_action_func=action_space.sample,
      )
  
      opt = optim.Adam(
          q_func.parameters(),
          lr=hyperparams["lr"],
          eps=hyperparams["adam_eps"],
      )
  
      rbuf_capacity = steps
      rbuf = replay_buffers.ReplayBuffer(rbuf_capacity)
  
      agent = DQN(
          q_func,
          opt,
          rbuf,
          gpu=gpu,
          gamma=hyperparams["gamma"],
          explorer=explorer,
          replay_start_size=hyperparams["replay_start_size"],
          target_update_interval=hyperparams["target_update_interval"],
          update_interval=hyperparams["update_interval"],
          minibatch_size=batch_size,
      )
  
      eval_env = make_env(test=True)
  
      evaluation_hooks = [OptunaPrunerHook(trial=trial)]
      _, eval_stats_history = experiments.train_agent_with_evaluation(
          agent=agent,
          env=env,
          steps=steps,
          eval_n_steps=None,
          eval_n_episodes=eval_n_episodes,
          eval_interval=eval_interval,
          outdir=outdir,
          eval_env=eval_env,
          train_max_episode_len=train_max_episode_len,
          evaluation_hooks=evaluation_hooks,
      )
  
      score = _get_score_from_eval_stats_history(eval_stats_history)
  
      return score
  
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 1 for calling a global function with example usage: _objective_core(trial, env_id, outdir, seed, monitor, gpu, steps, train_max_episode_len, eval_n_episodes, eval_interval, batch_size, hyperparams) and returns: score, env