Go to the documentation of this file.
32 TableFactor::TableFactor() {}
50 double denom =
table.size();
53 denominators_.insert(std::pair<Key, double>(dkey.first, denom));
75 std::vector<double> probs =
dt.probabilities();
76 std::vector<double> ordered;
78 size_t n = dkeys[0].second;
80 for (
size_t k = 0; k <
n; ++k) {
81 for (
size_t idx = 0; idx < probs.size(); ++idx) {
83 ordered.push_back(probs[idx]);
101 const std::vector<double>&
table) {
110 sparse_table.pruned();
118 std::vector<double> ys;
119 std::istringstream iss(
table);
120 std::copy(std::istream_iterator<double>(iss), std::istream_iterator<double>(),
121 std::back_inserter(ys));
141 idx += card *
values.at(it->first);
152 for (
auto it =
keys_.rbegin(); it !=
keys_.rend(); ++it) {
154 idx += card *
values.at(*it);
179 std::vector<double>
table;
190 if (parent_keys.empty())
return *
this;
195 for (
auto it =
keys_.rbegin(); it !=
keys_.rend(); ++it) {
196 if (parent_assign.find(*it) != parent_assign.end()) {
197 unique += parent_assign.at(*it) * card;
204 std::sort(parent_keys.begin(), parent_keys.end());
206 parent_keys.begin(), parent_keys.end(),
207 std::back_inserter(child_dkeys));
212 child_card *= child_dkey.second;
214 child_sparse_table_.
reserve(child_card);
221 if (parent_unique == unique) {
223 child_sparse_table_.
insert(idx) = it.value();
227 child_sparse_table_.pruned();
229 return TableFactor(child_dkeys, child_sparse_table_);
237 return (
a == 0 ||
b == 0) ? 0 : (
a /
b);
246 cout <<
" ]" << endl;
249 for (
auto&& kv : assignment) {
250 cout <<
"(" <<
formatter(kv.first) <<
", " << kv.second <<
")";
252 cout <<
" | " << it.value() <<
" | " << it.index() << endl;
267 sparse_table.
coeffRef(it.index()) = op(it.value());
271 sparse_table.pruned();
287 sparse_table.
coeffRef(it.index()) = op(assignment, it.value());
291 sparse_table.pruned();
300 else if (
f.keys_.empty() &&
f.sparse_table_.nonZeros() == 0)
307 unordered_map<uint64_t, AssignValList> map_f =
308 f.createMap(contract_dkeys, f_free_dkeys);
311 for (
auto u_dkey : union_dkeys) card *= u_dkey.second;
313 mult_sparse_table.
reserve(card);
317 if (map_f.find(contract_unique) == map_f.end())
continue;
318 for (
auto assignVal : map_f[contract_unique]) {
320 mult_sparse_table.
insert(union_idx) = op(it.value(), assignVal.second);
324 mult_sparse_table.pruned();
327 return TableFactor(union_dkeys, mult_sparse_table);
335 f.sorted_dkeys_.begin(),
f.sorted_dkeys_.end(),
336 back_inserter(contract));
345 f.sorted_dkeys_.begin(),
f.sorted_dkeys_.end(),
346 back_inserter(free));
355 f.sorted_dkeys_.end(), back_inserter(union_dkeys));
364 for (
auto it = union_keys.rbegin(); it != union_keys.rend(); it++) {
365 if (f_free.find(it->first) == f_free.end()) {
368 union_idx += f_free.at(it->first) * card;
379 unordered_map<uint64_t, AssignValList> map_f;
386 for (
auto&
key : free)
389 if (map_f.find(unique_rep) == map_f.end()) {
390 map_f[unique_rep] = {make_pair(free_assignments, it.value())};
392 map_f[unique_rep].push_back(make_pair(free_assignments, it.value()));
401 if (dkeys.empty())
return 0;
403 for (
auto it = dkeys.rbegin(); it != dkeys.rend(); it++) {
412 if (assignments.empty())
return 0;
414 for (
auto it = assignments.rbegin(); it != assignments.rend(); it++) {
415 unique_rep += it->second * card;
433 if (nrFrontals >
size()) {
434 throw invalid_argument(
435 "TableFactor::combine: invalid number of frontal "
437 to_string(nrFrontals) +
", nr.keys=" + std::to_string(
size()));
442 for (
auto i = nrFrontals;
i <
keys_.size();
i++) {
452 double new_val = op(combined_table.
coeff(idx), it.value());
453 combined_table.
coeffRef(idx) = new_val;
456 combined_table.pruned();
458 return std::make_shared<TableFactor>(remain_dkeys, combined_table);
464 if (frontalKeys.size() >
size()) {
465 throw invalid_argument(
466 "TableFactor::combine: invalid number of frontal "
468 std::to_string(frontalKeys.size()) +
469 ", nr.keys=" + std::to_string(
size()));
475 if (std::find(frontalKeys.begin(), frontalKeys.end(),
key) ==
487 double new_val = op(combined_table.
coeff(idx), it.value());
488 combined_table.
coeffRef(idx) = new_val;
491 combined_table.pruned();
493 return std::make_shared<TableFactor>(remain_dkeys, combined_table);
505 std::vector<std::pair<Key, size_t>> pairs =
discreteKeys();
507 std::vector<std::pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
510 std::vector<std::pair<DiscreteValues, double>>
result;
511 for (
const auto& assignment : assignments) {
512 result.emplace_back(assignment,
operator()(assignment));
526 ss << keyFormatter(
key) <<
"|";
532 for (
size_t j = 0;
j <
size();
j++)
ss <<
":-:|";
540 size_t index = assignment.at(
key);
543 ss << it.value() <<
"|\n";
554 ss <<
"<div>\n<table class='TableFactor'>\n <thead>\n";
559 ss <<
"<th>" << keyFormatter(
key) <<
"</th>";
561 ss <<
"<th>value</th></tr>\n";
564 ss <<
" </thead>\n <tbody>\n";
571 size_t index = assignment.at(
key);
574 ss <<
"<td>" << it.value() <<
"</td>";
577 ss <<
" </tbody>\n</table>\n</div>";
583 const size_t N = maxNrAssignments;
586 vector<pair<Eigen::Index, double>> probabilities;
590 probabilities.emplace_back(it.index(), it.value());
594 if (probabilities.size() <=
N)
return *
this;
597 sort(probabilities.begin(), probabilities.end(),
598 [](
const std::pair<Eigen::Index, double>&
a,
599 const std::pair<Eigen::Index, double>&
b) {
600 return a.second > b.second;
604 if (probabilities.size() >
N) probabilities.resize(
N);
608 pruned_vec.
reserve(probabilities.size());
611 for (
const auto& prob : probabilities) {
612 pruned_vec.
insert(prob.first) = prob.second;
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
TableFactor choose(const DiscreteValues assignments, DiscreteKeys parent_keys) const
Create a TableFactor that is a subset of this TableFactor.
void reserve(Index reserveSize)
Eigen::SparseVector< double > sparse_table_
SparseVector of nonzero probabilities.
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
const KeyFormatter & formatter
uint64_t uniqueRep(const DiscreteKeys &keys, const uint64_t idx) const
Create unique representation.
const EIGEN_DEVICE_FUNC LogReturnType log() const
DiscreteKeys is a set of keys that can be assembled using the & operator.
DiscreteKeys contractDkeys(const TableFactor &f) const
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 ...
std::vector< std::pair< DiscreteValues, double > > enumerate() const
Enumerate all values into a map from values to double.
static std::stringstream ss
uint64_t unionRep(const DiscreteKeys &keys, const DiscreteValues &assign, const uint64_t idx) const
Create unique representation of union modes.
double findValue(const DiscreteValues &values) const
Find value for corresponding DiscreteValues.
A thin wrapper around std::set that uses boost's fast_pool_allocator.
DiscreteKeys sorted_dkeys_
Sorted DiscreteKeys to use internally.
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
DiscreteValues findAssignments(const uint64_t idx) const
Find DiscreteValues for corresponding index.
TableFactor prune(size_t maxNrAssignments) const
Prune the decision tree of discrete variables.
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
std::unordered_map< uint64_t, AssignValList > createMap(const DiscreteKeys &contract, const DiscreteKeys &free) const
static Eigen::SparseVector< double > Convert(const std::vector< double > &table)
shared_ptr combine(size_t nrFrontals, Binary op) const
const gtsam::Symbol key('X', 0)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
size_t cardinality(Key j) const
std::function< double(const double, const double)> Binary
double error(const DiscreteValues &values) const override
Calculate error for DiscreteValues x, is -log(probability).
std::function< double(const double &)> Unary
std::function< double(const Assignment< Key > &, const double &)> UnaryAssignment
std::uint64_t index() const
TableFactor operator*(const TableFactor &f) const
multiply two TableFactors
KeyVector keys_
The keys involved in this factor.
DiscreteValues::Names Names
Translation table from values to strings.
const KeyVector & keys() const
Access the factor's involved variable keys.
std::pair< Key, size_t > DiscreteKey
static double safe_div(const double &a, const double &b)
DiscreteKeys freeDkeys(const TableFactor &f) const
Return keys in free mode which are the dimensions not involved in the contraction operation.
std::map< Key, size_t > denominators_
Map of Keys and their denominators used in keyValueForIndex.
double evaluate(const DiscreteValues &values) const
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
equality
DiscreteKey discreteKey(size_t i) const
Return ith key in keys_ as a DiscreteKey.
TableFactor apply(Unary op) const
void print(const std::string &s="TableFactor:\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
DiscreteKeys discreteKeys() const
Return all the discrete keys associated with this factor.
unsigned __int64 uint64_t
DiscreteKeys unionDkeys(const TableFactor &f) const
Return union of DiscreteKeys in two factors.
double operator()(const DiscreteValues &values) const override
Evaluate probability distribution, sugar.
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
std::uint64_t Key
Integer nonlinear key type.
Scalar & coeffRef(Index row, Index col)
std::shared_ptr< TableFactor > shared_ptr
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Scalar & insert(Index row, Index col)
Scalar coeff(Index row, Index col) const
std::vector< double > ComputeLeafOrdering(const DiscreteKeys &dkeys, const DecisionTreeFactor &dt)
Compute the correct ordering of the leaves in the decision tree.
gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:05:22