Signature.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #include <sstream>
20 
21 #include "Signature.h"
22 
23 #include <boost/spirit/include/qi.hpp> // for parsing
24 #include <boost/spirit/include/phoenix.hpp> // for qi::_val
25 
26 namespace gtsam {
27 
28  using namespace std;
29 
30  namespace qi = boost::spirit::qi;
31  namespace ph = boost::phoenix;
32 
33  // parser for strings of form "99/1 80/20" etc...
34  namespace parser {
35  typedef string::const_iterator It;
36  using boost::phoenix::val;
37  using boost::phoenix::ref;
38  using boost::phoenix::push_back;
39 
40  // Special rows, true and false
42  Signature::Row r(2);
43  r[0] = 1;
44  r[1] = 0;
45  return r;
46  }
48  Signature::Row r(2);
49  r[0] = 0;
50  r[1] = 1;
51  return r;
52  }
54 
55  // Special tables (inefficient, but do we care for user input?)
56  Signature::Table logic(bool ff, bool ft, bool tf, bool tt) {
58  t[0] = ff ? T : F;
59  t[1] = ft ? T : F;
60  t[2] = tf ? T : F;
61  t[3] = tt ? T : F;
62  return t;
63  }
64 
65  struct Grammar {
66  qi::rule<It, qi::space_type, Signature::Table()> table, or_, and_, rows;
67  qi::rule<It, Signature::Row()> true_, false_, row;
68  Grammar() {
69  table = or_ | and_ | rows;
70  or_ = qi::lit("OR")[qi::_val = logic(false, true, true, true)];
71  and_ = qi::lit("AND")[qi::_val = logic(false, false, false, true)];
72  rows = +(row | true_ | false_); // only loads first of the rows under boost 1.42
73  row = qi::double_ >> +("/" >> qi::double_);
74  true_ = qi::lit("T")[qi::_val = T];
75  false_ = qi::lit("F")[qi::_val = F];
76  }
77  } grammar;
78 
79  // Create simpler parsing function to avoid the issue of only parsing a single row
80  bool parse_table(const string& spec, Signature::Table& table) {
81  // check for OR, AND on whole phrase
82  It f = spec.begin(), l = spec.end();
83  if (qi::parse(f, l,
84  qi::lit("OR")[ph::ref(table) = logic(false, true, true, true)]) ||
85  qi::parse(f, l,
86  qi::lit("AND")[ph::ref(table) = logic(false, false, false, true)]))
87  return true;
88 
89  // tokenize into separate rows
90  istringstream iss(spec);
91  string token;
92  while (iss >> token) {
94  It tf = token.begin(), tl = token.end();
95  bool r = qi::parse(tf, tl,
96  qi::double_[push_back(ph::ref(values), qi::_1)] >> +("/" >> qi::double_[push_back(ph::ref(values), qi::_1)]) |
97  qi::lit("T")[ph::ref(values) = T] |
98  qi::lit("F")[ph::ref(values) = F] );
99  if (!r)
100  return false;
101  table.push_back(values);
102  }
103 
104  return true;
105  }
106  } // \namespace parser
107 
108  ostream& operator <<(ostream &os, const Signature::Row &row) {
109  os << row[0];
110  for (size_t i = 1; i < row.size(); i++)
111  os << " " << row[i];
112  return os;
113  }
114 
115  ostream& operator <<(ostream &os, const Signature::Table &table) {
116  for (size_t i = 0; i < table.size(); i++)
117  os << table[i] << endl;
118  return os;
119  }
120 
122  key_(key) {
123  }
124 
127  keys.push_back(key_);
128  for (const DiscreteKey& key : parents_) keys.push_back(key);
129  return keys;
130  }
131 
133  KeyVector js;
134  js.push_back(key_.first);
135  for (const DiscreteKey& key : parents_) js.push_back(key.first);
136  return js;
137  }
138 
139  vector<double> Signature::cpt() const {
140  vector<double> cpt;
141  if (table_) {
142  const size_t nrStates = table_->at(0).size();
143  for (size_t j = 0; j < nrStates; j++) {
144  for (const Row& row : *table_) {
145  assert(row.size() == nrStates);
146  cpt.push_back(row[j]);
147  }
148  }
149  }
150  return cpt;
151  }
152 
154  parents_.push_back(parent);
155  return *this;
156  }
157 
158  static void normalize(Signature::Row& row) {
159  double sum = 0;
160  for (size_t i = 0; i < row.size(); i++)
161  sum += row[i];
162  for (size_t i = 0; i < row.size(); i++)
163  row[i] /= sum;
164  }
165 
166  Signature& Signature::operator=(const string& spec) {
167  spec_.reset(spec);
168  Table table;
169  // NOTE: using simpler parse function to ensure boost back compatibility
170 // parser::It f = spec.begin(), l = spec.end();
171  bool success = //
172 // qi::phrase_parse(f, l, parser::grammar.table, qi::space, table); // using full grammar
173  parser::parse_table(spec, table);
174  if (success) {
175  for(Row& row: table)
176  normalize(row);
177  table_.reset(table);
178  }
179  return *this;
180  }
181 
183  Table table = t;
184  for(Row& row: table)
185  normalize(row);
186  table_.reset(table);
187  return *this;
188  }
189 
190  ostream& operator <<(ostream &os, const Signature &s) {
191  os << s.key_.first;
192  if (s.parents_.empty()) {
193  os << " % ";
194  } else {
195  os << " | " << s.parents_[0].first;
196  for (size_t i = 1; i < s.parents_.size(); i++)
197  os << " && " << s.parents_[i].first;
198  os << " = ";
199  }
200  os << (s.spec_ ? *s.spec_ : "no spec") << endl;
201  if (s.table_)
202  os << (*s.table_);
203  else
204  os << "spec could not be parsed" << endl;
205  return os;
206  }
207 
208  Signature operator|(const DiscreteKey& key, const DiscreteKey& parent) {
209  Signature s(key);
210  return s, parent;
211  }
212 
213  Signature operator%(const DiscreteKey& key, const string& parent) {
214  Signature s(key);
215  return s = parent;
216  }
217 
219  Signature s(key);
220  return s = parent;
221  }
222 
223 } // namespace gtsam
Signature::Row createT()
Definition: Signature.cpp:47
const boost::optional< Table > & table() const
Definition: Signature.h:96
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Definition: base/Matrix.h:225
std::ostream & operator<<(std::ostream &os, const Dih6 &m)
Definition: testGroup.cpp:109
qi::rule< It, qi::space_type, Signature::Table()> table
Definition: Signature.cpp:66
signatures for conditional densities
Signature::Row createF()
Definition: Signature.cpp:41
DiscreteKey key_
Definition: Signature.h:63
boost::optional< std::string > spec_
Definition: Signature.h:69
leaf::MyValues values
Signature & operator,(const DiscreteKey &parent)
Definition: Signature.cpp:153
Definition: Half.h:150
boost::optional< Table > table_
Definition: Signature.h:72
Signature operator|(const DiscreteKey &key, const DiscreteKey &parent)
Definition: Signature.cpp:208
std::vector< double > cpt() const
Definition: Signature.cpp:139
static void normalize(Signature::Row &row)
Definition: Signature.cpp:158
Signature & operator=(const std::string &spec)
Definition: Signature.cpp:166
DiscreteKeys discreteKeys() const
Definition: Signature.cpp:125
DiscreteKeys parents_
Definition: Signature.h:66
std::vector< Row > Table
Definition: Signature.h:58
static const Line3 l(Rot3(), 1, 1)
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
Signature::Row F
Definition: Signature.cpp:53
KeyVector indices() const
Definition: Signature.cpp:132
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
Signature(const DiscreteKey &key)
Definition: Signature.cpp:121
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Signature::Table logic(bool ff, bool ft, bool tf, bool tt)
Definition: Signature.cpp:56
RealScalar s
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
Signature::Row T
Definition: Signature.cpp:53
Signature operator%(const DiscreteKey &key, const string &parent)
Definition: Signature.cpp:213
const_iterator begin() const
Definition: Values.h:241
string::const_iterator It
Definition: Signature.cpp:35
std::vector< double > Row
Definition: Signature.h:57
qi::rule< It, Signature::Row()> true_
Definition: Signature.cpp:67
traits
Definition: chartTesting.h:28
ofstream os("timeSchurFactors.csv")
ArrayXXf table(10, 4)
GTSAM_EXPORT friend std::ostream & operator<<(std::ostream &os, const Signature &s)
const KeyVector keys
struct gtsam::parser::Grammar grammar
def parse(input_path, output_path, quiet=False, generate_xml_flag=True)
Definition: parse_xml.py:10
bool parse_table(const string &spec, Signature::Table &table)
Definition: Signature.cpp:80
std::ptrdiff_t j
Point2 t(10, 10)
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:37
const DiscreteKey & key() const
Definition: Signature.h:80


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:44:02