|
| 1 | +\page bitrl_example_2 BitRL Example 2 Using GymWalk Environment |
| 2 | + |
| 3 | +The \ref bitrl::envs::gdrl::GymWalk "bitrl::envs::gdrl::GymWalk" is an implementation of the |
| 4 | +environment from https://github.com/mimoralea/gym-walk. Just like with \ref bitrl_example_1, you will need |
| 5 | +an instance of the <a href="https://github.com/pockerman/bitrl-rest-api">bitrl-envs-api</a> server running in order |
| 6 | +to actually use the environment. Other than that there isn't much to say for this example. |
| 7 | +Below is the driver code. |
| 8 | + |
| 9 | +@code{.cpp} |
| 10 | +#include "bitrl/bitrl_types.h" |
| 11 | +#include "bitrl/envs/gdrl/gym_walk.h" |
| 12 | +#include "bitrl/network/rest_rl_env_client.h" |
| 13 | + |
| 14 | +#include <any> |
| 15 | +#include <iostream> |
| 16 | +#include <string> |
| 17 | +#include <unordered_map> |
| 18 | + |
| 19 | +namespace example_1 |
| 20 | +{ |
| 21 | +using namespace bitrl; |
| 22 | + |
| 23 | +const std::string SERVER_URL = "http://0.0.0.0:8001/api"; |
| 24 | + |
| 25 | +using bitrl::envs::gdrl::GymWalk; |
| 26 | +using bitrl::network::RESTRLEnvClient; |
| 27 | + |
| 28 | +void test_gymwalk(RESTRLEnvClient &server) |
| 29 | +{ |
| 30 | +// the environment is not registered with the server |
| 31 | +std::cout << "Is environment registered: " << server.is_env_registered(GymWalk<4>::name) |
| 32 | +<< std::endl; |
| 33 | + |
| 34 | + // when the environment is created we register it with the REST client |
| 35 | + GymWalk<4> env(server); |
| 36 | + |
| 37 | + // environment name can also be accessed via env.env_name() |
| 38 | + std::cout << "Is environment registered: " << server.is_env_registered(env.env_name()) |
| 39 | + << std::endl; |
| 40 | + std::cout << "Environment URL: " << env.get_url() << std::endl; |
| 41 | + |
| 42 | + // make the environment we pass both make options |
| 43 | + // and reset options |
| 44 | + std::unordered_map<std::string, std::any> make_ops; |
| 45 | + std::unordered_map<std::string, std::any> reset_ops; |
| 46 | + reset_ops.insert({"seed", static_cast<uint_t>(42)}); |
| 47 | + env.make("v1", make_ops, reset_ops); |
| 48 | + |
| 49 | + // query the environemnt version |
| 50 | + std::cout << "Environment version: " << env.version() << std::endl; |
| 51 | + |
| 52 | + // once the env is created we can get it's id |
| 53 | + std::cout << "Environment idx is: " << env.idx() << std::endl; |
| 54 | + |
| 55 | + // the created flag should be true |
| 56 | + std::cout << "Is environment created? " << env.is_created() << std::endl; |
| 57 | + |
| 58 | + // environment should be alive on the server |
| 59 | + std::cout << "Is environment alive? " << env.is_alive() << std::endl; |
| 60 | + |
| 61 | + // FrozenLake is a discrete state-action env so we can |
| 62 | + // query number of actions and states |
| 63 | + std::cout << "Number of valid actions? " << env.n_actions() << std::endl; |
| 64 | + std::cout << "Number of states? " << env.n_states() << std::endl; |
| 65 | + |
| 66 | + // reset the environment |
| 67 | + auto time_step = env.reset(); |
| 68 | + |
| 69 | + std::cout << "Reward on reset: " << time_step.reward() << std::endl; |
| 70 | + std::cout << "Observation on reset: " << time_step.observation() << std::endl; |
| 71 | + std::cout << "Is terminal state: " << time_step.done() << std::endl; |
| 72 | + |
| 73 | + //...print the time_step |
| 74 | + std::cout << time_step << std::endl; |
| 75 | + |
| 76 | + // take an action in the environment |
| 77 | + // 2 = RIGHT |
| 78 | + auto new_time_step = env.step(1); |
| 79 | + std::cout << new_time_step << std::endl; |
| 80 | + |
| 81 | + // get the dynamics of the environment for the given state and action |
| 82 | + auto state = 0; |
| 83 | + auto action = 1; |
| 84 | + auto dynamics = env.p(state, action); |
| 85 | + |
| 86 | + std::cout << "Dynamics for state=" << state << " and action=" << action << std::endl; |
| 87 | + for (auto item : dynamics) |
| 88 | + { |
| 89 | + std::cout << std::get<0>(item) << std::endl; |
| 90 | + std::cout << std::get<1>(item) << std::endl; |
| 91 | + std::cout << std::get<2>(item) << std::endl; |
| 92 | + std::cout << std::get<3>(item) << std::endl; |
| 93 | + } |
| 94 | + |
| 95 | + // discrete action environments can sample |
| 96 | + // actions |
| 97 | + action = env.sample_action(); |
| 98 | + std::cout << "Action sampled: " << action << std::endl; |
| 99 | + |
| 100 | + new_time_step = env.step(action); |
| 101 | + std::cout << new_time_step << std::endl; |
| 102 | + |
| 103 | + // close the environment |
| 104 | + env.close(); |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace example_1 |
| 108 | + |
| 109 | +int main() |
| 110 | +{ |
| 111 | + |
| 112 | + using namespace example_1; |
| 113 | + |
| 114 | + RESTRLEnvClient server(SERVER_URL, false); |
| 115 | + |
| 116 | + std::cout << "Testing GymWalk..." << std::endl; |
| 117 | + example_1::test_gymwalk(server); |
| 118 | + std::cout << "====================" << std::endl; |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +@endcode |
0 commit comments