12#include "bitrl/base/bitrl_config.h"
19#include "bitsim/extern/nlohmann/json/json.hpp"
26#include <unordered_map>
41template <u
int_t state_size>
42class GymWalk final :
public EnvBase<TimeStep<uint_t>, ScalarDiscreteEnv<state_size, 1, 0, 0>>
45 static const std::string
name;
46 static const std::string
URI;
54 typedef std::vector<std::tuple<real_t, uint_t, real_t, bool>>
dynamics_t;
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;
65 virtual void close() override final;
76 std::string
get_url()
const {
return api_server_->get_env_url(this->
env_name()); }
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;
94template <u
int_t state_size>
98 api_server_->register_if_not(
name,
URI);
101template <u
int_t state_size>
103GymWalk<state_size>::build_dynamics_from_response_(
const nlohmann::json &response)
const
105 auto dynamics = response[
"dynamics"];
109template <u
int_t state_size>
111GymWalk<state_size>::create_time_step_from_response_(
const nlohmann::json &response)
const
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"];
121 discount, std::unordered_map<std::string, std::any>());
124template <u
int_t state_size>
126 const std::unordered_map<std::string, std::any> &options,
127 const std::unordered_map<std::string, std::any> &
reset_options)
138 auto idx = response[
"idx"];
144template <u
int_t state_size>
149 assert(this->
is_created() &&
"Environment has not been created");
154 return this->
reset();
157 const auto response = api_server_->step(this->
env_name(), this->
idx(), action);
165 auto response = this->api_server_->is_alive(this->
env_name(), this->
idx());
166 return response[
"result"];
171 auto response = this->api_server_->n_copies(this->
env_name());
172 return response[
"copies"];
183 auto response = this->api_server_->close(this->
env_name(), this->
idx());
187template <u
int_t state_size>
194 assert(this->
is_created() &&
"Environment has not been created");
203 auto response = this->api_server_->reset(this->
env_name(), this->
idx(), seed, nlohmann::json());
205 this->create_time_step_from_response_(response);
209template <u
int_t state_size>
214 assert(this->
is_created() &&
"Environment has not been created");
217 auto response = this->api_server_->dynamics(this->
env_name(), this->
idx(), sidx, aidx);
218 return build_dynamics_from_response_(response);
ScalarDiscreteEnv< state_size, 1, 0, 0 >::state_space state_space_type
Definition env_base.h:45
void invalidate_is_created_flag_() noexcept
Definition env_base.h:203
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
TimeStep< uint_t > time_step_type
Definition env_base.h:42
uint_t next_reset_seed_()
Definition env_base.h:193
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
std::string version() const noexcept
Definition env_base.h:143
time_step_type & get_current_time_step_() noexcept
Definition env_base.h:209
std::string idx() const noexcept
Definition env_base.h:125
void make_created_() noexcept
Definition env_base.h:206
bool is_created() const noexcept
Definition env_base.h:131
std::string env_name() const noexcept
Definition env_base.h:137
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
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