ray/rllib/execution/replay_ops.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

37 lines
1.2 KiB
Python
Raw Normal View History

from typing import Optional
import random
from ray.rllib.utils.replay_buffers.replay_buffer import warn_replay_capacity
from ray.rllib.utils.typing import SampleBatchType
# TODO(ekl) deprecate this in favor of the replay_sequence_length option.
2020-05-12 13:07:19 -07:00
class SimpleReplayBuffer:
"""Simple replay buffer that operates over batches."""
2020-12-24 06:30:33 -08:00
def __init__(self, num_slots: int, replay_proportion: Optional[float] = None):
2020-05-12 13:07:19 -07:00
"""Initialize SimpleReplayBuffer.
Args:
num_slots: Number of batches to store in total.
2020-05-12 13:07:19 -07:00
"""
self.num_slots = num_slots
self.replay_batches = []
self.replay_index = 0
2020-12-24 06:30:33 -08:00
def add_batch(self, sample_batch: SampleBatchType) -> None:
warn_replay_capacity(item=sample_batch, num_items=self.num_slots)
2020-05-12 13:07:19 -07:00
if self.num_slots > 0:
if len(self.replay_batches) < self.num_slots:
self.replay_batches.append(sample_batch)
else:
self.replay_batches[self.replay_index] = sample_batch
self.replay_index += 1
self.replay_index %= self.num_slots
2020-12-24 06:30:33 -08:00
def replay(self) -> SampleBatchType:
2020-05-12 13:07:19 -07:00
return random.choice(self.replay_batches)
def __len__(self):
return len(self.replay_batches)