HybridNonlinearFactorGraph.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 
26 
27 namespace gtsam {
28 
29 /* ************************************************************************* */
30 void HybridNonlinearFactorGraph::print(const std::string& s,
31  const KeyFormatter& keyFormatter) const {
32  // Base::print(str, keyFormatter);
33  std::cout << (s.empty() ? "" : s + " ") << std::endl;
34  std::cout << "size: " << size() << std::endl;
35  for (size_t i = 0; i < factors_.size(); i++) {
36  std::stringstream ss;
37  ss << "factor " << i << ": ";
38  if (factors_[i]) {
39  factors_[i]->print(ss.str(), keyFormatter);
40  std::cout << std::endl;
41  }
42  }
43 }
44 
45 /* ************************************************************************* */
47  const HybridValues& values, const std::string& str,
48  const KeyFormatter& keyFormatter,
49  const std::function<bool(const Factor* /*factor*/, double /*whitenedError*/,
50  size_t /*index*/)>& printCondition) const {
51  std::cout << str << "size: " << size() << std::endl << std::endl;
52 
53  std::stringstream ss;
54 
55  for (size_t i = 0; i < factors_.size(); i++) {
56  auto&& factor = factors_[i];
57  std::cout << "Factor " << i << ": ";
58 
59  // Clear the stringstream
60  ss.str(std::string());
61 
62  if (auto mf = std::dynamic_pointer_cast<HybridNonlinearFactor>(factor)) {
63  if (factor == nullptr) {
64  std::cout << "nullptr"
65  << "\n";
66  } else {
67  factor->print(ss.str(), keyFormatter);
68  std::cout << "error = ";
69  mf->errorTree(values.nonlinear()).print("", keyFormatter);
70  std::cout << std::endl;
71  }
72  } else if (auto gmf =
73  std::dynamic_pointer_cast<HybridGaussianFactor>(factor)) {
74  if (factor == nullptr) {
75  std::cout << "nullptr"
76  << "\n";
77  } else {
78  factor->print(ss.str(), keyFormatter);
79  std::cout << "error = ";
80  gmf->errorTree(values.continuous()).print("", keyFormatter);
81  std::cout << std::endl;
82  }
83  } else if (auto gm = std::dynamic_pointer_cast<HybridGaussianConditional>(
84  factor)) {
85  if (factor == nullptr) {
86  std::cout << "nullptr"
87  << "\n";
88  } else {
89  factor->print(ss.str(), keyFormatter);
90  std::cout << "error = ";
91  gm->errorTree(values.continuous()).print("", keyFormatter);
92  std::cout << std::endl;
93  }
94  } else if (auto nf = std::dynamic_pointer_cast<NonlinearFactor>(factor)) {
95  const double errorValue = (factor != nullptr ? nf->error(values) : .0);
96  if (!printCondition(factor.get(), errorValue, i))
97  continue; // User-provided filter did not pass
98 
99  if (factor == nullptr) {
100  std::cout << "nullptr"
101  << "\n";
102  } else {
103  factor->print(ss.str(), keyFormatter);
104  std::cout << "error = " << errorValue << "\n";
105  }
106  } else if (auto gf = std::dynamic_pointer_cast<GaussianFactor>(factor)) {
107  const double errorValue = (factor != nullptr ? gf->error(values) : .0);
108  if (!printCondition(factor.get(), errorValue, i))
109  continue; // User-provided filter did not pass
110 
111  if (factor == nullptr) {
112  std::cout << "nullptr"
113  << "\n";
114  } else {
115  factor->print(ss.str(), keyFormatter);
116  std::cout << "error = " << errorValue << "\n";
117  }
118  } else if (auto df = std::dynamic_pointer_cast<DiscreteFactor>(factor)) {
119  if (factor == nullptr) {
120  std::cout << "nullptr"
121  << "\n";
122  } else {
123  factor->print(ss.str(), keyFormatter);
124  std::cout << "error = ";
125  df->errorTree().print("", keyFormatter);
126  std::cout << std::endl;
127  }
128 
129  } else {
130  continue;
131  }
132 
133  std::cout << "\n";
134  }
135  std::cout.flush();
136 }
137 
138 /* ************************************************************************* */
140  const Values& continuousValues) const {
141  using std::dynamic_pointer_cast;
142 
143  // create an empty linear FG
144  auto linearFG = std::make_shared<HybridGaussianFactorGraph>();
145 
146  linearFG->reserve(size());
147 
148  // linearize all hybrid factors
149  for (auto& f : factors_) {
150  // First check if it is a valid factor
151  if (!f) {
152  continue;
153  }
154  // Check if it is a hybrid nonlinear factor
155  if (auto mf = dynamic_pointer_cast<HybridNonlinearFactor>(f)) {
157  mf->linearize(continuousValues);
158  linearFG->push_back(gmf);
159  } else if (auto nlf = dynamic_pointer_cast<NonlinearFactor>(f)) {
160  const GaussianFactor::shared_ptr& gf = nlf->linearize(continuousValues);
161  linearFG->push_back(gf);
162  } else if (dynamic_pointer_cast<DiscreteFactor>(f)) {
163  // If discrete-only: doesn't need linearization.
164  linearFG->push_back(f);
165  } else if (auto gmf = dynamic_pointer_cast<HybridGaussianFactor>(f)) {
166  linearFG->push_back(gmf);
167  } else if (auto gm = dynamic_pointer_cast<HybridGaussianConditional>(f)) {
168  linearFG->push_back(gm);
169  } else if (dynamic_pointer_cast<GaussianFactor>(f)) {
170  linearFG->push_back(f);
171  } else {
172  auto& fr = *f;
173  throw std::invalid_argument(
174  std::string("HybridNonlinearFactorGraph::linearize: factor type "
175  "not handled: ") +
176  demangle(typeid(fr).name()));
177  }
178  }
179  return linearFG;
180 }
181 
182 /* ************************************************************************* */
184  const Values& values) const {
186 
187  // Iterate over each factor.
188  for (auto& factor : factors_) {
189  if (auto hnf = std::dynamic_pointer_cast<HybridNonlinearFactor>(factor)) {
190  // Compute factor error and add it.
191  result = result + hnf->errorTree(values);
192 
193  } else if (auto nf = std::dynamic_pointer_cast<NonlinearFactor>(factor)) {
194  // If continuous only, get the (double) error
195  // and add it to every leaf of the result
196  result = result + nf->error(values);
197 
198  } else if (auto df = std::dynamic_pointer_cast<DiscreteFactor>(factor)) {
199  // If discrete, just add its errorTree as well
200  result = result + df->errorTree();
201 
202  } else {
203  throw std::runtime_error(
204  "HybridNonlinearFactorGraph::errorTree(Values) not implemented for "
205  "factor type " +
206  demangle(typeid(factor).name()) + ".");
207  }
208  }
209 
210  return result;
211 }
212 
213 /* ************************************************************************ */
215  const Values& continuousValues) const {
216  AlgebraicDecisionTree<Key> errors = this->errorTree(continuousValues);
217  AlgebraicDecisionTree<Key> p = errors.apply([](double error) {
218  // NOTE: The 0.5 term is handled by each factor
219  return exp(-error);
220  });
221  return p / p.sum();
222 }
223 
224 /* ************************************************************************ */
225 } // namespace gtsam
gtsam::HybridGaussianFactorGraph::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to This
Definition: HybridGaussianFactorGraph.h:119
gtsam::HybridValues
Definition: HybridValues.h:37
gtsam::FactorGraph< Factor >::factors_
FastVector< sharedFactor > factors_
Definition: FactorGraph.h:92
gtsam::HybridNonlinearFactorGraph::print
void print(const std::string &s="HybridNonlinearFactorGraph", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print the factor graph.
Definition: HybridNonlinearFactorGraph.cpp:30
HybridGaussianConditional.h
A hybrid conditional in the Conditional Linear Gaussian scheme.
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::FactorGraph< Factor >::error
double error(const HybridValues &values) const
Definition: FactorGraph-inst.h:66
gtsam::HybridGaussianFactor::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: HybridGaussianFactor.h:64
HybridNonlinearFactorGraph.h
Nonlinear hybrid factor graph that uses type erasure.
gtsam::HybridNonlinearFactorGraph::printErrors
void printErrors(const HybridValues &values, const std::string &str="HybridNonlinearFactorGraph: ", const KeyFormatter &keyFormatter=DefaultKeyFormatter, const std::function< bool(const Factor *, double, size_t)> &printCondition=[](const Factor *, double, size_t) { return true;}) const
Definition: HybridNonlinearFactorGraph.cpp:46
gtsam::HybridNonlinearFactorGraph::discretePosterior
AlgebraicDecisionTree< Key > discretePosterior(const Values &continuousValues) const
Computer posterior P(M|X=x) when all continuous values X are given. This is efficient as this simply ...
Definition: HybridNonlinearFactorGraph.cpp:214
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::Factor
Definition: Factor.h:70
exp
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
Definition: ArrayCwiseUnaryOps.h:97
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::HybridNonlinearFactorGraph::errorTree
AlgebraicDecisionTree< Key > errorTree(const Values &values) const
Compute error of (hybrid) nonlinear factors and discrete factors over each discrete assignment,...
Definition: HybridNonlinearFactorGraph.cpp:183
name
static char name[]
Definition: rgamma.c:72
gtsam::AlgebraicDecisionTree< Key >
ss
static std::stringstream ss
Definition: testBTree.cpp:31
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::GaussianFactor::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42
mf
Matrix2f mf
Definition: MatrixBase_cast.cpp:2
gtsam::DecisionTree::apply
DecisionTree apply(const Unary &op) const
Definition: DecisionTree-inl.h:1000
TableFactor.h
str
Definition: pytypes.h:1558
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
NonlinearFactor.h
Non-linear factor base classes.
gtsam::FactorGraph< Factor >::size
size_t size() const
Definition: FactorGraph.h:297
HybridNonlinearFactor.h
A set of nonlinear factors indexed by a set of discrete keys.
HybridGaussianFactorGraph.h
Linearized Hybrid factor graph that uses type erasure.
gtsam
traits
Definition: SFMdata.h:40
gtsam::Values
Definition: Values.h:65
gtsam::demangle
std::string demangle(const char *name)
Pretty print Value type name.
Definition: types.cpp:37
p
float * p
Definition: Tutorial_Map_using.cpp:9
gtsam::HybridNonlinearFactorGraph::linearize
std::shared_ptr< HybridGaussianFactorGraph > linearize(const Values &continuousValues) const
Linearize all the continuous factors in the HybridNonlinearFactorGraph.
Definition: HybridNonlinearFactorGraph.cpp:139
DecisionTreeFactor.h
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:43