-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrl_example_6.cpp
executable file
·77 lines (53 loc) · 2.03 KB
/
rl_example_6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "cubeai/base/cubeai_config.h"
#ifdef USE_RLENVS_CPP
#include "cubeai/base/cubeai_types.h"
#include "cubeai/rl/algorithms/dp/iterative_policy_evaluation.h"
#include "cubeai/rl/trainers/rl_serial_agent_trainer.h"
#include "cubeai/rl/policies/uniform_discrete_policy.h"
#include "rlenvs/rlenvs_types_v2.h"
#include "rlenvs/envs/gymnasium/toy_text/frozen_lake_env.h"
#include "rlenvs/time_step.h"
#include <iostream>
namespace rl_example_6
{
const std::string SERVER_URL = "http://0.0.0.0:8001/api";
using cubeai::real_t;
using cubeai::uint_t;
using cubeai::rl::policies::UniformDiscretePolicy;
using cubeai::rl::algos::dp::IterativePolicyEvalutationSolver;
using cubeai::rl::algos::dp::IterativePolicyEvalConfig;
using cubeai::rl::RLSerialAgentTrainer;
using cubeai::rl::RLSerialTrainerConfig;
using rlenvscpp::envs::gymnasium::FrozenLake;
typedef FrozenLake<4> env_type;
}
int main() {
using namespace rl_example_6;
// create the environment
FrozenLake<4> env(SERVER_URL);
std::cout<<"Environment URL: "<<env.get_url()<<std::endl;
std::unordered_map<std::string, std::any> options;
std::cout<<"Creating the environment..."<<std::endl;
env.make("v1", options);
env.reset();
std::cout<<"Done..."<<std::endl;
UniformDiscretePolicy policy(env.n_states(), env.n_actions());
IterativePolicyEvalConfig config;
config.tolerance = 1.0e-8;
IterativePolicyEvalutationSolver<env_type, UniformDiscretePolicy> algorithm(config, policy);
RLSerialTrainerConfig trainer_config = {10, 10000, 1.0e-8};
RLSerialAgentTrainer<env_type,
IterativePolicyEvalutationSolver<env_type, UniformDiscretePolicy>> trainer(trainer_config, algorithm);
auto info = trainer.train(env);
std::cout<<info<<std::endl;
// save the value function for plotting
//algorithm.save("iterative_policy_evaluation_frozen_lake.csv");
return 0;
}
#else
#include <iostream>
int main() {
std::cout<<"This example requires gymfcpp. Configure cubeai to use gymfcpp"<<std::endl;
return 0;
}
#endif