Skip to content

Replay/Rollout Buffer

robot_nav.replay_buffer

ReplayBuffer

Bases: object

Standard experience replay buffer for off-policy reinforcement learning algorithms.

Stores tuples of (state, action, reward, done, next_state) up to a fixed capacity, enabling sampling of uncorrelated mini-batches for training.

Source code in robot_nav/replay_buffer.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
class ReplayBuffer(object):
    """
    Standard experience replay buffer for off-policy reinforcement learning algorithms.

    Stores tuples of (state, action, reward, done, next_state) up to a fixed capacity,
    enabling sampling of uncorrelated mini-batches for training.
    """

    def __init__(self, buffer_size, random_seed=123):
        """
        Initialize the replay buffer.

        Args:
            buffer_size (int): Maximum number of transitions to store in the buffer.
            random_seed (int): Seed for random number generation.
        """
        self.buffer_size = buffer_size
        self.count = 0
        self.buffer = deque()
        random.seed(random_seed)

    def add(self, s, a, r, t, s2):
        """
        Add a transition to the buffer.

        Args:
            s (np.ndarray): State.
            a (np.ndarray): Action.
            r (float): Reward.
            t (bool): Done flag (True if episode ended).
            s2 (np.ndarray): Next state.
        """
        experience = (s, a, r, t, s2)
        if self.count < self.buffer_size:
            self.buffer.append(experience)
            self.count += 1
        else:
            self.buffer.popleft()
            self.buffer.append(experience)

    def size(self):
        """
        Get the number of elements currently in the buffer.

        Returns:
            int: Current buffer size.
        """
        return self.count

    def sample_batch(self, batch_size):
        """
        Sample a batch of experiences from the buffer.

        Args:
            batch_size (int): Number of experiences to sample.

        Returns:
            Tuple of np.ndarrays: Batches of states, actions, rewards, done flags, and next states.
        """
        if self.count < batch_size:
            batch = random.sample(self.buffer, self.count)
        else:
            batch = random.sample(self.buffer, batch_size)

        s_batch = np.array([_[0] for _ in batch])
        a_batch = np.array([_[1] for _ in batch])
        r_batch = np.array([_[2] for _ in batch]).reshape(-1, 1)
        t_batch = np.array([_[3] for _ in batch]).reshape(-1, 1)
        s2_batch = np.array([_[4] for _ in batch])

        return s_batch, a_batch, r_batch, t_batch, s2_batch

    def return_buffer(self):
        """
        Return the entire buffer contents as separate arrays.

        Returns:
            Tuple of np.ndarrays: Full arrays of states, actions, rewards, done flags, and next states.
        """
        s = np.array([_[0] for _ in self.buffer])
        a = np.array([_[1] for _ in self.buffer])
        r = np.array([_[2] for _ in self.buffer]).reshape(-1, 1)
        t = np.array([_[3] for _ in self.buffer]).reshape(-1, 1)
        s2 = np.array([_[4] for _ in self.buffer])

        return s, a, r, t, s2

    def clear(self):
        """
        Clear all contents of the buffer.
        """
        self.buffer.clear()
        self.count = 0

__init__(buffer_size, random_seed=123)

Initialize the replay buffer.

Parameters:

Name Type Description Default
buffer_size int

Maximum number of transitions to store in the buffer.

required
random_seed int

Seed for random number generation.

123
Source code in robot_nav/replay_buffer.py
16
17
18
19
20
21
22
23
24
25
26
27
def __init__(self, buffer_size, random_seed=123):
    """
    Initialize the replay buffer.

    Args:
        buffer_size (int): Maximum number of transitions to store in the buffer.
        random_seed (int): Seed for random number generation.
    """
    self.buffer_size = buffer_size
    self.count = 0
    self.buffer = deque()
    random.seed(random_seed)

add(s, a, r, t, s2)

Add a transition to the buffer.

Parameters:

