class_docstr: A simple RL environment loop.
  
  This takes `Environment` and `Actor` instances and coordinates their
  interaction. Agent is updated if `should_update=True`. This can be used as:
  
    loop = EnvironmentLoop(environment, actor)
    loop.run(num_episodes)
  
  A `Counter` instance can optionally be given in order to maintain counts
  between different Acme components. If not given a local Counter will be
  created to maintain counts between calls to the `run` method.
  
  A `Logger` instance can also be passed in order to control the output of the
  loop. If not given a platform-specific default logger will be used as defined
  by utils.loggers.make_default_logger. A string `label` can be passed to easily
  change the label associated with the default logger; this is ignored if a
  `Logger` instance is given.
  
  A list of 'Observer' instances can be specified to generate additional metrics
  to be logged by the logger. They have access to the 'Environment' instance,
  the current timestep datastruct and the current action.
class_docstr_tok: ["A","simple","RL","environment","loop",".","This","takes","`","Environment","`","and","`","Actor","`","instances","and","coordinates","their","interaction",".","Agent","is","updated","if","`","should_update=True","`",".","This","can","be","used","as",":","loop","=","EnvironmentLoop","(","environment",",","actor",")","loop.run","(","num_episodes",")","A","`","Counter","`","instance","can","optionally","be","given","in","order","to","maintain","counts","between","different","Acme","components",".","If","not","given","a","local","Counter","will","be","created","to","maintain","counts","between","calls","to","the","`","run","`","method",".","A","`","Logger","`","instance","can","also","be","passed","in","order","to","control","the","output","of","the","loop",".","If","not","given","a","platform-specific","default","logger","will","be","used","as","defined","by","utils.loggers.make_default_logger",".","A","string","`","label","`","can","be","passed","to","easily","change","the","label","associated","with","the","default","logger",";","this","is","ignored","if","a","`","Logger","`","instance","is","given",".","A","list","of","'Observer","'","instances","can","be","specified","to","generate","additional","metrics","to","be","logged","by","the","logger",".","They","have","access","to","the","'Environment","'","instance",",","the","current","timestep","datastruct","and","the","current","action","."]
docstr: Run one episode.
  
  Each episode is a loop which interacts first with the environment to get an
  observation and then give that observation to the agent in order to retrieve
  an action.
  
  Returns:
    An instance of `loggers.LoggingData`.
docstr_tok: ["Run","one","episode",".","Each","episode","is","a","loop","which","interacts","first","with","the","environment","to","get","an","observation","and","then","give","that","observation","to","the","agent","in","order","to","retrieve","an","action",".","Returns",":","An","instance","of","`","loggers.LoggingData","`","."]
code: def run_episode(self) -> loggers.LoggingData:
      """Run one episode.
  
      Each episode is a loop which interacts first with the environment to get an
      observation and then give that observation to the agent in order to retrieve
      an action.
  
      Returns:
        An instance of `loggers.LoggingData`.
      """
      # Reset any counts and start the environment.
      episode_start_time = time.time()
      select_action_durations: List[float] = []
      env_step_durations: List[float] = []
      episode_steps: int = 0
  
      # For evaluation, this keeps track of the total undiscounted reward
      # accumulated during the episode.
      episode_return = tree.map_structure(
          _generate_zeros_from_spec, self._environment.reward_spec()
      )
      env_reset_start = time.time()
      timestep = self._environment.reset()
      env_reset_duration = time.time() - env_reset_start
      # Make the first observation.
      self._actor.observe_first(timestep)
      for observer in self._observers:
          # Initialize the observer with the current state of the env after reset
          # and the initial timestep.
          observer.observe_first(self._environment, timestep)
  
      # Run an episode.
      while not timestep.last():
          # Book-keeping.
          episode_steps += 1
  
          # Generate an action from the agent's policy.
          select_action_start = time.time()
          action = self._actor.select_action(timestep.observation)
          select_action_durations.append(
              time.time() - select_action_start
          )
  
          # Step the environment with the agent's selected action.
          env_step_start = time.time()
          timestep = self._environment.step(action)
          env_step_durations.append(time.time() - env_step_start)
  
          # Have the agent and observers observe the timestep.
          self._actor.observe(action, next_timestep=timestep)
          for observer in self._observers:
              # One environment step was completed. Observe the current state of the
              # environment, the current timestep and the action.
              observer.observe(self._environment, timestep, action)
  
          # Give the actor the opportunity to update itself.
          if self._should_update:
              self._actor.update()
  
          # Equivalent to: episode_return += timestep.reward
          # We capture the return value because if timestep.reward is a JAX
          # DeviceArray, episode_return will not be mutated in-place. (In all other
          # cases, the returned episode_return will be the same object as the
          # argument episode_return.)
          episode_return = tree.map_structure(
              operator.iadd, episode_return, timestep.reward
          )
  
      # Record counts.
      counts = self._counter.increment(episodes=1, steps=episode_steps)
  
      # Collect the results and combine with counts.
      steps_per_second = episode_steps / (
          time.time() - episode_start_time
      )
      result = {
          "episode_length": episode_steps,
          "episode_return": episode_return,
          "steps_per_second": steps_per_second,
          "env_reset_duration_sec": env_reset_duration,
          "select_action_duration_sec": np.mean(
              select_action_durations
          ),
          "env_step_duration_sec": np.mean(env_step_durations),
      }
      result.update(counts)
      for observer in self._observers:
          result.update(observer.get_metrics())
      return result
  
file_path: acme/acme/environment_loop.py
imports: import operator
  import time
  from typing import List, Optional, Sequence
  from acme import core
  from acme.utils import counting
  from acme.utils import loggers
  from acme.utils import observers
  from acme.utils import signals
  import dm_env
  from dm_env import specs
  import numpy
  import tree
label_desc: Use image node_id 2 for calling the EnvironmentLoop obj's underlying member method code with example usage: obj.run_episode() and returns: result