TableFactor.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 <gtsam/base/FastSet.h>
24 
25 #include <utility>
26 
27 using namespace std;
28 
29 namespace gtsam {
30 
31 /* ************************************************************************ */
32 TableFactor::TableFactor() {}
33 
34 /* ************************************************************************ */
35 TableFactor::TableFactor(const DiscreteKeys& dkeys,
36  const TableFactor& potentials)
37  : DiscreteFactor(dkeys.indices(), dkeys.cardinalities()) {
38  sparse_table_ = potentials.sparse_table_;
39  denominators_ = potentials.denominators_;
41  sort(sorted_dkeys_.begin(), sorted_dkeys_.end());
42 }
43 
44 /* ************************************************************************ */
47  : DiscreteFactor(dkeys.indices(), dkeys.cardinalities()),
48  sparse_table_(table.size()) {
50  double denom = table.size();
51  for (const DiscreteKey& dkey : dkeys) {
52  denom /= dkey.second;
53  denominators_.insert(std::pair<Key, double>(dkey.first, denom));
54  }
56  sort(sorted_dkeys_.begin(), sorted_dkeys_.end());
57 }
58 
59 /* ************************************************************************ */
61  const DecisionTree<Key, double>& dtree)
62  : TableFactor(dkeys, DecisionTreeFactor(dkeys, dtree)) {}
63 
76  const DiscreteKeys& dkeys, const DecisionTreeFactor& dt) {
77  // SparseVector needs to know the maximum possible index,
78  // so we compute the product of cardinalities.
79  size_t cardinalityProduct = 1;
80  for (auto&& [_, c] : dt.cardinalities()) {
81  cardinalityProduct *= c;
82  }
83  Eigen::SparseVector<double> sparseTable(cardinalityProduct);
84  size_t nrValues = 0;
85  dt.visit([&nrValues](double x) {
86  if (x > 0) nrValues += 1;
87  });
88  sparseTable.reserve(nrValues);
89 
90  std::set<Key> allKeys(dt.keys().begin(), dt.keys().end());
91 
102  auto op = [&](const Assignment<Key>& assignment, double p) {
103  if (p > 0) {
104  // Get all the keys involved in this assignment
105  std::set<Key> assignmentKeys;
106  for (auto&& [k, _] : assignment) {
107  assignmentKeys.insert(k);
108  }
109 
110  // Find the keys missing in the assignment
111  std::vector<Key> diff;
112  std::set_difference(allKeys.begin(), allKeys.end(),
113  assignmentKeys.begin(), assignmentKeys.end(),
114  std::back_inserter(diff));
115 
116  // Generate all assignments using the missing keys
117  DiscreteKeys extras;
118  for (auto&& key : diff) {
119  extras.push_back({key, dt.cardinality(key)});
120  }
121  auto&& extraAssignments = DiscreteValues::CartesianProduct(extras);
122 
123  for (auto&& extra : extraAssignments) {
124  // Create new assignment using the extra assignment
125  DiscreteValues updatedAssignment(assignment);
126  updatedAssignment.insert(extra);
127 
128  // Generate index and add to the sparse vector.
129  Eigen::Index idx = 0;
130  size_t previousCardinality = 1;
131  // We go in reverse since a DecisionTree has the highest label first
132  for (auto&& it = updatedAssignment.rbegin();
133  it != updatedAssignment.rend(); it++) {
134  idx += previousCardinality * it->second;
135  previousCardinality *= dt.cardinality(it->first);
136  }
137  sparseTable.coeffRef(idx) = p;
138  }
139  }
140  };
141 
142  // Visit each leaf in `dt` to get the Assignment and leaf value
143  // to populate the sparseTable.
144  dt.visitWith(op);
145 
146  return sparseTable;
147 }
148 
149 /* ************************************************************************ */
151  const DecisionTreeFactor& dtf)
152  : TableFactor(dkeys, ComputeSparseTable(dkeys, dtf)) {}
153 
154 /* ************************************************************************ */
156  : TableFactor(dtf.discreteKeys(),
157  ComputeSparseTable(dtf.discreteKeys(), dtf)) {}
158 
159 /* ************************************************************************ */
161  : TableFactor(c.discreteKeys(), c) {}
162 
163 /* ************************************************************************ */
165  const DiscreteKeys& keys, const std::vector<double>& table) {
166  size_t max_size = 1;
167  for (auto&& [_, cardinality] : keys.cardinalities()) {
168  max_size *= cardinality;
169  }
170  if (table.size() != max_size) {
171  throw std::runtime_error(
172  "The cardinalities of the keys don't match the number of values in the "
173  "input.");
174  }
175 
176  Eigen::SparseVector<double> sparse_table(table.size());
177  // Count number of nonzero elements in table and reserve the space.
178  const uint64_t nnz = std::count_if(table.begin(), table.end(),
179  [](uint64_t i) { return i != 0; });
180  sparse_table.reserve(nnz);
181  for (uint64_t i = 0; i < table.size(); i++) {
182  if (table[i] != 0) sparse_table.insert(i) = table[i];
183  }
184  sparse_table.pruned();
185  sparse_table.data().squeeze();
186  return sparse_table;
187 }
188 
189 /* ************************************************************************ */
191  const std::string& table) {
192  // Convert string to doubles.
193  std::vector<double> ys;
194  std::istringstream iss(table);
195  std::copy(std::istream_iterator<double>(iss), std::istream_iterator<double>(),
196  std::back_inserter(ys));
197  return Convert(keys, ys);
198 }
199 
200 /* ************************************************************************ */
201 bool TableFactor::equals(const DiscreteFactor& other, double tol) const {
202  if (!dynamic_cast<const TableFactor*>(&other)) {
203  return false;
204  } else {
205  const auto& f(static_cast<const TableFactor&>(other));
206  return Base::equals(other, tol) &&
207  sparse_table_.isApprox(f.sparse_table_, tol);
208  }
209 }
210 
211 /* ************************************************************************ */
213  // a b c d => D * (C * (B * (a) + b) + c) + d
214  uint64_t idx = 0, card = 1;
215  for (auto it = sorted_dkeys_.rbegin(); it != sorted_dkeys_.rend(); ++it) {
216  if (values.find(it->first) != values.end()) {
217  idx += card * values.at(it->first);
218  }
219  card *= it->second;
220  }
221  return sparse_table_.coeff(idx);
222 }
223 
224 /* ************************************************************************ */
226  // a b c d => D * (C * (B * (a) + b) + c) + d
227  uint64_t idx = 0, card = 1;
228  for (auto it = keys_.rbegin(); it != keys_.rend(); ++it) {
229  if (values.find(*it) != values.end()) {
230  idx += card * values.at(*it);
231  }
232  card *= cardinality(*it);
233  }
234  return sparse_table_.coeff(idx);
235 }
236 
237 /* ************************************************************************ */
239  return -log(evaluate(values));
240 }
241 
242 /* ************************************************************************ */
243 double TableFactor::error(const HybridValues& values) const {
244  return error(values.discrete());
245 }
246 
247 /* ************************************************************************ */
249  return toDecisionTreeFactor() * f;
250 }
251 
252 /* ************************************************************************ */
254  DiscreteKeys dkeys = discreteKeys();
255  std::vector<double> table;
256  for (auto i = 0; i < sparse_table_.size(); i++) {
257  table.push_back(sparse_table_.coeff(i));
258  }
259  // NOTE(Varun): This constructor is really expensive!!
260  DecisionTreeFactor f(dkeys, table);
261  return f;
262 }
263 
264 /* ************************************************************************ */
266  DiscreteKeys parent_keys) const {
267  if (parent_keys.empty()) return *this;
268 
269  // Unique representation of parent values.
270  uint64_t unique = 0;
271  uint64_t card = 1;
272  for (auto it = keys_.rbegin(); it != keys_.rend(); ++it) {
273  if (parent_assign.find(*it) != parent_assign.end()) {
274  unique += parent_assign.at(*it) * card;
275  card *= cardinality(*it);
276  }
277  }
278 
279  // Find child DiscreteKeys
280  DiscreteKeys child_dkeys;
281  std::sort(parent_keys.begin(), parent_keys.end());
282  std::set_difference(sorted_dkeys_.begin(), sorted_dkeys_.end(),
283  parent_keys.begin(), parent_keys.end(),
284  std::back_inserter(child_dkeys));
285 
286  // Create child sparse table to populate.
287  uint64_t child_card = 1;
288  for (const DiscreteKey& child_dkey : child_dkeys)
289  child_card *= child_dkey.second;
290  Eigen::SparseVector<double> child_sparse_table_(child_card);
291  child_sparse_table_.reserve(child_card);
292 
293  // Populate child sparse table.
294  for (SparseIt it(sparse_table_); it; ++it) {
295  // Create unique representation of parent keys
296  uint64_t parent_unique = uniqueRep(parent_keys, it.index());
297  // Populate the table
298  if (parent_unique == unique) {
299  uint64_t idx = uniqueRep(child_dkeys, it.index());
300  child_sparse_table_.insert(idx) = it.value();
301  }
302  }
303 
304  child_sparse_table_.pruned();
305  child_sparse_table_.data().squeeze();
306  return TableFactor(child_dkeys, child_sparse_table_);
307 }
308 
309 /* ************************************************************************ */
310 double TableFactor::safe_div(const double& a, const double& b) {
311  // The use for safe_div is when we divide the product factor by the sum
312  // factor. If the product or sum is zero, we accord zero probability to the
313  // event.
314  return (a == 0 || b == 0) ? 0 : (a / b);
315 }
316 
317 /* ************************************************************************ */
318 void TableFactor::print(const string& s, const KeyFormatter& formatter) const {
319  cout << s;
320  cout << " f[";
321  for (auto&& key : keys())
322  cout << " (" << formatter(key) << "," << cardinality(key) << "),";
323  cout << " ]" << endl;
324  for (SparseIt it(sparse_table_); it; ++it) {
325  DiscreteValues assignment = findAssignments(it.index());
326  for (auto&& kv : assignment) {
327  cout << "(" << formatter(kv.first) << ", " << kv.second << ")";
328  }
329  cout << " | " << std::setw(10) << std::left << it.value() << " | "
330  << it.index() << endl;
331  }
332  cout << "number of nnzs: " << sparse_table_.nonZeros() << endl;
333 }
334 
335 /* ************************************************************************ */
337  // Initialize new factor.
338  uint64_t cardi = 1;
339  for (auto [key, c] : cardinalities_) cardi *= c;
340  Eigen::SparseVector<double> sparse_table(cardi);
341  sparse_table.reserve(sparse_table_.nonZeros());
342 
343  // Populate
344  for (SparseIt it(sparse_table_); it; ++it) {
345  sparse_table.coeffRef(it.index()) = op(it.value());
346  }
347 
348  // Free unused memory and return.
349  sparse_table.pruned();
350  sparse_table.data().squeeze();
351  return TableFactor(discreteKeys(), sparse_table);
352 }
353 
354 /* ************************************************************************ */
356  // Initialize new factor.
357  uint64_t cardi = 1;
358  for (auto [key, c] : cardinalities_) cardi *= c;
359  Eigen::SparseVector<double> sparse_table(cardi);
360  sparse_table.reserve(sparse_table_.nonZeros());
361 
362  // Populate
363  for (SparseIt it(sparse_table_); it; ++it) {
364  DiscreteValues assignment = findAssignments(it.index());
365  sparse_table.coeffRef(it.index()) = op(assignment, it.value());
366  }
367 
368  // Free unused memory and return.
369  sparse_table.pruned();
370  sparse_table.data().squeeze();
371  return TableFactor(discreteKeys(), sparse_table);
372 }
373 
374 /* ************************************************************************ */
376  if (keys_.empty() && sparse_table_.nonZeros() == 0)
377  return f;
378  else if (f.keys_.empty() && f.sparse_table_.nonZeros() == 0)
379  return *this;
380  // 1. Identify keys for contract and free modes.
381  DiscreteKeys contract_dkeys = contractDkeys(f);
382  DiscreteKeys f_free_dkeys = f.freeDkeys(*this);
383  DiscreteKeys union_dkeys = unionDkeys(f);
384  // 2. Create hash table for input factor f
385  unordered_map<uint64_t, AssignValList> map_f =
386  f.createMap(contract_dkeys, f_free_dkeys);
387  // 3. Initialize multiplied factor.
388  uint64_t card = 1;
389  for (auto u_dkey : union_dkeys) card *= u_dkey.second;
390  Eigen::SparseVector<double> mult_sparse_table(card);
391  mult_sparse_table.reserve(card);
392  // 3. Multiply.
393  for (SparseIt it(sparse_table_); it; ++it) {
394  uint64_t contract_unique = uniqueRep(contract_dkeys, it.index());
395  if (map_f.find(contract_unique) == map_f.end()) continue;
396  for (auto assignVal : map_f[contract_unique]) {
397  uint64_t union_idx = unionRep(union_dkeys, assignVal.first, it.index());
398  mult_sparse_table.insert(union_idx) = op(it.value(), assignVal.second);
399  }
400  }
401  // 4. Free unused memory.
402  mult_sparse_table.pruned();
403  mult_sparse_table.data().squeeze();
404  // 5. Create union keys and return.
405  return TableFactor(union_dkeys, mult_sparse_table);
406 }
407 
408 /* ************************************************************************ */
410  // Find contract modes.
411  DiscreteKeys contract;
412  set_intersection(sorted_dkeys_.begin(), sorted_dkeys_.end(),
413  f.sorted_dkeys_.begin(), f.sorted_dkeys_.end(),
414  back_inserter(contract));
415  return contract;
416 }
417 
418 /* ************************************************************************ */
420  // Find free modes.
421  DiscreteKeys free;
422  set_difference(sorted_dkeys_.begin(), sorted_dkeys_.end(),
423  f.sorted_dkeys_.begin(), f.sorted_dkeys_.end(),
424  back_inserter(free));
425  return free;
426 }
427 
428 /* ************************************************************************ */
430  // Find union modes.
431  DiscreteKeys union_dkeys;
432  set_union(sorted_dkeys_.begin(), sorted_dkeys_.end(), f.sorted_dkeys_.begin(),
433  f.sorted_dkeys_.end(), back_inserter(union_dkeys));
434  return union_dkeys;
435 }
436 
437 /* ************************************************************************ */
439  const DiscreteValues& f_free,
440  const uint64_t idx) const {
441  uint64_t union_idx = 0, card = 1;
442  for (auto it = union_keys.rbegin(); it != union_keys.rend(); it++) {
443  if (f_free.find(it->first) == f_free.end()) {
444  union_idx += keyValueForIndex(it->first, idx) * card;
445  } else {
446  union_idx += f_free.at(it->first) * card;
447  }
448  card *= it->second;
449  }
450  return union_idx;
451 }
452 
453 /* ************************************************************************ */
454 unordered_map<uint64_t, TableFactor::AssignValList> TableFactor::createMap(
455  const DiscreteKeys& contract, const DiscreteKeys& free) const {
456  // 1. Initialize map.
457  unordered_map<uint64_t, AssignValList> map_f;
458  // 2. Iterate over nonzero elements.
459  for (SparseIt it(sparse_table_); it; ++it) {
460  // 3. Create unique representation of contract modes.
461  uint64_t unique_rep = uniqueRep(contract, it.index());
462  // 4. Create assignment for free modes.
463  DiscreteValues free_assignments;
464  for (auto& key : free)
465  free_assignments[key.first] = keyValueForIndex(key.first, it.index());
466  // 5. Populate map.
467  if (map_f.find(unique_rep) == map_f.end()) {
468  map_f[unique_rep] = {make_pair(free_assignments, it.value())};
469  } else {
470  map_f[unique_rep].push_back(make_pair(free_assignments, it.value()));
471  }
472  }
473  return map_f;
474 }
475 
476 /* ************************************************************************ */
478  const uint64_t idx) const {
479  if (dkeys.empty()) return 0;
480  uint64_t unique_rep = 0, card = 1;
481  for (auto it = dkeys.rbegin(); it != dkeys.rend(); it++) {
482  unique_rep += keyValueForIndex(it->first, idx) * card;
483  card *= it->second;
484  }
485  return unique_rep;
486 }
487 
488 /* ************************************************************************ */
489 uint64_t TableFactor::uniqueRep(const DiscreteValues& assignments) const {
490  if (assignments.empty()) return 0;
491  uint64_t unique_rep = 0, card = 1;
492  for (auto it = assignments.rbegin(); it != assignments.rend(); it++) {
493  unique_rep += it->second * card;
494  card *= cardinalities_.at(it->first);
495  }
496  return unique_rep;
497 }
498 
499 /* ************************************************************************ */
501  DiscreteValues assignment;
502  for (Key key : keys_) {
503  assignment[key] = keyValueForIndex(key, idx);
504  }
505  return assignment;
506 }
507 
508 /* ************************************************************************ */
510  Binary op) const {
511  if (nrFrontals > size()) {
512  throw invalid_argument(
513  "TableFactor::combine: invalid number of frontal "
514  "keys " +
515  to_string(nrFrontals) + ", nr.keys=" + std::to_string(size()));
516  }
517  // Find remaining keys.
518  DiscreteKeys remain_dkeys;
519  uint64_t card = 1;
520  for (auto i = nrFrontals; i < keys_.size(); i++) {
521  remain_dkeys.push_back(discreteKey(i));
522  card *= cardinality(keys_[i]);
523  }
524  // Create combined table.
525  Eigen::SparseVector<double> combined_table(card);
526  combined_table.reserve(sparse_table_.nonZeros());
527  // Populate combined table.
528  for (SparseIt it(sparse_table_); it; ++it) {
529  uint64_t idx = uniqueRep(remain_dkeys, it.index());
530  double new_val = op(combined_table.coeff(idx), it.value());
531  combined_table.coeffRef(idx) = new_val;
532  }
533  // Free unused memory.
534  combined_table.pruned();
535  combined_table.data().squeeze();
536  return std::make_shared<TableFactor>(remain_dkeys, combined_table);
537 }
538 
539 /* ************************************************************************ */
541  Binary op) const {
542  if (frontalKeys.size() > size()) {
543  throw invalid_argument(
544  "TableFactor::combine: invalid number of frontal "
545  "keys " +
546  std::to_string(frontalKeys.size()) +
547  ", nr.keys=" + std::to_string(size()));
548  }
549  // Find remaining keys.
550  DiscreteKeys remain_dkeys;
551  uint64_t card = 1;
552  for (Key key : keys_) {
553  if (std::find(frontalKeys.begin(), frontalKeys.end(), key) ==
554  frontalKeys.end()) {
555  remain_dkeys.emplace_back(key, cardinality(key));
556  card *= cardinality(key);
557  }
558  }
559  // Create combined table.
560  Eigen::SparseVector<double> combined_table(card);
561  combined_table.reserve(sparse_table_.nonZeros());
562  // Populate combined table.
563  for (SparseIt it(sparse_table_); it; ++it) {
564  uint64_t idx = uniqueRep(remain_dkeys, it.index());
565  double new_val = op(combined_table.coeff(idx), it.value());
566  combined_table.coeffRef(idx) = new_val;
567  }
568  // Free unused memory.
569  combined_table.pruned();
570  combined_table.data().squeeze();
571  return std::make_shared<TableFactor>(remain_dkeys, combined_table);
572 }
573 
574 /* ************************************************************************ */
575 size_t TableFactor::keyValueForIndex(Key target_key, uint64_t index) const {
576  // http://phrogz.net/lazy-cartesian-product
577  return (index / denominators_.at(target_key)) % cardinality(target_key);
578 }
579 
580 /* ************************************************************************ */
581 std::vector<std::pair<DiscreteValues, double>> TableFactor::enumerate() const {
582  // Get all possible assignments
583  std::vector<std::pair<Key, size_t>> pairs = discreteKeys();
584  // Reverse to make cartesian product output a more natural ordering.
585  std::vector<std::pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
586  const auto assignments = DiscreteValues::CartesianProduct(rpairs);
587  // Construct unordered_map with values
588  std::vector<std::pair<DiscreteValues, double>> result;
589  for (const auto& assignment : assignments) {
590  result.emplace_back(assignment, operator()(assignment));
591  }
592  return result;
593 }
594 
595 // Print out header.
596 /* ************************************************************************ */
597 string TableFactor::markdown(const KeyFormatter& keyFormatter,
598  const Names& names) const {
599  stringstream ss;
600 
601  // Print out header.
602  ss << "|";
603  for (auto& key : keys()) {
604  ss << keyFormatter(key) << "|";
605  }
606  ss << "value|\n";
607 
608  // Print out separator with alignment hints.
609  ss << "|";
610  for (size_t j = 0; j < size(); j++) ss << ":-:|";
611  ss << ":-:|\n";
612 
613  // Print out all rows.
614  for (SparseIt it(sparse_table_); it; ++it) {
615  DiscreteValues assignment = findAssignments(it.index());
616  ss << "|";
617  for (auto& key : keys()) {
618  size_t index = assignment.at(key);
619  ss << DiscreteValues::Translate(names, key, index) << "|";
620  }
621  ss << it.value() << "|\n";
622  }
623  return ss.str();
624 }
625 
626 /* ************************************************************************ */
627 string TableFactor::html(const KeyFormatter& keyFormatter,
628  const Names& names) const {
629  stringstream ss;
630 
631  // Print out preamble.
632  ss << "<div>\n<table class='TableFactor'>\n <thead>\n";
633 
634  // Print out header row.
635  ss << " <tr>";
636  for (auto& key : keys()) {
637  ss << "<th>" << keyFormatter(key) << "</th>";
638  }
639  ss << "<th>value</th></tr>\n";
640 
641  // Finish header and start body.
642  ss << " </thead>\n <tbody>\n";
643 
644  // Print out all rows.
645  for (SparseIt it(sparse_table_); it; ++it) {
646  DiscreteValues assignment = findAssignments(it.index());
647  ss << " <tr>";
648  for (auto& key : keys()) {
649  size_t index = assignment.at(key);
650  ss << "<th>" << DiscreteValues::Translate(names, key, index) << "</th>";
651  }
652  ss << "<td>" << it.value() << "</td>"; // value
653  ss << "</tr>\n";
654  }
655  ss << " </tbody>\n</table>\n</div>";
656  return ss.str();
657 }
658 
659 /* ************************************************************************ */
660 TableFactor TableFactor::prune(size_t maxNrAssignments) const {
661  const size_t N = maxNrAssignments;
662 
663  // Get the probabilities in the TableFactor so we can threshold.
664  vector<pair<Eigen::Index, double>> probabilities;
665 
666  // Store non-zero probabilities along with their indices in a vector.
667  for (SparseIt it(sparse_table_); it; ++it) {
668  probabilities.emplace_back(it.index(), it.value());
669  }
670 
671  // The number of probabilities can be lower than max_leaves.
672  if (probabilities.size() <= N) return *this;
673 
674  // Sort the vector in descending order based on the element values.
675  sort(probabilities.begin(), probabilities.end(),
676  [](const std::pair<Eigen::Index, double>& a,
677  const std::pair<Eigen::Index, double>& b) {
678  return a.second > b.second;
679  });
680 
681  // Keep the largest N probabilities in the vector.
682  if (probabilities.size() > N) probabilities.resize(N);
683 
684  // Create pruned sparse vector.
685  Eigen::SparseVector<double> pruned_vec(sparse_table_.size());
686  pruned_vec.reserve(probabilities.size());
687 
688  // Populate pruned sparse vector.
689  for (const auto& prob : probabilities) {
690  pruned_vec.insert(prob.first) = prob.second;
691  }
692 
693  // Create pruned decision tree factor and return.
694  return TableFactor(this->discreteKeys(), pruned_vec);
695 }
696 
697 /* ************************************************************************ */
698 } // namespace gtsam
gtsam::TableFactor::markdown
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
Definition: TableFactor.cpp:597
gtsam::TableFactor
Definition: TableFactor.h:47
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:45
gtsam::HybridValues
Definition: HybridValues.h:37
gtsam::TableFactor::choose
TableFactor choose(const DiscreteValues assignments, DiscreteKeys parent_keys) const
Create a TableFactor that is a subset of this TableFactor.
Definition: TableFactor.cpp:265
s
RealScalar s
Definition: level1_cplx_impl.h:126
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
Eigen::SparseVector::reserve
void reserve(Index reserveSize)
Definition: SparseVector.h:204
gtsam::TableFactor::sparse_table_
Eigen::SparseVector< double > sparse_table_
SparseVector of nonzero probabilities.
Definition: TableFactor.h:50
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::DiscreteFactor::cardinalities_
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
Definition: DiscreteFactor.h:56
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
dt
const double dt
Definition: testVelocityConstraint.cpp:15
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::TableFactor::uniqueRep
uint64_t uniqueRep(const DiscreteKeys &keys, const uint64_t idx) const
Create unique representation.
Definition: TableFactor.cpp:477
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
log
const EIGEN_DEVICE_FUNC LogReturnType log() const
Definition: ArrayCwiseUnaryOps.h:128
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
DiscreteConditional.h
gtsam::TableFactor::contractDkeys
DiscreteKeys contractDkeys(const TableFactor &f) const
Definition: TableFactor.cpp:409
gtsam::TableFactor::keyValueForIndex
size_t keyValueForIndex(Key target_key, uint64_t index) const
Uses lazy cartesian product to find nth entry in the cartesian product of arrays in O(1) Example) v0 ...
Definition: TableFactor.cpp:575
result
Values result
Definition: OdometryOptimize.cpp:8
Eigen::SparseCompressedBase::InnerIterator
Definition: SparseCompressedBase.h:158
gtsam::TableFactor::enumerate
std::vector< std::pair< DiscreteValues, double > > enumerate() const
Enumerate all values into a map from values to double.
Definition: TableFactor.cpp:581
test_eigen_tensor.indices
indices
Definition: test_eigen_tensor.py:33
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::TableFactor::unionRep
uint64_t unionRep(const DiscreteKeys &keys, const DiscreteValues &assign, const uint64_t idx) const
Create unique representation of union modes.
Definition: TableFactor.cpp:438
gtsam::TableFactor::findValue
double findValue(const DiscreteValues &values) const
Find value for corresponding DiscreteValues.
Definition: TableFactor.cpp:225
FastSet.h
A thin wrapper around std::set that uses boost's fast_pool_allocator.
Eigen::SparseVector::nonZeros
Index nonZeros() const
Definition: SparseVector.h:140
gtsam::TableFactor::sorted_dkeys_
DiscreteKeys sorted_dkeys_
Sorted DiscreteKeys to use internally.
Definition: TableFactor.h:56
table
ArrayXXf table(10, 4)
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::DiscreteValues::CartesianProduct
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
Definition: DiscreteValues.h:85
left
static char left
Definition: blas_interface.hh:62
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
gtsam::TableFactor::findAssignments
DiscreteValues findAssignments(const uint64_t idx) const
Find DiscreteValues for corresponding index.
Definition: TableFactor.cpp:500
gtsam::TableFactor::prune
TableFactor prune(size_t maxNrAssignments) const
Prune the decision tree of discrete variables.
Definition: TableFactor.cpp:660
gtsam::TableFactor::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
Definition: TableFactor.cpp:627
gtsam::TableFactor::createMap
std::unordered_map< uint64_t, AssignValList > createMap(const DiscreteKeys &contract, const DiscreteKeys &free) const
Definition: TableFactor.cpp:454
gtsam::Assignment< Key >
TableFactor.h
gtsam::DiscreteFactor::Binary
std::function< double(const double, const double)> Binary
Definition: DiscreteFactor.h:52
gtsam::TableFactor::Convert
static Eigen::SparseVector< double > Convert(const DiscreteKeys &keys, const std::vector< double > &table)
Definition: TableFactor.cpp:164
gtsam::TableFactor::combine
shared_ptr combine(size_t nrFrontals, Binary op) const
Definition: TableFactor.cpp:509
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam::DecisionTree< Key, double >
gtsam::DiscreteFactor::cardinality
size_t cardinality(Key j) const
Definition: DiscreteFactor.h:98
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam::b
const G & b
Definition: Group.h:79
gtsam::DiscreteConditional
Definition: DiscreteConditional.h:37
gtsam::TableFactor::error
double error(const DiscreteValues &values) const override
Calculate error for DiscreteValues x, is -log(probability).
Definition: TableFactor.cpp:238
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
gtsam::Symbol::index
std::uint64_t index() const
Definition: inference/Symbol.h:80
gtsam::TableFactor::operator*
TableFactor operator*(const TableFactor &f) const
multiply two TableFactors
Definition: TableFactor.h:166
gtsam
traits
Definition: SFMdata.h:40
gtsam::Factor::keys_
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:88
gtsam::DiscreteFactor::Names
DiscreteValues::Names Names
Translation table from values to strings.
Definition: DiscreteFactor.h:139
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:143
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
Eigen::SparseVector< double >
std
Definition: BFloat16.h:88
gtsam::TableFactor::safe_div
static double safe_div(const double &a, const double &b)
Definition: TableFactor.cpp:310
gtsam::TableFactor::freeDkeys
DiscreteKeys freeDkeys(const TableFactor &f) const
Return keys in free mode which are the dimensions not involved in the contraction operation.
Definition: TableFactor.cpp:419
gtsam::TableFactor::denominators_
std::map< Key, size_t > denominators_
Map of Keys and their denominators used in keyValueForIndex.
Definition: TableFactor.h:54
gtsam::DiscreteValues::insert
std::pair< iterator, bool > insert(const value_type &value)
Definition: DiscreteValues.h:68
p
float * p
Definition: Tutorial_Map_using.cpp:9
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::TableFactor::equals
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
equality
Definition: TableFactor.cpp:201
gtsam::TableFactor::discreteKey
DiscreteKey discreteKey(size_t i) const
Return ith key in keys_ as a DiscreteKey.
Definition: TableFactor.h:79
gtsam::TableFactor::apply
TableFactor apply(Unary op) const
Definition: TableFactor.cpp:336
gtsam::TableFactor::print
void print(const std::string &s="TableFactor:\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: TableFactor.cpp:318
gtsam::DiscreteFactor::discreteKeys
DiscreteKeys discreteKeys() const
Return all the discrete keys associated with this factor.
Definition: DiscreteFactor.cpp:37
gtsam::TableFactor::evaluate
double evaluate(const Assignment< Key > &values) const override
Evaluate probability distribution, is just look up in TableFactor.
Definition: TableFactor.cpp:212
uint64_t
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
N
#define N
Definition: igam.h:9
gtsam::DiscreteFactor
Definition: DiscreteFactor.h:39
gtsam::DiscreteFactor::UnaryAssignment
std::function< double(const Assignment< Key > &, const double &)> UnaryAssignment
Definition: DiscreteFactor.h:51
gtsam::TableFactor::unionDkeys
DiscreteKeys unionDkeys(const TableFactor &f) const
Return union of DiscreteKeys in two factors.
Definition: TableFactor.cpp:429
gtsam::DiscreteValues::Translate
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
Definition: DiscreteValues.cpp:78
gtsam::TableFactor::TableFactor
TableFactor()
Definition: TableFactor.cpp:32
gtsam::Factor::size
size_t size() const
Definition: Factor.h:160
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::DiscreteFactor::Unary
std::function< double(const double &)> Unary
Definition: DiscreteFactor.h:49
_
constexpr descr< N - 1 > _(char const (&text)[N])
Definition: descr.h:109
HybridValues.h
gtsam::Ordering
Definition: inference/Ordering.h:33
Eigen::SparseVector::coeffRef
Scalar & coeffRef(Index row, Index col)
Definition: SparseVector.h:113
DecisionTreeFactor.h
Eigen::SparseVector::data
Storage & data()
Definition: SparseVector.h:98
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::internal::CompressedStorage::squeeze
void squeeze()
Definition: CompressedStorage.h:83
gtsam::TableFactor::shared_ptr
std::shared_ptr< TableFactor > shared_ptr
Definition: TableFactor.h:98
gtsam::TableFactor::toDecisionTreeFactor
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: TableFactor.cpp:253
Eigen::SparseVector::insert
Scalar & insert(Index row, Index col)
Definition: SparseVector.h:172
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
gtsam::ComputeSparseTable
static Eigen::SparseVector< double > ComputeSparseTable(const DiscreteKeys &dkeys, const DecisionTreeFactor &dt)
Compute the indexing of the leaves in the decision tree based on the assignment and add the (index,...
Definition: TableFactor.cpp:75
Eigen::SparseVector::coeff
Scalar coeff(Index row, Index col) const
Definition: SparseVector.h:102
gtsam::DiscreteFactor::equals
virtual bool equals(const DiscreteFactor &lf, double tol=1e-9) const
equals
Definition: DiscreteFactor.cpp:32


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:14:23