code: def run_episode(
      env: EnvWrapper,
      agent: Agent,
      mdp_id: int = 0,
      max_steps: Optional[int] = None,
  ) -> Trajectory:
      """
      Return sum of rewards from episode.
      After max_steps (if specified), the environment is assumed to be terminal.
      Can also specify the mdp_id and gamma of episode.
      """
      trajectory = Trajectory()
      obs = env.reset()
      possible_actions_mask = env.possible_actions_mask
      terminal = False
      num_steps = 0
      info = {}
      while not terminal:
          action, log_prob = agent.act(obs, possible_actions_mask)
          next_obs, reward, terminal, info = env.step(action)
          next_possible_actions_mask = env.possible_actions_mask
          if max_steps is not None and num_steps >= (max_steps - 1):
              terminal = True
  
          # Only partially filled. Agent can fill in more fields.
          transition = Transition(
              mdp_id=mdp_id,
              sequence_number=num_steps,
              observation=obs,
              action=action,
              reward=float(reward),
              terminal=bool(terminal),
              log_prob=log_prob,
              possible_actions_mask=possible_actions_mask,
          )
          agent.post_step(transition)
          trajectory.add_transition(transition)
          SummaryWriterContext.increase_global_step()
          obs = next_obs
          possible_actions_mask = next_possible_actions_mask
          num_steps += 1
      agent.post_episode(trajectory, info)
      return trajectory
  
file_path: ReAgent/reagent/gym/runners/gymrunner.py
imports: import logging
  import pickle
  from typing import Optional, Sequence
  import numpy
  import torch.multiprocessing
  from reagent.core.multiprocess_utils import unwrap_function_outputs, wrap_function_arguments
  from reagent.core.tensorboardX import SummaryWriterContext
  from reagent.gym.agents.agent import Agent
  from reagent.gym.envs import EnvWrapper
  from reagent.gym.types import Trajectory, Transition
label_desc: Use image node_id 1 for calling a global function with example usage: run_episode(env, agent, mdp_id, max_steps) and returns: trajectory