Scheduler
tabularActionValues.cpp
Go to the documentation of this file.
1 
10 #include "tabularActionValues.h"
11 
12 #include <cassert>
13 #include <stdexcept>
14 #include <fstream>
15 #include <iostream>
16 
17 #include <utils/stringUtils.h>
18 #include <utils/randomGenerator.h>
19 
20 #include <mdp/stateSpace.h>
21 #include <mdp/actionSpace.h>
22 #include <mdp/mdpConfiguration.h>
23 
24 using namespace Mdp;
25 
26 TabularActionValues::TabularActionValues(std::shared_ptr<Context> context)
27 {
28  init(context);
29 }
30 
31 std::vector<std::vector<double>> TabularActionValues::fromFile(size_t S, size_t A, std::string filename)
32 {
33  std::ifstream stream;
34  stream.open(filename);
35  if (!stream.is_open())
36  throw std::runtime_error("cannot open action values file");
37  std::string line;
38  std::vector<std::vector<double>> av;
39  while(std::getline(stream, line))
40  {
41  std::vector<std::string> vect = Utils::StringUtils::split(line, ' ');
42  if (vect.size() > 0)
43  {
44  if (vect.size() != A)
45  throw std::runtime_error(
46  "number of values not equal to number of actions");
47  std::vector<double> row(A);
48  for (size_t i = 0; i < A; i++)
49  row[i] = std::stod(vect[i]);
50  av.push_back(row);
51  }
52  }
53  if (av.size() != S)
54  throw std::runtime_error(
55  "The number of lines in the file is not equal to the nnumber of states");
56  return av;
57 }
58 
59 
60 
61 
62 
63 
64 std::vector<std::vector<double>> TabularActionValues::randomly(size_t S, size_t A, time_t seed, double lowerBound, double upperBound)
65 {
66  if (lowerBound > upperBound)
67  {
68  throw std::invalid_argument("The lower bound is greater than the upper bound");
69  }
71  std::vector<std::vector<double>> av(S, std::vector<double>(A));
72  for (size_t s = 0; s < S; s++)
73  {
74  for (size_t a = 0; a < A; a++)
75  {
76  av[s][a] = gen.drawUniform(lowerBound, upperBound);
77  }
78  }
79  return av;
80 }
81 
82 std::vector<std::vector<double>> TabularActionValues::uniformly(size_t S, size_t A, double value)
83 {
84  std::vector<std::vector<double>> av(S, std::vector<double>(A, value));
85  return av;
86 }
87 
88 void TabularActionValues::init(std::shared_ptr<Context> context)
89 {
90  size_t S = context->stateSpace->size();
91  size_t A = context->actionSpace->size();
92 
93  std::string str = context->conf->getStringValue("reinforcementLearning", "actionValuesInitialization");
94  if (!str.compare("uniform"))
95  {
96  double initialAV = context->conf->getDoubleValue("reinforcementLearning", "initialActionValue");
97  actionValues = uniformly(S, A, initialAV);
98  }
99  else if (!str.compare("fromFile"))
100  {
101  actionValues = fromFile(S, A, "configuration/initialActionValues");
102  }
103  else if (!str.compare("random"))
104  {
105  time_t seed = context->conf->getIntValue("reinforcementLearning", "seed");
106  double lowerBound = context->conf->getDoubleValue("reinforcementLearning", "lowerBound");
107  double upperBound = context->conf->getDoubleValue("reinforcementLearning", "upperBound");
108  actionValues = randomly(S, A, seed, lowerBound, upperBound);
109  }
110  else
111  {
112  throw std::invalid_argument("invalid value for actionValuesInitialization");
113  }
114 }
115 
116 
117 
119 {
120  assert(s < actionValues.size());
121  assert(a < actionValues[s].size());
122  return actionValues[s][a];
123 }
124 
126 {
127  actionValues[s][a] = value;
128 }
129 
131 {
132  return actionValues.size();
133 }
134 
135 
136 std::vector<double> TabularActionValues::getValues(state_t state)
137 {
138  return actionValues[state];
139 }
const size_t A
const size_t S
line
Definition: bigtemp.py:6
std::vector< std::vector< double > > fromFile(size_t S, size_t A, std::string filename)
void init(std::shared_ptr< Context > context)
void updateValue(state_t state, action_t action, double value)
double getValue(state_t state, action_t action) override
std::vector< std::vector< double > > randomly(size_t S, size_t A, time_t seed, double lowerBound, double upperBound)
string filename
Definition: aging.py:5
size_t action_t
Definition: action_impl.h:18
std::vector< std::vector< double > > uniformly(size_t S, size_t A, double initialActionValues)
Definition: action.h:18
std::vector< double > getValues(state_t state) override
std::vector< std::vector< double > > actionValues
size_t state_t
Definition: state.h:19
TabularActionValues(std::shared_ptr< Context > context)
static std::vector< std::string > split(std::string str, char delimiter)
Definition: stringUtils.cpp:18
double drawUniform(double min, double max)