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<MixtureFactor>(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<GaussianMixtureFactor>(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<GaussianMixture>(factor)) {
84  if (factor == nullptr) {
85  std::cout << "nullptr"
86  << "\n";
87  } else {
88  factor->print(ss.str(), keyFormatter);
89  std::cout << "error = ";
90  gm->errorTree(values.continuous()).print("", keyFormatter);
91  std::cout << std::endl;
92  }
93  } else if (auto nf = std::dynamic_pointer_cast<NonlinearFactor>(factor)) {
94  const double errorValue = (factor != nullptr ? nf->error(values) : .0);
95  if (!printCondition(factor.get(), errorValue, i))
96  continue; // User-provided filter did not pass
97 
98  if (factor == nullptr) {
99  std::cout << "nullptr"
100  << "\n";
101  } else {
102  factor->print(ss.str(), keyFormatter);
103  std::cout << "error = " << errorValue << "\n";
104  }
105  } else if (auto gf = std::dynamic_pointer_cast<GaussianFactor>(factor)) {
106  const double errorValue = (factor != nullptr ? gf->error(values) : .0);
107  if (!printCondition(factor.get(), errorValue, i))
108  continue; // User-provided filter did not pass
109 
110  if (factor == nullptr) {
111  std::cout << "nullptr"
112  << "\n";
113  } else {
114  factor->print(ss.str(), keyFormatter);
115  std::cout << "error = " << errorValue << "\n";
116  }
117  } else if (auto df = std::dynamic_pointer_cast<DiscreteFactor>(factor)) {
118  if (factor == nullptr) {
119  std::cout << "nullptr"
120  << "\n";
121  } else {
122  factor->print(ss.str(), keyFormatter);
123  std::cout << "error = ";
124  df->errorTree().print("", keyFormatter);
125  std::cout << std::endl;
126  }
127 
128  } else {
129  continue;
130  }
131 
132  std::cout << "\n";
133  }
134  std::cout.flush();
135 }
136 
137 /* ************************************************************************* */
139  const Values& continuousValues) const {
140  using std::dynamic_pointer_cast;
141 
142  // create an empty linear FG
143  auto linearFG = std::make_shared<HybridGaussianFactorGraph>();
144 
145  linearFG->reserve(size());
146 
147  // linearize all hybrid factors
148  for (auto& f : factors_) {
149  // First check if it is a valid factor
150  if (!f) {
151  continue;
152  }
153  // Check if it is a nonlinear mixture factor
154  if (auto mf = dynamic_pointer_cast<MixtureFactor>(f)) {
156  mf->linearize(continuousValues);
157  linearFG->push_back(gmf);
158  } else if (auto nlf = dynamic_pointer_cast<NonlinearFactor>(f)) {
159  const GaussianFactor::shared_ptr& gf = nlf->linearize(continuousValues);
160  linearFG->push_back(gf);
161  } else if (dynamic_pointer_cast<DiscreteFactor>(f)) {
162  // If discrete-only: doesn't need linearization.
163  linearFG->push_back(f);
164  } else if (auto gmf = dynamic_pointer_cast<GaussianMixtureFactor>(f)) {
165  linearFG->push_back(gmf);
166  } else if (auto gm = dynamic_pointer_cast<GaussianMixture>(f)) {
167  linearFG->push_back(gm);
168  } else if (dynamic_pointer_cast<GaussianFactor>(f)) {
169  linearFG->push_back(f);
170  } else {
171  auto& fr = *f;
172  throw std::invalid_argument(
173  std::string("HybridNonlinearFactorGraph::linearize: factor type "
174  "not handled: ") +
175  demangle(typeid(fr).name()));
176  }
177  }
178  return linearFG;
179 }
180 
181 } // namespace gtsam
gtsam::HybridGaussianFactorGraph::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to This
Definition: HybridGaussianFactorGraph.h:118
gtsam::HybridValues
Definition: HybridValues.h:38
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
s
RealScalar s
Definition: level1_cplx_impl.h:126
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::Factor
Definition: Factor.h:69
name
static char name[]
Definition: rgamma.c:72
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::GaussianMixtureFactor::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: GaussianMixtureFactor.h:51
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
GaussianMixture.h
A hybrid conditional in the Conditional Linear Gaussian scheme.
mf
Matrix2f mf
Definition: MatrixBase_cast.cpp:2
TableFactor.h
MixtureFactor.h
Nonlinear Mixture factor of continuous and discrete.
str
Definition: pytypes.h:1524
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
HybridGaussianFactorGraph.h
Linearized Hybrid factor graph that uses type erasure.
gtsam
traits
Definition: chartTesting.h:28
leaf::values
leaf::MyValues values
gtsam::Values
Definition: Values.h:65
gtsam::demangle
std::string demangle(const char *name)
Pretty print Value type name.
Definition: types.cpp:37
gtsam::HybridNonlinearFactorGraph::linearize
std::shared_ptr< HybridGaussianFactorGraph > linearize(const Values &continuousValues) const
Linearize all the continuous factors in the HybridNonlinearFactorGraph.
Definition: HybridNonlinearFactorGraph.cpp:138
DecisionTreeFactor.h
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:01:00