bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
rest_rl_env_client.h
Go to the documentation of this file.
1
2
3#ifndef REST_RL_ENV_CLIENT_H
4#define REST_RL_ENV_CLIENT_H
5
7#include "bitrl/bitrl_types.h"
8#include "bitrl/extern/HTTPRequest.hpp"
9#include "bitrl/extern/nlohmann/json/json.hpp"
10
11#include <any>
12#include <iostream>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
17
18namespace bitrl
19{
20namespace network
21{
22
29{
30 public:
34 explicit RESTRLEnvClient(const std::string &url = "http://0.0.0.0:8001/api",
35 const bool initialize = true);
36
40 bool is_inisialised() const noexcept { return is_init_; }
41
45 std::string get_url() const noexcept { return url_; }
46
52 bool is_env_registered(const std::string &env_name) const noexcept;
53
58 std::string get_env_url(const std::string &name) const noexcept;
59
65 std::string get_uri(const std::string &name) const noexcept;
66
71 void register_new(const std::string &name, const std::string &uri);
72
76 void register_if_not(const std::string &name, const std::string &uri);
77
83 nlohmann::json n_copies(const std::string &name, const std::string& version=bitrl::consts::INVALID_STR) const;
84
90 nlohmann::json is_alive(const std::string &env_name, const std::string &idx) const;
91
97 nlohmann::json close(const std::string &env_name, const std::string &idx) const;
98
106 template <typename ActionType>
107 nlohmann::json step(const std::string &env_name, const std::string &idx,
108 const ActionType &action) const;
109
116 nlohmann::json reset(const std::string &env_name, const std::string &idx, const uint_t seed,
117 const nlohmann::json &options) const;
118
125 nlohmann::json make(const std::string &env_name, const std::string &version,
126 const nlohmann::json &options) const;
127
132 nlohmann::json dynamics(const std::string &env_name, const std::string &idx, const uint_t sidx,
133 const uint_t aidx) const;
134
135 bool has_gymnasium() const;
136 std::vector<std::string> gymnasium_envs() const;
137
138 private:
142 const std::string url_;
143
148 bool is_init_{false};
149
154 std::unordered_map<std::string, std::string> envs_;
155
159 void init_();
160};
161
162template <typename ActionType>
163nlohmann::json RESTRLEnvClient::step(const std::string &env_name, const std::string &idx,
164 const ActionType &action) const
165{
166
167 // find the source
168 auto url_ = get_env_url(env_name);
169
170 if (url_ == bitrl::consts::INVALID_STR)
171 {
172 throw std::logic_error("Environment: " + env_name + " is not registered");
173 }
174
175 const auto request_url = url_ + "/" + idx + "/step";
176 http::Request request{request_url};
177
178 nlohmann::json body;
179 body["action"] = action;
180
181 std::cout << "Sending body: " << body.dump() << std::endl;
182
183 const auto response = request.send("POST", body.dump());
184 if (response.status.code != 202)
185 {
186 throw std::runtime_error("Environment server failed to step environment");
187 }
188
189 auto str_response = std::string(response.body.begin(), response.body.end());
190 nlohmann::json j = nlohmann::json::parse(str_response);
191 return j;
192}
193
194} // namespace network
195} // namespace bitrl
196#endif // APISERVER_H
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:29
nlohmann::json step(const std::string &env_name, const std::string &idx, const ActionType &action) const
Step in the environment with the given name and the given copy index executing action....
Definition rest_rl_env_client.h:163
std::string get_url() const noexcept
Returns the remote url.
Definition rest_rl_env_client.h:45
bool is_env_registered(const std::string &env_name) const noexcept
Definition rest_rl_env_client.cpp:39
std::string get_uri(const std::string &name) const noexcept
Returns the URI of the environment with the given name Returns INVALID_STR if the environment is not ...
Definition rest_rl_env_client.cpp:74
nlohmann::json close(const std::string &env_name, const std::string &idx) const
Close the environment with the given name. Throws std::logic_error is the environment is not register...
Definition rest_rl_env_client.cpp:117
bool is_inisialised() const noexcept
Returns true if the server is initialised.
Definition rest_rl_env_client.h:40
nlohmann::json n_copies(const std::string &name, const std::string &version=bitrl::consts::INVALID_STR) const
Definition rest_rl_env_client.cpp:141
std::string get_env_url(const std::string &name) const noexcept
Return the url for the environment with the given name.
Definition rest_rl_env_client.cpp:86
void register_if_not(const std::string &name, const std::string &uri)
Same as register_new but swallows the thrown exception.
Definition rest_rl_env_client.cpp:62
bool has_gymnasium() const
Definition rest_rl_env_client.cpp:250
nlohmann::json is_alive(const std::string &env_name, const std::string &idx) const
Queries the remote server if the environment with the given cidx is alive Throws std::logic_error is ...
Definition rest_rl_env_client.cpp:98
nlohmann::json make(const std::string &env_name, const std::string &version, const nlohmann::json &options) const
Make the the environment with the given name and the given copy index executing action....
Definition rest_rl_env_client.cpp:191
std::vector< std::string > gymnasium_envs() const
Definition rest_rl_env_client.cpp:260
nlohmann::json reset(const std::string &env_name, const std::string &idx, const uint_t seed, const nlohmann::json &options) const
Reset the the environment with the given name and the given copy index executing action....
Definition rest_rl_env_client.cpp:159
void register_new(const std::string &name, const std::string &uri)
Register a new environment. Throws std::logic_error if the environment name already exists.
Definition rest_rl_env_client.cpp:49
const std::string INVALID_STR
Invalid string.
Definition bitrl_consts.h:26
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43