Tianshou/tianshou/data/__init__.py
Juno T d42a5fb354
Hindsight Experience Replay as a replay buffer (#753)
## implementation
I implemented HER solely as a replay buffer. It is done by temporarily
directly re-writing transitions storage (`self._meta`) during the
`sample_indices()` call. The original transitions are cached and will be
restored at the beginning of the next sampling or when other methods is
called. This will make sure that. for example, n-step return calculation
can be done without altering the policy.

There is also a problem with the original indices sampling. The sampled
indices are not guaranteed to be from different episodes. So I decided
to perform re-writing based on the episode. This guarantees that the
sampled transitions from the same episode will have the same re-written
goal. This also make the re-writing ratio calculation slightly differ
from the paper, but it won't be too different if there are many episodes
in the buffer.

In the current commit, HER replay buffer only support 'future' strategy
and online sampling. This is the best of HER in term of performance and
memory efficiency.

I also add a few more convenient replay buffers
(`HERVectorReplayBuffer`, `HERReplayBufferManager`), test env
(`MyGoalEnv`), gym wrapper (`TruncatedAsTerminated`), unit tests, and a
simple example (examples/offline/fetch_her_ddpg.py).

## verification
I have added unit tests for almost everything I have implemented.
HER replay buffer was also tested using DDPG on [`FetchReach-v3`
env](https://github.com/Farama-Foundation/Gymnasium-Robotics). I used
default DDPG parameters from mujoco example and didn't tune anything
further to get this good result! (train script:
examples/offline/fetch_her_ddpg.py).


![Screen Shot 2022-10-02 at 19 22
53](https://user-images.githubusercontent.com/42699114/193454066-0dd0c65c-fd5f-4587-8912-b441d39de88a.png)
2022-10-30 16:54:54 -07:00

42 lines
1.2 KiB
Python

"""Data package."""
# isort:skip_file
from tianshou.data.batch import Batch
from tianshou.data.utils.converter import to_numpy, to_torch, to_torch_as
from tianshou.data.utils.segtree import SegmentTree
from tianshou.data.buffer.base import ReplayBuffer
from tianshou.data.buffer.prio import PrioritizedReplayBuffer
from tianshou.data.buffer.her import HERReplayBuffer
from tianshou.data.buffer.manager import (
ReplayBufferManager,
PrioritizedReplayBufferManager,
HERReplayBufferManager,
)
from tianshou.data.buffer.vecbuf import (
HERVectorReplayBuffer,
PrioritizedVectorReplayBuffer,
VectorReplayBuffer,
)
from tianshou.data.buffer.cached import CachedReplayBuffer
from tianshou.data.collector import Collector, AsyncCollector
__all__ = [
"Batch",
"to_numpy",
"to_torch",
"to_torch_as",
"SegmentTree",
"ReplayBuffer",
"PrioritizedReplayBuffer",
"HERReplayBuffer",
"ReplayBufferManager",
"PrioritizedReplayBufferManager",
"HERReplayBufferManager",
"VectorReplayBuffer",
"PrioritizedVectorReplayBuffer",
"HERVectorReplayBuffer",
"CachedReplayBuffer",
"Collector",
"AsyncCollector",
]