DiscreteFactor.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 
20 #include <gtsam/base/Vector.h>
23 
24 #include <cmath>
25 #include <sstream>
26 
27 using namespace std;
28 
29 namespace gtsam {
30 
31 /* ************************************************************************ */
32 DiscreteKeys DiscreteFactor::discreteKeys() const {
34  for (auto&& key : keys()) {
35  DiscreteKey dkey(key, cardinality(key));
36  if (std::find(result.begin(), result.end(), dkey) == result.end()) {
37  result.push_back(dkey);
38  }
39  }
40  return result;
41 }
42 
43 /* ************************************************************************* */
45  return -std::log((*this)(values));
46 }
47 
48 /* ************************************************************************* */
49 double DiscreteFactor::error(const HybridValues& c) const {
50  return this->error(c.discrete());
51 }
52 
53 /* ************************************************************************* */
54 std::vector<double> expNormalize(const std::vector<double>& logProbs) {
55  double maxLogProb = -std::numeric_limits<double>::infinity();
56  for (size_t i = 0; i < logProbs.size(); i++) {
57  double logProb = logProbs[i];
58  if ((logProb != std::numeric_limits<double>::infinity()) &&
59  logProb > maxLogProb) {
60  maxLogProb = logProb;
61  }
62  }
63 
64  // After computing the max = "Z" of the log probabilities L_i, we compute
65  // the log of the normalizing constant, log S, where S = sum_j exp(L_j - Z).
66  double total = 0.0;
67  for (size_t i = 0; i < logProbs.size(); i++) {
68  double probPrime = exp(logProbs[i] - maxLogProb);
69  total += probPrime;
70  }
71  double logTotal = log(total);
72 
73  // Now we compute the (normalized) probability (for each i):
74  // p_i = exp(L_i - Z - log S)
75  double checkNormalization = 0.0;
76  std::vector<double> probs;
77  for (size_t i = 0; i < logProbs.size(); i++) {
78  double prob = exp(logProbs[i] - maxLogProb - logTotal);
79  probs.push_back(prob);
80  checkNormalization += prob;
81  }
82 
83  // Numerical tolerance for floating point comparisons
84  double tol = 1e-9;
85 
86  if (!gtsam::fpEqual(checkNormalization, 1.0, tol)) {
87  std::string errMsg =
88  std::string("expNormalize failed to normalize probabilities. ") +
89  std::string("Expected normalization constant = 1.0. Got value: ") +
90  std::to_string(checkNormalization) +
91  std::string(
92  "\n This could have resulted from numerical overflow/underflow.");
93  throw std::logic_error(errMsg);
94  }
95  return probs;
96 }
97 
98 } // namespace gtsam
gtsam::HybridValues
Definition: HybridValues.h:38
Vector.h
typedef and functions to augment Eigen's VectorXd
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
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
exp
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
Definition: ArrayCwiseUnaryOps.h:97
result
Values result
Definition: OdometryOptimize.cpp:8
DiscreteFactor.h
gtsam::fpEqual
bool fpEqual(double a, double b, double tol, bool check_relative_also)
Definition: Vector.cpp:41
key
const gtsam::Symbol key('X', 0)
gtsam::HybridValues::discrete
const DiscreteValues & discrete() const
Return the discrete values.
Definition: HybridValues.h:92
gtsam
traits
Definition: chartTesting.h:28
error
static double error
Definition: testRot3.cpp:37
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
leaf::values
leaf::MyValues values
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::expNormalize
std::vector< double > expNormalize(const std::vector< double > &logProbs)
Normalize a set of log probabilities.
Definition: DiscreteFactor.cpp:54
gtsam::tol
const G double tol
Definition: Group.h:79
HybridValues.h
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:02:14