bitsim
Model, simulate, estimate and control robots in C++
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
7
8#include <any>
9#include <string>
10#include <type_traits>
11#include <unordered_map>
12
13namespace bitrl
14{
15namespace envs
16{
17
30template <typename TimeStepType, typename SpaceType> class EnvBase : public SpaceType
31{
32 public:
33 static_assert(std::is_default_constructible<TimeStepType>::value &&
34 "TimeStepType should be default constructible");
35 static_assert(std::is_default_constructible<SpaceType>::value &&
36 "SpaceType should be default constructible");
37
39 static constexpr uint_t DEFAULT_ENV_SEED = 42;
40
42 typedef TimeStepType time_step_type;
43
45 typedef typename SpaceType::state_space state_space_type;
46
48 typedef typename SpaceType::state_type state_type;
49
51 typedef typename SpaceType::action_space action_space_type;
52
54 typedef typename SpaceType::action_type action_type;
55
57 virtual ~EnvBase() = default;
58
69 virtual void make(const std::string &version,
70 const std::unordered_map<std::string, std::any> &make_options,
71 const std::unordered_map<std::string, std::any> &reset_options) = 0;
72
74 virtual void close() = 0;
75
82 virtual time_step_type reset() = 0;
83
90 virtual time_step_type step(const action_type &action) = 0;
91
96 const std::unordered_map<std::string, std::any> &make_options() const noexcept
97 {
98 return make_options_;
99 }
100
105 const std::unordered_map<std::string, std::any> &reset_options() const noexcept
106 {
107 return reset_options_;
108 }
109
118 template <typename T> T read_option(const std::string &op_name) const;
119
125 std::string idx() const noexcept { return idx_; }
126
131 bool is_created() const noexcept { return is_created_; }
132
137 std::string env_name() const noexcept { return name_; }
138
143 std::string version() const noexcept { return version_; }
144
151 uint_t last_reset_seed() const noexcept { return last_reset_seed_; }
152
153 protected:
159 explicit EnvBase(const std::string &idx = bitsim::consts::INVALID_STR,
160 const std::string &name = bitsim::consts::INVALID_STR);
161
163 EnvBase(const EnvBase &);
164
169 void set_version_(const std::string &version) noexcept { version_ = version; }
170
175 void set_idx_(const std::string &idx) noexcept { idx_ = idx; }
176
178 void set_make_options_(const std::unordered_map<std::string, std::any> &options) noexcept
179 {
180 make_options_ = options;
181 }
182
184 void set_reset_options_(const std::unordered_map<std::string, std::any> &options) noexcept
185 {
186 reset_options_ = options;
187 }
188
194 {
195 const uint_t seed = reset_options_.contains("seed")
196 ? bitsim::utils::resolve<uint_t>("seed", reset_options_)
198 last_reset_seed_ = seed + n_resets_++;
199 return last_reset_seed_;
200 }
201
203 void invalidate_is_created_flag_() noexcept { is_created_ = false; }
204
206 void make_created_() noexcept { is_created_ = true; }
207
209 time_step_type &get_current_time_step_() noexcept { return current_state_; }
210
212 const time_step_type &get_current_time_step_() const noexcept { return current_state_; }
213
214 private:
215 bool is_created_;
216 std::string idx_;
217 std::string version_;
218 const std::string name_;
219 std::unordered_map<std::string, std::any> make_options_;
220 std::unordered_map<std::string, std::any> reset_options_;
221 time_step_type current_state_;
222 uint_t n_resets_{0};
223 uint_t last_reset_seed_{DEFAULT_ENV_SEED};
224};
225
226template <typename TimeStepType, typename SpaceType>
227EnvBase<TimeStepType, SpaceType>::EnvBase(const std::string &idx, const std::string &name)
228 : SpaceType(), is_created_(false), idx_(idx), version_(), name_(name), current_state_()
229{
230}
231
232template <typename TimeStepType, typename SpaceType>
234 : SpaceType(), is_created_(other.is_created_), idx_(other.idx_), version_(other.version_),
235 name_(other.name_), current_state_()
236{
237}
238
239template <typename TimeStepType, typename SpaceType> void EnvBase<TimeStepType, SpaceType>::close()
240{
241 this->is_created_ = false;
242}
243
244template <typename TimeStepType, typename SpaceType>
245template <typename T>
246T EnvBase<TimeStepType, SpaceType>::read_option(const std::string &op_name) const
247{
248
249 auto op_itr = make_options_.find(op_name);
250 if (op_itr != make_options_.end())
251 {
252 return std::any_cast<T>(op_itr->second);
253 }
254
255 throw std::logic_error("Option: " + op_name + " not found");
256}
257
258template <typename TimeStepType, typename SpaceType>
260 const std::string &version, const std::unordered_map<std::string, std::any> &make_options,
261 const std::unordered_map<std::string, std::any> &reset_options)
262{
263 version_ = version;
264 n_resets_ = 0;
265 make_options_ = make_options;
266 reset_options_ = reset_options;
267}
268
269} // namespace envs
270} // namespace bitrl
271
272#endif // ENV_BASE_H
ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::state_space state_space_type
Definition env_base.h:45
void invalidate_is_created_flag_() noexcept
Mark environment as not created.
Definition env_base.h:203
ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::state_type state_type
Definition env_base.h:48
EnvBase(const EnvBase &)
Copy constructor.
Definition env_base.h:233
uint_t last_reset_seed() const noexcept
The seed of the latest reset(), for the environments that take one. The first reset after make() uses...
Definition env_base.h:151
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
Definition env_base.h:105
virtual void close()=0
Close and release any acquired environment resources.
Definition env_base.h:239
const time_step_type & get_current_time_step_() const noexcept
Read-only access to the current time step.
Definition env_base.h:212
EnvBase(const std::string &idx=bitsim::consts::INVALID_STR, const std::string &name=bitsim::consts::INVALID_STR)
Constructor (protected — for subclassing only).
Definition env_base.h:227
void set_idx_(const std::string &idx) noexcept
Set the id of the environment.
Definition env_base.h:175
ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::action_space action_space_type
Definition env_base.h:51
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:184
uint_t next_reset_seed_()
The seed for the next reset: the "seed" reset option, or DEFAULT_ENV_SEED, plus the number of resets ...
Definition env_base.h:193
const std::unordered_map< std::string, std::any > & make_options() const noexcept
Definition env_base.h:96
ContinuousVectorStateDiscreteActionEnv< 4, 2, 0, real_t >::action_type action_type
Definition env_base.h:54
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:259
virtual ~EnvBase()=default
Virtual destructor.
time_step_type & get_current_time_step_() noexcept
Mutable access to the current time step.
Definition env_base.h:209
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:246
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:125
void make_created_() noexcept
Mark environment creation as successful.
Definition env_base.h:206
bool is_created() const noexcept
Check if make() has successfully initialized the environment.
Definition env_base.h:131
std::string env_name() const noexcept
Get the name of this environment instance.
Definition env_base.h:137
void set_make_options_(const std::unordered_map< std::string, std::any > &options) noexcept
Store options for future access.
Definition env_base.h:178
void set_version_(const std::string &version) noexcept
Set internal version string.
Definition env_base.h:169
Definition cart_pole.h:18
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
const std::string INVALID_STR
Invalid string.
Definition bitsim_consts.h:21
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:24
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Various utilities for std::map.