Better set calculations

release/4.3a0
Frank Dellaert 2012-09-16 13:28:50 +00:00
parent 9094fe2744
commit 16c8cfb1cf
2 changed files with 23 additions and 25 deletions

View File

@ -809,10 +809,10 @@
</target> </target>
<target name="testDiscreteBayesTree.run" path="build/gtsam/discrete" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testDiscreteBayesTree.run" path="build/gtsam/discrete" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments>-j5</buildArguments> <buildArguments>-j1</buildArguments>
<buildTarget>testDiscreteBayesTree.run</buildTarget> <buildTarget>testDiscreteBayesTree.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
<runAllBuilders>true</runAllBuilders> <runAllBuilders>true</runAllBuilders>
</target> </target>
<target name="testDiscreteFactorGraph.run" path="build/gtsam/discrete" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testDiscreteFactorGraph.run" path="build/gtsam/discrete" targetID="org.eclipse.cdt.build.MakeTargetBuilder">

View File

@ -36,6 +36,16 @@ class Clique: public BayesTreeCliqueBase<Clique, DiscreteConditional> {
protected: protected:
/// Calculate set S\B
vector<Index> separatorShortcutVariables(derived_ptr B) const {
sharedConditional p_F_S = this->conditional();
vector<Index> &indicesB = B->conditional()->keys();
vector<Index> S_setminus_B;
set_difference(p_F_S->beginParents(), p_F_S->endParents(), //
indicesB.begin(), indicesB.end(), back_inserter(S_setminus_B));
return S_setminus_B;
}
/** /**
* Determine variable indices to keep in recursive separator shortcut calculation * Determine variable indices to keep in recursive separator shortcut calculation
* The factor graph p_Cp_B has keys from the parent clique Cp and from B. * The factor graph p_Cp_B has keys from the parent clique Cp and from B.
@ -44,23 +54,18 @@ protected:
vector<Index> indices(derived_ptr B, vector<Index> indices(derived_ptr B,
const FactorGraph<FactorType>& p_Cp_B) const { const FactorGraph<FactorType>& p_Cp_B) const {
// Get all keys
set<Index> allKeys = p_Cp_B.keys();
// We do this by first merging S and B // We do this by first merging S and B
boost::iterator_range<FactorType::iterator> indicesS = sharedConditional p_F_S = this->conditional();
this->conditional()->parents();
size_t sizeS = indicesS.end() - indicesS.begin();
vector<Index> &indicesB = B->conditional()->keys(); vector<Index> &indicesB = B->conditional()->keys();
vector<Index> S_union_B(indicesB.size() + sizeS); vector<Index> S_union_B;
vector<Index>::iterator it = set_union(indicesS.begin(), indicesS.end(), set_union(p_F_S->beginParents(), p_F_S->endParents(), //
indicesB.begin(), indicesB.end(), S_union_B.begin()); indicesB.begin(), indicesB.end(), back_inserter(S_union_B));
// then intersecting S_union_B with allKeys // then intersecting S_union_B with all keys in p_Cp_B
vector<Index> keepers(indicesB.size() + sizeS); set<Index> allKeys = p_Cp_B.keys();
it = set_intersection(S_union_B.begin(), it, allKeys.begin(), allKeys.end(), vector<Index> keepers;
keepers.begin()); set_intersection(S_union_B.begin(), S_union_B.end(), //
keepers.erase(it, keepers.end()); allKeys.begin(), allKeys.end(), back_inserter(keepers));
return keepers; return keepers;
} }
@ -153,16 +158,9 @@ public:
FactorGraph<FactorType>::shared_ptr fg = separatorShortcut(B); FactorGraph<FactorType>::shared_ptr fg = separatorShortcut(B);
if (fg) { if (fg) {
// calculate set S\B of indices to keep in Bayes net // calculate set S\B of indices to keep in Bayes net
vector<Index> indicesS(this->conditional()->beginParents(), vector<Index> S_setminus_B = separatorShortcutVariables(B);
this->conditional()->endParents());
// now get B indices out
vector<Index> &indicesB = B->conditional()->keys();
vector<Index> S_setminus_B(indicesS.size());
vector<Index>::iterator it = set_difference(indicesS.begin(),
indicesS.end(), indicesB.begin(), indicesB.end(),
S_setminus_B.begin());
S_setminus_B.erase(it, S_setminus_B.end());
set<Index> keep(S_setminus_B.begin(), S_setminus_B.end()); set<Index> keep(S_setminus_B.begin(), S_setminus_B.end());
BOOST_FOREACH (FactorType::shared_ptr factor,*fg) { BOOST_FOREACH (FactorType::shared_ptr factor,*fg) {
DecisionTreeFactor::shared_ptr df = boost::dynamic_pointer_cast< DecisionTreeFactor::shared_ptr df = boost::dynamic_pointer_cast<
DecisionTreeFactor>(factor); DecisionTreeFactor>(factor);