class_docstr: Encapsulates an agent, space interfaces and a stack of wrappers, to make the agent's MazeActions accessible to
  an external env.
  
  Note: The policy, env, and wrappers parameters are compatible with hydra configuration used for rollouts
  (alternatively, an already instantiated policy/env can be passed in as well).
  
  How it works:
  
  External env should supply states to agent deployment object, and can query it for agent MazeActions. The
  agent with the supplied policy (or multiple policies) is run on a separate thread.
  
  Note that the two threads (main thread running this wrapper and the second thread running the agent, wrappers etc.)
  never run in parallel, i.e. one is always suspended. This is enforced using the queues. Either the main thread
  runs and the agent thread is waiting for the state to be passed from the main thread, or the agent thread is
  running (computing the MazeAction) and the main thread is waiting until the MazeAction is passed back (then,
  the second thread is suspended again until the next state is passed in via the queue).
  
  Queues have max size of one, enforcing that one step can be taken at a time.
  
  :param policy: Structured policy to query for actions, or a config which will be used to built the policy.
  :param env: Either an instantiated simulation environment which will be used for action and observation processing
              (i.e., the Maze env, wrapper stack and env context will be used), or a config for instantiating
              such env.
  :param wrappers: Configuration for (additional) wrappers, if required.
class_docstr_tok: ["Encapsulates","an","agent",",","space","interfaces","and","a","stack","of","wrappers",",","to","make","the","agent","'s","MazeActions","accessible","to","an","external","env",".","Note",":","The","policy",",","env",",","and","wrappers","parameters","are","compatible","with","hydra","configuration","used","for","rollouts","(","alternatively",",","an","already","instantiated","policy\/env","can","be","passed","in","as","well",")",".","How","it","works",":","External","env","should","supply","states","to","agent","deployment","object",",","and","can","query","it","for","agent","MazeActions",".","The","agent","with","the","supplied","policy","(","or","multiple","policies",")","is","run","on","a","separate","thread",".","Note","that","the","two","threads","(","main","thread","running","this","wrapper","and","the","second","thread","running","the","agent",",","wrappers","etc",".",")","never","run","in","parallel",",","i.e",".","one","is","always","suspended",".","This","is","enforced","using","the","queues",".","Either","the","main","thread","runs","and","the","agent","thread","is","waiting","for","the","state","to","be","passed","from","the","main","thread",",","or","the","agent","thread","is","running","(","computing","the","MazeAction",")","and","the","main","thread","is","waiting","until","the","MazeAction","is","passed","back","(","then",",","the","second","thread","is","suspended","again","until","the","next","state","is","passed","in","via","the","queue",")",".","Queues","have","max","size","of","one",",","enforcing","that","one","step","can","be","taken","at","a","time",".",":","param","policy",":","Structured","policy","to","query","for","actions",",","or","a","config","which","will","be","used","to","built","the","policy",".",":","param","env",":","Either","an","instantiated","simulation","environment","which","will","be","used","for","action","and","observation","processing","(","i.e.",",","the","Maze","env",",","wrapper","stack","and","env","context","will","be","used",")",",","or","a","config","for","instantiating","such","env",".",":","param","wrappers",":","Configuration","for","(","additional",")","wrappers",",","if","required","."]
docstr: Query the agent for MazeAction derived from the given state.
  
  Passes the state etc. to the agent's thread, where it is integrated into an ordinary env rollout loop.
  In the first step, an env reset call is propagated through the env wrapper stack on agent's thread.
  
  :param maze_state: Current state of the environment.
  :param reward: Reward for the previous step (can be null in initial step)
  :param done: Whether the external environment is done
  :param info: Info dictionary
  :param events: List of events to be recorded for this step (mainly useful for statistics and event logs)
  :param actor_id: Optional ID of the actor to run next (comprised of policy_id and agent_id)
  :return: MazeAction from the agent
docstr_tok: ["Query","the","agent","for","MazeAction","derived","from","the","given","state",".","Passes","the","state","etc",".","to","the","agent","'s","thread",",","where","it","is","integrated","into","an","ordinary","env","rollout","loop",".","In","the","first","step",",","an","env","reset","call","is","propagated","through","the","env","wrapper","stack","on","agent","'s","thread",".",":","param","maze_state",":","Current","state","of","the","environment",".",":","param","reward",":","Reward","for","the","previous","step","(","can","be","null","in","initial","step",")",":","param","done",":","Whether","the","external","environment","is","done",":","param","info",":","Info","dictionary",":","param","events",":","List","of","events","to","be","recorded","for","this","step","(","mainly","useful","for","statistics","and","event","logs",")",":","param","actor_id",":","Optional","ID","of","the","actor","to","run","next","(","comprised","of","policy_id","and","agent_id",")",":","return",":","MazeAction","from","the","agent"]
code: def act(
      self,
      maze_state: MazeStateType,
      reward: Union[None, float, np.ndarray, Any],
      done: bool,
      info: Union[None, Dict[Any, Any]],
      events: Optional[List[EventRecord]] = None,
      actor_id: ActorID = ActorID(0, 0),
  ) -> MazeActionType:
      """Query the agent for MazeAction derived from the given state.
  
      Passes the state etc. to the agent's thread, where it is integrated into an ordinary env rollout loop.
      In the first step, an env reset call is propagated through the env wrapper stack on agent's thread.
  
      :param maze_state: Current state of the environment.
      :param reward: Reward for the previous step (can be null in initial step)
      :param done: Whether the external environment is done
      :param info: Info dictionary
      :param events: List of events to be recorded for this step (mainly useful for statistics and event logs)
      :param actor_id: Optional ID of the actor to run next (comprised of policy_id and agent_id)
      :return: MazeAction from the agent
      """
      if self.rollout_done:
          raise RuntimeError(
              "External env has been declared done already. Please create a new connector object for"
              "a new episode."
          )
  
      self.external_core_env.set_actor_id(actor_id)
      self.state_queue.put((maze_state, reward, done, info, events))
      # Here, the MazeAction is suspended until the agent on the second thread runs another step and the MazeAction
      # is passed back through the MazeAction queue.
      maze_action = self.maze_action_queue.get()
  
      # If exception occurs in the agent thread, it will be passed back using this same queue as an exception report.
      if isinstance(maze_action, ExceptionReport):
          exc_report = maze_action
          raise RuntimeError(
              "Error encountered in agent thread:\n"
              + exc_report.traceback
          ) from exc_report.exception
  
      return maze_action
  
file_path: maze/maze/core/agent_deployment/agent_deployment.py
imports: from queue import Queue
  from threading import Event, Thread
  from typing import Any, Dict, Union, List, Optional
  import numpy
  from maze.core.agent.policy import Policy
  from maze.core.agent_deployment.external_core_env import ExternalCoreEnv
  from maze.core.agent_deployment.policy_executor import PolicyExecutor, ExceptionReport
  from maze.core.env.maze_action import MazeActionType
  from maze.core.env.maze_env import MazeEnv
  from maze.core.env.maze_state import MazeStateType
  from maze.core.env.structured_env import ActorID
  from maze.core.events.event_record import EventRecord
  from maze.core.utils.config_utils import EnvFactory
  from maze.core.utils.factory import ConfigType, CollectionOfConfigType, Factory
  from maze.core.wrappers.log_stats_wrapper import LogStatsWrapper
label_desc: Use image node_id 2 for calling the AgentDeployment obj's underlying member method code with example usage: obj.act(maze_state, reward, done, info, events, actor_id) and returns: maze_action