Scheduler
reinforcementPerformanceTest.cpp
Go to the documentation of this file.
1 
11 #include <gtest/gtest.h>
12 
13 #include <utils/programTime.h>
14 
15 
16 #include <mdp/context.h>
17 #include <mdp/policy.h>
18 #include <mdp/stateSpace.h>
19 #include <mdp/mdpConfiguration.h>
20 #include <utils/randomGenerator.h>
21 
22 using namespace Mdp;
23 
24 class RlTester : public ReinforcedLearning
25 {
26 public:
27  RlTester(std::shared_ptr<Context> context) : ReinforcedLearning(context){};
28 };
29 
30 size_t NBOFSTATES = 5;
31 size_t NBOFACTIONS = 4;
32 
33 class CustomStateSpace : public StateSpace
34 {
35 public:
37  size_t size() override {return NBOFSTATES;};
38  state_t getState() override {return 0;};
39  void updateCurrentState() override {};
40  double getReward() override {return 1.0;};
41 };
42 
43 class CustomActionSpace : public ActionSpace
44 {
45 public:
46  size_t size() override {return NBOFACTIONS;};
47 };
48 
49 std::shared_ptr<Context> buildContext()
50 {
51  std::shared_ptr<Context> context = std::make_shared<Context>();
52  std::shared_ptr<Utils::RandomGenerator> gen = std::make_shared<Utils::RandomGenerator>();
53  std::shared_ptr<Policy> policy = std::make_shared<Policy>(NBOFSTATES, NBOFACTIONS, context->randomGenerator);
54 
55  std::shared_ptr<CustomStateSpace> stateSpace = std::make_shared<CustomStateSpace>();
56  std::shared_ptr<CustomActionSpace> actionSpace = std::make_shared<CustomActionSpace>();
57  std::shared_ptr<MdpConfiguration> conf = std::make_shared<MdpConfiguration>("configuration.conf");
58 
59  context->randomGenerator = gen;
60  context->policy = policy;
61  context->stateSpace = stateSpace;
62  context->actionSpace = actionSpace;
63  context->conf = conf;
64  return context;
65 }
66 
67 TEST(ReinforcementLearningTest, testBasic)
68 {
69  std::shared_ptr<Context> context = buildContext();
70  RlTester tester(context);
71  tester.initializeModel();
72  for (int i = 0; i < 10; i++)
73  {
75  for (int i = 0; i < 10000000; i++)
76  {
77  tester.updateModel();
78  }
79  std::cerr << "elapsed time: " << Utils::ProgramTime::elapsedTimeInSeconds() << "\n";;
80  }
81  tester.end();
82 }
83 
84 
85 
86 
87 
88 
89 
RlTester(std::shared_ptr< Context > context)
std::shared_ptr< Context > buildContext()
Definition: action.h:18
static void init()
Definition: programTime.cpp:17
size_t state_t
Definition: state.h:19
static double elapsedTimeInSeconds()
Definition: programTime.cpp:22
TEST(ReinforcementLearningTest, testBasic)