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 }
FACTOR::const_iterator endFrontals() const
Definition: Conditional.h:182
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:108
FACTOR::const_iterator beginFrontals() const
Definition: Conditional.h:179
std::pair< VectorValues::iterator, bool > emplace(Key j, Args &&... args)
Definition: VectorValues.h:185
#define gttic(label)
Definition: timing.h:295
Factor Graph Values.
Eigen::VectorXd Vector
Definition: Vector.h:38
Values result
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
const constBVector getb() const
DenseIndex getDim(const_iterator variable) const override
Conditional Gaussian Base class.
VectorValues optimizeBayesTree(const BAYESTREE &bayesTree)
OptimizeData operator()(const std::shared_ptr< CLIQUE > &clique, OptimizeData &parentData)
Values::const_iterator const_iterator
Const iterator over vector values.
Definition: VectorValues.h:82
FastMap< Key, VectorValues::const_iterator > cliqueResults
traits
Definition: chartTesting.h:28
const KeyVector & keys() const
Access the factor&#39;s involved variable keys.
Definition: Factor.h:142
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:82
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:33