bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
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/bitrl_config.h"
5#include "bitrl/bitrl_types.h"
6
7#ifdef BITRL_DEBUG
8#include <cassert>
9#endif
10
11#include <boost/noncopyable.hpp>
12#include <boost/circular_buffer.hpp>
13
14#include <vector>
15#include <random>
16
17namespace bitrl{
18namespace utils {
19
25template<typename ExperienceType>
26class ExperienceBuffer: private boost::noncopyable{
27
28public:
29
30 static const uint_t DEFAULT_CAPACITY = 100;
31
34
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 const_reverse_iterator;
40
45
50
55
60 uint_t size()const noexcept{return static_cast<uint_t>(buffer_.size());}
61
66 uint_t capacity()const noexcept{return static_cast<uint_t>(buffer_.capacity());}
67
72 bool empty()const noexcept{return buffer_.empty();}
73
77 void clear()noexcept{buffer_.clear();}
78
84 value_type& operator[](uint_t idx){return buffer_[idx];}
85
86
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>
110 void get(ContainerType& container)const;
111
115 template<typename ContainerType>
116 ContainerType get()const;
117
118 iterator begin(){return buffer_.begin();}
119 iterator end(){return buffer_.end();}
120
121 const_iterator begin()const{return buffer_.begin();}
122 const_iterator end()const{return buffer_.end();}
123
124 reverse_iterator rbegin(){return buffer_.rbegin();}
125 reverse_iterator rend(){return buffer_.rend();}
126
127 const_reverse_iterator rbegin()const{return buffer_.rbegin();}
128 const_reverse_iterator rend()const{return buffer_.rend();}
129
130private:
131
135 boost::circular_buffer<ExperienceType> buffer_;
136
137};
138
139template<typename ExperienceTp>
144
145template<typename ExperienceTp>
150
151template<typename ExperienceTp>
152void
157
158template<typename ExperienceTp>
159void
163
164template<typename ExperienceTp>
165template<typename BatchTp>
166void
168 BatchTp& batch, uint_t seed)const{
169
170#ifdef BITRL_DEBUG
171 assert(!empty() && "Cannot sample from an empty buffer");
172#endif
173
174 if(batch_size == 0){
175#ifdef BITRL_DEBUG
176 assert(false && "Cannot sample with zero batch_size");
177#endif
178
179 return;
180
181 }
182
183 // uniformly initialize weights
184 std::vector<real_t> weights(size(), 1.0/static_cast<real_t>(size()));
185 std::discrete_distribution<uint_t> distribution(weights.begin(), weights.end());
186
187 std::mt19937 generator(seed);
188 for(uint_t c=0; c < batch_size; ++c){
189
190 auto idx = distribution(generator);
191 batch.push_back(buffer_[idx]);
192 }
193
194}
195
196template<typename ExperienceType>
197template<typename ContainerType>
198void
200
201 for(auto val: buffer_){
202 container.push_back(val);
203 }
204}
205
206template<typename ExperienceType>
207template<typename ContainerType>
210
212 container_.reserve(size());
213 for(auto val: buffer_){
214 container_.push_back(val);
215 }
216
217 return container_;
218}
219
220}
221}
222
223#endif // EXPERIENCE_BUFFER_H
The ExperienceBuffer class. A buffer based on boost::circular_buffer to accumulate items of type Expe...
Definition experience_buffer.h:26
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:167
ExperienceType value_type
Definition experience_buffer.h:32
boost::circular_buffer< ExperienceType >::reverse_iterator reverse_iterator
Definition experience_buffer.h:38
reverse_iterator rbegin()
Definition experience_buffer.h:124
void clear() noexcept
clear
Definition experience_buffer.h:77
void resize(uint_t new_size, const experience_type &item=experience_type())
Resize the buffer.
Definition experience_buffer.h:160
boost::circular_buffer< ExperienceType >::iterator iterator
Definition experience_buffer.h:36
static const uint_t DEFAULT_CAPACITY
Definition experience_buffer.h:30
boost::circular_buffer< ExperienceType >::const_iterator const_iterator
Definition experience_buffer.h:37
value_type & operator[](uint_t idx)
operator []
Definition experience_buffer.h:84
bool empty() const noexcept
empty. Returns true if the buffer is empty
Definition experience_buffer.h:72
const_iterator end() const
Definition experience_buffer.h:122
uint_t size() const noexcept
size
Definition experience_buffer.h:60
ExperienceType experience_type
Definition experience_buffer.h:33
iterator begin()
Definition experience_buffer.h:118
iterator end()
Definition experience_buffer.h:119
ExperienceBuffer()
ExperienceBuffer.
Definition experience_buffer.h:140
boost::circular_buffer< ExperienceType >::const_reverse_iterator const_reverse_iterator
Definition experience_buffer.h:39
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:209
const_iterator begin() const
Definition experience_buffer.h:121
reverse_iterator rend()
Definition experience_buffer.h:125
uint_t capacity() const noexcept
capacity
Definition experience_buffer.h:66
void append(const experience_type &experience)
append Add the experience item in the buffer
Definition experience_buffer.h:153
const_reverse_iterator rbegin() const
Definition experience_buffer.h:127
const_reverse_iterator rend() const
Definition experience_buffer.h:128
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
double real_t
real_t
Definition bitrl_types.h:23
std::size_t uint_t
uint_t
Definition bitrl_types.h:43