bitsim
Model, simulate, estimate and control robots in C++
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
8#include "bitsim/extern/HTTPRequest.hpp"
9#include "bitsim/extern/nlohmann/json/json.hpp"
10
11#include <any>
12#include <iostream>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
17namespace bitrl
18{
19namespace network
20{
21
28{
29 public:
33 explicit RESTRLEnvClient(const std::string &url = "http://0.0.0.0:8001/api",
34 const bool initialize = true);
35
39 bool is_inisialised() const noexcept { return is_init_; }
40
44 std::string get_url() const noexcept { return url_; }
45
51 bool is_env_registered(const std::string &env_name) const noexcept;
52
57 std::string get_env_url(const std::string &name) const noexcept;
58
64 std::string get_uri(const std::string &name) const noexcept;
65
70 void register_new(const std::string &name, const std::string &uri);
71
75 void register_if_not(const std::string &name, const std::string &uri);
76
83 nlohmann::json n_copies(const std::string &name,
84 const std::string &version = bitsim::consts::INVALID_STR) const;
85
91 nlohmann::json is_alive(const std::string &env_name, const std::string &idx) const;
92
98 nlohmann::json close(const std::string &env_name, const std::string &idx) const;
99
107 template <typename ActionType>
108 nlohmann::json step(const std::string &env_name, const std::string &idx,
109 const ActionType &action) const;
110
117 nlohmann::json reset(const std::string &env_name, const std::string &idx, const uint_t seed,
118 const nlohmann::json &options) const;
119
126 nlohmann::json make(const std::string &env_name, const std::string &version,
127 const nlohmann::json &options) const;
128
133 nlohmann::json dynamics(const std::string &env_name, const std::string &idx, const uint_t sidx,
134 const uint_t aidx) const;
135
136 bool has_gymnasium() const;
137 std::vector<std::string> gymnasium_envs() const;
138
139 private:
143 const std::string url_;
144
149 bool is_init_{false};
150
155 std::unordered_map<std::string, std::string> envs_;
156
160 void init_();
161};
162
163template <typename ActionType>
164nlohmann::json RESTRLEnvClient::step(const std::string &env_name, const std::string &idx,
165 const ActionType &action) const
166{
167
168 // find the source
169 auto url_ = get_env_url(env_name);
170
171 if (url_ == bitsim::consts::INVALID_STR)
172 {
173 throw std::logic_error("Environment: " + env_name + " is not registered");
174 }
175
176 const auto request_url = url_ + "/" + idx + "/step";
177 http::Request request{request_url};
178
179 nlohmann::json body;
180 body["action"] = action;
181
182 std::cout << "Sending body: " << body.dump() << std::endl;
183
184 const auto response = request.send("POST", body.dump(), {{"Content-Type", "application/json"}});
185 if (response.status.code != 202)
186 {
187 throw std::runtime_error("Environment server failed to step environment");
188 }
189
190 auto str_response = std::string(response.body.begin(), response.body.end());
191 nlohmann::json j = nlohmann::json::parse(str_response);
192 return j;
193}
194
195} // namespace network
196} // namespace bitrl
197#endif // APISERVER_H
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:164
std::string get_url() const noexcept
Returns the remote url.
Definition rest_rl_env_client.h:44
bool is_env_registered(const std::string &env_name) const noexcept
RESTRLEnvClient(const std::string &url="http://0.0.0.0:8001/api", const bool initialize=true)
Constructor.
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 ...
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...
bool is_inisialised() const noexcept
Returns true if the server is initialised.
Definition rest_rl_env_client.h:39
std::string get_env_url(const std::string &name) const noexcept
Return the url for the environment with the given name.
nlohmann::json n_copies(const std::string &name, const std::string &version=bitsim::consts::INVALID_STR) const
void register_if_not(const std::string &name, const std::string &uri)
Same as register_new but swallows the thrown exception.
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 ...
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....
std::vector< std::string > gymnasium_envs() const
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....
nlohmann::json dynamics(const std::string &env_name, const std::string &idx, const uint_t sidx, const uint_t aidx) const
Get the dynamics of the environment. If the environment does not expose such an endpoint it returns 4...
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.h:20
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
const std::string INVALID_STR
Invalid string.
Definition bitsim_consts.h:21
std::size_t uint_t
uint_t
Definition bitsim_types.h:40