bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
gymnasium_env_base.h
Go to the documentation of this file.
1#ifndef GYMNASIUM_ENV_BASE_H
2#define GYMNASIUM_ENV_BASE_H
3
7
8#include "bitrl/bitrl_config.h"
10#include "bitrl/bitrl_types.h"
11#include "bitrl/envs/env_base.h"
12#include "bitrl/extern/nlohmann/json/json.hpp"
15
16#include <any>
17#include <string>
18#include <unordered_map>
19
20#ifdef BITRL_DEBUG
21#include <cassert>
22#endif
23
24namespace bitrl
25{
26namespace envs::gymnasium
27{
39template <typename TimeStepType, typename SpaceType>
40class GymnasiumEnvBase : public EnvBase<TimeStepType, SpaceType>
41{
42 public:
45
48
51
54
57
60
62 using base_type::reset;
63
68
73 virtual bool is_alive() const;
74
78 virtual void close() override;
79
86 virtual time_step_type reset() override;
87
93
99
104 std::string get_url() const;
105
106 protected:
114 GymnasiumEnvBase(network::RESTRLEnvClient &api_server, const std::string &name);
115
120
125
135 virtual time_step_type
136 create_time_step_from_response_(const nlohmann::json &response) const = 0;
137};
138
139template <typename TimeStepType, typename SpaceType>
141 const std::string &name)
142 : EnvBase<TimeStepType, SpaceType>(bitrl::consts::INVALID_STR, name), api_server_(&api_server)
143{
144}
145
146template <typename TimeStepType, typename SpaceType>
149 : EnvBase<TimeStepType, SpaceType>(other), api_server_(other.api_server_)
150{
151}
152
153template <typename TimeStepType, typename SpaceType>
155{
156
157 try
158 {
159 close();
160 }
161 catch (...)
162 {
163 }
164}
165
166template <typename TimeStepType, typename SpaceType>
168{
169 auto response = this->api_server_->is_alive(this->env_name(), this->idx());
170 return response["result"];
171}
172template <typename TimeStepType, typename SpaceType>
174{
175 auto response = this->api_server_->n_copies(this->env_name());
176 return response["copies"];
177}
178
179template <typename TimeStepType, typename SpaceType>
181{
182
183 if (!this->is_created())
184 {
185 return;
186 }
187
188 auto response = this->api_server_->close(this->env_name(), this->idx());
189 this->invalidate_is_created_flag_();
190}
191
192template <typename TimeStepType, typename SpaceType>
195{
196
197 if (!this->is_created())
198 {
199#ifdef BITRL_DEBUG
200 assert(this->is_created() && "Environment has not been created");
201#endif
202 return time_step_type();
203 }
204
205 auto &reset_ops = this->reset_options();
206 auto seed = utils::resolve<uint_t>("seed", reset_ops);
207
208 auto response = this->api_server_->reset(this->env_name(), this->idx(), seed, nlohmann::json());
209
210 this->get_current_time_step_() = this->create_time_step_from_response_(response);
211 return this->get_current_time_step_();
212}
213
214template <typename TimeStepType, typename SpaceType>
216{
217 return api_server_->get_env_url(this->env_name());
218}
219
220} // namespace envs::gymnasium
221} // namespace bitrl
222
223#endif // GYMNASIUMENVBASE_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
SpaceType::state_type state_type
Type describing an individual state.
Definition env_base.h:47
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.
TimeStepType time_step_type
Alias for the type returned when stepping the environment.
Definition env_base.h:41
SpaceType::action_type action_type
Type representing an individual action.
Definition env_base.h:53
Base class for all Gymnasium environment wrappers.
Definition gymnasium_env_base.h:41
base_type::action_type action_type
Type representing a valid action to execute.
Definition gymnasium_env_base.h:56
GymnasiumEnvBase(network::RESTRLEnvClient &api_server, const std::string &name)
Constructor.
Definition gymnasium_env_base.h:140
base_type::time_step_type time_step_type
Time step returned at each environment step.
Definition gymnasium_env_base.h:47
network::RESTRLEnvClient * api_server_
Server wrapper handling communication with remote Gymnasium environment.
Definition gymnasium_env_base.h:124
std::string get_url() const
Get the full URL for this environment endpoint on the server.
Definition gymnasium_env_base.h:215
EnvBase< TimeStepType, SpaceType > base_type
Base environment type alias.
Definition gymnasium_env_base.h:44
virtual ~GymnasiumEnvBase()
Virtual destructor.
Definition gymnasium_env_base.h:154
uint_t n_copies() const
Definition gymnasium_env_base.h:173
virtual void close() override
Close the environment on the server and release any resources.
Definition gymnasium_env_base.h:180
virtual time_step_type reset() override
Reset the environment to an initial state using the reset options specified during make.
Definition gymnasium_env_base.h:194
base_type::state_space_type state_space_type
Type describing the observation/state space of the environment.
Definition gymnasium_env_base.h:50
GymnasiumEnvBase(const GymnasiumEnvBase &)
Copy constructor.
Definition gymnasium_env_base.h:147
base_type::action_space_type action_space_type
Type describing the action space of the environment.
Definition gymnasium_env_base.h:53
base_type::state_type state_type
Type representing a state/observation returned by the environment.
Definition gymnasium_env_base.h:59
network::RESTRLEnvClient & get_api_server() const
Retrieve the REST API wrapper instance used for communication.
Definition gymnasium_env_base.h:98
virtual time_step_type create_time_step_from_response_(const nlohmann::json &response) const =0
Build a TimeStepType instance from a server JSON response.
virtual bool is_alive() const
Check whether the environment is still alive/connected.
Definition gymnasium_env_base.h:167
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:29
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43