docstr: If `policy` is not given, we will try to create a random policy
docstr_tok: ["If","`","policy","`","is","not","given",",","we","will","try","to","create","a","random","policy"]
code: def create_for_env(
      cls,
      env: EnvWrapper,
      policy: Optional[Policy],
      *,
      device: Union[str, torch.device] = "cpu",
      obs_preprocessor=None,
      action_extractor=None,
      **kwargs,
  ) -> "Agent":
      """
      If `policy` is not given, we will try to create a random policy
      """
      if isinstance(device, str):
          device = torch.device(device)
  
      if obs_preprocessor is None:
          obs_preprocessor = env.get_obs_preprocessor(device=device)
  
      if action_extractor is None:
          action_extractor = env.get_action_extractor()
  
      if policy is None:
          policy = make_random_policy_for_env(env)
  
      return cls(
          policy,
          obs_preprocessor=obs_preprocessor,
          action_extractor=action_extractor,
          device=device,
          **kwargs,
      )
  
file_path: ReAgent/reagent/gym/agents/agent.py
imports: from typing import Any, Dict, Optional, Tuple, Union
  import numpy
  import torch
  from reagent.gym.envs.env_wrapper import EnvWrapper
  from reagent.gym.policies.policy import Policy
  from reagent.gym.policies.random_policies import make_random_policy_for_env
  from reagent.gym.types import PostEpisode, PostStep, Trajectory, Transition
label_desc: Use image node_id 2 for calling the Agent obj's underlying member method code with example usage: obj.create_for_env(cls, env, policy) and returns: cls