gtsam
Loading...
Searching...
No Matches
linearAlgorithms-inst.h
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
17
18#pragma once
19
24
25#include <memory>
26#include <optional>
27
28namespace gtsam
29{
30 namespace internal
31 {
32 namespace linearAlgorithms
33 {
34 /* ************************************************************************* */
35 struct OptimizeData {
36 OptimizeData* parentData = nullptr;
38 //VectorValues ancestorResults;
39 //VectorValues results;
40 };
41
42 /* ************************************************************************* */
49 template<class CLIQUE>
51 {
52 VectorValues collectedResult;
53
54 OptimizeData operator()(
55 const std::shared_ptr<CLIQUE>& clique,
56 OptimizeData& parentData)
57 {
58 OptimizeData myData;
59 myData.parentData = &parentData;
60 // Take any ancestor results we'll need
61 for(Key parent: clique->conditional_->parents())
62 myData.cliqueResults.emplace(parent, myData.parentData->cliqueResults.at(parent));
63
64 // Solve and store in our results
65 {
66 GaussianConditional& c = *clique->conditional();
67 // Solve matrix
68 Vector xS;
69 {
70 // Count dimensions of vector
71 DenseIndex dim = 0;
73 parentPointers.reserve(clique->conditional()->nrParents());
74 for(Key parent: clique->conditional()->parents()) {
75 parentPointers.push_back(myData.cliqueResults.at(parent));
76 dim += parentPointers.back()->second.size();
77 }
78
79 // Fill parent vector
80 xS.resize(dim);
81 DenseIndex vectorPos = 0;
82 for(const VectorValues::const_iterator& parentPointer: parentPointers) {
83 const Vector& parentVector = parentPointer->second;
84 xS.block(vectorPos,0,parentVector.size(),1) = parentVector.block(0,0,parentVector.size(),1);
85 vectorPos += parentVector.size();
86 }
87 }
88
89 Vector solution;
90 internal::solveUpperConditional(c.R(), c.S(), c.getb(), xS,
91 &solution);
92 if (solution.hasNaN()) {
93 throw IndeterminateSystemException(c.keys().front());
94 }
95
96 // Insert solution into a VectorValues
97 DenseIndex vectorPosition = 0;
98 for(GaussianConditional::const_iterator frontal = c.beginFrontals(); frontal != c.endFrontals(); ++frontal) {
99 auto result = collectedResult.emplace(*frontal, solution.segment(vectorPosition, c.getDim(frontal)));
100 if(!result.second)
101 throw std::runtime_error(
102 "Internal error while optimizing clique. Trying to insert key '" + DefaultKeyFormatter(*frontal)
103 + "' that exists.");
104
105 VectorValues::const_iterator r = result.first;
106 myData.cliqueResults.emplace(r->first, r);
107 vectorPosition += c.getDim(frontal);
108 }
109 }
110 return myData;
111 }
112 };
113
114 /* ************************************************************************* */
115 //OptimizeData OptimizePreVisitor(const GaussianBayesTreeClique::shared_ptr& clique, OptimizeData& parentData)
116 //{
117 // // Create data - holds a pointer to our parent, a copy of parent solution, and our results
118 // OptimizeData myData;
119 // myData.parentData = parentData;
120 // // Take any ancestor results we'll need
121 // for(Key parent: clique->conditional_->parents())
122 // myData.ancestorResults.insert(parent, myData.parentData->ancestorResults[parent]);
123 // // Solve and store in our results
124 // myData.results.insert(clique->conditional()->solve(myData.ancestorResults));
125 // myData.ancestorResults.insert(myData.results);
126 // return myData;
127 //}
128
129 /* ************************************************************************* */
130 //void OptimizePostVisitor(const GaussianBayesTreeClique::shared_ptr& clique, OptimizeData& myData)
131 //{
132 // // Conglomerate our results to the parent
133 // myData.parentData->results.insert(myData.results);
134 //}
135
136 /* ************************************************************************* */
137 template<class BAYESTREE>
138 VectorValues optimizeBayesTree(const BAYESTREE& bayesTree)
139 {
140 gttic(linear_optimizeBayesTree);
141 //internal::OptimizeData rootData; // Will hold final solution
142 //treeTraversal::DepthFirstForest(*this, rootData, internal::OptimizePreVisitor, internal::OptimizePostVisitor);
143 //return rootData.results;
144 OptimizeData rootData;
146 treeTraversal::no_op postVisitor;
147 TbbOpenMPMixedScope threadLimiter; // Limits OpenMP threads since we're mixing TBB and OpenMP
148 treeTraversal::DepthFirstForestParallel(bayesTree, rootData, preVisitor, postVisitor);
149 return preVisitor.collectedResult;
150 }
151 }
152 }
153}
void solveUpperConditional(const Eigen::MatrixBase< RDerived > &R, const Eigen::MatrixBase< SDerived > &S, const Eigen::MatrixBase< DDerived > &d, const Eigen::MatrixBase< ParentsDerived > &parents, Vector *result)
Solve the block upper-triangular system R*x = d - S*parents.
Definition Matrix.h:278
Conditional Gaussian Base class.
Factor Graph Values.
Exceptions that may be thrown by linear solver components.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:49
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
Traverse a forest depth-first with pre-order and post-order visits.
Definition treeTraversal-inst.h:181
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition FastMap.h:40
An object whose scope defines a block where TBB and OpenMP parallelism are mixed.
Definition types.h:87
FACTOR::const_iterator endFrontals() const
Iterator pointing past the last frontal key.
Definition Conditional.h:185
FACTOR::const_iterator beginFrontals() const
Iterator pointing to first frontal key.
Definition Conditional.h:182
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition Factor.h:83
A GaussianConditional functions as the node in a Bayes network.
Definition GaussianConditional.h:43
constABlock R() const
Return a view of the upper-triangular R block of the conditional.
Definition GaussianConditional.h:237
constABlock S() const
Get a view of the parent blocks.
Definition GaussianConditional.h:240
const constBVector getb() const
Get a view of the r.h.s.
Definition JacobianFactor.h:344
DenseIndex getDim(const_iterator variable) const override
Return the dimension of the variable pointed to by the given key iterator todo: Remove this in favor ...
Definition JacobianFactor.h:323
Definition linearAlgorithms-inst.h:35
Pre-order visitor for back-substitution in a Bayes tree.
Definition linearAlgorithms-inst.h:51
Thrown when a linear system is ill-posed.
Definition linearExceptions.h:111
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73
Values::const_iterator const_iterator
Const iterator over vector values.
Definition VectorValues.h:81