gtsam  4.0.0
gtsam
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 
21 
22 #include <boost/optional.hpp>
23 #include <boost/shared_ptr.hpp>
24 
25 namespace gtsam
26 {
27  namespace internal
28  {
29  namespace linearAlgorithms
30  {
31  /* ************************************************************************* */
32  struct OptimizeData {
33  boost::optional<OptimizeData&> parentData;
35  //VectorValues ancestorResults;
36  //VectorValues results;
37  };
38 
39  /* ************************************************************************* */
46  template<class CLIQUE>
48  {
49  VectorValues collectedResult;
50 
51  OptimizeData operator()(
52  const boost::shared_ptr<CLIQUE>& clique,
53  OptimizeData& parentData)
54  {
55  OptimizeData myData;
56  myData.parentData = parentData;
57  // Take any ancestor results we'll need
58  for(Key parent: clique->conditional_->parents())
59  myData.cliqueResults.emplace(parent, myData.parentData->cliqueResults.at(parent));
60 
61  // Solve and store in our results
62  {
63  GaussianConditional& c = *clique->conditional();
64  // Solve matrix
65  Vector xS;
66  {
67  // Count dimensions of vector
68  DenseIndex dim = 0;
69  FastVector<VectorValues::const_iterator> parentPointers;
70  parentPointers.reserve(clique->conditional()->nrParents());
71  for(Key parent: clique->conditional()->parents()) {
72  parentPointers.push_back(myData.cliqueResults.at(parent));
73  dim += parentPointers.back()->second.size();
74  }
75 
76  // Fill parent vector
77  xS.resize(dim);
78  DenseIndex vectorPos = 0;
79  for(const VectorValues::const_iterator& parentPointer: parentPointers) {
80  const Vector& parentVector = parentPointer->second;
81  xS.block(vectorPos,0,parentVector.size(),1) = parentVector.block(0,0,parentVector.size(),1);
82  vectorPos += parentVector.size();
83  }
84  }
85 
86  // NOTE(gareth): We can no longer write: xS = b - S * xS
87  // This is because Eigen (as of 3.3) no longer evaluates S * xS into
88  // a temporary, and the operation trashes valus in xS.
89  // See: http://eigen.tuxfamily.org/index.php?title=3.3
90  const Vector rhs = c.getb() - c.S() * xS;
91 
92  // TODO(gareth): Inline instantiation of Eigen::Solve and check flag
93  const Vector solution = c.R().triangularView<Eigen::Upper>().solve(rhs);
94 
95  // Check for indeterminant solution
96  if(solution.hasNaN()) throw IndeterminantLinearSystemException(c.keys().front());
97 
98  // Insert solution into a VectorValues
99  DenseIndex vectorPosition = 0;
100  for(GaussianConditional::const_iterator frontal = c.beginFrontals(); frontal != c.endFrontals(); ++frontal) {
102  collectedResult.emplace(*frontal, solution.segment(vectorPosition, c.getDim(frontal)));
103  myData.cliqueResults.emplace(r->first, r);
104  vectorPosition += c.getDim(frontal);
105  }
106  }
107  return myData;
108  }
109  };
110 
111  /* ************************************************************************* */
112  //OptimizeData OptimizePreVisitor(const GaussianBayesTreeClique::shared_ptr& clique, OptimizeData& parentData)
113  //{
114  // // Create data - holds a pointer to our parent, a copy of parent solution, and our results
115  // OptimizeData myData;
116  // myData.parentData = parentData;
117  // // Take any ancestor results we'll need
118  // for(Key parent: clique->conditional_->parents())
119  // myData.ancestorResults.insert(parent, myData.parentData->ancestorResults[parent]);
120  // // Solve and store in our results
121  // myData.results.insert(clique->conditional()->solve(myData.ancestorResults));
122  // myData.ancestorResults.insert(myData.results);
123  // return myData;
124  //}
125 
126  /* ************************************************************************* */
127  //void OptimizePostVisitor(const GaussianBayesTreeClique::shared_ptr& clique, OptimizeData& myData)
128  //{
129  // // Conglomerate our results to the parent
130  // myData.parentData->results.insert(myData.results);
131  //}
132 
133  /* ************************************************************************* */
134  template<class BAYESTREE>
135  VectorValues optimizeBayesTree(const BAYESTREE& bayesTree)
136  {
137  gttic(linear_optimizeBayesTree);
138  //internal::OptimizeData rootData; // Will hold final solution
139  //treeTraversal::DepthFirstForest(*this, rootData, internal::OptimizePreVisitor, internal::OptimizePostVisitor);
140  //return rootData.results;
141  OptimizeData rootData;
143  treeTraversal::no_op postVisitor;
144  TbbOpenMPMixedScope threadLimiter; // Limits OpenMP threads since we're mixing TBB and OpenMP
145  treeTraversal::DepthFirstForestParallel(bayesTree, rootData, preVisitor, postVisitor);
146  return preVisitor.collectedResult;
147  }
148  }
149  }
150 }
Values::const_iterator const_iterator
Const iterator over vector values.
Definition: VectorValues.h:81
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:63
A conditional Gaussian functions as the node in a Bayes network It has a set of parents y,...
Definition: GaussianConditional.h:36
Conditional Gaussian Base class.
const constBVector getb() const
Get a view of the r.h.s.
Definition: JacobianFactor.h:264
constABlock S() const
Get a view of the parent blocks.
Definition: GaussianConditional.h:100
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Thrown when a linear system is ill-posed.
Definition: linearExceptions.h:94
iterator emplace(Key j, const Vector &value)
Emplace a vector value with key j.
Definition: VectorValues.cpp:88
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:73
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:154
An object whose scope defines a block where TBB and OpenMP parallelism are mixed.
Definition: types.h:148
Pre-order visitor for back-substitution in a Bayes tree.
Definition: linearAlgorithms-inst.h:47
virtual DenseIndex getDim(const_iterator variable) const
Return the dimension of the variable pointed to by the given key iterator todo: Remove this in favor ...
Definition: JacobianFactor.h:245
Definition: linearAlgorithms-inst.h:32
constABlock R() const
Return a view of the upper-triangular R block of the conditional.
Definition: GaussianConditional.h:97
FACTOR::const_iterator endFrontals() const
Iterator pointing past the last frontal key.
Definition: Conditional.h:107
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:118
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:67
Factor Graph Values.
FACTOR::const_iterator beginFrontals() const
Iterator pointing to first frontal key.
Definition: Conditional.h:104