class_docstr: The generic environment runner for the single agent case.
class_docstr_tok: ["The","generic","environment","runner","for","the","single","agent","case","."]
code: def __init__(self, config: AlgorithmConfig, **kwargs):
      super().__init__(config=config)
  
      # Get the worker index on which this instance is running.
      self.worker_index: int = kwargs.get("worker_index")
  
      # Create our callbacks object.
      self._callbacks: DefaultCallbacks = self.config.callbacks_class()
  
      # Create the vectorized gymnasium env.
  
      # Register env for the local context.
      # Note, `gym.register` has to be called on each worker.
      if isinstance(self.config.env, str) and _global_registry.contains(
          ENV_CREATOR, self.config.env
      ):
          entry_point = partial(
              _global_registry.get(ENV_CREATOR, self.config.env),
              self.config.env_config,
          )
  
      else:
          entry_point = partial(
              _gym_env_creator,
              env_context=self.config.env_config,
              env_descriptor=self.config.env,
          )
      gym.register(
          "rllib-single-agent-env-runner-v0", entry_point=entry_point
      )
  
      # Wrap into `VectorListInfo`` wrapper to get infos as lists.
      self.env: gym.Wrapper = gym.wrappers.VectorListInfo(
          gym.vector.make(
              "rllib-single-agent-env-runner-v0",
              num_envs=self.config.num_envs_per_worker,
              asynchronous=self.config.remote_worker_envs,
          )
      )
      self.num_envs: int = self.env.num_envs
      assert self.num_envs == self.config.num_envs_per_worker
  
      # Call the `on_environment_created` callback.
      self._callbacks.on_environment_created(
          env_runner=self,
          env=self.env,
          env_config=self.config.env_config,
      )
  
      # Create the env-to-module connector pipeline.
      self._env_to_module = self.config.build_env_to_module_connector(
          self.env
      )
  
      # Create our own instance of the (single-agent) `RLModule` (which
      # the needs to be weight-synched) each iteration.
      try:
          module_spec: SingleAgentRLModuleSpec = (
              self.config.get_default_rl_module_spec()
          )
          module_spec.observation_space = (
              self._env_to_module.observation_space
          )
          # TODO (simon): The `gym.Wrapper` for `gym.vector.VectorEnv` should
          #  actually hold the spaces for a single env, but for boxes the
          #  shape is (1, 1) which brings a problem with the action dists.
          #  shape=(1,) is expected.
          module_spec.action_space = self.env.envs[0].action_space
          module_spec.model_config_dict = self.config.model
          self.module: RLModule = module_spec.build()
      except NotImplementedError:
          self.module = None
  
      # Create the two connector pipelines: env-to-module and module-to-env.
      self._module_to_env = self.config.build_module_to_env_connector(
          self.env
      )
  
      # This should be the default.
      self._needs_initial_reset: bool = True
      self._episodes: List[Optional["SingleAgentEpisode"]] = [
          None for _ in range(self.num_envs)
      ]
  
      self._done_episodes_for_metrics: List["SingleAgentEpisode"] = []
      self._ongoing_episodes_for_metrics: Dict[List] = defaultdict(list)
      self._ts_since_last_metrics: int = 0
      self._weights_seq_no: int = 0
  
imports: import gymnasium
  import numpy
  import tree
  from collections import defaultdict
  from functools import partial
  from typing import Dict, List, Optional
  from ray.rllib.algorithms.algorithm_config import AlgorithmConfig
  from ray.rllib.algorithms.callbacks import DefaultCallbacks
  from ray.rllib.core.rl_module.rl_module import RLModule, SingleAgentRLModuleSpec
  from ray.rllib.env.env_runner import EnvRunner
  from ray.rllib.env.single_agent_episode import SingleAgentEpisode
  from ray.rllib.env.utils import _gym_env_creator
  from ray.rllib.evaluation.metrics import RolloutMetrics
  from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID, SampleBatch
  from ray.rllib.utils.annotations import ExperimentalAPI, override
  from ray.rllib.utils.framework import try_import_tf, try_import_torch
  from ray.rllib.utils.torch_utils import convert_to_torch_tensor
  from ray.rllib.utils.typing import TensorType
  from ray.tune.registry import ENV_CREATOR, _global_registry
label_desc: Use image node_id 1 to create a new SingleAgentEnvRunner object from inherited base classes: EnvRunner with example: obj = SingleAgentEnvRunner(config)