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  KeySet allKeys(dt.keys().begin(), dt.keys().end());
91 
92  // Compute denominators to be used in computing sparse table indices
93  std::map<Key, size_t> denominators;
94  double denom = sparseTable.size();
95  for (const DiscreteKey& dkey : dkeys) {
96  denom /= dkey.second;
97  denominators.insert(std::pair<Key, double>(dkey.first, denom));
98  }
99 
110  auto op = [&](const Assignment<Key>& assignment, double p) {
111  if (p > 0) {
112  // Get all the keys involved in this assignment
113  KeySet assignmentKeys;
114  for (auto&& [k, _] : assignment) {
115  assignmentKeys.insert(k);
116  }
117 
118  // Find the keys missing in the assignment
119  KeyVector diff;
120  std::set_difference(allKeys.begin(), allKeys.end(),
121  assignmentKeys.begin(), assignmentKeys.end(),
122  std::back_inserter(diff));
123 
124  // Generate all assignments using the missing keys
125  DiscreteKeys extras;
126  for (auto&& key : diff) {
127  extras.push_back({key, dt.cardinality(key)});
128  }
129  auto&& extraAssignments = DiscreteValues::CartesianProduct(extras);
130 
131  for (auto&& extra : extraAssignments) {
132  // Create new assignment using the extra assignment
133  DiscreteValues updatedAssignment(assignment);
134  updatedAssignment.insert(extra);
135 
136  // Generate index and add to the sparse vector.
137  Eigen::Index idx = 0;
138  // We go in reverse since a DecisionTree has the highest label first
139  for (auto&& it = updatedAssignment.rbegin();
140  it != updatedAssignment.rend(); it++) {
141  idx += it->second * denominators.at(it->first);
142  }
143  sparseTable.coeffRef(idx) = p;
144  }
145  }
146  };
147 
148  // Visit each leaf in `dt` to get the Assignment and leaf value
149  // to populate the sparseTable.
150  dt.visitWith(op);
151 
152  return sparseTable;
153 }
154 
155 /* ************************************************************************ */
157  const DecisionTreeFactor& dtf)
158  : TableFactor(dkeys, ComputeSparseTable(dkeys, dtf)) {}
159 
160 /* ************************************************************************ */
162  : TableFactor(dtf.discreteKeys(), dtf) {}
163 
164 /* ************************************************************************ */
166  : TableFactor(c.discreteKeys(), c) {}
167 
168 /* ************************************************************************ */
170  const DiscreteKeys& keys, const std::vector<double>& table) {
171  size_t max_size = 1;
172  for (auto&& [_, cardinality] : keys.cardinalities()) {
173  max_size *= cardinality;
174  }
175  if (table.size() != max_size) {
176  throw std::runtime_error(
177  "The cardinalities of the keys don't match the number of values in the "
178  "input.");
179  }
180 
181  Eigen::SparseVector<double> sparse_table(table.size());
182  // Count number of nonzero elements in table and reserve the space.
183  const uint64_t nnz = std::count_if(table.begin(), table.end(),
184  [](uint64_t i) { return i != 0; });
185  sparse_table.reserve(nnz);
186  for (uint64_t i = 0; i < table.size(); i++) {
187  if (table[i] != 0) sparse_table.insert(i) = table[i];
188  }
189  sparse_table.pruned();
190  sparse_table.data().squeeze();
191  return sparse_table;
192 }
193 
194 /* ************************************************************************ */
196  const std::string& table) {
197  // Convert string to doubles.
198  std::vector<double> ys;
199  std::istringstream iss(table);
200  std::copy(std::istream_iterator<double>(iss), std::istream_iterator<double>(),
201  std::back_inserter(ys));
202  return Convert(keys, ys);
203 }
204 
205 /* ************************************************************************ */
206 bool TableFactor::equals(const DiscreteFactor& other, double tol) const {
207  if (!dynamic_cast<const TableFactor*>(&other)) {
208  return false;
209  } else {
210  const auto& f(static_cast<const TableFactor&>(other));
211  return Base::equals(other, tol) &&
212  sparse_table_.isApprox(f.sparse_table_, tol);
213  }
214 }
215 
216 /* ************************************************************************ */
218  // a b c d => D * (C * (B * (a) + b) + c) + d
219  uint64_t idx = 0, card = 1;
220  for (auto it = sorted_dkeys_.rbegin(); it != sorted_dkeys_.rend(); ++it) {
221  if (values.find(it->first) != values.end()) {
222  idx += card * values.at(it->first);
223  }
224  card *= it->second;
225  }
226  return sparse_table_.coeff(idx);
227 }
228 
229 /* ************************************************************************ */
231  // a b c d => D * (C * (B * (a) + b) + c) + d
232  uint64_t idx = 0, card = 1;
233  for (auto it = keys_.rbegin(); it != keys_.rend(); ++it) {
234  if (values.find(*it) != values.end()) {
235  idx += card * values.at(*it);
236  }
237  card *= cardinality(*it);
238  }
239  return sparse_table_.coeff(idx);
240 }
241 
242 /* ************************************************************************ */
244  return -log(evaluate(values));
245 }
246 
247 /* ************************************************************************ */
248 double TableFactor::error(const HybridValues& values) const {
249  return error(values.discrete());
250 }
251 
252 /* ************************************************************************ */
254  return toDecisionTreeFactor() * f;
255 }
256 
257 /* ************************************************************************ */
259  DiscreteKeys dkeys = discreteKeys();
260 
261  // If no keys, then return empty DecisionTreeFactor
262  if (dkeys.size() == 0) {
264  // We can have an empty sparse_table_ or one with a single value.
265  if (sparse_table_.size() != 0) {
267  }
268  return DecisionTreeFactor(dkeys, tree);
269  }
270 
271  std::vector<double> table(sparse_table_.size(), 0.0);
272  for (SparseIt it(sparse_table_); it; ++it) {
273  table[it.index()] = it.value();
274  }
275 
277  DecisionTreeFactor f(dkeys, tree);
278  return f;
279 }
280 
281 /* ************************************************************************ */
283  DiscreteKeys parent_keys) const {
284  if (parent_keys.empty()) return *this;
285 
286  // Unique representation of parent values.
287  uint64_t unique = 0;
288  uint64_t card = 1;
289  for (auto it = keys_.rbegin(); it != keys_.rend(); ++it) {
290  if (parent_assign.find(*it) != parent_assign.end()) {
291  unique += parent_assign.at(*it) * card;
292  card *= cardinality(*it);
293  }
294  }
295 
296  // Find child DiscreteKeys
297  DiscreteKeys child_dkeys;
298  std::sort(parent_keys.begin(), parent_keys.end());
299  std::set_difference(sorted_dkeys_.begin(), sorted_dkeys_.end(),
300  parent_keys.begin(), parent_keys.end(),
301  std::back_inserter(child_dkeys));
302 
303  // Create child sparse table to populate.
304  uint64_t child_card = 1;
305  for (const DiscreteKey& child_dkey : child_dkeys)
306  child_card *= child_dkey.second;
307  Eigen::SparseVector<double> child_sparse_table_(child_card);
308  child_sparse_table_.reserve(child_card);
309 
310  // Populate child sparse table.
311  for (SparseIt it(sparse_table_); it; ++it) {
312  // Create unique representation of parent keys
313  uint64_t parent_unique = uniqueRep(parent_keys, it.index());
314  // Populate the table
315  if (parent_unique == unique) {
316  uint64_t idx = uniqueRep(child_dkeys, it.index());
317  child_sparse_table_.insert(idx) = it.value();
318  }
319  }
320 
321  child_sparse_table_.pruned();
322  child_sparse_table_.data().squeeze();
323  return TableFactor(child_dkeys, child_sparse_table_);
324 }
325 
326 /* ************************************************************************ */
327 double TableFactor::safe_div(const double& a, const double& b) {
328  // The use for safe_div is when we divide the product factor by the sum
329  // factor. If the product or sum is zero, we accord zero probability to the
330  // event.
331  return (a == 0 || b == 0) ? 0 : (a / b);
332 }
333 
334 /* ************************************************************************ */
335 void TableFactor::print(const string& s, const KeyFormatter& formatter) const {
336  cout << s;
337  cout << " f[";
338  for (auto&& key : keys())
339  cout << " (" << formatter(key) << "," << cardinality(key) << "),";
340  cout << " ]" << endl;
341  for (SparseIt it(sparse_table_); it; ++it) {
342  DiscreteValues assignment = findAssignments(it.index());
343  for (auto&& kv : assignment) {
344  cout << "(" << formatter(kv.first) << ", " << kv.second << ")";
345  }
346  cout << " | " << std::setw(10) << std::left << it.value() << " | "
347  << it.index() << endl;
348  }
349  cout << "number of nnzs: " << sparse_table_.nonZeros() << endl;
350 }
351 
352 /* ************************************************************************ */
354  // Initialize new factor.
355  uint64_t cardi = 1;
356  for (auto [key, c] : cardinalities_) cardi *= c;
357  Eigen::SparseVector<double> sparse_table(cardi);
358  sparse_table.reserve(sparse_table_.nonZeros());
359 
360  // Populate
361  for (SparseIt it(sparse_table_); it; ++it) {
362  sparse_table.coeffRef(it.index()) = op(it.value());
363  }
364 
365  // Free unused memory and return.
366  sparse_table.pruned();
367  sparse_table.data().squeeze();
368  return TableFactor(discreteKeys(), sparse_table);
369 }
370 
371 /* ************************************************************************ */
373  // Initialize new factor.
374  uint64_t cardi = 1;
375  for (auto [key, c] : cardinalities_) cardi *= c;
376  Eigen::SparseVector<double> sparse_table(cardi);
377  sparse_table.reserve(sparse_table_.nonZeros());
378 
379  // Populate
380  for (SparseIt it(sparse_table_); it; ++it) {
381  DiscreteValues assignment = findAssignments(it.index());
382  sparse_table.coeffRef(it.index()) = op(assignment, it.value());
383  }
384 
385  // Free unused memory and return.
386  sparse_table.pruned();
387  sparse_table.data().squeeze();
388  return TableFactor(discreteKeys(), sparse_table);
389 }
390 
391 /* ************************************************************************ */
393  if (keys_.empty() && sparse_table_.nonZeros() == 0)
394  return f;
395  else if (f.keys_.empty() && f.sparse_table_.nonZeros() == 0)
396  return *this;
397  // 1. Identify keys for contract and free modes.
398  DiscreteKeys contract_dkeys = contractDkeys(f);
399  DiscreteKeys f_free_dkeys = f.freeDkeys(*this);
400  DiscreteKeys union_dkeys = unionDkeys(f);
401  // 2. Create hash table for input factor f
402  unordered_map<uint64_t, AssignValList> map_f =
403  f.createMap(contract_dkeys, f_free_dkeys);
404  // 3. Initialize multiplied factor.
405  uint64_t card = 1;
406  for (auto u_dkey : union_dkeys) card *= u_dkey.second;
407  Eigen::SparseVector<double> mult_sparse_table(card);
408  mult_sparse_table.reserve(card);
409  // 3. Multiply.
410  for (SparseIt it(sparse_table_); it; ++it) {
411  uint64_t contract_unique = uniqueRep(contract_dkeys, it.index());
412  if (map_f.find(contract_unique) == map_f.end()) continue;
413  for (auto assignVal : map_f[contract_unique]) {
414  uint64_t union_idx = unionRep(union_dkeys, assignVal.first, it.index());
415  mult_sparse_table.insert(union_idx) = op(it.value(), assignVal.second);
416  }
417  }
418  // 4. Free unused memory.
419  mult_sparse_table.pruned();
420  mult_sparse_table.data().squeeze();
421  // 5. Create union keys and return.
422  return TableFactor(union_dkeys, mult_sparse_table);
423 }
424 
425 /* ************************************************************************ */
427  // Find contract modes.
428  DiscreteKeys contract;
429  set_intersection(sorted_dkeys_.begin(), sorted_dkeys_.end(),
430  f.sorted_dkeys_.begin(), f.sorted_dkeys_.end(),
431  back_inserter(contract));
432  return contract;
433 }
434 
435 /* ************************************************************************ */
437  // Find free modes.
438  DiscreteKeys free;
439  set_difference(sorted_dkeys_.begin(), sorted_dkeys_.end(),
440  f.sorted_dkeys_.begin(), f.sorted_dkeys_.end(),
441  back_inserter(free));
442  return free;
443 }
444 
445 /* ************************************************************************ */
447  // Find union modes.
448  DiscreteKeys union_dkeys;
449  set_union(sorted_dkeys_.begin(), sorted_dkeys_.end(), f.sorted_dkeys_.begin(),
450  f.sorted_dkeys_.end(), back_inserter(union_dkeys));
451  return union_dkeys;
452 }
453 
454 /* ************************************************************************ */
456  const DiscreteValues& f_free,
457  const uint64_t idx) const {
458  uint64_t union_idx = 0, card = 1;
459  for (auto it = union_keys.rbegin(); it != union_keys.rend(); it++) {
460  if (f_free.find(it->first) == f_free.end()) {
461  union_idx += keyValueForIndex(it->first, idx) * card;
462  } else {
463  union_idx += f_free.at(it->first) * card;
464  }
465  card *= it->second;
466  }
467  return union_idx;
468 }
469 
470 /* ************************************************************************ */
471 unordered_map<uint64_t, TableFactor::AssignValList> TableFactor::createMap(
472  const DiscreteKeys& contract, const DiscreteKeys& free) const {
473  // 1. Initialize map.
474  unordered_map<uint64_t, AssignValList> map_f;
475  // 2. Iterate over nonzero elements.
476  for (SparseIt it(sparse_table_); it; ++it) {
477  // 3. Create unique representation of contract modes.
478  uint64_t unique_rep = uniqueRep(contract, it.index());
479  // 4. Create assignment for free modes.
480  DiscreteValues free_assignments;
481  for (auto& key : free)
482  free_assignments[key.first] = keyValueForIndex(key.first, it.index());
483  // 5. Populate map.
484  if (map_f.find(unique_rep) == map_f.end()) {
485  map_f[unique_rep] = {make_pair(free_assignments, it.value())};
486  } else {
487  map_f[unique_rep].push_back(make_pair(free_assignments, it.value()));
488  }
489  }
490  return map_f;
491 }
492 
493 /* ************************************************************************ */
495  const uint64_t idx) const {
496  if (dkeys.empty()) return 0;
497  uint64_t unique_rep = 0, card = 1;
498  for (auto it = dkeys.rbegin(); it != dkeys.rend(); it++) {
499  unique_rep += keyValueForIndex(it->first, idx) * card;
500  card *= it->second;
501  }
502  return unique_rep;
503 }
504 
505 /* ************************************************************************ */
506 uint64_t TableFactor::uniqueRep(const DiscreteValues& assignments) const {
507  if (assignments.empty()) return 0;
508  uint64_t unique_rep = 0, card = 1;
509  for (auto it = assignments.rbegin(); it != assignments.rend(); it++) {
510  unique_rep += it->second * card;
511  card *= cardinalities_.at(it->first);
512  }
513  return unique_rep;
514 }
515 
516 /* ************************************************************************ */
518  DiscreteValues assignment;
519  for (Key key : keys_) {
520  assignment[key] = keyValueForIndex(key, idx);
521  }
522  return assignment;
523 }
524 
525 /* ************************************************************************ */
527  Binary op) const {
528  if (nrFrontals > size()) {
529  throw invalid_argument(
530  "TableFactor::combine: invalid number of frontal "
531  "keys " +
532  to_string(nrFrontals) + ", nr.keys=" + std::to_string(size()));
533  }
534  // Find remaining keys.
535  DiscreteKeys remain_dkeys;
536  uint64_t card = 1;
537  for (auto i = nrFrontals; i < keys_.size(); i++) {
538  remain_dkeys.push_back(discreteKey(i));
539  card *= cardinality(keys_[i]);
540  }
541  // Create combined table.
542  Eigen::SparseVector<double> combined_table(card);
543  combined_table.reserve(sparse_table_.nonZeros());
544  // Populate combined table.
545  for (SparseIt it(sparse_table_); it; ++it) {
546  uint64_t idx = uniqueRep(remain_dkeys, it.index());
547  double new_val = op(combined_table.coeff(idx), it.value());
548  combined_table.coeffRef(idx) = new_val;
549  }
550  // Free unused memory.
551  combined_table.pruned();
552  combined_table.data().squeeze();
553  return std::make_shared<TableFactor>(remain_dkeys, combined_table);
554 }
555 
556 /* ************************************************************************ */
558  Binary op) const {
559  if (frontalKeys.size() > size()) {
560  throw invalid_argument(
561  "TableFactor::combine: invalid number of frontal "
562  "keys " +
563  std::to_string(frontalKeys.size()) +
564  ", nr.keys=" + std::to_string(size()));
565  }
566  // Find remaining keys.
567  DiscreteKeys remain_dkeys;
568  uint64_t card = 1;
569  for (Key key : keys_) {
570  if (std::find(frontalKeys.begin(), frontalKeys.end(), key) ==
571  frontalKeys.end()) {
572  remain_dkeys.emplace_back(key, cardinality(key));
573  card *= cardinality(key);
574  }
575  }
576  // Create combined table.
577  Eigen::SparseVector<double> combined_table(card);
578  combined_table.reserve(sparse_table_.nonZeros());
579  // Populate combined table.
580  for (SparseIt it(sparse_table_); it; ++it) {
581  uint64_t idx = uniqueRep(remain_dkeys, it.index());
582  double new_val = op(combined_table.coeff(idx), it.value());
583  combined_table.coeffRef(idx) = new_val;
584  }
585  // Free unused memory.
586  combined_table.pruned();
587  combined_table.data().squeeze();
588  return std::make_shared<TableFactor>(remain_dkeys, combined_table);
589 }
590 
591 /* ************************************************************************ */
592 size_t TableFactor::keyValueForIndex(Key target_key, uint64_t index) const {
593  // http://phrogz.net/lazy-cartesian-product
594  return (index / denominators_.at(target_key)) % cardinality(target_key);
595 }
596 
597 /* ************************************************************************ */
598 std::vector<std::pair<DiscreteValues, double>> TableFactor::enumerate() const {
599  // Get all possible assignments
600  std::vector<std::pair<Key, size_t>> pairs = discreteKeys();
601  // Reverse to make cartesian product output a more natural ordering.
602  std::vector<std::pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
603  const auto assignments = DiscreteValues::CartesianProduct(rpairs);
604  // Construct unordered_map with values
605  std::vector<std::pair<DiscreteValues, double>> result;
606  for (const auto& assignment : assignments) {
607  result.emplace_back(assignment, operator()(assignment));
608  }
609  return result;
610 }
611 
612 // Print out header.
613 /* ************************************************************************ */
614 string TableFactor::markdown(const KeyFormatter& keyFormatter,
615  const Names& names) const {
616  stringstream ss;
617 
618  // Print out header.
619  ss << "|";
620  for (auto& key : keys()) {
621  ss << keyFormatter(key) << "|";
622  }
623  ss << "value|\n";
624 
625  // Print out separator with alignment hints.
626  ss << "|";
627  for (size_t j = 0; j < size(); j++) ss << ":-:|";
628  ss << ":-:|\n";
629 
630  // Print out all rows.
631  for (SparseIt it(sparse_table_); it; ++it) {
632  DiscreteValues assignment = findAssignments(it.index());
633  ss << "|";
634  for (auto& key : keys()) {
635  size_t index = assignment.at(key);
636  ss << DiscreteValues::Translate(names, key, index) << "|";
637  }
638  ss << it.value() << "|\n";
639  }
640  return ss.str();
641 }
642 
643 /* ************************************************************************ */
644 string TableFactor::html(const KeyFormatter& keyFormatter,
645  const Names& names) const {
646  stringstream ss;
647 
648  // Print out preamble.
649  ss << "<div>\n<table class='TableFactor'>\n <thead>\n";
650 
651  // Print out header row.
652  ss << " <tr>";
653  for (auto& key : keys()) {
654  ss << "<th>" << keyFormatter(key) << "</th>";
655  }
656  ss << "<th>value</th></tr>\n";
657 
658  // Finish header and start body.
659  ss << " </thead>\n <tbody>\n";
660 
661  // Print out all rows.
662  for (SparseIt it(sparse_table_); it; ++it) {
663  DiscreteValues assignment = findAssignments(it.index());
664  ss << " <tr>";
665  for (auto& key : keys()) {
666  size_t index = assignment.at(key);
667  ss << "<th>" << DiscreteValues::Translate(names, key, index) << "</th>";
668  }
669  ss << "<td>" << it.value() << "</td>"; // value
670  ss << "</tr>\n";
671  }
672  ss << " </tbody>\n</table>\n</div>";
673  return ss.str();
674 }
675 
676 /* ************************************************************************ */
677 TableFactor TableFactor::prune(size_t maxNrAssignments) const {
678  const size_t N = maxNrAssignments;
679 
680  // Get the probabilities in the TableFactor so we can threshold.
681  vector<pair<Eigen::Index, double>> probabilities;
682 
683  // Store non-zero probabilities along with their indices in a vector.
684  for (SparseIt it(sparse_table_); it; ++it) {
685  probabilities.emplace_back(it.index(), it.value());
686  }
687 
688  // The number of probabilities can be lower than max_leaves.
689  if (probabilities.size() <= N) return *this;
690 
691  // Sort the vector in descending order based on the element values.
692  sort(probabilities.begin(), probabilities.end(),
693  [](const std::pair<Eigen::Index, double>& a,
694  const std::pair<Eigen::Index, double>& b) {
695  return a.second > b.second;
696  });
697 
698  // Keep the largest N probabilities in the vector.
699  if (probabilities.size() > N) probabilities.resize(N);
700 
701  // Create pruned sparse vector.
702  Eigen::SparseVector<double> pruned_vec(sparse_table_.size());
703  pruned_vec.reserve(probabilities.size());
704 
705  // Populate pruned sparse vector.
706  for (const auto& prob : probabilities) {
707  pruned_vec.insert(prob.first) = prob.second;
708  }
709 
710  // Create pruned decision tree factor and return.
711  return TableFactor(this->discreteKeys(), pruned_vec);
712 }
713 
714 /* ************************************************************************ */
715 } // namespace gtsam
gtsam::TableFactor::markdown
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
Definition: TableFactor.cpp:614
gtsam::TableFactor
Definition: TableFactor.h:53
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:282
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:56
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
tree
Definition: testExpression.cpp:212
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:494
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
gtsam::FastSet< Key >
DiscreteConditional.h
gtsam::TableFactor::contractDkeys
DiscreteKeys contractDkeys(const TableFactor &f) const
Definition: TableFactor.cpp:426
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:592
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
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:598
gtsam::AlgebraicDecisionTree< Key >
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:455
gtsam::TableFactor::findValue
double findValue(const DiscreteValues &values) const
Find value for corresponding DiscreteValues.
Definition: TableFactor.cpp:230
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:62
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:517
gtsam::TableFactor::prune
TableFactor prune(size_t maxNrAssignments) const
Prune the decision tree of discrete variables.
Definition: TableFactor.cpp:677
gtsam::TableFactor::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
Definition: TableFactor.cpp:644
gtsam::TableFactor::createMap
std::unordered_map< uint64_t, AssignValList > createMap(const DiscreteKeys &contract, const DiscreteKeys &free) const
Definition: TableFactor.cpp:471
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:169
gtsam::TableFactor::combine
shared_ptr combine(size_t nrFrontals, Binary op) const
Definition: TableFactor.cpp:526
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:243
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:174
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:327
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:436
gtsam::TableFactor::denominators_
std::map< Key, size_t > denominators_
Map of Keys and their denominators used in keyValueForIndex.
Definition: TableFactor.h:60
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:206
gtsam::TableFactor::discreteKey
DiscreteKey discreteKey(size_t i) const
Return ith key in keys_ as a DiscreteKey.
Definition: TableFactor.h:85
gtsam::TableFactor::apply
TableFactor apply(Unary op) const
Definition: TableFactor.cpp:353
gtsam::TableFactor::print
void print(const std::string &s="TableFactor:\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: TableFactor.cpp:335
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:217
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:446
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:104
gtsam::TableFactor::toDecisionTreeFactor
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition: TableFactor.cpp:258
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 Sat Jan 4 2025 04:04:04