11#include <unordered_map>
30template <
typename TimeStepType,
typename SpaceType>
class EnvBase :
public SpaceType
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");
70 const std::unordered_map<std::string, std::any> &
make_options,
71 const std::unordered_map<std::string, std::any> &
reset_options) = 0;
96 const std::unordered_map<std::string, std::any> &
make_options() const noexcept
105 const std::unordered_map<std::string, std::any> &
reset_options() const noexcept
107 return reset_options_;
118 template <
typename T> T
read_option(
const std::string &op_name)
const;
125 std::string
idx() const noexcept {
return idx_; }
137 std::string
env_name() const noexcept {
return name_; }
143 std::string
version() const noexcept {
return version_; }
180 make_options_ = options;
186 reset_options_ = options;
195 const uint_t seed = reset_options_.contains(
"seed")
198 last_reset_seed_ = seed + n_resets_++;
199 return last_reset_seed_;
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_;
226template <
typename TimeStepType,
typename SpaceType>
228 : SpaceType(), is_created_(false), idx_(
idx), version_(), name_(name), current_state_()
232template <
typename TimeStepType,
typename SpaceType>
234 : SpaceType(), is_created_(other.is_created_), idx_(other.idx_), version_(other.version_),
235 name_(other.name_), current_state_()
241 this->is_created_ =
false;
244template <
typename TimeStepType,
typename SpaceType>
249 auto op_itr = make_options_.find(op_name);
250 if (op_itr != make_options_.end())
252 return std::any_cast<T>(op_itr->second);
255 throw std::logic_error(
"Option: " + op_name +
" not found");
258template <
typename TimeStepType,
typename SpaceType>
261 const std::unordered_map<std::string, std::any> &
reset_options)
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
TimeStep< std::vector< real_t > > time_step_type
Definition env_base.h:42
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.
std::string version() const noexcept
Definition env_base.h:143
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
static constexpr uint_t DEFAULT_ENV_SEED
Definition env_base.h:39
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.