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 <<
": ";
86 cout <<
"nullptr" <<
"\n";
88 factor->print(
ss.str(), keyFormatter);
89 cout <<
"error = " << errorValue <<
"\n";
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) {
154 std::stringstream
ss;
160 void NonlinearFactorGraph::saveGraph(
const std::string&
filename,
171 gttic(NonlinearFactorGraph_error);
172 double total_error = 0.;
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_);
230 result_[
i] = GaussianFactor::shared_ptr();
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++) {
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());
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);
388 result.push_back(
f->rekey(rekey_mapping));