Scheduler
policy.cpp
Go to the documentation of this file.
1 
10 #include "policy.h"
11 
12 #include <iostream>
13 #include <fstream>
14 #include <cassert>
15 #include <stdexcept>
16 
17 #include <utils/randomGenerator.h>
18 #include <utils/stringUtils.h>
19 
20 using namespace Mdp;
21 
22 Policy::Policy(int S, int A, std::shared_ptr<Utils::RandomGenerator> gen)
23  : policy(std::vector<std::vector<double>>(S, std::vector<double>(A)))
24  , nbOfStates(S)
25  , nbOfActions(A)
26  , randomGenerator(gen)
27 {
28 }
29 
30 
31 
32 void Policy::initializeRandomly(std::shared_ptr<Utils::RandomGenerator> gen)
33 {
34  for (state_t i = 0; i < nbOfStates; i++)
35  {
36  std::vector<double> vector = gen->drawDistribution(nbOfActions);
37  update(i, vector);
38  }
39 }
40 
42 {
43  for (state_t i = 0; i < nbOfStates; i++)
44  {
45  std::vector<double> vect(nbOfActions, 1.0/nbOfActions);
46  update(i, vect);
47  }
48 }
49 
50 /*TODO: the format is not clear*/
52 {
53  std::fstream stream;
54  stream.open(filename);
55  if (!stream.is_open())
56  throw std::runtime_error("cannot open file");
57  std::string line;
58  std::vector<std::vector<double>> pol;
59  while(std::getline(stream, line))
60  {
61  std::vector<std::string> elements = Utils::StringUtils::split(line, ' ');
62  std::vector<double> row;
63  /*TODO: this can be rewritten more elegantly*/
64  for (size_t i = 0; i < elements.size(); i++)
65  {
66  row.push_back(std::stod(elements[i]));
67  }
68  pol.push_back(row);
69  }
70  policy = pol;
71 }
72 
73 void Policy::saveToFile(std::string /*filename*/)
74 {
75  /*FIXME: TODO*/
76 }
77 
78 
79 void Policy::update(state_t state, const std::vector<double> &vector)
80 {
81  policy[state] = vector;
82  /*
83  std::cerr << "updating policy for state "<<state<<" with:";
84  for (size_t i = 0; i < vector.size(); i++)
85  {
86  std::cerr << " " << vector[i];
87  }
88  std::cerr << "\n";
89  */
90 }
91 
92 
93 std::vector<double> *Policy::getActionVector(state_t state)
94 {
95  return &policy[state];
96 }
97 
99 {
100  std::vector<double> *vector = getActionVector(state);
101  double d = randomGenerator->drawUniform(0.0, 1.0);
102  double cumulative = 0.0;
103  //std::cerr << "for state " << state <<", policy is:";
104  for (action_t i = 0; i < vector->size(); i++)
105  {
106  //std::cerr << " " << (*vector)[i];
107  cumulative += (*vector)[i];
108  if (d <= cumulative)
109  {
110  return i;
111  }
112  }
113  //std::cerr << "\n";
114  handleErrorInGettingAction(vector, state, d);
115  return 0;
116 }
117 
118 void Policy::handleErrorInGettingAction(std::vector<double> *vector, state_t state, double d)
119 {
120  std::cerr << "Bug occured. here is the action vector distribution for state ";
121  std::cerr << state <<":\n";
122  for (action_t i = 0; i < vector->size(); i++)
123  {
124  std::cerr << " "<<(*vector)[i]<<"\n";
125  }
126  std::cerr << "d is "<<d<<"\n";
127  throw std::runtime_error("Bug occured in selecting action from policy");
128 }
129 
130 
131 
132 void Policy::print(std::ostream& /*stream*/)
133 {
134 #ifdef PRINT
135  stream << "Printing the policy:\n";
136  for (state_t i = 0; i < nbOfStates; i++)
137  {
138  for (action_t j = 0; j < nbOfActions; j++)
139  {
140  stream << policy[i][j] << " ";
141  }
142  stream << "\n";
143  }
144 #endif
145 }
146 
147 
148 
150 {
151  return nbOfStates;
152 }
153 
155 {
156  return nbOfActions;
157 }
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
void update(state_t state, const std::vector< double > &vector)
Definition: policy.cpp:79
void saveToFile(std::string filename)
Definition: policy.cpp:73
const size_t A
std::vector< std::vector< double > > policy
Definition: policy.h:41
const size_t S
void initializeFromFile(std::string filename)
Definition: policy.cpp:51
line
Definition: bigtemp.py:6
std::shared_ptr< Utils::RandomGenerator > randomGenerator
Definition: policy.h:44
std::vector< double > * getActionVector(state_t state)
Definition: policy.cpp:93
void initializeUniformly()
Definition: policy.cpp:41
void handleErrorInGettingAction(std::vector< double > *vector, state_t state, double d)
Definition: policy.cpp:118
action_t getAction(state_t state)
Definition: policy.cpp:98
string filename
Definition: aging.py:5
Policy(int nbOfStates, int nbOfActions, std::shared_ptr< Utils::RandomGenerator > gen)
Definition: policy.cpp:22
size_t nbOfActions
Definition: policy.h:43
size_t action_t
Definition: action_impl.h:18
size_t getNbOfActions()
Definition: policy.cpp:154
Definition: action.h:18
const double pol
size_t getNbOfStates()
Definition: policy.cpp:149
size_t nbOfStates
Definition: policy.h:42
size_t state_t
Definition: state.h:19
void initializeRandomly(std::shared_ptr< Utils::RandomGenerator > gen)
Definition: policy.cpp:32
void print(std::ostream &stream)
Definition: policy.cpp:132
static std::vector< std::string > split(std::string str, char delimiter)
Definition: stringUtils.cpp:18