1. add policy.eval() in all test scripts' "watch performance" 2. remove dict return support for collector preprocess_fn 3. add `__contains__` and `pop` in batch: `key in batch`, `batch.pop(key, deft)` 4. exact n_episode for a list of n_episode limitation and save fake data in cache_buffer when self.buffer is None (#184) 5. fix tensorboard logging: h-axis stands for env step instead of gradient step; add test results into tensorboard 6. add test_returns (both GAE and nstep) 7. change the type-checking order in batch.py and converter.py in order to meet the most often case first 8. fix shape inconsistency for torch.Tensor in replay buffer 9. remove `**kwargs` in ReplayBuffer 10. remove default value in batch.split() and add merge_last argument (#185) 11. improve nstep efficiency 12. add max_batchsize in onpolicy algorithms 13. potential bugfix for subproc.wait 14. fix RecurrentActorProb 15. improve the code-coverage (from 90% to 95%) and remove the dead code 16. fix some incorrect type annotation The above improvement also increases the training FPS: on my computer, the previous version is only ~1800 FPS and after that, it can reach ~2050 (faster than v0.2.4.post1).
66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
import gym
|
|
import numpy as np
|
|
from abc import ABC, abstractmethod
|
|
from typing import List, Tuple, Optional, Callable, Any
|
|
|
|
|
|
class EnvWorker(ABC):
|
|
"""An abstract worker for an environment."""
|
|
|
|
def __init__(self, env_fn: Callable[[], gym.Env]) -> None:
|
|
self._env_fn = env_fn
|
|
self.is_closed = False
|
|
self.result = (None, None, None, None)
|
|
|
|
@abstractmethod
|
|
def __getattr__(self, key: str) -> Any:
|
|
pass
|
|
|
|
@abstractmethod
|
|
def reset(self) -> Any:
|
|
pass
|
|
|
|
@abstractmethod
|
|
def send_action(self, action: np.ndarray) -> None:
|
|
pass
|
|
|
|
def get_result(self) -> Tuple[
|
|
np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
|
|
return self.result
|
|
|
|
def step(self, action: np.ndarray
|
|
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
|
|
"""``send_action`` and ``get_result`` are coupled in sync simulation,
|
|
so typically users only call ``step`` function. But they can be called
|
|
separately in async simulation, i.e. someone calls ``send_action``
|
|
first, and calls ``get_result`` later.
|
|
"""
|
|
self.send_action(action)
|
|
return self.get_result()
|
|
|
|
@staticmethod
|
|
def wait(workers: List['EnvWorker'],
|
|
wait_num: int,
|
|
timeout: Optional[float] = None) -> List['EnvWorker']:
|
|
"""Given a list of workers, return those ready ones."""
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def seed(self, seed: Optional[int] = None) -> List[int]:
|
|
pass
|
|
|
|
@abstractmethod
|
|
def render(self, **kwargs) -> Any:
|
|
"""Renders the environment."""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def close_env(self) -> None:
|
|
pass
|
|
|
|
def close(self) -> None:
|
|
if self.is_closed:
|
|
return None
|
|
self.is_closed = True
|
|
self.close_env()
|