bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
experience_buffer.h
Go to the documentation of this file.
1#ifndef EXPERIENCE_BUFFER_H
2#define EXPERIENCE_BUFFER_H
3
4#include "bitrl/base/bitrl_config.h"
6
7#ifdef BITRL_DEBUG
8#include <cassert>
9#endif
10
11#include <boost/circular_buffer.hpp>
12#include <boost/noncopyable.hpp>
13
14#include <random>
15#include <vector>
16
17namespace bitrl
18{
19namespace utils
20{
21
27template <typename ExperienceType> class ExperienceBuffer : private boost::noncopyable
28{
29
30 public:
31 static const uint_t DEFAULT_CAPACITY = 100;
32
33 typedef ExperienceType value_type;
34 typedef ExperienceType experience_type;
35
36 typedef typename boost::circular_buffer<ExperienceType>::iterator iterator;
37 typedef typename boost::circular_buffer<ExperienceType>::const_iterator const_iterator;
38 typedef typename boost::circular_buffer<ExperienceType>::reverse_iterator reverse_iterator;
39 typedef typename boost::circular_buffer<ExperienceType>::const_reverse_iterator
41
46
51
55 void append(const experience_type &experience);
56
61 uint_t size() const noexcept { return static_cast<uint_t>(buffer_.size()); }
62
67 uint_t capacity() const noexcept { return static_cast<uint_t>(buffer_.capacity()); }
68
73 bool empty() const noexcept { return buffer_.empty(); }
74
78 void clear() noexcept { buffer_.clear(); }
79
85 value_type &operator[](uint_t idx) { return buffer_[idx]; }
86
90 void resize(uint_t new_size, const experience_type &item = experience_type());
91
97 const value_type &operator[](uint_t idx) const { return buffer_[idx]; }
98
103 template <typename BatchTp>
104 void sample(uint_t batch_size, BatchTp &batch, uint_t seed = 42) const;
105
109 template <typename ContainerType> void get(ContainerType &container) const;
110
114 template <typename ContainerType> ContainerType get() const;
115
116 iterator begin() { return buffer_.begin(); }
117 iterator end() { return buffer_.end(); }
118
119 const_iterator begin() const { return buffer_.begin(); }
120 const_iterator end() const { return buffer_.end(); }
121
122 reverse_iterator rbegin() { return buffer_.rbegin(); }
123 reverse_iterator rend() { return buffer_.rend(); }
124
125 const_reverse_iterator rbegin() const { return buffer_.rbegin(); }
126 const_reverse_iterator rend() const { return buffer_.rend(); }
127
128 private:
132 boost::circular_buffer<ExperienceType> buffer_;
133};
134
135template <typename ExperienceTp>
140
141template <typename ExperienceTp>
143{
144}
145
146template <typename ExperienceTp>
148{
149
150 buffer_.push_back(experience);
151}
152
153template <typename ExperienceTp>
155{
156 buffer_.resize(new_size, item);
157}
158
159template <typename ExperienceTp>
160template <typename BatchTp>
161void ExperienceBuffer<ExperienceTp>::sample(uint_t batch_size, BatchTp &batch, uint_t seed) const
162{
163
164#ifdef BITRL_DEBUG
165 assert(!empty() && "Cannot sample from an empty buffer");
166#endif
167
168 if (batch_size == 0)
169 {
170#ifdef BITRL_DEBUG
171 assert(false && "Cannot sample with zero batch_size");
172#endif
173
174 return;
175 }
176
177 // uniformly initialize weights
178 std::vector<real_t> weights(size(), 1.0 / static_cast<real_t>(size()));
179 std::discrete_distribution<uint_t> distribution(weights.begin(), weights.end());
180
181 std::mt19937 generator(seed);
182 for (uint_t c = 0; c < batch_size; ++c)
183 {
184
185 auto idx = distribution(generator);
186 batch.push_back(buffer_[idx]);
187 }
188}
189
190template <typename ExperienceType>
191template <typename ContainerType>
192void ExperienceBuffer<ExperienceType>::get(ContainerType &container) const
193{
194
195 for (auto val : buffer_)
196 {
197 container.push_back(val);
198 }
199}
200
201template <typename ExperienceType>
202template <typename ContainerType>
204{
205
206 ContainerType container_;
207 container_.reserve(size());
208 for (auto val : buffer_)
209 {
210 container_.push_back(val);
211 }
212
213 return container_;
214}
215
216} // namespace utils
217} // namespace bitrl
218
219#endif // EXPERIENCE_BUFFER_H
void get(ContainerType &container) const
Copy the contents of the buffer to the given container.
Definition experience_buffer.h:192
void sample(uint_t batch_size, BatchTp &batch, uint_t seed=42) const
sample. Sample batch_size experiences from the buffer and transfer them in the BatchTp container.
Definition experience_buffer.h:161
experience_tuple_type value_type
Definition experience_buffer.h:33
boost::circular_buffer< experience_tuple_type >::reverse_iterator reverse_iterator
Definition experience_buffer.h:38
reverse_iterator rbegin()
Definition experience_buffer.h:122
void clear() noexcept
clear
Definition experience_buffer.h:78
void resize(uint_t new_size, const experience_type &item=experience_type())
Resize the buffer.
Definition experience_buffer.h:154
boost::circular_buffer< experience_tuple_type >::iterator iterator
Definition experience_buffer.h:36
static const uint_t DEFAULT_CAPACITY
Definition experience_buffer.h:31
boost::circular_buffer< experience_tuple_type >::const_iterator const_iterator
Definition experience_buffer.h:37
value_type & operator[](uint_t idx)
operator []
Definition experience_buffer.h:85
ExperienceBuffer(uint_t capacity)
ExperienceBuffer.
Definition experience_buffer.h:142
bool empty() const noexcept
empty. Returns true if the buffer is empty
Definition experience_buffer.h:73
const_iterator end() const
Definition experience_buffer.h:120
uint_t size() const noexcept
size
Definition experience_buffer.h:61
experience_tuple_type experience_type
Definition experience_buffer.h:34
iterator begin()
Definition experience_buffer.h:116
iterator end()
Definition experience_buffer.h:117
ExperienceBuffer()
ExperienceBuffer.
Definition experience_buffer.h:136
boost::circular_buffer< experience_tuple_type >::const_reverse_iterator const_reverse_iterator
Definition experience_buffer.h:40
const value_type & operator[](uint_t idx) const
operator []
Definition experience_buffer.h:97
ContainerType get() const
Copy the contents of the buffer to the given container.
Definition experience_buffer.h:203
const_iterator begin() const
Definition experience_buffer.h:119
reverse_iterator rend()
Definition experience_buffer.h:123
uint_t capacity() const noexcept
Definition experience_buffer.h:67
void append(const experience_type &experience)
append Add the experience item in the buffer
Definition experience_buffer.h:147
const_reverse_iterator rbegin() const
Definition experience_buffer.h:125
const_reverse_iterator rend() const
Definition experience_buffer.h:126
Definition bitrl_concepts.h:22
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20