11 #include <boost/make_shared.hpp> 14 #include "OrderedSymbols.h" 17 namespace gtsam {
namespace partition {
20 template <
class NLG,
class SubNLG,
class GenericGraph>
22 const NLG& fg,
const Ordering&
ordering,
const int numNodeStopPartition,
const int minNodesPerMap,
const bool verbose) :
23 fg_(fg), ordering_(ordering){
26 boost::tie(unaryFactors, gfg) = fg.createGenericGraph(ordering);
29 int numNodes = ordering.size();
31 Ordering::const_iterator it = ordering.begin(), itLast = ordering.end();
36 keys.reserve(numNodes);
37 for(
int i=0;
i<ordering.size(); ++
i)
41 root_ =
recursivePartition(gfg, unaryFactors, keys, vector<size_t>(), numNodeStopPartition, minNodesPerMap, boost::shared_ptr<SubNLG>(), workspace, verbose);
45 template <
class NLG,
class SubNLG,
class GenericGraph>
50 boost::tie(unaryFactors, gfg) = fg.createGenericGraph(ordering);
53 int numNodes = ordering.size();
55 Ordering::const_iterator it = ordering.begin(), itLast = ordering.end();
60 keys.reserve(numNodes);
61 for(
int i=0;
i<ordering.size(); ++
i)
65 root_ =
recursivePartition(gfg, unaryFactors, keys, vector<size_t>(), cuts, boost::shared_ptr<SubNLG>(), workspace, verbose);
69 template <
class NLG,
class SubNLG,
class GenericGraph>
71 const NLG& fg,
const vector<size_t>& frontals,
const vector<size_t>&
sep,
const boost::shared_ptr<SubNLG>& parent)
const {
72 OrderedSymbols frontalKeys;
73 for(
const size_t index: frontals)
76 UnorderedSymbols sepKeys;
77 for(
const size_t index: sep)
80 return boost::make_shared<SubNLG>(fg, frontalKeys, sepKeys, parent);
84 template <
class NLG,
class SubNLG,
class GenericGraph>
86 const typename GenericGraph::value_type& factor,
const std::vector<int>& partitionTable,
87 vector<GenericGraph>& frontalFactors, NLG& sepFactors, vector<
set<size_t> >& childSeps,
88 typename SubNLG::Weeklinks& weeklinks)
const {
90 int partition1 = partitionTable[factor->key1.index];
91 int partition2 = partitionTable[factor->key2.index];
92 if (partition1 <= 0 && partition2 <= 0) {
93 sepFactors.push_back(
fg_[factor->index]);
95 else if (partition1 > 0 && partition2 > 0 && partition1 != partition2) {
96 weeklinks.push_back(
fg_[factor->index]);
98 else if (partition1 > 0 && partition2 > 0 && partition1 == partition2) {
99 frontalFactors[partition1 - 1].push_back(factor);
102 if (partition1 > 0 && partition2 <= 0) {
103 frontalFactors[partition1 - 1].push_back(factor);
104 childSeps[partition1 - 1].
insert(factor->key2.index);
105 }
else if (partition1 <= 0 && partition2 > 0) {
106 frontalFactors[partition2 - 1].push_back(factor);
107 childSeps[partition2 - 1].insert(factor->key1.index);
109 throw runtime_error(
"processFactor: unexpected entries in the partition table!");
121 template <
class NLG,
class SubNLG,
class GenericGraph>
124 const std::vector<int>& partitionTable,
const int numSubmaps,
125 vector<GenericGraph>& frontalFactors, vector<GenericUnaryGraph>& frontalUnaryFactors, NLG& sepFactors,
126 vector<vector<size_t> >& childFrontals, vector<vector<size_t> >& childSeps, vector<size_t>& localFrontals,
127 typename SubNLG::Weeklinks& weeklinks)
const {
131 childFrontals.resize(numSubmaps);
132 for(
const size_t key: keys){
133 partition = partitionTable[
key];
136 case 0: localFrontals.push_back(
key);
break;
137 default: childFrontals[partition-1].push_back(
key);
142 vector<set<size_t> > childSeps_;
143 childSeps_.resize(numSubmaps);
144 childSeps.reserve(numSubmaps);
145 frontalFactors.resize(numSubmaps);
146 frontalUnaryFactors.resize(numSubmaps);
147 for(
typename GenericGraph::value_type factor: fg)
148 processFactor(factor, partitionTable, frontalFactors, sepFactors, childSeps_, weeklinks);
150 childSeps.push_back(vector<size_t>(childSep.begin(), childSep.end()));
154 partition = partitionTable[unaryFactor_->key.index];
155 if (!partition) sepFactors.push_back(
fg_[unaryFactor_->index]);
156 else frontalUnaryFactors[partition-1].push_back(unaryFactor_);
161 template <
class NLG,
class SubNLG,
class GenericGraph>
165 typename GenericGraph::const_iterator it = gfg.begin(), itLast = gfg.end();
166 while(it!=itLast) sepFactors.push_back(
fg_[(*it++)->index]);
168 sepFactors.push_back(
fg_[unaryFactor_->index]);
173 template <
class NLG,
class SubNLG,
class GenericGraph>
175 const GenericGraph& gfg,
const GenericUnaryGraph& unaryFactors,
const vector<size_t>& frontals,
const vector<size_t>&
sep,
176 const int numNodeStopPartition,
const int minNodesPerMap,
const boost::shared_ptr<SubNLG>& parent,
WorkSpace& workspace,
const bool verbose)
const {
180 if (frontals.size() <= numNodeStopPartition || gfg.size() <= numNodeStopPartition) {
182 return makeSubNLG(sepFactors, frontals, sep, parent);
187 NLG::minNrConstraintsPerCamera(),NLG::minNrConstraintsPerLandmark());
189 if (numSubmaps == 0)
throw runtime_error(
"recursivePartition: get zero submap after ND!");
192 vector<GenericGraph> frontalFactors; vector<GenericUnaryGraph> frontalUnaryFactors;
typename SubNLG::Weeklinks weeklinks;
193 vector<size_t> localFrontals; vector<vector<size_t> > childFrontals, childSeps;
195 frontalFactors, frontalUnaryFactors, sepFactors, childFrontals, childSeps, localFrontals, weeklinks);
198 boost::shared_ptr<SubNLG> current =
makeSubNLG(sepFactors, localFrontals, sep, parent);
199 current->setWeeklinks(weeklinks);
202 for (
int i=0;
i<numSubmaps;
i++) {
203 checkSingularity(frontalFactors[
i], childFrontals[i], workspace, NLG::minNrConstraintsPerCamera(),NLG::minNrConstraintsPerLandmark());
207 for (
int i=0;
i<numSubmaps;
i++) {
208 boost::shared_ptr<SubNLG> child =
recursivePartition(frontalFactors[
i], frontalUnaryFactors[i], childFrontals[i], childSeps[i],
209 numNodeStopPartition, minNodesPerMap, current, workspace, verbose);
210 current->addChild(child);
217 template <
class NLG,
class SubNLG,
class GenericGraph>
219 const GenericGraph& gfg,
const GenericUnaryGraph& unaryFactors,
const vector<size_t>& frontals,
const vector<size_t>&
sep,
220 const boost::shared_ptr<Cuts>& cuts,
const boost::shared_ptr<SubNLG>& parent,
WorkSpace& workspace,
const bool verbose)
const {
226 return makeSubNLG(sepFactors, frontals, sep, parent);
234 vector<GenericGraph> frontalFactors; vector<GenericUnaryGraph> frontalUnaryFactors;
typename SubNLG::Weeklinks weeklinks;
235 vector<size_t> localFrontals; vector<vector<size_t> > childFrontals, childSeps;
237 frontalFactors, frontalUnaryFactors, sepFactors, childFrontals, childSeps, localFrontals, weeklinks);
240 boost::shared_ptr<SubNLG> current =
makeSubNLG(sepFactors, localFrontals, sep, parent);
241 current->setWeeklinks(weeklinks);
244 for (
int i=0;
i<2;
i++) {
245 boost::shared_ptr<SubNLG> child =
recursivePartition(frontalFactors[
i], frontalUnaryFactors[i], childFrontals[i], childSeps[i],
246 cuts->children.empty() ? boost::shared_ptr<Cuts>() : cuts->children[i], current, workspace, verbose);
247 current->addChild(child);
void checkSingularity(const GenericGraph3D &graph, const std::vector< size_t > &frontals, WorkSpace &workspace, const size_t minNrConstraintsPerCamera, const size_t minNrConstraintsPerLandmark)
static enum @843 ordering
NLG collectOriginalFactors(const GenericGraph &gfg, const GenericUnaryGraph &unaryFactors) const
std::vector< sharedGenericUnaryFactor > GenericUnaryGraph
sharedSubNLG recursivePartition(const GenericGraph &gfg, const GenericUnaryGraph &unaryFactors, const std::vector< size_t > &frontals, const std::vector< size_t > &sep, const int numNodeStopPartition, const int minNodesPerMap, const boost::shared_ptr< SubNLG > &parent, WorkSpace &workspace, const bool verbose) const
PartitionTable partitionTable
constexpr int first(int i)
Implementation details for constexpr functions.
void processFactor(const typename GenericGraph::value_type &factor, const std::vector< int > &partitionTable, std::vector< GenericGraph > &frontalFactors, NLG &sepFactors, std::vector< std::set< size_t > > &childSeps, typename SubNLG::Weeklinks &weeklinks) const
void insert(size_t index, T &&val) const
std::vector< int > PartitionTable
sharedSubNLG makeSubNLG(const NLG &fg, const std::vector< size_t > &frontals, const std::vector< size_t > &sep, const boost::shared_ptr< SubNLG > &parent) const
std::vector< Symbol > int2symbol_
boost::shared_ptr< GenericUnaryFactor > sharedGenericUnaryFactor
NestedDissection(const NLG &fg, const Ordering &ordering, const int numNodeStopPartition, const int minNodesPerMap, const bool verbose=false)
int findSeparator(const GenericGraph &graph, const std::vector< size_t > &keys, const int minNodesPerMap, WorkSpace &workspace, bool verbose, const boost::optional< std::vector< Symbol > > &int2symbol, const bool reduceGraph, const int minNrConstraintsPerCamera, const int minNrConstraintsPerLandmark)
void partitionFactorsAndVariables(const GenericGraph &fg, const GenericUnaryGraph &unaryFactors, const std::vector< size_t > &keys, const std::vector< int > &partitionTable, const int numSubmaps, std::vector< GenericGraph > &frontalFactors, vector< GenericUnaryGraph > &frontalUnaryFactors, NLG &sepFactors, std::vector< std::vector< size_t > > &childFrontals, std::vector< std::vector< size_t > > &childSeps, std::vector< size_t > &localFrontals, typename SubNLG::Weeklinks &weeklinks) const