1#ifndef GYMNASIUM_ENV_BASE_H
2#define GYMNASIUM_ENV_BASE_H
8#include "bitrl/bitrl_config.h"
12#include "bitrl/extern/nlohmann/json/json.hpp"
18#include <unordered_map>
26namespace envs::gymnasium
39template <
typename TimeStepType,
typename SpaceType>
139template <
typename TimeStepType,
typename SpaceType>
141 const std::string &name)
142 :
EnvBase<TimeStepType, SpaceType>(
bitrl::consts::INVALID_STR, name), api_server_(&api_server)
146template <
typename TimeStepType,
typename SpaceType>
149 :
EnvBase<TimeStepType, SpaceType>(other), api_server_(other.api_server_)
153template <
typename TimeStepType,
typename SpaceType>
166template <
typename TimeStepType,
typename SpaceType>
169 auto response = this->api_server_->is_alive(this->env_name(), this->idx());
170 return response[
"result"];
172template <
typename TimeStepType,
typename SpaceType>
175 auto response = this->api_server_->n_copies(this->env_name());
176 return response[
"copies"];
179template <
typename TimeStepType,
typename SpaceType>
183 if (!this->is_created())
188 auto response = this->api_server_->close(this->env_name(), this->idx());
189 this->invalidate_is_created_flag_();
192template <
typename TimeStepType,
typename SpaceType>
197 if (!this->is_created())
200 assert(this->is_created() &&
"Environment has not been created");
205 auto &reset_ops = this->reset_options();
208 auto response = this->api_server_->reset(this->env_name(), this->idx(), seed, nlohmann::json());
210 this->get_current_time_step_() = this->create_time_step_from_response_(response);
211 return this->get_current_time_step_();
214template <
typename TimeStepType,
typename SpaceType>
217 return api_server_->get_env_url(this->env_name());
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