Name Type Description Default
s ndarray

State.

required
a ndarray

Action.

required
r float

Reward.

required
t bool

Done flag (True if episode ended).

required
s2 ndarray

Next state.

required
Source code in robot_nav/replay_buffer.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def add(self, s, a, r, t, s2):
    """
    Add a transition to the buffer.

    Args:
        s (np.ndarray): State.
        a (np.ndarray): Action.
        r (float): Reward.
        t (bool): Done flag (True if episode ended).
        s2 (np.ndarray): Next state.
    """
    experience = (s, a, r, t, s2)
    if self.count < self.buffer_size:
        self.buffer.append(experience)
        self.count += 1
    else:
        self.buffer.popleft()
        self.buffer.append(experience)

clear()

Clear all contents of the buffer.

Source code in robot_nav/replay_buffer.py
 95
 96
 97
 98
 99
100
def clear(self):
    """
    Clear all contents of the buffer.
    """
    self.buffer.clear()
    self.count = 0

return_buffer()

Return the entire buffer contents as separate arrays.

Returns:

Type Description

Tuple of np.ndarrays: Full arrays of states, actions, rewards, done flags, and next states.

Source code in robot_nav/replay_buffer.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def return_buffer(self):
    """
    Return the entire buffer contents as separate arrays.

    Returns:
        Tuple of np.ndarrays: Full arrays of states, actions, rewards, done flags, and next states.
    """
    s = np.array([_[0] for _ in self.buffer])
    a = np.array([_[1] for _ in self.buffer])
    r = np.array([_[2] for _ in self.buffer]).reshape(-1, 1)
    t = np.array([_[3] for _ in self.buffer]).reshape(-1, 1)
    s2 = np.array([_[4] for _ in self.buffer])

    return s, a, r, t, s2

sample_batch(batch_size)

Sample a batch of experiences from the buffer.

Parameters:

Name Type Description Default
batch_size int

Number of experiences to sample.

required

Returns:

Type Description

Tuple of np.ndarrays: Batches of states, actions, rewards, done flags, and next states.

Source code in robot_nav/replay_buffer.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def sample_batch(self, batch_size):
    """
    Sample a batch of experiences from the buffer.

    Args:
        batch_size (int): Number of experiences to sample.

    Returns:
        Tuple of np.ndarrays: Batches of states, actions, rewards, done flags, and next states.
    """
    if self.count < batch_size:
        batch = random.sample(self.buffer, self.count)
    else:
        batch = random.sample(self.buffer, batch_size)

    s_batch = np.array([_[0] for _ in batch])
    a_batch = np.array([_[1] for _ in batch])
    r_batch = np.array([_[2] for _ in batch]).reshape(-1, 1)
    t_batch = np.array([_[3] for _ in batch]).reshape(-1, 1)
    s2_batch = np.array([_[4] for _ in batch])

    return s_batch, a_batch, r_batch, t_batch, s2_batch

size()

Get the number of elements currently in the buffer.

Returns:

Name Type Description
int

Current buffer size.

Source code in robot_nav/replay_buffer.py
48
49
50
51
52
53
54
55
def size(self):
    """
    Get the number of elements currently in the buffer.

    Returns:
        int: Current buffer size.
    """
    return self.count

RolloutReplayBuffer

Bases: object

Replay buffer that stores full episode rollouts, allowing access to historical trajectories.

Useful for algorithms that condition on sequences of past states (e.g., RNN-based policies).

