bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
env_base.h
Go to the documentation of this file.
1#ifndef ENV_BASE_H
2#define ENV_BASE_H
3
5#include "bitrl/bitrl_types.h"
6
7#include <any>
8#include <string>
9#include <type_traits>
10#include <unordered_map>
11
12namespace bitrl
13{
14namespace envs
15{
16
29template <typename TimeStepType, typename SpaceType> class EnvBase : public SpaceType
30{
31 public:
32 static_assert(std::is_default_constructible<TimeStepType>::value &&
33 "TimeStepType should be default constructible");
34 static_assert(std::is_default_constructible<SpaceType>::value &&
35 "SpaceType should be default constructible");
36
38 static const uint_t DEFAULT_ENV_SEED = 42;
39
41 typedef TimeStepType time_step_type;
42
44 typedef typename SpaceType::state_space state_space_type;
45
47 typedef typename SpaceType::state_type state_type;
48
50 typedef typename SpaceType::action_space action_space_type;
51
53 typedef typename SpaceType::action_type action_type;
54
56 virtual ~EnvBase() = default;
57
68 virtual void make(const std::string &version,
69 const std::unordered_map<std::string, std::any> &make_options,
70 const std::unordered_map<std::string, std::any> &reset_options) = 0;
71
73 virtual void close() = 0;
74
81 virtual time_step_type reset() = 0;
82
89 virtual time_step_type step(const action_type &action) = 0;
90
95 const std::unordered_map<std::string, std::any> &make_options() const noexcept
96 {
97 return make_options_;
98 }
99
104 const std::unordered_map<std::string, std::any> &reset_options() const noexcept
105 {
106 return reset_options_;
107 }
108
117 template <typename T> T read_option(const std::string &op_name) const;
118
124 std::string idx() const noexcept { return idx_; }
125
130 bool is_created() const noexcept { return is_created_; }
131
136 std::string env_name() const noexcept { return name_; }
137
142 std::string version() const noexcept { return version_; }
143
144 protected:
150 explicit EnvBase(const std::string &idx = bitrl::consts::INVALID_STR,
151 const std::string &name = bitrl::consts::INVALID_STR);
152
154 EnvBase(const EnvBase &);
155
160 void set_version_(const std::string &version) noexcept { version_ = version; }
161
166 void set_idx_(const std::string &idx) noexcept { idx_ = idx; }
167
169 void set_make_options_(const std::unordered_map<std::string, std::any> &options) noexcept
170 {
171 make_options_ = options;
172 }
173
175 void set_reset_options_(const std::unordered_map<std::string, std::any> &options) noexcept
176 {
177 reset_options_ = options;
178 }
179
181 void invalidate_is_created_flag_() noexcept { is_created_ = false; }
182
184 void make_created_() noexcept { is_created_ = true; }
185
187 time_step_type &get_current_time_step_() noexcept { return current_state_; }
188
190 const time_step_type &get_current_time_step_() const noexcept { return current_state_; }
191
192 private:
193 bool is_created_;
194 std::string idx_;
195 std::string version_;
196 const std::string name_;
197 std::unordered_map<std::string, std::any> make_options_;
198 std::unordered_map<std::string, std::any> reset_options_;
199 time_step_type current_state_;
200};
201
202template <typename TimeStepType, typename SpaceType>
203EnvBase<TimeStepType, SpaceType>::EnvBase(const std::string &idx, const std::string &name)
204 : SpaceType(), is_created_(false), idx_(idx), version_(), name_(name), current_state_()
205{
206}
207
208template <typename TimeStepType, typename SpaceType>
210 : SpaceType(), is_created_(other.is_created_), idx_(other.idx_), version_(other.version_),
211 name_(other.name_), current_state_()
212{
213}
214
215template <typename TimeStepType, typename SpaceType> void EnvBase<TimeStepType, SpaceType>::close()
216{
217 this->is_created_ = false;
218}
219
220template <typename TimeStepType, typename SpaceType>
221template <typename T>
222T EnvBase<TimeStepType, SpaceType>::read_option(const std::string &op_name) const
223{
224
225 auto op_itr = make_options_.find(op_name);
226 if (op_itr != make_options_.end())
227 {
228 return std::any_cast<T>(op_itr->second);
229 }
230
231 throw std::logic_error("Option: " + op_name + " not found");
232}
233
234template <typename TimeStepType, typename SpaceType>
236 const std::string &version, const std::unordered_map<std::string, std::any> &make_options,
237 const std::unordered_map<std::string, std::any> &reset_options)
238{
239 version_ = version;
240 make_options_ = make_options;
241 reset_options_ = reset_options;
242}
243
244} // namespace envs
245} // namespace bitrl
246
247#endif // ENV_BASE_H
Base class interface for Reinforcement Learning environments.
Definition env_base.h:30
SpaceType::state_space state_space_type
Type describing the environment state space.
Definition env_base.h:44
void invalidate_is_created_flag_() noexcept
Mark environment as not created.
Definition env_base.h:181
SpaceType::state_type state_type
Type describing an individual state.
Definition env_base.h:47
virtual time_step_type step(const action_type &action)=0
Perform one step in the environment using an action.
const std::unordered_map< std::string, std::any > & reset_options() const noexcept
Access the configuration options provided to make().
Definition env_base.h:104
virtual void make(const std::string &version, const std::unordered_map< std::string, std::any > &make_options, const std::unordered_map< std::string, std::any > &reset_options)=0
Construct the environment instance.
Definition env_base.h:235
const time_step_type & get_current_time_step_() const noexcept
Read-only access to the current time step.
Definition env_base.h:190
void set_idx_(const std::string &idx) noexcept
Set the id of the environment.
Definition env_base.h:166
EnvBase(const std::string &idx=bitrl::consts::INVALID_STR, const std::string &name=bitrl::consts::INVALID_STR)
Constructor (protected — for subclassing only).
Definition env_base.h:203
SpaceType::action_space action_space_type
Type describing the environment action space.
Definition env_base.h:50
virtual time_step_type reset()=0
Reset the environment to an initial state using the reset options specified during make.
void set_reset_options_(const std::unordered_map< std::string, std::any > &options) noexcept
Store reset options for future access.
Definition env_base.h:175
TimeStepType time_step_type
Alias for the type returned when stepping the environment.
Definition env_base.h:41
virtual void close()=0
Close and release any acquired environment resources.
Definition env_base.h:215
const std::unordered_map< std::string, std::any > & make_options() const noexcept
Access the configuration options provided to make().
Definition env_base.h:95
SpaceType::action_type action_type
Type representing an individual action.
Definition env_base.h:53
T read_option(const std::string &op_name) const
Read a specific make() option and cast it to the requested type.
Definition env_base.h:222
virtual ~EnvBase()=default
Virtual destructor.
std::string version() const noexcept
Get the environment version set during make().
Definition env_base.h:142
time_step_type & get_current_time_step_() noexcept
Mutable access to the current time step.
Definition env_base.h:187
std::string idx() const noexcept
Get the id identifying this environment within a simulation batch. The id is valid only if make has b...
Definition env_base.h:124
void make_created_() noexcept
Mark environment creation as successful.
Definition env_base.h:184
bool is_created() const noexcept
Check if make() has successfully initialized the environment.
Definition env_base.h:130
std::string env_name() const noexcept
Get the name of this environment instance.
Definition env_base.h:136
void set_make_options_(const std::unordered_map< std::string, std::any > &options) noexcept
Store options for future access.
Definition env_base.h:169
static const uint_t DEFAULT_ENV_SEED
Default seed used in reset() if none provided.
Definition env_base.h:38
void set_version_(const std::string &version) noexcept
Set internal version string.
Definition env_base.h:160
EnvBase(const EnvBase &)
Copy constructor.
Definition env_base.h:209
const std::string INVALID_STR
Invalid string.
Definition bitrl_consts.h:26
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43