class_docstr: Tensorforce runner utility.
  
  Args:
      agent (specification | Agent object | Agent.load kwargs): Agent specification or object
          (<b>note</b>: if passed as object, `agent.close()` is not (!) automatically triggered
          as part of `runner.close()`), or keyword arguments to `Agent.load()` in particular
          containing `directory`, in all cases argument `environment` is implicitly specified
          as the following argument, and argument `parallel_interactions` is either implicitly
          specified as `num_parallel` or expected to be at least `num_parallel`
          (<span style="color:#C00000"><b>required</b></span>).
      environment (specification | Environment object): Environment specification or object
          (<b>note</b>: if passed as object, `environment.close()` is not (!) automatically
          triggered as part of `runner.close()`), where argument `max_episode_timesteps` is
          implicitly specified as the following argument
          (<span style="color:#C00000"><b>required</b></span>, or alternatively `environments`,
          invalid for "socket-client" remote mode).
      max_episode_timesteps (int > 0): Maximum number of timesteps per episode, overwrites the
          environment default if defined
          (<span style="color:#00C000"><b>default</b></span>: environment default, invalid for
          "socket-client" remote mode).
      num_parallel (int >= 2): Number of environment instances to execute in parallel, usually
          requires argument `remote` to be specified for proper parallel execution unless
          vectorizable environment
          (<span style="color:#00C000"><b>default</b></span>: no parallel execution, implicitly
          specified by `environments`).
      environments (list[specification | Environment object]): Environment specifications or
          objects to execute in parallel, the latter are not closed automatically as part of
          `runner.close()`
          (<span style="color:#00C000"><b>default</b></span>: no parallel execution,
          alternatively specified via `environment` and `num_parallel`, invalid for
          "socket-client" remote mode).
      evaluation (bool): Whether to run the last of multiple parallel environments in evaluation
          mode, only valid with `num_parallel` or `environments`
          (<span style="color:#00C000"><b>default</b></span>: no evaluation).
      remote ("multiprocessing" | "socket-client"): Communication mode for remote environment
          execution of parallelized environment execution, not compatible with environment(s)
          given as Environment objects, "socket-client" mode requires a corresponding
          "socket-server" running
          (<span style="color:#00C000"><b>default</b></span>: local execution).
      blocking (bool): Whether remote environment calls should be blocking, only valid if remote
          mode given
          (<span style="color:#00C000"><b>default</b></span>: not blocking, invalid unless
          "multiprocessing" or "socket-client" remote mode).
      host (str, iter[str]): Socket server hostname(s) or IP address(es)
          (<span style="color:#C00000"><b>required</b></span> only for "socket-client" remote
          mode).
      port (int, iter[int]): Socket server port(s), increasing sequence if single host and port
          given
          (<span style="color:#C00000"><b>required</b></span> only for "socket-client" remote
          mode).