Source code in robot_nav/replay_buffer.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
class RolloutReplayBuffer(object):
    """
    Replay buffer that stores full episode rollouts, allowing access to historical trajectories.

    Useful for algorithms that condition on sequences of past states (e.g., RNN-based policies).
    """

    def __init__(self, buffer_size, random_seed=123, history_len=10):
        """
        Initialize the rollout replay buffer.

        Args:
            buffer_size (int): Maximum number of episodes (rollouts) to store.
            random_seed (int): Seed for random number generation.
            history_len (int): Number of past steps to return for each sampled state.
        """
        self.buffer_size = buffer_size
        self.count = 0
        self.buffer = deque(maxlen=buffer_size)
        random.seed(random_seed)
        self.buffer.append([])
        self.history_len = history_len

    def add(self, s, a, r, t, s2):
        """
        Add a transition to the current episode.

        If the transition ends the episode (t=True), a new episode is started.

        Args:
            s (np.ndarray): State.
            a (np.ndarray): Action.
            r (float): Reward.
            t (bool): Done flag.
            s2 (np.ndarray): Next state.
        """
        experience = (s, a, r, t, s2)
        if t:
            self.count += 1
            self.buffer[-1].append(experience)
            self.buffer.append([])
        else:
            self.buffer[-1].append(experience)

    def size(self):
        """
        Get the number of complete episodes in the buffer.

        Returns:
            int: Number of episodes.
        """
        return self.count

    def sample_batch(self, batch_size):
        """
        Sample a batch of state sequences and corresponding transitions from full episodes.

        Returns past `history_len` steps for each sampled transition, padded with the earliest step if necessary.

        Args:
            batch_size (int): Number of sequences to sample.

        Returns:
            Tuple of np.ndarrays: Sequences of past states, actions, rewards, done flags, and next states.
        """
        if self.count < batch_size:
            batch = random.sample(
                list(itertools.islice(self.buffer, 0, len(self.buffer) - 1)), self.count
            )
        else:
            batch = random.sample(
                list(itertools.islice(self.buffer, 0, len(self.buffer) - 1)), batch_size
            )

        idx = [random.randint(0, len(b) - 1) for b in batch]

        s_batch = []
        s2_batch = []
        for i in range(len(batch)):
            if idx[i] == len(batch[i]):
                s = batch[i]
                s2 = batch[i]
            else:
                s = batch[i][: idx[i] + 1]
                s2 = batch[i][: idx[i] + 1]
            s = [v[0] for v in s]
            s = s[::-1]

            s2 = [v[4] for v in s2]
            s2 = s2[::-1]

            if len(s) < self.history_len:
                missing = self.history_len - len(s)
                s += [s[-1]] * missing
                s2 += [s2[-1]] * missing
            else:
                s = s[: self.history_len]
                s2 = s2[: self.history_len]
            s = s[::-1]
            s_batch.append(s)
            s2 = s2[::-1]
            s2_batch.append(s2)

        a_batch = np.array([batch[i][idx[i]][1] for i in range(len(batch))])
        r_batch = np.array([batch[i][idx[i]][2] for i in range(len(batch))]).reshape(
            -1, 1
        )
        t_batch = np.array([batch[i][idx[i]][3] for i in range(len(batch))]).reshape(
            -1, 1
        )

        return np.array(s_batch), a_batch, r_batch, t_batch, np.array(s2_batch)

    def clear(self):
        """
        Clear all stored episodes from the buffer.
        """
        self.buffer.clear()
        self.count = 0

__init__(buffer_size, random_seed=123, history_len=10)

Initialize the rollout replay buffer.

Parameters:

Name Type Description Default
buffer_size int

Maximum number of episodes (rollouts) to store.

required
random_seed int

Seed for random number generation.

123
history_len int

Number of past steps to return for each sampled state.

10
Source code in robot_nav/replay_buffer.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def __init__(self, buffer_size, random_seed=123, history_len=10):
    """
    Initialize the rollout replay buffer.

    Args:
        buffer_size (int): Maximum number of episodes (rollouts) to store.
        random_seed (int): Seed for random number generation.
        history_len (int): Number of past steps to return for each sampled state.
    """
    self.buffer_size = buffer_size
    self.count = 0
    self.buffer = deque(maxlen=buffer_size)
    random.seed(random_seed)
    self.buffer.append([])
    self.history_len = history_len

