bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
gym_walk.h
Go to the documentation of this file.
1/*
2 * GymWalk environment from
3 * <a href="https://github.com/mimoralea/gym-walk2">gym_walk</a>
4 *
5 *
6 *
7 */
8
9#ifndef GYM_WALK_H
10#define GYM_WALK_H
11
12#include "bitrl/base/bitrl_config.h"
14#include "bitrl/envs/env_base.h"
19#include "bitsim/extern/nlohmann/json/json.hpp"
21
22#include <any>
23#include <memory>
24#include <string>
25#include <tuple>
26#include <unordered_map>
27#include <vector>
28
29#ifdef BITRL_DEBUG
30#include <cassert>
31#endif
32
33namespace bitrl
34{
35namespace envs::gdrl
36{
37
41template <uint_t state_size>
42class GymWalk final : public EnvBase<TimeStep<uint_t>, ScalarDiscreteEnv<state_size, 1, 0, 0>>
43{
44 public:
45 static const std::string name;
46 static const std::string URI;
47
54 typedef std::vector<std::tuple<real_t, uint_t, real_t, bool>> dynamics_t;
55
60
61 virtual void
62 make(const std::string &version, const std::unordered_map<std::string, std::any> &options,
63 const std::unordered_map<std::string, std::any> &reset_options) override final;
64 bool is_alive() const;
65 virtual void close() override final;
66 virtual time_step_type step(const action_type &action) override final;
67 virtual time_step_type reset() override final;
68 uint_t n_states() const noexcept { return state_space_type::size; }
69 uint_t n_actions() const noexcept { return action_space_type::size + 1; }
70 dynamics_t p(uint_t sidx, uint_t aidx) const;
71
76 std::string get_url() const { return api_server_->get_env_url(this->env_name()); }
77
82 uint_t n_copies() const;
83
84 private:
85 dynamics_t build_dynamics_from_response_(const nlohmann::json &response) const;
86 time_step_type create_time_step_from_response_(const nlohmann::json &response) const;
87
88 network::RESTRLEnvClient *api_server_;
89};
90
91template <uint_t state_size> const std::string GymWalk<state_size>::name = "GymWalk";
92template <uint_t state_size> const std::string GymWalk<state_size>::URI = "/gdrl/gym-walk-env";
93
94template <uint_t state_size>
96 : base_type(bitsim::consts::INVALID_STR, name), api_server_(&api_server)
97{
98 api_server_->register_if_not(name, URI);
99}
100
101template <uint_t state_size>
103GymWalk<state_size>::build_dynamics_from_response_(const nlohmann::json &response) const
104{
105 auto dynamics = response["dynamics"];
106 return dynamics;
107}
108
109template <uint_t state_size>
111GymWalk<state_size>::create_time_step_from_response_(const nlohmann::json &response) const
112{
113
114 auto step_type = response["time_step"]["step_type"];
115 auto reward = response["time_step"]["reward"];
116 auto discount = response["time_step"]["discount"];
117 auto observation = response["time_step"]["observation"];
118 auto info = response["time_step"]["info"];
120 TimeStepEnumUtils::time_step_type_from_int(step_type.get<uint_t>()), reward, observation,
121 discount, std::unordered_map<std::string, std::any>());
122}
123
124template <uint_t state_size>
125void GymWalk<state_size>::make(const std::string &version,
126 const std::unordered_map<std::string, std::any> &options,
127 const std::unordered_map<std::string, std::any> &reset_options)
128{
129
130 if (this->is_created())
131 {
132 return;
133 }
134
135 nlohmann::json ops;
136 auto response = api_server_->make(this->env_name(), version, ops);
137
138 auto idx = response["idx"];
139 this->set_idx_(idx);
140 this->base_type::make(version, options, reset_options);
141 this->make_created_();
142}
143
144template <uint_t state_size>
146{
147
148#ifdef BITRL_DEBUG
149 assert(this->is_created() && "Environment has not been created");
150#endif
151
152 if (this->get_current_time_step_().last())
153 {
154 return this->reset();
155 }
156
157 const auto response = api_server_->step(this->env_name(), this->idx(), action);
158
159 this->get_current_time_step_() = this->create_time_step_from_response_(response);
160 return this->get_current_time_step_();
161}
162
163template <uint_t state_size> bool GymWalk<state_size>::is_alive() const
164{
165 auto response = this->api_server_->is_alive(this->env_name(), this->idx());
166 return response["result"];
167}
168
169template <uint_t state_size> uint_t GymWalk<state_size>::n_copies() const
170{
171 auto response = this->api_server_->n_copies(this->env_name());
172 return response["copies"];
173}
174
175template <uint_t state_size> void GymWalk<state_size>::close()
176{
177
178 if (!this->is_created())
179 {
180 return;
181 }
182
183 auto response = this->api_server_->close(this->env_name(), this->idx());
185}
186
187template <uint_t state_size>
189{
190
191 if (!this->is_created())
192 {
193#ifdef BITRL_DEBUG
194 assert(this->is_created() && "Environment has not been created");
195#endif
196 return time_step_type();
197 }
198
199 // the seed given to make(), or the default seed, on the first reset, and
200 // the next seed on every reset after it: the same seed every time would
201 // start every episode from the same state
202 const uint_t seed = this->next_reset_seed_();
203 auto response = this->api_server_->reset(this->env_name(), this->idx(), seed, nlohmann::json());
204
205 this->create_time_step_from_response_(response);
206 return this->get_current_time_step_();
207}
208
209template <uint_t state_size>
211{
212
213#ifdef BITRL_DEBUG
214 assert(this->is_created() && "Environment has not been created");
215#endif
216
217 auto response = this->api_server_->dynamics(this->env_name(), this->idx(), sidx, aidx);
218 return build_dynamics_from_response_(response);
219}
220
221} // namespace envs::gdrl
222} // namespace bitrl
223
224#endif // GYM_WALK_H
ScalarDiscreteEnv< state_size, 1, 0, 0 >::state_space state_space_type
Definition env_base.h:45
ScalarDiscreteEnv< state_size, 1, 0, 0 >::state_type state_type
Definition env_base.h:48
const std::unordered_map< std::string, std::any > & reset_options() const noexcept
Definition env_base.h:105
EnvBase(const std::string &idx=bitsim::consts::INVALID_STR, const std::string &name=bitsim::consts::INVALID_STR)
void set_idx_(const std::string &idx) noexcept
Definition env_base.h:175
ScalarDiscreteEnv< state_size, 1, 0, 0 >::action_space action_space_type
Definition env_base.h:51
ScalarDiscreteEnv< state_size, 1, 0, 0 >::action_type action_type
Definition env_base.h:54
virtual void make(const std::string &version, const std::unordered_map< std::string, std::any > &make_options, const std::unordered_map< std::string, std::any > &reset_options)=0
time_step_type & get_current_time_step_() noexcept
Definition env_base.h:209
virtual time_step_type step(const action_type &action) override final
Perform one step in the environment using an action.
Definition gym_walk.h:145
uint_t n_actions() const noexcept
Definition gym_walk.h:69
base_type::state_type state_type
Definition gym_walk.h:53
bool is_alive() const
Definition gym_walk.h:163
base_type::action_space_type action_space_type
Definition gym_walk.h:51
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) override final
Construct the environment instance.
Definition gym_walk.h:125
uint_t n_states() const noexcept
Definition gym_walk.h:68
std::string get_url() const
Get the full URL for this environment endpoint on the server.
Definition gym_walk.h:76
static const std::string URI
Definition gym_walk.h:46
static const std::string name
Definition gym_walk.h:45
EnvBase< TimeStep< uint_t >, ScalarDiscreteEnv< state_size, 1, 0, 0 > > base_type
Definition gym_walk.h:48
virtual time_step_type reset() override final
Reset the environment to an initial state using the reset options specified during make.
Definition gym_walk.h:188
base_type::time_step_type time_step_type
Definition gym_walk.h:49
GymWalk(network::RESTRLEnvClient &api_server)
Constructor.
Definition gym_walk.h:95
base_type::action_type action_type
Definition gym_walk.h:52
uint_t n_copies() const
Definition gym_walk.h:169
virtual void close() override final
Close and release any acquired environment resources.
Definition gym_walk.h:175
dynamics_t p(uint_t sidx, uint_t aidx) const
Definition gym_walk.h:210
base_type::state_space_type state_space_type
Definition gym_walk.h:50
std::vector< std::tuple< real_t, uint_t, real_t, bool > > dynamics_t
Definition gym_walk.h:54
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:28
Definition gym_walk.h:36
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
Definition bitsim_consts.cpp:10
Models of how things move: wheeled robots, a bicycle, a quadrotor and a fixed wing aircraft,...
Definition bicycle_vehicle_model_dynamics.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.
static TimeStepTp time_step_type_from_int(int aidx)
Definition env_types.h:13
static constexpr uint_t size
Definition space_type.h:38