Version: 1.0
hypercube.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_HYPERCUBE_H_
8 #define GRAPHGEN_HYPERCUBE_H_
9 
10 #include <algorithm>
11 #include <cassert>
12 #include <iostream>
13 
14 #include "conact_tree.h"
15 #include "rule_set.h"
16 
17 typedef unsigned char byte;
18 
19 
20 enum VDim { Zero = 0, One = 1, Indifference = 2 };
21 
22 struct VIndex {
23  size_t m_iDim;
24  std::vector<VDim> m_arrIndex;
25 
26  VIndex(int iDim = 0) : m_iDim(iDim), m_arrIndex(iDim) {
27  }
28 
29  VIndex(const std::string &s) : m_iDim(s.length()), m_arrIndex(s.length()) {
30  SetIndex(s);
31  }
32 
33  void SetDim(int iDim) {
34  m_iDim = iDim;
35  m_arrIndex.resize(iDim);
36  }
37 
38  bool SetIndex(const std::string &s) {
39  if (s.length() != m_iDim)
40  return false;
41  for (size_t i = 0; i<m_iDim; i++) {
42  if (s[i] == '0')
43  m_arrIndex[i] = Zero;
44  else if (s[i] == '1')
45  m_arrIndex[i] = One;
46  else if (s[i] == '-')
48  else
49  return false;
50  }
51  return true;
52  }
53 
54  std::string GetIndexString() const {
55  std::string s;
56  s.resize(m_iDim);
57  char aRepresentation[3] = { '0','1','-' };
58  for (size_t i = 0; i<m_iDim; i++) {
59  s[i] = aRepresentation[m_arrIndex[i]];
60  }
61  return s;
62  }
63 
64  unsigned GetIndex() const {
65  unsigned ui(0);
66  for (size_t i = 0; i<m_iDim; i++) {
67  ui *= 3;
68  ui += m_arrIndex[i];
69  }
70  return ui;
71  }
72 
73  bool MoveNext() {
74  for (int i = int(m_iDim) - 1; i >= 0; i--) {
75  if (m_arrIndex[i] == Indifference)
76  continue;
77  else if (m_arrIndex[i] == Zero) {
78  m_arrIndex[i] = One;
79  return true;
80  }
81  else
82  m_arrIndex[i] = Zero;
83  }
84  return false;
85  }
86 };
87 
88 #pragma pack(push)
89 #pragma pack(1)
90 struct VNode {
91  std::bitset<131/*CTBE needs 131 bits*/> uiAction;
92  /*unsigned*/ unsigned long long uiProb;
93  /*unsigned*/ unsigned long long uiGain;
95  unsigned neq = 1;
96 
97  VNode() : uiAction(0), uiProb(0), uiGain(0), uiMaxGainIndex(0) {
98  }
99 };
100 #pragma pack(pop)
101 
102 template <typename T>
103 std::istream& rawread(std::istream& is, T& val, size_t n) {
104  return is.read(reinterpret_cast<char*>(&val), n);
105 }
106 template <typename T>
107 std::ostream& rawwrite(std::ostream& os, const T& val, size_t n) {
108  return os.write(reinterpret_cast<const char*>(&val), n);
109 }
110 
111 struct VHyperCube {
112  size_t m_iDim;
113  std::vector<VNode> m_arrIndex;
114  const rule_set& m_rs;
115 
116  VHyperCube(const rule_set& rs) : m_rs(rs), m_iDim(rs.conditions.size()), m_arrIndex(unsigned(pow(3.0, rs.conditions.size()))) {
117  // Initialize hypercube nodes using the rules defined in the ruleset
118  auto nrules = rs.rules.size();
119  for (size_t i = 0; i < nrules; ++i) {
120  // for each rule generate the hypercube index
121  std::string s = binary(i, m_iDim);
122  std::reverse(begin(s), end(s));
123  VIndex idx(s);
124  // and set its values
125  m_arrIndex[idx.GetIndex()].uiProb = rs.rules[i].frequency;
126  m_arrIndex[idx.GetIndex()].uiAction = rs.rules[i].actions;
127  }
128  }
129 
130  std::istream& read(std::istream& is) {
131  return rawread(is, m_arrIndex[0], m_arrIndex.size() * sizeof(VNode));
132  }
133  std::ostream& write(std::ostream& os) {
134  return rawwrite(os, m_arrIndex[0], m_arrIndex.size() * sizeof(VNode));
135  }
136 
137  VNode& operator[](const VIndex &idx) {
138  return m_arrIndex[idx.GetIndex()];
139  }
140  const VNode& operator[](const VIndex &idx) const {
141  return m_arrIndex[idx.GetIndex()];
142  }
143 
144  BinaryDrag<conact> optimize(bool bVerbose = false);
145 };
146 
147 // Generates an Optimal Decision Tree from the given rule_set,
148 // and store it in the filename when specified.
150 BinaryDrag<conact> GenerateOdt(const rule_set& rs, const std::string& filename);
151 
164 BinaryDrag<conact> GetOdt(const rule_set& rs, bool force_generation = false);
165 
166 
180 BinaryDrag<conact> GetOdtWithFileSuffix(const rule_set& rs, const std::string& file_suffix, bool force_generation = false);
181 
182 
183 #endif // !GRAPHGEN_CONACT_TREE_H_
unsigned char byte
Definition: hypercube.h:17
std::istream & rawread(std::istream &is, T &val, size_t n)
Definition: hypercube.h:103
std::ostream & rawwrite(std::ostream &os, const T &val, size_t n)
Definition: hypercube.h:107
VDim
Definition: hypercube.h:20
@ Indifference
Definition: hypercube.h:20
@ Zero
Definition: hypercube.h:20
@ One
Definition: hypercube.h:20
BinaryDrag< conact > GetOdt(const rule_set &rs, bool force_generation=false)
Returns the optimal (or pseudo optimal) decision tree generated from the given rule set.
Definition: hypercube.cpp:198
BinaryDrag< conact > GenerateOdt(const rule_set &rs)
Definition: hypercube.cpp:179
BinaryDrag< conact > GetOdtWithFileSuffix(const rule_set &rs, const std::string &file_suffix, bool force_generation=false)
Returns the optimal (or pseudo optimal) decision tree generated from the given rule set.
VHyperCube(const rule_set &rs)
Definition: hypercube.h:116
const rule_set & m_rs
Definition: hypercube.h:114
const VNode & operator[](const VIndex &idx) const
Definition: hypercube.h:140
VNode & operator[](const VIndex &idx)
Definition: hypercube.h:137
std::istream & read(std::istream &is)
Definition: hypercube.h:130
std::ostream & write(std::ostream &os)
Definition: hypercube.h:133
std::vector< VNode > m_arrIndex
Definition: hypercube.h:113
size_t m_iDim
Definition: hypercube.h:112
BinaryDrag< conact > optimize(bool bVerbose=false)
Definition: hypercube.cpp:35
VIndex(const std::string &s)
Definition: hypercube.h:29
bool MoveNext()
Definition: hypercube.h:73
VIndex(int iDim=0)
Definition: hypercube.h:26
size_t m_iDim
Definition: hypercube.h:23
std::string GetIndexString() const
Definition: hypercube.h:54
unsigned GetIndex() const
Definition: hypercube.h:64
std::vector< VDim > m_arrIndex
Definition: hypercube.h:24
void SetDim(int iDim)
Definition: hypercube.h:33
bool SetIndex(const std::string &s)
Definition: hypercube.h:38
byte uiMaxGainIndex
Definition: hypercube.h:94
unsigned neq
Definition: hypercube.h:95
VNode()
Definition: hypercube.h:97
unsigned long long uiProb
Definition: hypercube.h:92
unsigned long long uiGain
Definition: hypercube.h:93
std::bitset< 131 > uiAction
Definition: hypercube.h:91
std::vector< rule > rules
Definition: rule_set.h:52
std::string binary(size_t u, size_t nbits)
Definition: utilities.cpp:11