bitsim
Model, simulate, estimate and control robots in C++
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/base/bitrl_config.h"
10#include "bitrl/envs/env_base.h"
13#include "bitsim/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:
113 GymnasiumEnvBase(network::RESTRLEnvClient &api_server, const std::string &name);
114
119
124
134 virtual time_step_type
135 create_time_step_from_response_(const nlohmann::json &response) const = 0;
136};
137
138template <typename TimeStepType, typename SpaceType>
140 const std::string &name)
141 : EnvBase<TimeStepType, SpaceType>(bitsim::consts::INVALID_STR, name), api_server_(&api_server)
142{
143}
144
145template <typename TimeStepType, typename SpaceType>
151
152template <typename TimeStepType, typename SpaceType>
154{
155
156 try
157 {
158 close();
159 }
160 catch (...)
161 {
162 }
163}
164
165template <typename TimeStepType, typename SpaceType>
167{
168 auto response = this->api_server_->is_alive(this->env_name(), this->idx());
169 return response["result"];
170}
171template <typename TimeStepType, typename SpaceType>
173{
174 auto response = this->api_server_->n_copies(this->env_name());
175 return response["copies"];
176}
177
178template <typename TimeStepType, typename SpaceType>
180{
181
182 if (!this->is_created())
183 {
184 return;
185 }
186
187 auto response = this->api_server_->close(this->env_name(), this->idx());
189}
190
191template <typename TimeStepType, typename SpaceType>
194{
195
196 if (!this->is_created())
197 {
198#ifdef BITRL_DEBUG
199 assert(this->is_created() && "Environment has not been created");
200#endif
201 return time_step_type();
202 }
203
204 // the seed given to make(), or the default seed, on the first reset, and
205 // the next seed on every reset after it: the same seed every time would
206 // start every episode from the same state
207 const uint_t seed = this->next_reset_seed_();
208
209 auto response = this->api_server_->reset(this->env_name(), this->idx(), seed, nlohmann::json());
210
212 return this->get_current_time_step_();
213}
214
215template <typename TimeStepType, typename SpaceType>
217{
218 return api_server_->get_env_url(this->env_name());
219}
220
221} // namespace envs::gymnasium
222} // namespace bitrl
223
224#endif // GYMNASIUMENVBASE_H
SpaceType::state_space state_space_type
Type describing the environment state space.
Definition env_base.h:45
void invalidate_is_created_flag_() noexcept
Mark environment as not created.
Definition env_base.h:203
SpaceType::state_type state_type
Type describing an individual state.
Definition env_base.h:48
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
SpaceType::action_space action_space_type
Type describing the environment action space.
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.
TimeStepType time_step_type
Alias for the type returned when stepping the environment.
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
SpaceType::action_type action_type
Type representing an individual action.
Definition env_base.h:54
time_step_type & get_current_time_step_() noexcept
Mutable access to the current time step.
Definition env_base.h:209
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
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
base_type::action_type action_type
Definition gymnasium_env_base.h:56
GymnasiumEnvBase(network::RESTRLEnvClient &api_server, const std::string &name)
Constructor.
Definition gymnasium_env_base.h:139
base_type::time_step_type time_step_type
Definition gymnasium_env_base.h:47
network::RESTRLEnvClient * api_server_
Definition gymnasium_env_base.h:123
std::string get_url() const
Get the full URL for this environment endpoint on the server.
Definition gymnasium_env_base.h:216
EnvBase< TimeStepType, SpaceType > base_type
Definition gymnasium_env_base.h:44
virtual ~GymnasiumEnvBase()
Virtual destructor.
Definition gymnasium_env_base.h:153
uint_t n_copies() const
Definition gymnasium_env_base.h:172
virtual void close() override
Close the environment on the server and release any resources.
Definition gymnasium_env_base.h:179
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:193
base_type::state_space_type state_space_type
Definition gymnasium_env_base.h:50
GymnasiumEnvBase(const GymnasiumEnvBase &)
Copy constructor.
Definition gymnasium_env_base.h:146
base_type::action_space_type action_space_type
Definition gymnasium_env_base.h:53
base_type::state_type state_type
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:166
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:28
Definition lunar_lander_env.h:18
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
Definition bitsim_consts.cpp:10
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Various utilities for std::map.