1#ifndef EXPERIENCE_BUFFER_H
2#define EXPERIENCE_BUFFER_H
4#include "bitrl/base/bitrl_config.h"
11#include <boost/circular_buffer.hpp>
12#include <boost/noncopyable.hpp>
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
73 bool empty() const noexcept {
return buffer_.empty(); }
78 void clear() noexcept { buffer_.clear(); }
103 template <
typename BatchTp>
109 template <
typename ContainerType>
void get(ContainerType &container)
const;
114 template <
typename ContainerType> ContainerType
get()
const;
132 boost::circular_buffer<ExperienceType> buffer_;
135template <
typename ExperienceTp>
141template <
typename ExperienceTp>
146template <
typename ExperienceTp>
150 buffer_.push_back(experience);
153template <
typename ExperienceTp>
156 buffer_.resize(new_size, item);
159template <
typename ExperienceTp>
160template <
typename BatchTp>
165 assert(!
empty() &&
"Cannot sample from an empty buffer");
171 assert(
false &&
"Cannot sample with zero batch_size");
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());
181 std::mt19937 generator(seed);
182 for (
uint_t c = 0; c < batch_size; ++c)
185 auto idx = distribution(generator);
186 batch.push_back(buffer_[idx]);
190template <
typename ExperienceType>
191template <
typename ContainerType>
195 for (
auto val : buffer_)
197 container.push_back(val);
201template <
typename ExperienceType>
202template <
typename ContainerType>
206 ContainerType container_;
207 container_.reserve(
size());
208 for (
auto val : buffer_)
210 container_.push_back(val);
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