class_docstr_tok: ["Tensorforce","runner","utility",".","Args",":","agent","(","specification","|","Agent","object","|","Agent.load","kwargs",")",":","Agent","specification","or","object","(","<","b",">","note","<","\/b",">",":","if","passed","as","object",",","`","agent.close","(",")","`","is","not","(","!",")","automatically","triggered","as","part","of","`","runner.close","(",")","`",")",",","or","keyword","arguments","to","`","Agent.load","(",")","`","in","particular","containing","`","directory","`",",","in","all","cases","argument","`","environment","`","is","implicitly","specified","as","the","following","argument",",","and","argument","`","parallel_interactions","`","is","either","implicitly","specified","as","`","num_parallel","`","or","expected","to","be","at","least","`","num_parallel","`","(","<","span","style=","''","color",":","#","C00000","''",">","<","b",">","required","<","\/b",">","<","\/span",">",")",".","environment","(","specification","|","Environment","object",")",":","Environment","specification","or","object","(","<","b",">","note","<","\/b",">",":","if","passed","as","object",",","`","environment.close","(",")","`","is","not","(","!",")","automatically","triggered","as","part","of","`","runner.close","(",")","`",")",",","where","argument","`","max_episode_timesteps","`","is","implicitly","specified","as","the","following","argument","(","<","span","style=","''","color",":","#","C00000","''",">","<","b",">","required","<","\/b",">","<","\/span",">",",","or","alternatively","`","environments","`",",","invalid","for","``","socket-client","''","remote","mode",")",".","max_episode_timesteps","(","int",">","0",")",":","Maximum","number","of","timesteps","per","episode",",","overwrites","the","environment","default","if","defined","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","environment","default",",","invalid","for","``","socket-client","''","remote","mode",")",".","num_parallel","(","int",">","=","2",")",":","Number","of","environment","instances","to","execute","in","parallel",",","usually","requires","argument","`","remote","`","to","be","specified","for","proper","parallel","execution","unless","vectorizable","environment","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","no","parallel","execution",",","implicitly","specified","by","`","environments","`",")",".","environments","(","list","[","specification","|","Environment","object","]",")",":","Environment","specifications","or","objects","to","execute","in","parallel",",","the","latter","are","not","closed","automatically","as","part","of","`","runner.close","(",")","`","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","no","parallel","execution",",","alternatively","specified","via","`","environment","`","and","`","num_parallel","`",",","invalid","for","``","socket-client","''","remote","mode",")",".","evaluation","(","bool",")",":","Whether","to","run","the","last","of","multiple","parallel","environments","in","evaluation","mode",",","only","valid","with","`","num_parallel","`","or","`","environments","`","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","no","evaluation",")",".","remote","(","``","multiprocessing","''","|","``","socket-client","''",")",":","Communication","mode","for","remote","environment","execution","of","parallelized","environment","execution",",","not","compatible","with","environment","(","s",")","given","as","Environment","objects",",","``","socket-client","''","mode","requires","a","corresponding","``","socket-server","''","running","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","local","execution",")",".","blocking","(","bool",")",":","Whether","remote","environment","calls","should","be","blocking",",","only","valid","if","remote","mode","given","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","not","blocking",",","invalid","unless","``","multiprocessing","''","or","``","socket-client","''","remote","mode",")",".","host","(","str",",","iter","[","str","]",")",":","Socket","server","hostname","(","s",")","or","IP","address","(","es",")","(","<","span","style=","''","color",":","#","C00000","''",">","<","b",">","required","<","\/b",">","<","\/span",">","only","for","``","socket-client","''","remote","mode",")",".","port","(","int",",","iter","[","int","]",")",":","Socket","server","port","(","s",")",",","increasing","sequence","if","single","host","and","port","given","(","<","span","style=","''","color",":","#","C00000","''",">","<","b",">","required","<","\/b",">","<","\/span",">","only","for","``","socket-client","''","remote","mode",")","."]
code: def __init__(
      self,
      agent,
      environment=None,
      max_episode_timesteps=None,
      num_parallel=None,
      environments=None,
      evaluation=False,
      remote=None,
      blocking=False,
      host=None,
      port=None,
  ):
      if environment is None and environments is None:
          if remote != "socket-client":
              raise TensorforceError.required(
                  name="Runner", argument="environment or environments"
              )
          if num_parallel is None:
              raise TensorforceError.required(
                  name="Runner",
                  argument="num_parallel",
                  condition="socket-client remote mode",
              )
          environments = [None for _ in range(num_parallel)]
  
      elif environment is None:
          if environments is None:
              raise TensorforceError.required(
                  name="Runner", argument="environment or environments"
              )
          if not util.is_iterable(x=environments):
              raise TensorforceError.type(
                  name="Runner",
                  argument="environments",
                  value=environments,
              )
          if len(environments) == 0:
              raise TensorforceError.value(
                  name="Runner",
                  argument="len(environments)",
                  value=len(environments),
              )
          if num_parallel is not None and num_parallel != len(
              environments
          ):
              raise TensorforceError.value(
                  name="Runner",
                  argument="num_parallel",
                  value=num_parallel,
                  hint="!= len(environments)",
              )
          num_parallel = len(environments)
          environments = list(environments)
  
      elif num_parallel is None:
          if environments is not None:
              raise TensorforceError.invalid(
                  name="Runner",
                  argument="environments",
                  condition="environment is specified",
              )
          if evaluation:
              raise TensorforceError.invalid(
                  name="Runner",
                  argument="evaluation",
                  condition="single environment",
              )
          num_parallel = 1
          environments = [environment]
  
      else:
          if not isinstance(num_parallel, int):
              raise TensorforceError.value(
                  name="Runner",
                  argument="num_parallel",
                  dtype=type(num_parallel),
              )
          elif num_parallel < 2:
              raise TensorforceError.value(
                  name="Runner",
                  argument="num_parallel",
                  value=num_parallel,
                  hint="< 2",
              )
          if environments is not None:
              raise TensorforceError.invalid(
                  name="Runner",
                  argument="environments",
                  condition="environment is specified",
              )
          if isinstance(environment, Environment):
              raise TensorforceError.value(
                  name="Runner",
                  argument="environment",
                  value=environment,
                  condition="num_parallel",
                  hint="is Environment instance, but specification dict is required",
              )
          environments = [environment for _ in range(num_parallel)]
  
      if port is None or isinstance(port, int):
          if isinstance(host, str):
              port = [port + n for n in range(num_parallel)]
          else:
              port = [port for _ in range(num_parallel)]
      else:
          if len(port) != num_parallel:
              raise TensorforceError.value(
                  name="Runner",
                  argument="len(port)",
                  value=len(port),
                  hint="!= num_parallel",
              )
      if host is None or isinstance(host, str):
          host = [host for _ in range(num_parallel)]
      else:
          if len(host) != num_parallel:
              raise TensorforceError.value(
                  name="Runner",
                  argument="len(host)",
                  value=len(host),
                  hint="!= num_parallel",
              )
  
      self.environments = list()
      self.is_environment_external = isinstance(
          environments[0], Environment
      )
      environment = Environment.create(
          environment=environments[0],
          max_episode_timesteps=max_episode_timesteps,
          remote=remote,
          blocking=blocking,
          host=host[0],
          port=port[0],
      )
      self.is_environment_remote = isinstance(
          environment, RemoteEnvironment
      )
      states = environment.states()
      actions = environment.actions()
      self.environments.append(environment)
      if (
          remote is None
          and num_parallel > 1
          and environment.is_vectorizable()
      ):
          self.num_vectorized = num_parallel
          environments = environments[:1]
          if evaluation:
              raise TensorforceError.invalid(
                  name="Runner",
                  argument="evaluation",
                  condition="vectorized environment",
              )
      elif environment.num_actors() > 1:
          assert num_parallel == 1
          num_parallel = environment.num_actors()
          self.num_vectorized = environment.num_actors()
      else:
          self.num_vectorized = None
  
      for n, environment in enumerate(environments[1:], start=1):
          assert (
              isinstance(environment, Environment)
              == self.is_environment_external
          )
          environment = Environment.create(
              environment=environment,
              max_episode_timesteps=max_episode_timesteps,
              remote=remote,
              blocking=blocking,
              host=host[n],
              port=port[n],
          )
          assert (
              isinstance(environment, RemoteEnvironment)
              == self.is_environment_remote
          )
          assert util.is_equal(x=environment.states(), y=states)
          assert util.is_equal(x=environment.actions(), y=actions)
          self.environments.append(environment)
  
      self.evaluation = evaluation
  
      self.is_agent_external = isinstance(agent, Agent)
      if not self.is_agent_external and "directory" in agent:
          self.agent = Agent.load(
              **agent,
              environment=environment,
              parallel_interactions=(
                  num_parallel - int(self.evaluation)
              ),
          )
      elif num_parallel - int(self.evaluation) > 1:
          self.agent = Agent.create(
              agent=agent,
              environment=environment,
              parallel_interactions=(
                  num_parallel - int(self.evaluation)
              ),
          )
      else:
          self.agent = Agent.create(
              agent=agent, environment=environment
          )
  
file_path: tensorforce/tensorforce/execution/runner.py
imports: import time
  import numpy
  from tqdm.auto import tqdm
  from tensorforce import Agent, Environment, TensorforceError, util
  from tensorforce.environments import RemoteEnvironment
label_desc: Use image node_id 1 to create a new Runner object from inherited base classes: object with example: obj = Runner(agent, environment, max_episode_timesteps, num_parallel, environments, evaluation, remote, blocking, host, port)