def __init__(self, env_fns): """ Arguments: env_fns: iterable of callables functions that build environments """ self.envs = [fn() for fn in env_fns] env = self.envs[0] VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space) obs_space = env.observation_space self._has_vector_obs = hasattr(self.observation_space, 'spaces') # if self._has_vector_obs: # self.keys = ['visual', 'vector'] # shapes = { # 'visual':obs_space[0].shape, # 'vector':obs_space[1].shape # } # dtypes = { # 'visual':obs_space[0].dtype, # 'vector':obs_space[1].dtype # } # else: self.keys, shapes, dtypes = obs_space_info(obs_space) self.buf_obs = { k: np.zeros((self.num_envs,) + tuple(shapes[k]), dtype=dtypes[k]) for k in self.keys } self.buf_dones = np.zeros((self.num_envs,), dtype=np.bool) self.buf_rews = np.zeros((self.num_envs,), dtype=np.float32) self.buf_infos = [{} for _ in range(self.num_envs)] self.actions = None self.spec = self.envs[0].spec
def __init__(self, num_fields, max_ticks=2400): self.num_fields = num_fields self.num_envs = num_fields * 2 self.max_ticks = max_ticks self.connections = [] # type: List[Pipe] self.processes = [] # type: List[Process] for i in range(self.num_fields): parent_conn, child_conn = Pipe() p = Process(target=env_worker, daemon=True, args=(child_conn, ), kwargs=dict(max_ticks=self.max_ticks)) p.start() self.connections.append(parent_conn) self.processes.append(p) # self.connections[0].send(('get_spaces_spec', None)) tmp_env = Haxball() observation_space = tmp_env.observation_space action_space = tmp_env.action_space # spec = tmp_env.spec # observation_space, action_space, spec = self.connections[0].recv() self.observation_space = observation_space self.action_space = action_space self.keys, shapes, dtypes = obs_space_info(self.observation_space) self.waiting = False
def __init__(self, env_fns): """ Arguments: env_fns: iterable of callables functions that build environments """ self.envs = [fn() for fn in env_fns] env = self.envs[0] VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space) obs_space = env.observation_space self.n_actors = env.n_actors self.keys, shapes, dtypes = obs_space_info(obs_space) self.buf_obs = { k: np.zeros(( self.num_envs, self.n_actors, ) + tuple(shapes[k]), dtype=dtypes[k]) for k in self.keys } self.buf_dones = np.zeros((self.num_envs, ), dtype=np.bool) self.buf_rews = np.zeros(( self.num_envs, env.n_actors, ), dtype=np.float32) self.buf_infos = [{} for _ in range(self.num_envs)] self.actions = None self.spec = self.envs[0].spec
def __init__(self, env_fns, spaces=None, context='spawn'): """ If you don't specify observation_space, we'll have to create a dummy environment to get it. """ ctx = mp.get_context(context) if spaces: observation_space, action_space = spaces else: logger.log('Creating dummy env object to get spaces') with logger.scoped_configure(format_strs=[]): dummy = env_fns[0]() observation_space, action_space = dummy.observation_space, dummy.action_space dummy.close() del dummy VecEnv.__init__(self, len(env_fns), observation_space, action_space) self.obs_keys, self.obs_shapes, self.obs_dtypes = obs_space_info(observation_space) self.obs_bufs = [ {k: ctx.Array(_NP_TO_CT[self.obs_dtypes[k].type], int(np.prod(self.obs_shapes[k]))) for k in self.obs_keys} for _ in env_fns] self.parent_pipes = [] self.procs = [] with clear_mpi_env_vars(): for env_fn, obs_buf in zip(env_fns, self.obs_bufs): wrapped_fn = CloudpickleWrapper(env_fn) parent_pipe, child_pipe = ctx.Pipe() proc = ctx.Process(target=_subproc_worker, args=(child_pipe, parent_pipe, wrapped_fn, obs_buf, self.obs_shapes, self.obs_dtypes, self.obs_keys)) proc.daemon = True self.procs.append(proc) self.parent_pipes.append(parent_pipe) proc.start() child_pipe.close() self.waiting_step = False self.viewer = None
def __init__(self, num_fields, max_ticks=2400): self.num_fields = num_fields self.num_envs = num_fields * 2 self.envs = [] for i in range(num_fields): gameplay = Ccreate_start_conditions() env = Haxball(gameplay=gameplay, max_ticks=max_ticks) self.envs.append(env) env = self.envs[0] self.observation_space = env.observation_space self.action_space = env.action_space self.keys, shapes, dtypes = obs_space_info(self.observation_space) self.buf_obs = { k: np.zeros((self.num_envs, ) + tuple(shapes[k]), dtype=dtypes[k]) for k in self.keys } self.buf_dones = np.zeros((self.num_envs, ), dtype=np.bool) self.buf_rews = np.zeros((self.num_envs, ), dtype=np.float32) self.buf_infos = [{} for _ in range(self.num_envs)] self.actions = None self.spec = self.envs[0].spec