LearnTA  0.0.1
timed_word.hh
1 
6 #pragma once
7 
8 #include <iostream>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 #include <cassert>
13 
14 #include <boost/functional/hash.hpp>
15 
16 #include "common_types.hh"
17 
18 namespace learnta {
25  class TimedWord {
26  private:
27  std::string word;
28  std::vector<double> durations;
29  public:
30  TimedWord() : durations({0}) {}
31 
37  TimedWord(const std::string& word, const std::vector<double>& durations) {
38  assert(word.size() + 1 == durations.size());
39  this->word.reserve(word.size());
40  this->durations = {durations.front()};
41  this->durations.reserve(durations.size());
42  for (std::size_t i = 0; i < word.size(); ++i) {
43  if (word.at(i) != UNOBSERVABLE) {
44  this->word.push_back(word.at(i));
45  this->durations.push_back(durations.at(i + 1));
46  } else {
47  this->durations.back() += durations.at(i + 1);
48  }
49  }
50 
51  assert(this->word.size() + 1 == this->durations.size());
52  }
53 
59  TimedWord operator+(const TimedWord &another) const {
60  TimedWord result = *this;
61  result.word += another.word;
62  auto it = another.durations.begin();
63  result.durations.back() += *it++;
64  result.durations.reserve(this->durations.size() + another.durations.size() - 1);
65  for (; it != another.durations.end(); it++) {
66  result.durations.push_back(*it);
67  }
68 
69  return result;
70  }
71 
77  TimedWord operator+(const char action) {
78  TimedWord result = *this;
79  result.word.push_back(action);
80  result.durations.push_back(0);
81 
82  return result;
83  }
84 
90  TimedWord operator+(const double duration) {
91  TimedWord result = *this;
92  result.durations.back() += duration;
93 
94  return result;
95  }
96 
102  std::ostream &print(std::ostream &stream) const {
103  stream << durations[0];
104  for (std::size_t i = 0; i < word.size(); i++) {
105  stream << " " << word[i] << " " << durations[i + 1];
106  }
107  return stream;
108  }
109 
110  [[nodiscard]] const std::string &getWord() const {
111  return word;
112  }
113 
114  [[nodiscard]] const std::vector<double> &getDurations() const {
115  return durations;
116  }
117 
123  [[nodiscard]] std::vector<double> getAccumulatedDurations() const {
124  std::vector<double> accumulatedDurations;
125  accumulatedDurations.resize(durations.size());
126 
127  accumulatedDurations.back() = durations.back();
128  for (int i = durations.size() - 2; i >= 0; --i) {
129  accumulatedDurations.at(i) = accumulatedDurations.at(i + 1) +durations.at(i);
130  }
131  return accumulatedDurations;
132  }
133 
139  [[nodiscard]] TimedWord getSuffix(const TimedWord& prefix) const {
140  const auto suffixWord = this->word.substr(prefix.wordSize());
141  auto suffixDurations = std::vector<double>(this->durations.begin() + prefix.wordSize(), this->durations.end());
142  suffixDurations.front() -= prefix.getDurations().back();
143 
144  return TimedWord{suffixWord, suffixDurations};
145  }
146 
150  [[nodiscard]] std::size_t wordSize() const {
151  return this->word.size();
152  }
153 
154  bool operator==(const TimedWord &another) const {
155  return this->word == another.word && this->durations == another.durations;
156  }
157  };
158 
159  static inline std::ostream &print(std::ostream &os, const learnta::TimedWord &word) {
160  os << word.getDurations().front();
161  for (std::size_t i = 0; i < word.wordSize(); ++i) {
162  os << " " << word.getWord().at(i) << " " << word.getDurations().at(i + 1);
163  }
164  return os;
165  }
166 
167  static inline std::ostream &operator<<(std::ostream &os, const learnta::TimedWord &word) {
168  return learnta::print(os, word);
169  }
170 
171  static inline std::size_t hash_value(const TimedWord &word) {
172  return boost::hash_value(std::make_tuple(word.getWord(), word.getDurations()));
173  }
174 }
A timed word.
Definition: timed_word.hh:25
TimedWord operator+(const double duration)
Return the concatenation of this timed word and a time elapse.
Definition: timed_word.hh:90
TimedWord(const std::string &word, const std::vector< double > &durations)
Constructor of timed words.
Definition: timed_word.hh:37
std::ostream & print(std::ostream &stream) const
Print the timed word to the stream.
Definition: timed_word.hh:102
std::size_t wordSize() const
Return the number of the actions in this timed word.
Definition: timed_word.hh:150
TimedWord getSuffix(const TimedWord &prefix) const
Definition: timed_word.hh:139
TimedWord operator+(const char action)
Return the concatenation of this timed word and an action.
Definition: timed_word.hh:77
std::vector< double > getAccumulatedDurations() const
Construct and return the (tail) accumulated duration.
Definition: timed_word.hh:123
TimedWord operator+(const TimedWord &another) const
Return the concatenation of two timed words.
Definition: timed_word.hh:59
Definition: experiment_runner.hh:23