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 
18 #pragma once
19 
23 
24 #include <memory>
25 
26 #include <optional>
27 
28 namespace gtsam
29 {
30  namespace internal
31  {
32  namespace linearAlgorithms
33  {
34  /* ************************************************************************* */
35  struct OptimizeData {
38  //VectorValues ancestorResults;
39  //VectorValues results;
40  };
41 
42  /* ************************************************************************* */
49  template<class CLIQUE>
51  {
53 
55  const std::shared_ptr<CLIQUE>& clique,
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  // NOTE(gareth): We can no longer write: xS = b - S * xS
90  // This is because Eigen (as of 3.3) no longer evaluates S * xS into
91  // a temporary, and the operation trashes valus in xS.
92  // See: http://eigen.tuxfamily.org/index.php?title=3.3
93  const Vector rhs = c.getb() - c.S() * xS;
94 
95  // TODO(gareth): Inline instantiation of Eigen::Solve and check flag
96  const Vector solution = c.R().triangularView<Eigen::Upper>().solve(rhs);
97 
98  // Check for indeterminant solution
99  if(solution.hasNaN()) throw IndeterminantLinearSystemException(c.keys().front());
100 
101  // Insert solution into a VectorValues
102  DenseIndex vectorPosition = 0;
103  for(GaussianConditional::const_iterator frontal = c.beginFrontals(); frontal != c.endFrontals(); ++frontal) {
104  auto result = collectedResult.emplace(*frontal, solution.segment(vectorPosition, c.getDim(frontal)));
105  if(!result.second)
106  throw std::runtime_error(
107  "Internal error while optimizing clique. Trying to insert key '" + DefaultKeyFormatter(*frontal)
108  + "' that exists.");
109 
111  myData.cliqueResults.emplace(r->first, r);
112  vectorPosition += c.getDim(frontal);
113  }
114  }
115  return myData;
116  }
117  };
118 
119  /* ************************************************************************* */
120  //OptimizeData OptimizePreVisitor(const GaussianBayesTreeClique::shared_ptr& clique, OptimizeData& parentData)
121  //{
122  // // Create data - holds a pointer to our parent, a copy of parent solution, and our results
123  // OptimizeData myData;
124  // myData.parentData = parentData;
125  // // Take any ancestor results we'll need
126  // for(Key parent: clique->conditional_->parents())
127  // myData.ancestorResults.insert(parent, myData.parentData->ancestorResults[parent]);
128  // // Solve and store in our results
129  // myData.results.insert(clique->conditional()->solve(myData.ancestorResults));
130  // myData.ancestorResults.insert(myData.results);
131  // return myData;
132  //}
133 
134  /* ************************************************************************* */
135  //void OptimizePostVisitor(const GaussianBayesTreeClique::shared_ptr& clique, OptimizeData& myData)
136  //{
137  // // Conglomerate our results to the parent
138  // myData.parentData->results.insert(myData.results);
139  //}
140 
141  /* ************************************************************************* */
142  template<class BAYESTREE>
143  VectorValues optimizeBayesTree(const BAYESTREE& bayesTree)
144  {
145  gttic(linear_optimizeBayesTree);
146  //internal::OptimizeData rootData; // Will hold final solution
147  //treeTraversal::DepthFirstForest(*this, rootData, internal::OptimizePreVisitor, internal::OptimizePostVisitor);
148  //return rootData.results;
149  OptimizeData rootData;
151  treeTraversal::no_op postVisitor;
152  TbbOpenMPMixedScope threadLimiter; // Limits OpenMP threads since we're mixing TBB and OpenMP
153  treeTraversal::DepthFirstForestParallel(bayesTree, rootData, preVisitor, postVisitor);
154  return preVisitor.collectedResult;
155  }
156  }
157  }
158 }
GaussianConditional.h
Conditional Gaussian Base class.
treeTraversal-inst.h
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
gtsam::FastMap< Key, VectorValues::const_iterator >
Eigen::Upper
@ Upper
Definition: Constants.h:211
gtsam::internal::linearAlgorithms::OptimizeData
Definition: linearAlgorithms-inst.h:35
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::internal::linearAlgorithms::OptimizeClique::operator()
OptimizeData operator()(const std::shared_ptr< CLIQUE > &clique, OptimizeData &parentData)
Definition: linearAlgorithms-inst.h:54
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::VectorValues::const_iterator
Values::const_iterator const_iterator
Const iterator over vector values.
Definition: VectorValues.h:85
parentData
DATA & parentData
Definition: treeTraversal-inst.h:45
gtsam::VectorValues
Definition: VectorValues.h:74
gtsam::GaussianConditional
Definition: GaussianConditional.h:40
VectorValues.h
Factor Graph Values.
gtsam::internal::linearAlgorithms::optimizeBayesTree
VectorValues optimizeBayesTree(const BAYESTREE &bayesTree)
Definition: linearAlgorithms-inst.h:143
gtsam::internal::linearAlgorithms::OptimizeData::cliqueResults
FastMap< Key, VectorValues::const_iterator > cliqueResults
Definition: linearAlgorithms-inst.h:37
gtsam::treeTraversal::DepthFirstForestParallel
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
Definition: treeTraversal-inst.h:156
gtsam::Factor::const_iterator
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:83
gtsam
traits
Definition: SFMdata.h:40
gtsam::DenseIndex
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:103
internal
Definition: BandTriangularSolver.h:13
gtsam::internal::linearAlgorithms::OptimizeData::parentData
OptimizeData * parentData
Definition: linearAlgorithms-inst.h:36
gtsam::internal::linearAlgorithms::OptimizeClique
Definition: linearAlgorithms-inst.h:50
gtsam::IndeterminantLinearSystemException
Definition: linearExceptions.h:94
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::internal::linearAlgorithms::OptimizeClique::collectedResult
VectorValues collectedResult
Definition: linearAlgorithms-inst.h:52
gtsam::VectorValues::emplace
std::pair< VectorValues::iterator, bool > emplace(Key j, Args &&... args)
Definition: VectorValues.h:188
gtsam::TbbOpenMPMixedScope
Definition: types.h:162
gttic
#define gttic(label)
Definition: timing.h:295


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:42