30 #include <gtsam/config.h> 33 # include <tbb/parallel_for.h> 46 template class FactorGraph<NonlinearFactor>;
49 double NonlinearFactorGraph::probPrime(
const Values&
values)
const {
56 cout << str <<
"size: " <<
size() << endl << endl;
57 for (
size_t i = 0;
i < factors_.size();
i++) {
59 ss <<
"Factor " <<
i <<
": ";
60 if (factors_[i] !=
nullptr) {
61 factors_[
i]->print(ss.str(), keyFormatter);
64 cout << ss.str() <<
"nullptr\n";
71 void NonlinearFactorGraph::printErrors(
const Values&
values,
const std::string&
str,
73 const std::function<
bool(
const Factor* ,
double ,
size_t )>& printCondition)
const 75 cout << str <<
"size: " <<
size() << endl
77 for (
size_t i = 0;
i < factors_.size();
i++) {
79 const double errorValue = (factor !=
nullptr ? factors_[
i]->error(values) : .0);
80 if (!printCondition(factor.get(),errorValue,
i))
84 ss <<
"Factor " <<
i <<
": ";
85 if (factor ==
nullptr) {
86 cout <<
"nullptr" <<
"\n";
88 factor->print(ss.str(), keyFormatter);
89 cout <<
"error = " << errorValue <<
"\n";
98 return Base::equals(other, tol);
120 std::set<KeyVector> structure;
124 std::sort(factorKeys.begin(), factorKeys.end());
125 structure.insert(factorKeys);
131 for (
const KeyVector& factorKeys : structure) {
136 for (
size_t i = 0;
i <
size(); ++
i) {
139 const KeyVector& factorKeys = factor->keys();
154 std::stringstream
ss;
155 dot(ss, values, keyFormatter, writer);
160 void NonlinearFactorGraph::saveGraph(
const std::string&
filename,
164 std::ofstream of(filename);
165 dot(of, values, keyFormatter, writer);
171 gttic(NonlinearFactorGraph_error);
172 double total_error = 0.;
176 total_error += factor->error(values);
182 Ordering NonlinearFactorGraph::orderingCOLAMD()
const 184 return Ordering::Colamd(*
this);
190 return Ordering::ColamdConstrained(*
this, constraints);
198 symbolic->reserve(
size());
214 class _LinearizeOneFactor {
216 const Values& linearizationPoint_;
222 nonlinearGraph_(graph), linearizationPoint_(linearizationPoint), result_(result) {
225 void operator()(
const tbb::blocked_range<size_t>& blocked_range)
const {
226 for (
size_t i = blocked_range.begin();
i != blocked_range.end(); ++
i) {
227 if (nonlinearGraph_[
i] && nonlinearGraph_[
i]->sendable())
228 result_[
i] = nonlinearGraph_[
i]->linearize(linearizationPoint_);
241 gttic(NonlinearFactorGraph_linearize);
248 linearFG->resize(
size());
252 tbb::parallel_for(tbb::blocked_range<size_t>(0,
size()),
253 _LinearizeOneFactor(*
this, linearizationPoint, *linearFG));
256 for(
size_t i = 0;
i <
size();
i++) {
257 auto& factor = (*this)[
i];
258 if(factor && !(factor->sendable())) {
259 (*linearFG)[
i] = factor->linearize(linearizationPoint);
265 linearFG->reserve(
size());
270 linearFG->push_back(factor->linearize(linearizationPoint));
285 scatter.reserve(values.
size());
288 for (
const auto& key_dim : values.
dims()) {
289 scatter.
add(key_dim.first, key_dim.second);
300 scatter.reserve(values.
size());
303 for (
Key key : ordering) {
318 hessianFactor->info_.setZero();
323 if (nonlinearFactor) {
324 const auto& gaussianFactor = nonlinearFactor->linearize(values);
325 gaussianFactor->updateHessian(hessianFactor->keys_, &hessianFactor->info_);
329 if (dampen) dampen(hessianFactor);
331 return hessianFactor;
337 gttic(NonlinearFactorGraph_linearizeToHessianFactor);
340 return linearizeToHessianFactor(values, scatter, dampen);
346 gttic(NonlinearFactorGraph_linearizeToHessianFactor);
349 return linearizeToHessianFactor(values, scatter, dampen);
354 const Dampen& dampen)
const {
355 gttic(NonlinearFactorGraph_updateCholesky);
356 auto hessianFactor = linearizeToHessianFactor(values, dampen);
364 const Dampen& dampen)
const {
365 gttic(NonlinearFactorGraph_updateCholesky);
366 auto hessianFactor = linearizeToHessianFactor(values, ordering, dampen);
const gtsam::Symbol key('X', 0)
void processFactor(size_t i, const KeyVector &keys, const KeyFormatter &keyFormatter, const std::optional< Vector2 > &position, std::ostream *os) const
Draw a single factor, specified by its index i and its variable keys.
GTSAM_EXPORT void add(Key key, size_t dim)
Add a key/dim pair.
A non-templated config holding any types of Manifold-group elements.
Factor Graph consisting of non-linear factors.
std::shared_ptr< This > shared_ptr
A shared_ptr to this class.
const ValueType at(Key j) const
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
NonlinearFactorGraph graph
static enum @1107 ordering
void graphPreamble(std::ostream *os) const
Write out preamble for graph, including size.
Values retract(const VectorValues &delta) const
EIGEN_DEVICE_FUNC const ExpReturnType exp() const
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Linear Factor Graph where all factors are Gaussians.
std::map< Key, size_t > dims() const
std::shared_ptr< This > shared_ptr
shared_ptr to this class
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
virtual size_t dim() const =0
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Exceptions that may be thrown by linear solver components.
static std::stringstream ss
static Scatter scatterFromValues(const Values &values, const Ordering &ordering)
void drawVariable(Key key, const KeyFormatter &keyFormatter, const std::optional< Vector2 > &position, std::ostream *os) const
Create a variable dot fragment.
ofstream os("timeSchurFactors.csv")
std::shared_ptr< This > shared_ptr
shared_ptr to this class
std::shared_ptr< This > shared_ptr
A Gaussian factor using the canonical parameters (information form)
std::shared_ptr< NonlinearFactor > sharedFactor
Shared pointer to a factor.
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Point3 position(const NavState &X, OptionalJacobian< 3, 9 > H)
std::function< void(const std::shared_ptr< HessianFactor > &hessianFactor)> Dampen
typdef for dampen functions used below
std::uint64_t Key
Integer nonlinear key type.