bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
gymnasium_vector_env_base.h
Go to the documentation of this file.
1
8#ifndef GYMNASIUM_VECTOR_ENV_BASE_H
9#define GYMNASIUM_VECTOR_ENV_BASE_H
10
11#include "../../network/rest_rl_env_client.h"
12#include "bitrl/bitrl_config.h"
13#include "bitrl/bitrl_types.h"
16#include "bitrl/extern/nlohmann/json/json.hpp"
17
18#ifdef BITRL_DEBUG
19#include <cassert>
20#endif
21
22#include <stdexcept>
23
24namespace bitrl
25{
26namespace envs
27{
28namespace gymnasium
29{
30
31template <typename VectorTimeStepType, typename SpaceType>
32class GymnasiumVecEnvBase : public GymnasiumEnvBase<VectorTimeStepType, SpaceType>
33{
34
35 public:
40
45 typedef typename base_type::time_step_type time_step_type;
46
50 typedef typename base_type::state_space_type state_space_type;
51
55 typedef typename base_type::action_space_type action_space_type;
56
60 typedef typename base_type::action_type action_type;
61
65 typedef typename base_type::state_type state_type;
66
67 virtual void make(const std::string &version,
68 const std::unordered_map<std::string, std::any> &options,
69 const std::unordered_map<std::string, std::any> &reset_options) = 0;
70
74 uint_t get_n_envs() const noexcept { return n_envs_; }
75
79 void reset_if_any_done(bool flag) noexcept { reset_if_any_done_ = flag; }
80
84 bool get_reset_if_any_done() const noexcept { return reset_if_any_done_; }
85
86 protected:
90 GymnasiumVecEnvBase(network::RESTRLEnvClient &api_server, const std::string &name);
91
93
94 private:
95 uint_t n_envs_{0};
96
101 bool reset_if_any_done_{false};
102};
103
104template <typename VectorTimeStepType, typename SpaceType>
106 network::RESTRLEnvClient &api_server, const std::string &name)
107 : GymnasiumEnvBase<VectorTimeStepType, SpaceType>(api_server, name)
108{
109}
110
111template <typename VectorTimeStepType, typename SpaceType>
114 : GymnasiumEnvBase<VectorTimeStepType, SpaceType>(other), n_envs_(other.n_envs_),
115 reset_if_any_done_(other.reset_if_any_done_)
116{
117}
118
119template <typename VectorTimeStepType, typename SpaceType>
121 const std::string &version, const std::unordered_map<std::string, std::any> &options,
122 const std::unordered_map<std::string, std::any> &reset_options)
123{
124
125 auto reset_if_any_done_itr = options.find("reset_if_any_done");
126
127 if (reset_if_any_done_itr != options.end())
128 {
129 reset_if_any_done_ = std::any_cast<bool>(reset_if_any_done_itr->second);
130 }
131
132 auto n_envs_itr = options.find("num_envs");
133
134 if (n_envs_itr != options.end())
135 {
136 n_envs_ = std::any_cast<uint_t>(n_envs_itr->second);
137 return;
138 }
139
140 throw std::logic_error("num_envs variable is not provided");
141}
142
143} // namespace gymnasium
144} // namespace envs
145} // namespace bitrl
146
147#endif // GYMNASIUMENVBASE_H
Base class interface for Reinforcement Learning environments.
Definition env_base.h:30
const std::unordered_map< std::string, std::any > & reset_options() const noexcept
Access the configuration options provided to make().
Definition env_base.h:104
std::string version() const noexcept
Get the environment version set during make().
Definition env_base.h:142
Base class for all Gymnasium environment wrappers.
Definition gymnasium_env_base.h:41
Definition gymnasium_vector_env_base.h:33
uint_t get_n_envs() const noexcept
Returns the number of environments.
Definition gymnasium_vector_env_base.h:74
virtual void make(const std::string &version, const std::unordered_map< std::string, std::any > &options, const std::unordered_map< std::string, std::any > &reset_options)=0
Construct the environment instance.
Definition gymnasium_vector_env_base.h:120
GymnasiumVecEnvBase(const GymnasiumVecEnvBase &other)
Definition gymnasium_vector_env_base.h:112
base_type::time_step_type time_step_type
The time step type we return every time a step in the environment is performed.
Definition gymnasium_vector_env_base.h:45
void reset_if_any_done(bool flag) noexcept
Definition gymnasium_vector_env_base.h:79
base_type::state_type state_type
The type of the state.
Definition gymnasium_vector_env_base.h:65
GymnasiumEnvBase< VectorTimeStepType, SpaceType >::base_type base_type
The base type.
Definition gymnasium_vector_env_base.h:39
base_type::action_type action_type
The type of the action to be undertaken in the environment.
Definition gymnasium_vector_env_base.h:60
bool get_reset_if_any_done() const noexcept
Definition gymnasium_vector_env_base.h:84
GymnasiumVecEnvBase(network::RESTRLEnvClient &api_server, const std::string &name)
Constructor.
Definition gymnasium_vector_env_base.h:105
base_type::action_space_type action_space_type
The type of the action space for the environment.
Definition gymnasium_vector_env_base.h:55
base_type::state_space_type state_space_type
The type describing the state space for the environment.
Definition gymnasium_vector_env_base.h:50
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:29
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43