HybridGaussianFactor.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 
21 #include <gtsam/base/utilities.h>
28 
29 namespace gtsam {
30 
43  // Find the minimum value so we can "proselytize" to positive values.
44  // Done because we can't have sqrt of negative numbers.
45  HybridGaussianFactor::Factors gaussianFactors;
47  std::tie(gaussianFactors, valueTree) = unzip(factors);
48 
49  // Compute minimum value for normalization.
50  double min_value = valueTree.min();
51 
52  // Finally, update the [A|b] matrices.
53  auto update = [&min_value](const GaussianFactorValuePair &gfv) {
54  auto [gf, value] = gfv;
55 
56  auto jf = std::dynamic_pointer_cast<JacobianFactor>(gf);
57  if (!jf) return gf;
58 
59  double normalized_value = value - min_value;
60 
61  // If the value is 0, do nothing
62  if (normalized_value == 0.0) return gf;
63 
65  gfg.push_back(jf);
66 
67  Vector c(1);
68  // When hiding c inside the `b` vector, value == 0.5*c^2
69  c << std::sqrt(2.0 * normalized_value);
70  auto constantFactor = std::make_shared<JacobianFactor>(c);
71 
72  gfg.push_back(constantFactor);
73  return std::dynamic_pointer_cast<GaussianFactor>(
74  std::make_shared<JacobianFactor>(gfg));
75  };
77 }
78 
79 /* *******************************************************************************/
81  const DiscreteKeys &discreteKeys,
83  : Base(continuousKeys, discreteKeys), factors_(augment(factors)) {}
84 
85 /* *******************************************************************************/
86 bool HybridGaussianFactor::equals(const HybridFactor &lf, double tol) const {
87  const This *e = dynamic_cast<const This *>(&lf);
88  if (e == nullptr) return false;
89 
90  // This will return false if either factors_ is empty or e->factors_ is empty,
91  // but not if both are empty or both are not empty:
92  if (factors_.empty() ^ e->factors_.empty()) return false;
93 
94  // Check the base and the factors:
95  return Base::equals(*e, tol) &&
96  factors_.equals(e->factors_,
97  [tol](const sharedFactor &f1, const sharedFactor &f2) {
98  return f1->equals(*f2, tol);
99  });
100 }
101 
102 /* *******************************************************************************/
103 void HybridGaussianFactor::print(const std::string &s,
104  const KeyFormatter &formatter) const {
105  std::cout << (s.empty() ? "" : s + "\n");
106  std::cout << "HybridGaussianFactor" << std::endl;
108  std::cout << "{\n";
109  if (factors_.empty()) {
110  std::cout << " empty" << std::endl;
111  } else {
112  factors_.print(
113  "", [&](Key k) { return formatter(k); },
114  [&](const sharedFactor &gf) -> std::string {
115  RedirectCout rd;
116  std::cout << ":\n";
117  if (gf) {
118  gf->print("", formatter);
119  return rd.str();
120  } else {
121  return "nullptr";
122  }
123  });
124  }
125  std::cout << "}" << std::endl;
126 }
127 
128 /* *******************************************************************************/
130  const DiscreteValues &assignment) const {
131  return factors_(assignment);
132 }
133 
134 /* *******************************************************************************/
136  const GaussianFactorGraphTree &sum) const {
137  using Y = GaussianFactorGraph;
138  auto add = [](const Y &graph1, const Y &graph2) {
139  auto result = graph1;
140  result.push_back(graph2);
141  return result;
142  };
143  const auto tree = asGaussianFactorGraphTree();
144  return sum.empty() ? tree : sum.apply(tree, add);
145 }
146 
147 /* *******************************************************************************/
149  const {
150  auto wrap = [](const sharedFactor &gf) { return GaussianFactorGraph{gf}; };
151  return {factors_, wrap};
152 }
153 
154 /* *******************************************************************************/
156  const sharedFactor &gf, const VectorValues &values) const {
157  // Check if valid pointer
158  if (gf) {
159  return gf->error(values);
160  } else {
161  // If not valid, pointer, it means this component was pruned,
162  // so we return maximum error.
163  // This way the negative exponential will give
164  // a probability value close to 0.0.
166  }
167 }
168 /* *******************************************************************************/
170  const VectorValues &continuousValues) const {
171  // functor to convert from sharedFactor to double error value.
172  auto errorFunc = [this, &continuousValues](const sharedFactor &gf) {
173  return this->potentiallyPrunedComponentError(gf, continuousValues);
174  };
175  DecisionTree<Key, double> error_tree(factors_, errorFunc);
176  return error_tree;
177 }
178 
179 /* *******************************************************************************/
181  // Directly index to get the component, no need to build the whole tree.
182  const sharedFactor gf = factors_(values.discrete());
183  return potentiallyPrunedComponentError(gf, values.continuous());
184 }
185 
186 } // namespace gtsam
gtsam::HybridGaussianFactor::Factors
DecisionTree< Key, sharedFactor > Factors
typedef for Decision Tree of Gaussian factors.
Definition: HybridGaussianFactor.h:70
relicense.update
def update(text)
Definition: relicense.py:46
gtsam::HybridGaussianFactor::operator()
sharedFactor operator()(const DiscreteValues &assignment) const
Get factor at a given discrete assignment.
Definition: HybridGaussianFactor.cpp:129
GaussianFactorGraph.h
Linear Factor Graph where all factors are Gaussians.
gtsam::HybridValues
Definition: HybridValues.h:37
Y
const char Y
Definition: test/EulerAngles.cpp:31
test_constructor::f1
auto f1
Definition: testHybridNonlinearFactor.cpp:56
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::HybridGaussianFactor::errorTree
AlgebraicDecisionTree< Key > errorTree(const VectorValues &continuousValues) const override
Compute error of the HybridGaussianFactor as a tree.
Definition: HybridGaussianFactor.cpp:169
gtsam::HybridFactor::equals
virtual bool equals(const HybridFactor &lf, double tol=1e-9) const
equals
Definition: HybridFactor.cpp:86
gtsam::HybridGaussianFactor::HybridGaussianFactor
HybridGaussianFactor()=default
Default constructor, mainly for serialization.
gtsam::DecisionTree::empty
bool empty() const
Check if tree is empty.
Definition: DecisionTree.h:269
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
tree
Definition: testExpression.cpp:212
gtsam::DecisionTree::equals
bool equals(const DecisionTree &other, const CompareFunc &compare=&DefaultCompare) const
Definition: DecisionTree-inl.h:899
simple_graph::factors
const GaussianFactorGraph factors
Definition: testJacobianFactor.cpp:213
gtsam::RedirectCout
Definition: base/utilities.h:16
gtsam::HybridFactor
Definition: HybridFactor.h:54
wrap
mxArray * wrap(const Class &value)
Definition: matlab.h:126
simple::graph2
NonlinearFactorGraph graph2()
Definition: testInitializePose3.cpp:72
gtsam::augment
static HybridGaussianFactor::Factors augment(const HybridGaussianFactor::FactorValuePairs &factors)
Helper function to augment the [A|b] matrices in the factor components with the additional scalar val...
Definition: HybridGaussianFactor.cpp:41
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
f2
double f2(const Vector2 &x)
Definition: testNumericalDerivative.cpp:56
gtsam::HybridGaussianFactor::add
GaussianFactorGraphTree add(const GaussianFactorGraphTree &sum) const
Combine the Gaussian Factor Graphs in sum and this while maintaining the original tree structure.
Definition: HybridGaussianFactor.cpp:135
gtsam::Factor
Definition: Factor.h:70
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
gtsam::DecisionTree::print
void print(const std::string &s, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter) const
GTSAM-style print.
Definition: DecisionTree-inl.h:905
gtsam::HybridGaussianFactor::print
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: HybridGaussianFactor.cpp:103
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
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
HybridGaussianFactor.h
A set of GaussianFactors, indexed by a set of discrete keys.
utilities.h
gtsam::AlgebraicDecisionTree< Key >
gtsam::GaussianFactorGraph
Definition: GaussianFactorGraph.h:73
gtsam::GaussianFactorValuePair
std::pair< GaussianFactor::shared_ptr, double > GaussianFactorValuePair
Alias for pair of GaussianFactor::shared_pointer and a double value.
Definition: HybridGaussianFactor.h:37
gtsam::VectorValues
Definition: VectorValues.h:74
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
DecisionTree.h
Decision Tree for use in DiscreteFactors.
gtsam::DecisionTree::apply
DecisionTree apply(const Unary &op) const
Definition: DecisionTree-inl.h:922
gtsam::HybridGaussianFactor::equals
bool equals(const HybridFactor &lf, double tol=1e-9) const override
equals
Definition: HybridGaussianFactor.cpp:86
gtsam::HybridGaussianFactor::asGaussianFactorGraphTree
GaussianFactorGraphTree asGaussianFactorGraphTree() const
Helper function to return factors and functional to create a DecisionTree of Gaussian Factor Graphs.
Definition: HybridGaussianFactor.cpp:148
gtsam::DecisionTree< Key, sharedFactor >
gtsam::HybridFactor::print
void print(const std::string &s="HybridFactor\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: HybridFactor.cpp:94
gtsam
traits
Definition: chartTesting.h:28
DecisionTree-inl.h
gtsam::HybridGaussianFactor::sharedFactor
std::shared_ptr< GaussianFactor > sharedFactor
Definition: HybridGaussianFactor.h:65
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
leaf::values
leaf::MyValues values
gtsam::FactorGraph::push_back
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:147
gtsam::HybridGaussianFactor::potentiallyPrunedComponentError
double potentiallyPrunedComponentError(const sharedFactor &gf, const VectorValues &continuousValues) const
Helper method to compute the error of a component.
Definition: HybridGaussianFactor.cpp:155
gtsam::AlgebraicDecisionTree::min
double min() const
Find the minimum values amongst all leaves.
Definition: AlgebraicDecisionTree.h:219
gtsam::HybridGaussianFactor::factors_
Factors factors_
Decision tree of Gaussian factors indexed by discrete keys.
Definition: HybridGaussianFactor.h:74
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::HybridGaussianFactor::error
double error(const HybridValues &values) const override
Compute the log-likelihood, including the log-normalizing constant.
Definition: HybridGaussianFactor.cpp:180
GaussianFactor.h
A factor with a quadratic error function - a Gaussian.
gtsam::unzip
std::pair< DecisionTree< L, T1 >, DecisionTree< L, T2 > > unzip(const DecisionTree< L, std::pair< T1, T2 > > &input)
unzip a DecisionTree with std::pair values.
Definition: DecisionTree.h:454
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
max
#define max(a, b)
Definition: datatypes.h:20
HybridValues.h
test_callbacks.value
value
Definition: test_callbacks.py:160
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
gtsam::RedirectCout::str
std::string str() const
return the string
Definition: utilities.cpp:5


gtsam
Author(s):
autogenerated on Wed Sep 25 2024 03:02:32