10#include <unordered_map>
29template <
typename TimeStepType,
typename SpaceType>
class EnvBase :
public SpaceType
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");
69 const std::unordered_map<std::string, std::any> &
make_options,
70 const std::unordered_map<std::string, std::any> &
reset_options) = 0;
95 const std::unordered_map<std::string, std::any> &
make_options() const noexcept
104 const std::unordered_map<std::string, std::any> &
reset_options() const noexcept
106 return reset_options_;
117 template <
typename T> T
read_option(
const std::string &op_name)
const;
124 std::string
idx() const noexcept {
return idx_; }
136 std::string
env_name() const noexcept {
return name_; }
142 std::string
version() const noexcept {
return version_; }
171 make_options_ = options;
177 reset_options_ = options;
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_;
202template <
typename TimeStepType,
typename SpaceType>
204 : SpaceType(), is_created_(false), idx_(idx), version_(), name_(name), current_state_()
208template <
typename TimeStepType,
typename SpaceType>
210 : SpaceType(), is_created_(other.is_created_), idx_(other.idx_), version_(other.version_),
211 name_(other.name_), current_state_()
217 this->is_created_ =
false;
220template <
typename TimeStepType,
typename SpaceType>
225 auto op_itr = make_options_.find(op_name);
226 if (op_itr != make_options_.end())
228 return std::any_cast<T>(op_itr->second);
231 throw std::logic_error(
"Option: " + op_name +
" not found");
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)
240 make_options_ = make_options;
241 reset_options_ = reset_options;
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