Scheduler
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
matrixLearning.cpp
Go to the documentation of this file.
1 
10 #include "matrixLearning.h"
11 
12 #include <mdp/action_impl.h>
13 #include <mdp/actionSpace.h>
14 #include <mdp/context.h>
15 #include <mdp/policy.h>
16 #include <mdp/rewards.h>
17 #include <mdp/stateSpace.h>
18 #include <mdp/transitionMatrix.h>
19 
20 using namespace Mdp;
21 
22 MatrixLearning::MatrixLearning(std::shared_ptr<Context> c) : LearningStrategy(c)
23 {
24  size_t S = c->stateSpace->size();
25  size_t A = c->actionSpace->size();
26  occurences = std::vector<std::vector<std::vector<int>>>(S,
27  std::vector<std::vector<int>>(A, std::vector<int>(S, 0)));
28 }
29 
30 
32 {
33  context->policy->initializeRandomly(context->randomGenerator);
34  context->matrix->initializeRandomly(context->randomGenerator);
35  previousState = context->stateSpace->getState();
36 }
37 
39 {
40  context->policy->initializeRandomly(context->randomGenerator);//take a random action everytime
41  state_t state = context->stateSpace->getState();
42 
43  if (nbOfUpdates != 0)
44  occurences[previousState][previousAction][state]++;
45 
46  nbOfUpdates++;
47 
48  previousState = state;
49  previousAction = context->policy->getAction(previousState);
50  updateMatrix(); //not very efficient to do that here every time
51 }
52 
53 
54 void MatrixLearning::updateMatrix()
55 {
56  size_t S = context->stateSpace->size();
57  size_t A = context->actionSpace->size();
58  for (state_t i = 0; i < S; i++)
59  {
60  for (action_t j = 0; j < A; j++)
61  {
62  double N = 0;
63  for (state_t k = 0; k < S; k++)
64  {
65  N += (double) occurences[i][j][k];
66  }
67  for (state_t k = 0; k < S; k++)
68  {
69  context->matrix->set(i, k, j, ((double)occurences[i][j][k])/N);
70  }
71  }
72  }
73 }
74 
75 
76 
const size_t A
const size_t S
MatrixLearning(std::shared_ptr< Context > context)
size_t action_t
Definition: action_impl.h:18
Definition: action.h:18
size_t state_t
Definition: state.h:19
std::shared_ptr< Context > context