@@ -13,94 +13,121 @@ The following is an example how to use the
1313#include "bitrl/envs/gymnasium/toy_text/frozen_lake_env.h"
1414#include "bitrl/network/rest_rl_env_client.h"
1515
16+ #include <any>
1617#include <iostream>
1718#include <string>
1819#include <unordered_map>
19- #include <any>
2020
21- namespace example_1{
21+ namespace example_1
22+ {
23+ using namespace bitrl;
2224
2325const std::string SERVER_URL = "http://0.0.0.0:8001/api";
2426
2527using bitrl::envs::gymnasium::FrozenLake;
26- using bitrl::envs::RESTApiServerWrapper;
28+ using bitrl::network::RESTRLEnvClient;
29+
30+ void test_frozen_lake(RESTRLEnvClient &server)
31+ {
2732
28- void test_frozen_lake(const RESTApiServerWrapper& server){
33+ // the environment is not registered with the server
34+ std::cout << "Is environment registered: " << server.is_env_registered(FrozenLake<4>::name)
35+ << std::endl;
2936
37+ // when the environment is created we register it with the REST client
3038 FrozenLake<4> env(server);
3139
32- std::cout<<"Environame URL: "<<env.get_url()<<std::endl;
40+ // environment name can also be accessed via env.env_name()
41+ std::cout << "Is environment registered: " << server.is_env_registered(env.env_name())
42+ << std::endl;
43+ std::cout << "Environment URL: " << env.get_url() << std::endl;
3344
34- // make the environment
45+ // make the environment we pass both make options
46+ // and reset options
3547 std::unordered_map<std::string, std::any> make_ops;
3648 make_ops.insert({"is_slippery", false});
3749
38- std::unordered_map<std::string, std::any> reset_ops;
39- reset_ops.insert({"seed", static_cast<uint_t>(42)});
50+ std::unordered_map<std::string, std::any> reset_ops;
51+ reset_ops.insert({"seed", static_cast<uint_t>(42)});
4052 env.make("v1", make_ops, reset_ops);
4153
42- std::cout<<"Is environment created? "<<env.is_created()<<std::endl;
43- std::cout<<"Is environment alive? "<<env.is_alive()<<std::endl;
44- std::cout<<"Number of valid actions? "<<env.n_actions()<<std::endl;
45- std::cout<<"Number of states? "<<env.n_states()<<std::endl;
46- std::cout<<"Env idx: "<<env.idx()<<std::endl;
54+ // query the environemnt version
55+ std::cout << "Environment version: " << env.version() << std::endl;
56+
57+ // once the env is created we can get it's id
58+ std::cout << "Environment idx is: " << env.idx() << std::endl;
59+
60+ // the create flag should be true
61+ std::cout << "Is environment created? " << env.is_created() << std::endl;
62+
63+ // environment should be alive on the server
64+ std::cout << "Is environment alive? " << env.is_alive() << std::endl;
65+
66+ // FrozenLake is a discrete state-action env so we can
67+ // query number of actions and states
68+ std::cout << "Number of valid actions? " << env.n_actions() << std::endl;
69+ std::cout << "Number of states? " << env.n_states() << std::endl;
70+
71+ // how many copies of this environment
72+ auto n_copies = env.n_copies();
73+ std::cout << "n_copies: " << n_copies << std::endl;
4774
4875 // reset the environment
4976 auto time_step = env.reset();
5077
51- std::cout<<"Reward on reset: "<<time_step.reward()<<std::endl;
52- std::cout<<"Observation on reset: "<<time_step.observation()<<std::endl;
53- std::cout<<"Is terminal state: "<<time_step.done()<<std::endl;
54- std::cout<<"Env idx: "<<env.idx()<<std::endl;
78+ std::cout << "Reward on reset: " << time_step.reward() << std::endl;
79+ std::cout << "Observation on reset: " << time_step.observation() << std::endl;
80+ std::cout << "Is terminal state: " << time_step.done() << std::endl;
5581
5682 //...print the time_step
57- std::cout<< time_step<< std::endl;
83+ std::cout << time_step << std::endl;
5884
5985 // take an action in the environment
60- // 2 = RIGHT
86+ // 2 = RIGHT
6187 auto new_time_step = env.step(2);
62-
63- std::cout<<new_time_step<<std::endl;
88+ std::cout << new_time_step << std::endl;
6489
6590 // get the dynamics of the environment for the given state and action
6691 auto state = 0;
6792 auto action = 1;
6893 auto dynamics = env.p(state, action);
6994
70- std::cout<< "Dynamics for state="<< state<< " and action="<< action<< std::endl;
71-
72- for(auto item:dynamics) {
73- std::cout<< std::get<0>(item)<< std::endl;
74- std::cout<< std::get<1>(item)<< std::endl;
75- std::cout<< std::get<2>(item)<< std::endl;
76- std::cout<< std::get<3>(item)<< std::endl;
95+ std::cout << "Dynamics for state=" << state << " and action=" << action << std::endl;
96+ for (auto item : dynamics)
97+ {
98+ std::cout << std::get<0>(item) << std::endl;
99+ std::cout << std::get<1>(item) << std::endl;
100+ std::cout << std::get<2>(item) << std::endl;
101+ std::cout << std::get<3>(item) << std::endl;
77102 }
78-
79- action = env.sample_action();
80- std::cout<<"Action sampled: "<<action<<std::endl;
81-
82- new_time_step = env.step(action);
83- std::cout<<new_time_step<<std::endl;
103+
104+ // discrete action environments can sample
105+ // actions
106+ action = env.sample_action();
107+ std::cout << "Action sampled: " << action << std::endl;
108+
109+ new_time_step = env.step(action);
110+ std::cout << new_time_step << std::endl;
84111
85112 // close the environment
86113 env.close();
87114}
88- }
89115
116+ } // namespace example_1
90117
91- int main(){
92-
93- using namespace example_1;
94-
95- RESTApiServerWrapper server(SERVER_URL, true);
96-
97- std::cout<<"Testing FrozenLake..."<<std::endl;
118+ int main()
119+ {
120+ using namespace example_1;
121+ RESTRLEnvClient server(SERVER_URL, false);
122+
123+ std::cout << "Testing FrozenLake..." << std::endl;
98124 example_1::test_frozen_lake(server);
99- std::cout<< "===================="<< std::endl;
100-
125+ std::cout << "====================" << std::endl;
126+
101127 return 0;
102128}
103129
130+
104131```
105132
106133Gymnasium environments exposed over a REST like API can be found at: <a href =" https://github.com/pockerman/bitrl-rest-api " >bitrl-rest-api</a >
0 commit comments