11#include <boost/make_shared.hpp>
13#include "partition/FindSeparator-inl.h"
14#include "OrderedSymbols.h"
15#include "NestedDissection.h"
17namespace gtsam {
namespace partition {
20 template <
class NLG,
class SubNLG,
class GenericGraph>
21 NestedDissection<NLG, SubNLG, GenericGraph>::NestedDissection(
22 const NLG& fg,
const Ordering& ordering,
const int numNodeStopPartition,
const int minNodesPerMap,
const bool verbose) :
23 fg_(fg), ordering_(ordering){
24 GenericUnaryGraph unaryFactors;
26 boost::tie(unaryFactors, gfg) = fg.createGenericGraph(ordering);
29 int numNodes = ordering.size();
30 int2symbol_.resize(numNodes);
31 Ordering::const_iterator it = ordering.begin(), itLast = ordering.end();
33 int2symbol_[it->second] = (it++)->first;
36 keys.reserve(numNodes);
37 for(
int i=0; i<ordering.size(); ++i)
40 WorkSpace workspace(numNodes);
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>
46 NestedDissection<NLG, SubNLG, GenericGraph>::NestedDissection(
47 const NLG& fg,
const Ordering& ordering,
const boost::shared_ptr<Cuts>& cuts,
const bool verbose) : fg_(fg), ordering_(ordering){
48 GenericUnaryGraph unaryFactors;
50 boost::tie(unaryFactors, gfg) = fg.createGenericGraph(ordering);
53 int numNodes = ordering.size();
54 int2symbol_.resize(numNodes);
55 Ordering::const_iterator it = ordering.begin(), itLast = ordering.end();
57 int2symbol_[it->second] = (it++)->first;
60 keys.reserve(numNodes);
61 for(
int i=0; i<ordering.size(); ++i)
64 WorkSpace workspace(numNodes);
65 root_ = recursivePartition(gfg, unaryFactors, keys,
vector<size_t>(), cuts, boost::shared_ptr<SubNLG>(), workspace, verbose);
69 template <
class NLG,
class SubNLG,
class GenericGraph>
70 boost::shared_ptr<SubNLG> NestedDissection<NLG, SubNLG, GenericGraph>::makeSubNLG(
72 OrderedSymbols frontalKeys;
73 for(
const size_t index: frontals)
74 frontalKeys.push_back(int2symbol_[index]);
76 UnorderedSymbols sepKeys;
77 for(
const size_t index: sep)
78 sepKeys.insert(int2symbol_[index]);
80 return boost::make_shared<SubNLG>(fg, frontalKeys, sepKeys, parent);
84 template <
class NLG,
class SubNLG,
class GenericGraph>
85 void NestedDissection<NLG, SubNLG, GenericGraph>::processFactor(
86 const typename GenericGraph::value_type& factor,
const std::vector<int>& partitionTable,
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>
122 void NestedDissection<NLG, SubNLG, GenericGraph>::partitionFactorsAndVariables(
123 const GenericGraph& fg,
const GenericUnaryGraph& unaryFactors,
const std::vector<size_t>& keys,
124 const std::vector<int>& partitionTable,
const int numSubmaps,
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);
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);
149 for(
const set<size_t>& childSep: childSeps_)
150 childSeps.push_back(
vector<size_t>(childSep.begin(), childSep.end()));
153 for(
const sharedGenericUnaryFactor& unaryFactor_: unaryFactors) {
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>
162 NLG NestedDissection<NLG, SubNLG, GenericGraph>::collectOriginalFactors(
163 const GenericGraph& gfg,
const GenericUnaryGraph& unaryFactors)
const {
165 typename GenericGraph::const_iterator it = gfg.begin(), itLast = gfg.end();
166 while(it!=itLast) sepFactors.push_back(fg_[(*it++)->index]);
167 for(
const sharedGenericUnaryFactor& unaryFactor_: unaryFactors)
168 sepFactors.push_back(fg_[unaryFactor_->index]);
173 template <
class NLG,
class SubNLG,
class GenericGraph>
174 boost::shared_ptr<SubNLG> NestedDissection<NLG, SubNLG, GenericGraph>::recursivePartition(
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) {
181 sepFactors = collectOriginalFactors(gfg, unaryFactors);
182 return makeSubNLG(sepFactors, frontals, sep, parent);
186 int numSubmaps = findSeparator(gfg, frontals, minNodesPerMap, workspace, verbose, int2symbol_, NLG::reduceGraph(),
187 NLG::minNrConstraintsPerCamera(),NLG::minNrConstraintsPerLandmark());
188 partition::PartitionTable& partitionTable = workspace.partitionTable;
189 if (numSubmaps == 0)
throw runtime_error(
"recursivePartition: get zero submap after ND!");
194 partitionFactorsAndVariables(gfg, unaryFactors, frontals, partitionTable, numSubmaps,
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>
218 boost::shared_ptr<SubNLG> NestedDissection<NLG, SubNLG, GenericGraph>::recursivePartition(
220 const boost::shared_ptr<Cuts>& cuts,
const boost::shared_ptr<SubNLG>& parent, WorkSpace& workspace,
const bool verbose)
const {
225 sepFactors = collectOriginalFactors(gfg, unaryFactors);
226 return makeSubNLG(sepFactors, frontals, sep, parent);
231 partition::PartitionTable& partitionTable = cuts->partitionTable;
236 partitionFactorsAndVariables(gfg, unaryFactors, frontals, partitionTable, numSubmaps,
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);
Global functions in a separate testing namespace.
Definition chartTesting.h:28