bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
toy_text_base.h
Go to the documentation of this file.
1#ifndef TOY_TEXT_BASE_ENV_H
2#define TOY_TEXT_BASE_ENV_H
3
4#include "bitrl/bitrl_config.h"
5#include "bitrl/bitrl_types.h"
8#include "bitrl/extern/nlohmann/json/json.hpp"
10
11#include <any>
12#include <string>
13#include <tuple>
14#include <unordered_map>
15#include <vector>
16
17#ifdef BITRL_DEBUG
18#include <cassert>
19#endif
20
21namespace bitrl
22{
23namespace envs::gymnasium
24{
25
30
31template <typename TimeStepType, uint_t state_end, uint_t action_end>
33 : public GymnasiumEnvBase<TimeStepType, ScalarDiscreteEnv<state_end, action_end, 0, 0>>
34{
35 public:
39 typedef typename GymnasiumEnvBase<
41
46 typedef typename base_type::time_step_type time_step_type;
47
51 typedef typename base_type::state_space_type state_space_type;
52
56 typedef typename base_type::action_space_type action_space_type;
57
61 typedef typename base_type::action_type action_type;
62
66 typedef typename base_type::state_type state_type;
67
71 typedef std::vector<std::tuple<real_t, uint_t, real_t, bool>> dynamics_t;
72
76 virtual ~ToyTextEnvBase() = default;
77
83 dynamics_t p(uint_t sidx, uint_t aidx) const;
84
88 uint_t n_actions() const noexcept { return action_space_type::size; }
89
93 uint_t n_states() const noexcept { return state_space_type::size; }
94
95 protected:
99 ToyTextEnvBase(network::RESTRLEnvClient &api_server, const std::string &name);
100
105
109 virtual dynamics_t build_dynamics_from_response_(const nlohmann::json &response) const;
110};
111
112template <typename TimeStepType, uint_t state_end, uint_t action_end>
114 network::RESTRLEnvClient &api_server, const std::string &name)
115 : GymnasiumEnvBase<TimeStepType, ScalarDiscreteEnv<state_end, action_end>>(api_server, name)
116{
117}
118
119template <typename TimeStepType, uint_t state_end, uint_t action_end>
125
126template <typename TimeStepType, uint_t state_end, uint_t action_end>
129{
130
131#ifdef BITRL_DEBUG
132 assert(this->is_created() && "Environment has not been created");
133#endif
134
135 auto response = this->get_api_server().dynamics(this->env_name(), this->idx(), sidx, aidx);
136 return build_dynamics_from_response_(response);
137}
138
139template <typename TimeStepType, uint_t state_end, uint_t action_end>
142 const nlohmann::json &response) const
143{
144 auto dynamics = response["dynamics"];
145 return dynamics;
146}
147
148} // namespace envs::gymnasium
149} // namespace bitrl
150#endif
Base class for all Gymnasium environment wrappers.
Definition gymnasium_env_base.h:41
ToyTextEnvBase class. Base class for toy environments from Gymnasium. These environments have a discr...
Definition toy_text_base.h:34
base_type::time_step_type time_step_type
The time step type we return every time a step in the environment is performed.
Definition toy_text_base.h:46
virtual ~ToyTextEnvBase()=default
~FrozenLake. Destructor.
std::vector< std::tuple< real_t, uint_t, real_t, bool > > dynamics_t
dynamics_t
Definition toy_text_base.h:71
base_type::action_type action_type
The type of the action to be undertaken in the environment.
Definition toy_text_base.h:61
base_type::action_space_type action_space_type
The type of the action space for the environment.
Definition toy_text_base.h:56
base_type::state_type state_type
The type of the state.
Definition toy_text_base.h:66
ToyTextEnvBase(network::RESTRLEnvClient &api_server, const std::string &name)
Constructor.
Definition toy_text_base.h:113
virtual dynamics_t build_dynamics_from_response_(const nlohmann::json &response) const
build the dynamics from response
Definition toy_text_base.h:141
GymnasiumEnvBase< TimeStepType, ScalarDiscreteEnv< state_end, action_end, 0, 0 > >::base_type base_type
The base_type.
Definition toy_text_base.h:40
base_type::state_space_type state_space_type
The type describing the state space for the environment.
Definition toy_text_base.h:51
uint_t n_states() const noexcept
Number of states.
Definition toy_text_base.h:93
uint_t n_actions() const noexcept
n_actions. Returns the number of actions
Definition toy_text_base.h:88
ToyTextEnvBase(const ToyTextEnvBase &other)
Copy constructor.
Definition toy_text_base.h:120
dynamics_t p(uint_t sidx, uint_t aidx) const
P.
Definition toy_text_base.h:128
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
Definition env_types.h:13