LevelMaps.hpp 5.08 KB
Newer Older
niklas.baumgarten's avatar
niklas.baumgarten committed
1
2
3
4
5
6
#ifndef LEVELMAPS_HPP
#define LEVELMAPS_HPP

#include "WelfordAggregate.hpp"

#include <vector>
niklas.baumgarten's avatar
niklas.baumgarten committed
7
#include <math.h>
niklas.baumgarten's avatar
niklas.baumgarten committed
8
9
10
11
12
13
#include <map>


template<typename T>
struct LevelMap {
protected:
niklas.baumgarten's avatar
niklas.baumgarten committed
14
  std::map<int, T> _levelMap{};
niklas.baumgarten's avatar
niklas.baumgarten committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

public:
  LevelMap() {};

  ~LevelMap() {
    clear();
  }

  LevelMap(std::initializer_list<std::pair<int, T>> levelMap) {
    for (auto const &pair : levelMap) {
      _levelMap.emplace(pair.first, T(pair.second));
    }
  }

  std::vector<int> GetLevelVector() const {
    std::vector<int> levelVector;
    for (auto const &[level, entry]: _levelMap)
      levelVector.push_back(level);
    return levelVector;
  }

  std::vector<double> GetQVector() const {
    std::vector<double> qVector;
    for (auto const &[level, entry]: _levelMap)
      qVector.push_back(entry.Q);
    return qVector;
  }

  std::vector<double> GetYVector() const {
    std::vector<double> yVector;
    for (auto const &[level, entry]: _levelMap)
      yVector.push_back(entry.Y);
    return yVector;
  }

  std::vector<double> GetCostVector() const {
    std::vector<double> costVector;
    for (auto const &[level, entry]: _levelMap)
      costVector.push_back(entry.C);
    return costVector;
  }

  std::vector<int> GetMVector() const {
    std::vector<int> mVector;
    for (auto const &[level, entry]: _levelMap)
      mVector.push_back(entry.M);
    return mVector;
  }

  std::vector<int> GetdMVector() const {
    std::vector<int> dMVector;
    for (auto const &[level, entry]: _levelMap)
      dMVector.push_back(entry.dM);
    return dMVector;
  }

  auto size() const { return _levelMap.size(); }

  auto clear() {
    for (auto const &[level, entry] : _levelMap)
      if constexpr (std::is_pointer<T>::value)
        delete entry;
    _levelMap.clear();
  }

  auto insert(std::pair<int, T> &&pair) { _levelMap.insert(pair); }

  auto begin() { return _levelMap.begin(); }

  auto rbegin() { return _levelMap.rbegin(); }

  auto end() { return _levelMap.end(); }

  auto rend() { return _levelMap.end(); }

  auto begin() const { return _levelMap.begin(); }

  auto rbegin() const { return _levelMap.rbegin(); }

  auto end() const { return _levelMap.end(); }

  auto rend() const { return _levelMap.end(); }

  auto operator[](int level) { return _levelMap[level]; }

niklas.baumgarten's avatar
niklas.baumgarten committed
100
  auto at(int level) const { return _levelMap.at(level); }
niklas.baumgarten's avatar
niklas.baumgarten committed
101
102
103
104
105
106
107
108
109
110
111
112
113

  auto find(int level) { return _levelMap.find(level); }

  friend Logging &operator<<(Logging &s, const LevelMap<T> &levelMap) {
    s << "[";
    for (auto &[level, entry] : levelMap) {
      if (level != levelMap.rbegin()->first) s << entry << ", ";
      else s << entry;
    }
    return s << "]";
  }
};

niklas.baumgarten's avatar
niklas.baumgarten committed
114
115
template<typename T>
std::string vec2str(std::vector<T> vec);
niklas.baumgarten's avatar
niklas.baumgarten committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

struct EstimatorMap;

struct SampleCounterMap : public LevelMap<SampleCounter> {

  SampleCounterMap() {};

  SampleCounterMap(std::initializer_list<std::pair<int, SampleCounter>> ctrs) :
    LevelMap<SampleCounter>(ctrs) {};

  void Update(const EstimatorMap &estimatorMap);

  bool NoSamplesLeft();
};

struct MeanMap : public LevelMap<Mean> {
  double Q = 0.0;
  double Cost = 0.0;

  MeanMap() {};

  MeanMap(std::initializer_list<std::pair<int, Mean>> avgs) :
    LevelMap<Mean>(avgs) {};

  MeanMap(const EstimatorMap &estimatorMap) {
    Update(estimatorMap);
  };

  void Update(const EstimatorMap &estimatorMap);
};

struct SVarMap : public LevelMap<SVar> {

  SVarMap() {};

  SVarMap(std::initializer_list<std::pair<int, SVar>> vars) :
    LevelMap<SVar>(vars) {};

  SVarMap(const EstimatorMap &estimatorMap) {
    Update(estimatorMap);
  };

  void Update(const EstimatorMap &estimatorMap);
};

struct KurtosisMap : LevelMap<Kurtosis> {

  KurtosisMap() {};

  KurtosisMap(std::initializer_list<std::pair<int, Kurtosis>> kurtosis) :
    LevelMap<Kurtosis>(kurtosis) {};

  KurtosisMap(const EstimatorMap &estimatorMap) {
    Update(estimatorMap);
  };

  void Update(const EstimatorMap &estimatorMap);
};

struct MultilevelData {
  SampleCounterMap ctrs;

  MeanMap means;

  SVarMap sVars;

  KurtosisMap kurtosis;

  MultilevelData() {};

  MultilevelData(const EstimatorMap &estimatorMap) {
    ExtractDataFrom(estimatorMap);
  }

  MultilevelData(const SampleCounterMap &ctrs, const MeanMap &avgs,
                 const SVarMap &vars, const KurtosisMap &kurtosis) :
    ctrs(ctrs), means(avgs), sVars(vars), kurtosis(kurtosis) {};

  void ExtractDataFrom(const EstimatorMap &estimatorMap) {
    ctrs.Update(estimatorMap);
    means.Update(estimatorMap);
    sVars.Update(estimatorMap);
    kurtosis.Update(estimatorMap);
  }

niklas.baumgarten's avatar
niklas.baumgarten committed
201
  void MultilevelResults() const;
niklas.baumgarten's avatar
niklas.baumgarten committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215

  friend Logging &operator<<(Logging &s, const MultilevelData &data) {
    return s << "E(Qf-Qc)=" << data.means.GetYVector() << endl
             << "E(Qf)=" << data.means.GetQVector() << endl
             << "V(Qf-Qc)=" << data.sVars.GetYVector() << endl
             << "V(Qf)=" << data.sVars.GetQVector() << endl
             << "kurtosis=" << data.kurtosis.GetYVector() << endl
             << "E(cost)=" << data.means.GetCostVector() << endl
             << "Used Levels=" << data.means.GetLevelVector() << endl
             << "Used Samples=" << data.ctrs.GetMVector() << endl;
  }
};

#endif //LEVELMAPS_HPP