add(s, a, r, t, s2)

Add a transition to the current episode.

If the transition ends the episode (t=True), a new episode is started.

Parameters:

Name Type Description Default
s ndarray

State.

required
a ndarray

Action.

required
r float

Reward.

required
t bool

Done flag.

required
s2 ndarray

Next state.

required
Source code in robot_nav/replay_buffer.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def add(self, s, a, r, t, s2):
    """
    Add a transition to the current episode.

    If the transition ends the episode (t=True), a new episode is started.

    Args:
        s (np.ndarray): State.
        a (np.ndarray): Action.
        r (float): Reward.
        t (bool): Done flag.
        s2 (np.ndarray): Next state.
    """
    experience = (s, a, r, t, s2)
    if t:
        self.count += 1
        self.buffer[-1].append(experience)
        self.buffer.append([])
    else:
        self.buffer[-1].append(experience)

clear()

Clear all stored episodes from the buffer.

Source code in robot_nav/replay_buffer.py
216
217
218
219
220
221
def clear(self):
    """
    Clear all stored episodes from the buffer.
    """
    self.buffer.clear()
    self.count = 0

sample_batch(batch_size)

Sample a batch of state sequences and corresponding transitions from full episodes.

Returns past history_len steps for each sampled transition, padded with the earliest step if necessary.

Parameters:

Name Type Description Default
batch_size int

Number of sequences to sample.

required

Returns:

Type Description

Tuple of np.ndarrays: Sequences of past states, actions, rewards, done flags, and next states.

Source code in robot_nav/replay_buffer.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
def sample_batch(self, batch_size):
    """
    Sample a batch of state sequences and corresponding transitions from full episodes.

    Returns past `history_len` steps for each sampled transition, padded with the earliest step if necessary.

    Args:
        batch_size (int): Number of sequences to sample.

    Returns:
        Tuple of np.ndarrays: Sequences of past states, actions, rewards, done flags, and next states.
    """
    if self.count < batch_size:
        batch = random.sample(
            list(itertools.islice(self.buffer, 0, len(self.buffer) - 1)), self.count
        )
    else:
        batch = random.sample(
            list(itertools.islice(self.buffer, 0, len(self.buffer) - 1)), batch_size
        )

    idx = [random.randint(0, len(b) - 1) for b in batch]

    s_batch = []
    s2_batch = []
    for i in range(len(batch)):
        if idx[i] == len(batch[i]):
            s = batch[i]
            s2 = batch[i]
        else:
            s = batch[i][: idx[i] + 1]
            s2 = batch[i][: idx[i] + 1]
        s = [v[0] for v in s]
        s = s[::-1]

        s2 = [v[4] for v in s2]
        s2 = s2[::-1]

        if len(s) < self.history_len:
            missing = self.history_len - len(s)
            s += [s[-1]] * missing
            s2 += [s2[-1]] * missing
        else:
            s = s[: self.history_len]
            s2 = s2[: self.history_len]
        s = s[::-1]
        s_batch.append(s)
        s2 = s2[::-1]
        s2_batch.append(s2)

    a_batch = np.array([batch[i][idx[i]][1] for i in range(len(batch))])
    r_batch = np.array([batch[i][idx[i]][2] for i in range(len(batch))]).reshape(
        -1, 1
    )
    t_batch = np.array([batch[i][idx[i]][3] for i in range(len(batch))]).reshape(
        -1, 1
    )

    return np.array(s_batch), a_batch, r_batch, t_batch, np.array(s2_batch)

size()

Get the number of complete episodes in the buffer.

Returns:

Name Type Description
int

Number of episodes.

Source code in robot_nav/replay_buffer.py
147
148
149
150
151
152
153
154
def size(self):
    """
    Get the number of complete episodes in the buffer.

    Returns:
        int: Number of episodes.
    """
    return self.count