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 |
|
__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 |
|
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 |
|
clear()
Clear all contents of the buffer.
Source code in robot_nav/replay_buffer.py
95 96 97 98 99 100 |
|
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 |
|
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 |
|
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 |
|
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 |
|
__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 |
|
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 |
|
clear()
Clear all stored episodes from the buffer.
Source code in robot_nav/replay_buffer.py
216 217 218 219 220 221 |
|
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 |
|
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 |
|