3#ifndef REST_RL_ENV_CLIENT_H
4#define REST_RL_ENV_CLIENT_H
8#include "bitsim/extern/HTTPRequest.hpp"
9#include "bitsim/extern/nlohmann/json/json.hpp"
14#include <unordered_map>
34 const bool initialize =
true);
44 std::string
get_url() const noexcept {
return url_; }
57 std::string
get_env_url(
const std::string &name)
const noexcept;
64 std::string
get_uri(
const std::string &name)
const noexcept;
70 void register_new(
const std::string &name,
const std::string &uri);
83 nlohmann::json
n_copies(
const std::string &name,
91 nlohmann::json
is_alive(
const std::string &env_name,
const std::string &idx)
const;
98 nlohmann::json
close(
const std::string &env_name,
const std::string &idx)
const;
107 template <
typename ActionType>
108 nlohmann::json
step(
const std::string &env_name,
const std::string &idx,
109 const ActionType &action)
const;
117 nlohmann::json
reset(
const std::string &env_name,
const std::string &idx,
const uint_t seed,
118 const nlohmann::json &options)
const;
126 nlohmann::json
make(
const std::string &env_name,
const std::string &version,
127 const nlohmann::json &options)
const;
133 nlohmann::json
dynamics(
const std::string &env_name,
const std::string &idx,
const uint_t sidx,
143 const std::string url_;
149 bool is_init_{
false};
155 std::unordered_map<std::string, std::string> envs_;
163template <
typename ActionType>
165 const ActionType &action)
const
173 throw std::logic_error(
"Environment: " + env_name +
" is not registered");
176 const auto request_url = url_ +
"/" + idx +
"/step";
177 http::Request request{request_url};
180 body[
"action"] = action;
182 std::cout <<
"Sending body: " << body.dump() << std::endl;
184 const auto response = request.send(
"POST", body.dump(), {{
"Content-Type",
"application/json"}});
185 if (response.status.code != 202)
187 throw std::runtime_error(
"Environment server failed to step environment");
190 auto str_response = std::string(response.body.begin(), response.body.end());
191 nlohmann::json j = nlohmann::json::parse(str_response);
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.
bool has_gymnasium() const
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