forpy  2
types.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner. */
2 #pragma once
3 #ifndef FORPY_TYPES_H_
4 #define FORPY_TYPES_H_
5 
6 #include <Eigen/Dense>
7 #include <map>
8 #include <memory>
9 #include <numeric>
10 #include <utility>
11 #include <vector>
12 
13 #include "./util/hash.h"
14 #include "./util/storage.h"
15 
16 namespace forpy {
17 
19 typedef unsigned int uint;
20 
24 template <typename T>
25 struct Name {
26  static std::string value() { return "unknown"; };
27 };
28 template <>
29 struct Name<double> {
30  static std::string value() { return "d"; }
31 };
32 template <>
33 struct Name<float> {
34  static std::string value() { return "f"; }
35 };
36 template <>
37 struct Name<uint> {
38  static std::string value() { return "ui"; }
39 };
40 template <>
41 struct Name<uint8_t> {
42  static std::string value() { return "ui8"; }
43 };
44 template <>
45 struct Name<int16_t> {
46  static std::string value() { return "i16"; }
47 };
48 template <>
49 struct Name<int> {
50  static std::string value() { return "i"; }
51 };
52 
54 template <typename DT>
55 using Mat = Eigen::Matrix<DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
56 
58 template <typename DT>
59 using MatCM =
60  Eigen::Matrix<DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
61 
63 template <typename DT>
64 using MatCRef = Eigen::Ref<const Mat<DT>>;
65 
67 template <typename DT>
68 using MatCMCRef = Eigen::Ref<const MatCM<DT>>;
69 
71 template <typename DT>
72 using MatRef = Eigen::Ref<Mat<DT>>;
73 
75 template <typename DT>
76 using Vec = Eigen::Matrix<DT, Eigen::Dynamic, 1, Eigen::ColMajor>;
77 
78 template <typename DT>
79 using VecRM = Eigen::Matrix<DT, 1, Eigen::Dynamic, Eigen::RowMajor>;
80 
81 template <typename DT>
82 using VecRef = Eigen::Ref<Vec<DT>>;
83 
84 template <typename DT>
85 using VecRMRef = Eigen::Ref<VecRM<DT>>;
86 
87 template <typename DT>
88 using VecCRef = Eigen::Ref<const Vec<DT>>;
89 
90 template <typename DT>
91 using VecCMap =
92  Eigen::Map<const Eigen::Matrix<DT, 1, Eigen::Dynamic, Eigen::RowMajor>,
93  Eigen::Unaligned, Eigen::InnerStride<>>;
94 
98 enum class ECompletionLevel {
100  Node,
103  Level,
105  Complete
106 };
107 
109 typedef size_t id_t;
110 
111 typedef std::function<id_t(const Data<MatCRef> &, const id_t &,
112  const std::function<void(void *)> &)>
114 
117 
118 template <typename FT>
119 struct SplitOptRes {
120  id_t split_idx;
121  FT thresh;
122  float gain;
123  bool valid;
124 
125  inline friend std::ostream &operator<<(std::ostream &stream,
126  const SplitOptRes<FT> &self) {
127  stream << "forpy::SplitOptRes_X[<=" << self.thresh
128  << "; gain: " << self.gain << ", valid: " << self.valid << "]";
129  return stream;
130  };
131 
132  bool operator==(SplitOptRes<FT> const &rhs) const {
133  return split_idx == rhs.split_idx && thresh == rhs.thresh &&
134  gain == rhs.gain && valid == rhs.valid;
135  }
136 };
137 typedef mu::variant<SplitOptRes<float>, SplitOptRes<double>, SplitOptRes<uint>,
140 
141 typedef std::pair<id_t, id_t> interv_t;
142 
152 struct TodoMark {
153  inline TodoMark() : node_id(0), depth(0){};
154  inline TodoMark(std::shared_ptr<std::vector<id_t>> sample_ids,
155  const interv_t &interv, const id_t &node_id,
156  const uint &depth)
157  : sample_ids(sample_ids),
158  interv(interv),
159  node_id(node_id),
160  depth(depth){};
161  std::shared_ptr<std::vector<id_t>> sample_ids;
162  interv_t interv;
163  id_t node_id;
164  uint depth;
165  inline bool operator==(TodoMark const &rhs) const {
166  return node_id == rhs.node_id && depth == rhs.depth &&
167  interv == rhs.interv && *sample_ids == *(rhs.sample_ids);
168  }
169  inline friend std::ostream &operator<<(std::ostream &stream,
170  const TodoMark &self) {
171  stream << "forpy::TodoMark[node_id: " << self.node_id << ", depth "
172  << self.depth << "]";
173  return stream;
174  };
176 
177  private:
178  friend class cereal::access;
179  template <class Archive>
180  void serialize(Archive &ar, const uint &) {
181  ar(CEREAL_NVP(sample_ids), CEREAL_NVP(interv), CEREAL_NVP(node_id),
182  CEREAL_NVP(depth));
183  };
185 };
186 
187 typedef std::pair<ptrdiff_t, ptrdiff_t> regint_t;
188 
190 typedef std::vector<std::pair<std::shared_ptr<std::vector<size_t>>,
191  std::shared_ptr<std::vector<float> const>>>
193 
202 typedef std::pair<std::shared_ptr<std::vector<id_t>>, id_t> include_pair_t;
203 
205 enum class ESearchType { DFS, BFS };
206 
208 typedef std::unordered_set<std::vector<size_t>, vector_hasher> proposal_set_t;
209 
210 #pragma clang diagnostic push
211 #pragma clang diagnostic ignored "-Wunused-variable"
212 const double GAIN_EPS = 1E-7;
213 #pragma clang diagnostic pop
215 }; // namespace forpy
216 #endif // FORPY_TYPES_H_
std::pair< std::shared_ptr< std::vector< id_t > >, id_t > include_pair_t
A pair containing information about newly included samples.
Definition: types.h:202
uint depth
Definition: types.h:164
const double GAIN_EPS
Definition: types.h:212
static std::string value()
Definition: types.h:26
Eigen::Map< const Eigen::Matrix< DT, 1, Eigen::Dynamic, Eigen::RowMajor >, Eigen::Unaligned, Eigen::InnerStride<>> VecCMap
Definition: types.h:93
#define MOVE_ASSIGN(TypeName)
Definition: global.h:60
Eigen::Ref< VecRM< DT >> VecRMRef
Definition: types.h:85
interv_t interv
Definition: types.h:162
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: global.h:64
size_t id_t
Element id type.
Definition: types.h:109
const MatCRef< float > FORPY_ZERO_MATR(Mat< float >::Zero(0, 1))
std::vector< std::pair< std::shared_ptr< std::vector< size_t > >, std::shared_ptr< std::vector< float > const > > > usage_map_t
Describes how each sample is used for each tree.
Definition: types.h:192
bool operator==(SplitOptRes< FT > const &rhs) const
Definition: types.h:132
Eigen::Ref< const Vec< DT >> VecCRef
Definition: types.h:88
std::pair< id_t, id_t > interv_t
Definition: types.h:141
void serialize(Archive &ar, const uint &)
Definition: types.h:180
id_t node_id
Definition: types.h:163
std::pair< ptrdiff_t, ptrdiff_t > regint_t
Definition: types.h:187
TodoMark(std::shared_ptr< std::vector< id_t >> sample_ids, const interv_t &interv, const id_t &node_id, const uint &depth)
Definition: types.h:154
ESearchType
Definition: types.h:205
Stores the parameters for one marked tree node.
Definition: types.h:152
Eigen::Ref< const Mat< DT >> MatCRef
Parameterized const matrix ref type.
Definition: types.h:64
friend std::ostream & operator<<(std::ostream &stream, const TodoMark &self)
Definition: types.h:169
std::unordered_set< std::vector< size_t >, vector_hasher > proposal_set_t
The type of a set of dimension selections.
Definition: types.h:208
static std::string value()
Definition: types.h:50
Eigen::Ref< Vec< DT >> VecRef
Definition: types.h:82
Eigen::Ref< const MatCM< DT >> MatCMCRef
Parameterized const matrix column major matrix ref type.
Definition: types.h:68
bool operator==(TodoMark const &rhs) const
Definition: types.h:165
static std::string value()
Definition: types.h:34
Eigen::Matrix< DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatCM
Parameterized column major matrix type.
Definition: types.h:60
std::shared_ptr< std::vector< id_t > > sample_ids
Definition: types.h:160
mu::variant< SplitOptRes< float >, SplitOptRes< double >, SplitOptRes< uint >, SplitOptRes< uint8_t > > OptSplitV
Definition: types.h:139
EThresholdSelection
Specifies which thresholds should be used for a decision.
Definition: types.h:116
static std::string value()
Definition: types.h:42
Struct for translating primitive types to a short name.
Definition: types.h:25
Eigen::Ref< Mat< DT >> MatRef
Parameterized standard non-const matrix ref type.
Definition: types.h:72
Eigen::Matrix< DT, Eigen::Dynamic, 1, Eigen::ColMajor > Vec
Definition: types.h:76
Eigen::Matrix< DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Mat
Parameterized Matrix type (row major).
Definition: types.h:55
static std::string value()
Definition: types.h:38
A simple vector<size_t> hasher.
Definition: hash.h:34
friend std::ostream & operator<<(std::ostream &stream, const SplitOptRes< FT > &self)
Definition: types.h:125
static std::string value()
Definition: types.h:46
ECompletionLevel
Specifies the completion level for one training step.
Definition: types.h:98
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:19
Eigen::Matrix< DT, 1, Eigen::Dynamic, Eigen::RowMajor > VecRM
Definition: types.h:79
std::function< id_t(const Data< MatCRef > &, const id_t &, const std::function< void(void *)> &)> node_predf
Definition: types.h:113
static std::string value()
Definition: types.h:30