Working on converting discrete
parent
be0b27a003
commit
667facdbce
|
|
@ -45,14 +45,14 @@ namespace gtsam {
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
bool DecisionTreeFactor::equals(const This& other, double tol) const {
|
bool DecisionTreeFactor::equals(const This& other, double tol) const {
|
||||||
return IndexFactorOrdered::equals(other, tol) && Potentials::equals(other, tol);
|
return Factor::equals(other, tol) && Potentials::equals(other, tol);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
void DecisionTreeFactor::print(const string& s,
|
void DecisionTreeFactor::print(const string& s,
|
||||||
const IndexFormatter& formatter) const {
|
const IndexFormatter& formatter) const {
|
||||||
cout << s;
|
cout << s;
|
||||||
IndexFactorOrdered::print("IndexFactor:",formatter);
|
Base::print("Factor:",formatter);
|
||||||
Potentials::print("Potentials:",formatter);
|
Potentials::print("Potentials:",formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,26 +128,6 @@ namespace gtsam {
|
||||||
*/
|
*/
|
||||||
shared_ptr combine(size_t nrFrontals, ADT::Binary op) const;
|
shared_ptr combine(size_t nrFrontals, ADT::Binary op) const;
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Permutes the keys in Potentials and DiscreteFactor
|
|
||||||
*
|
|
||||||
* This re-implements the permuteWithInverse() in both Potentials
|
|
||||||
* and DiscreteFactor by doing both of them together.
|
|
||||||
*/
|
|
||||||
|
|
||||||
void permuteWithInverse(const Permutation& inversePermutation){
|
|
||||||
DiscreteFactor::permuteWithInverse(inversePermutation);
|
|
||||||
Potentials::permuteWithInverse(inversePermutation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply a reduction, which is a remapping of variable indices.
|
|
||||||
*/
|
|
||||||
virtual void reduceWithInverse(const internal::Reduction& inverseReduction) {
|
|
||||||
DiscreteFactor::reduceWithInverse(inverseReduction);
|
|
||||||
Potentials::reduceWithInverse(inverseReduction);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
};
|
};
|
||||||
// DecisionTreeFactor
|
// DecisionTreeFactor
|
||||||
|
|
|
||||||
|
|
@ -18,47 +18,41 @@
|
||||||
|
|
||||||
#include <gtsam/discrete/DiscreteBayesNet.h>
|
#include <gtsam/discrete/DiscreteBayesNet.h>
|
||||||
#include <gtsam/discrete/DiscreteConditional.h>
|
#include <gtsam/discrete/DiscreteConditional.h>
|
||||||
#include <gtsam/inference/BayesNetOrdered-inl.h>
|
#include <gtsam/inference/FactorGraph-inst.h>
|
||||||
|
|
||||||
#include <boost/make_shared.hpp>
|
#include <boost/make_shared.hpp>
|
||||||
|
#include <boost/foreach.hpp>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
// Explicitly instantiate so we don't have to include everywhere
|
|
||||||
template class BayesNetOrdered<DiscreteConditional> ;
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
void add_front(DiscreteBayesNet& bayesNet, const Signature& s) {
|
void DiscreteBayesNet::add(const Signature& s) {
|
||||||
bayesNet.push_front(boost::make_shared<DiscreteConditional>(s));
|
push_back(boost::make_shared<DiscreteConditional>(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
void add(DiscreteBayesNet& bayesNet, const Signature& s) {
|
double DiscreteBayesNet::evaluate(const DiscreteConditional::Values & values) {
|
||||||
bayesNet.push_back(boost::make_shared<DiscreteConditional>(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
double evaluate(const DiscreteBayesNet& bn, const DiscreteConditional::Values & values) {
|
|
||||||
// evaluate all conditionals and multiply
|
// evaluate all conditionals and multiply
|
||||||
double result = 1.0;
|
double result = 1.0;
|
||||||
BOOST_FOREACH(DiscreteConditional::shared_ptr conditional, bn)
|
BOOST_FOREACH(DiscreteConditional::shared_ptr conditional, *this)
|
||||||
result *= (*conditional)(values);
|
result *= (*conditional)(values);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
DiscreteFactor::sharedValues optimize(const DiscreteBayesNet& bn) {
|
DiscreteFactor::sharedValues DiscreteBayesNet::optimize() {
|
||||||
// solve each node in turn in topological sort order (parents first)
|
// solve each node in turn in topological sort order (parents first)
|
||||||
DiscreteFactor::sharedValues result(new DiscreteFactor::Values());
|
DiscreteFactor::sharedValues result(new DiscreteFactor::Values());
|
||||||
BOOST_REVERSE_FOREACH (DiscreteConditional::shared_ptr conditional, bn)
|
BOOST_REVERSE_FOREACH (DiscreteConditional::shared_ptr conditional, *this)
|
||||||
conditional->solveInPlace(*result);
|
conditional->solveInPlace(*result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
DiscreteFactor::sharedValues sample(const DiscreteBayesNet& bn) {
|
DiscreteFactor::sharedValues DiscreteBayesNet::sample() {
|
||||||
// sample each node in turn in topological sort order (parents first)
|
// sample each node in turn in topological sort order (parents first)
|
||||||
DiscreteFactor::sharedValues result(new DiscreteFactor::Values());
|
DiscreteFactor::sharedValues result(new DiscreteFactor::Values());
|
||||||
BOOST_REVERSE_FOREACH(DiscreteConditional::shared_ptr conditional, bn)
|
BOOST_REVERSE_FOREACH(DiscreteConditional::shared_ptr conditional, *this)
|
||||||
conditional->sampleInPlace(*result);
|
conditional->sampleInPlace(*result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,30 +17,50 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <gtsam/discrete/DiscreteConditional.h>
|
||||||
|
#include <gtsam/inference/FactorGraph.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <gtsam/inference/BayesNetOrdered.h>
|
|
||||||
#include <gtsam/discrete/DiscreteConditional.h>
|
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
typedef BayesNetOrdered<DiscreteConditional> DiscreteBayesNet;
|
class GTSAM_EXPORT DiscreteBayesNet : public FactorGraph<DiscreteConditional>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// @name Standard Constructors
|
||||||
|
/// @{
|
||||||
|
|
||||||
|
/** Construct empty factor graph */
|
||||||
|
DiscreteBayesNet() {}
|
||||||
|
|
||||||
|
/** Construct from iterator over conditionals */
|
||||||
|
template<typename ITERATOR>
|
||||||
|
DiscreteBayesNet(ITERATOR firstConditional, ITERATOR lastConditional) : Base(firstConditional, lastConditional) {}
|
||||||
|
|
||||||
|
/** Construct from container of factors (shared_ptr or plain objects) */
|
||||||
|
template<class CONTAINER>
|
||||||
|
explicit DiscreteBayesNet(const CONTAINER& conditionals) : Base(conditionals) {}
|
||||||
|
|
||||||
|
/** Implicit copy/downcast constructor to override explicit template container constructor */
|
||||||
|
template<class DERIVEDCONDITIONAL>
|
||||||
|
DiscreteBayesNet(const FactorGraph<DERIVEDCONDITIONAL>& graph) : Base(graph) {}
|
||||||
|
|
||||||
|
/// @}
|
||||||
|
|
||||||
/** Add a DiscreteCondtional */
|
/** Add a DiscreteCondtional */
|
||||||
GTSAM_EXPORT void add(DiscreteBayesNet&, const Signature& s);
|
void add(const Signature& s);
|
||||||
|
|
||||||
/** Add a DiscreteCondtional in front, when listing parents first*/
|
//** evaluate for given Values */
|
||||||
GTSAM_EXPORT void add_front(DiscreteBayesNet&, const Signature& s);
|
double evaluate(const DiscreteConditional::Values& values);
|
||||||
|
|
||||||
//** evaluate for given Values */
|
/** Optimize function for back-substitution. */
|
||||||
GTSAM_EXPORT double evaluate(const DiscreteBayesNet& bn, const DiscreteConditional::Values & values);
|
DiscreteFactor::sharedValues optimize();
|
||||||
|
|
||||||
/** Optimize function for back-substitution. */
|
/** Do ancestral sampling */
|
||||||
GTSAM_EXPORT DiscreteFactor::sharedValues optimize(const DiscreteBayesNet& bn);
|
DiscreteFactor::sharedValues sample();
|
||||||
|
};
|
||||||
/** Do ancestral sampling */
|
|
||||||
GTSAM_EXPORT DiscreteFactor::sharedValues sample(const DiscreteBayesNet& bn);
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,38 +35,34 @@ using namespace std;
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ******************************************************************************** */
|
||||||
DiscreteConditional::DiscreteConditional(const size_t nrFrontals,
|
DiscreteConditional::DiscreteConditional(const size_t nrFrontals, const DecisionTreeFactor& f) :
|
||||||
const DecisionTreeFactor& f) :
|
BaseFactor(f / (*f.sum(nrFrontals))), BaseConditional(nrFrontals) {}
|
||||||
IndexConditionalOrdered(f.keys(), nrFrontals), Potentials(
|
|
||||||
f / (*f.sum(nrFrontals))) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ******************************************************************************** */
|
||||||
DiscreteConditional::DiscreteConditional(const DecisionTreeFactor& joint,
|
DiscreteConditional::DiscreteConditional(const DecisionTreeFactor& joint, const DecisionTreeFactor& marginal) :
|
||||||
const DecisionTreeFactor& marginal) :
|
BaseFactor(ISDEBUG("DiscreteConditional::COUNT") ? joint : joint / marginal),
|
||||||
IndexConditionalOrdered(joint.keys(), joint.size() - marginal.size()), Potentials(
|
BaseConditional(joint.size() - marginal.size())
|
||||||
ISDEBUG("DiscreteConditional::COUNT") ? joint : joint / marginal) {
|
{
|
||||||
if (ISDEBUG("DiscreteConditional::DiscreteConditional"))
|
if (ISDEBUG("DiscreteConditional::DiscreteConditional"))
|
||||||
cout << (firstFrontalKey()) << endl; //TODO Print all keys
|
cout << (firstFrontalKey()) << endl; //TODO Print all keys
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ******************************************************************************** */
|
||||||
DiscreteConditional::DiscreteConditional(const Signature& signature) :
|
DiscreteConditional::DiscreteConditional(const Signature& signature) :
|
||||||
IndexConditionalOrdered(signature.indices(), 1), Potentials(
|
BaseFactor(signature.discreteKeysParentsFirst(), signature.cpt()),
|
||||||
signature.discreteKeysParentsFirst(), signature.cpt()) {
|
BaseConditional(1) {}
|
||||||
}
|
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ******************************************************************************** */
|
||||||
void DiscreteConditional::print(const std::string& s, const IndexFormatter& formatter) const {
|
void DiscreteConditional::print(const std::string& s, const KeyFormatter& formatter) const {
|
||||||
std::cout << s << std::endl;
|
BaseConditional::print(s, formatter);
|
||||||
IndexConditionalOrdered::print(s, formatter);
|
|
||||||
Potentials::print(s);
|
Potentials::print(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ******************************************************************************** */
|
||||||
bool DiscreteConditional::equals(const DiscreteConditional& other, double tol) const {
|
bool DiscreteConditional::equals(const DiscreteConditional& other, double tol) const {
|
||||||
return IndexConditionalOrdered::equals(other, tol)
|
return BaseFactor::equals(other, tol)
|
||||||
&& Potentials::equals(other, tol);
|
&& BaseConditional::equals(other, tol)
|
||||||
|
&& Potentials::equals(other, tol);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ******************************************************************************** */
|
||||||
|
|
@ -195,13 +191,6 @@ namespace gtsam {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
|
||||||
void DiscreteConditional::permuteWithInverse(const Permutation& inversePermutation){
|
|
||||||
IndexConditionalOrdered::permuteWithInverse(inversePermutation);
|
|
||||||
Potentials::permuteWithInverse(inversePermutation);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ******************************************************************************** */
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
#include <gtsam/discrete/DecisionTreeFactor.h>
|
#include <gtsam/discrete/DecisionTreeFactor.h>
|
||||||
#include <gtsam/discrete/Signature.h>
|
#include <gtsam/discrete/Signature.h>
|
||||||
#include <gtsam/inference/IndexConditionalOrdered.h>
|
#include <gtsam/inference/Conditional.h>
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <boost/make_shared.hpp>
|
#include <boost/make_shared.hpp>
|
||||||
|
|
||||||
|
|
@ -30,13 +30,16 @@ namespace gtsam {
|
||||||
* Discrete Conditional Density
|
* Discrete Conditional Density
|
||||||
* Derives from DecisionTreeFactor
|
* Derives from DecisionTreeFactor
|
||||||
*/
|
*/
|
||||||
class GTSAM_EXPORT DiscreteConditional: public IndexConditionalOrdered, public Potentials {
|
class GTSAM_EXPORT DiscreteConditional :
|
||||||
|
public DecisionTreeFactor,
|
||||||
|
public Conditional<DecisionTreeFactor, DiscreteConditional>
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
// typedefs needed to play nice with gtsam
|
// typedefs needed to play nice with gtsam
|
||||||
typedef DiscreteFactor FactorType;
|
typedef DiscreteConditional This;
|
||||||
typedef boost::shared_ptr<DiscreteConditional> shared_ptr;
|
typedef DecisionTreeFactor BaseFactor;
|
||||||
typedef IndexConditionalOrdered Base;
|
typedef Conditional<DecisionTreeFactor, DiscreteConditional> BaseConditional;
|
||||||
|
typedef boost::shared_ptr<This> shared_ptr;
|
||||||
|
|
||||||
/** A map from keys to values */
|
/** A map from keys to values */
|
||||||
typedef Assignment<Index> Values;
|
typedef Assignment<Index> Values;
|
||||||
|
|
@ -56,8 +59,7 @@ namespace gtsam {
|
||||||
DiscreteConditional(const Signature& signature);
|
DiscreteConditional(const Signature& signature);
|
||||||
|
|
||||||
/** construct P(X|Y)=P(X,Y)/P(Y) from P(X,Y) and P(Y) */
|
/** construct P(X|Y)=P(X,Y)/P(Y) from P(X,Y) and P(Y) */
|
||||||
DiscreteConditional(const DecisionTreeFactor& joint,
|
DiscreteConditional(const DecisionTreeFactor& joint, const DecisionTreeFactor& marginal);
|
||||||
const DecisionTreeFactor& marginal);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Combine several conditional into a single one.
|
* Combine several conditional into a single one.
|
||||||
|
|
@ -75,7 +77,7 @@ namespace gtsam {
|
||||||
|
|
||||||
/// GTSAM-style print
|
/// GTSAM-style print
|
||||||
void print(const std::string& s = "Discrete Conditional: ",
|
void print(const std::string& s = "Discrete Conditional: ",
|
||||||
const IndexFormatter& formatter = DefaultIndexFormatter) const;
|
const KeyFormatter& formatter = DefaultKeyFormatter) const;
|
||||||
|
|
||||||
/// GTSAM-style equals
|
/// GTSAM-style equals
|
||||||
bool equals(const DiscreteConditional& other, double tol = 1e-9) const;
|
bool equals(const DiscreteConditional& other, double tol = 1e-9) const;
|
||||||
|
|
@ -89,11 +91,6 @@ namespace gtsam {
|
||||||
return Potentials::operator()(values);
|
return Potentials::operator()(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convert to a factor */
|
|
||||||
DecisionTreeFactor::shared_ptr toFactor() const {
|
|
||||||
return DecisionTreeFactor::shared_ptr(new DecisionTreeFactor(*this));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Restrict to given parent values, returns AlgebraicDecisionDiagram */
|
/** Restrict to given parent values, returns AlgebraicDecisionDiagram */
|
||||||
ADT choose(const Assignment<Index>& parentsValues) const;
|
ADT choose(const Assignment<Index>& parentsValues) const;
|
||||||
|
|
||||||
|
|
@ -121,11 +118,6 @@ namespace gtsam {
|
||||||
/// sample in place, stores result in partial solution
|
/// sample in place, stores result in partial solution
|
||||||
void sampleInPlace(Values& parentsValues) const;
|
void sampleInPlace(Values& parentsValues) const;
|
||||||
|
|
||||||
/**
|
|
||||||
* Permutes both IndexConditional and Potentials.
|
|
||||||
*/
|
|
||||||
void permuteWithInverse(const Permutation& inversePermutation);
|
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <gtsam/discrete/Assignment.h>
|
#include <gtsam/discrete/Assignment.h>
|
||||||
#include <gtsam/inference/IndexFactorOrdered.h>
|
#include <gtsam/inference/Factor.h>
|
||||||
|
|
||||||
|
#include <boost/assign/list_of.hpp>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
|
|
@ -30,12 +32,13 @@ namespace gtsam {
|
||||||
* Base class for discrete probabilistic factors
|
* Base class for discrete probabilistic factors
|
||||||
* The most general one is the derived DecisionTreeFactor
|
* The most general one is the derived DecisionTreeFactor
|
||||||
*/
|
*/
|
||||||
class GTSAM_EXPORT DiscreteFactor: public IndexFactorOrdered {
|
class GTSAM_EXPORT DiscreteFactor : public Factor {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// typedefs needed to play nice with gtsam
|
// typedefs needed to play nice with gtsam
|
||||||
typedef DiscreteFactor This;
|
typedef DiscreteFactor This;
|
||||||
|
typedef Factor Base;
|
||||||
typedef DiscreteConditional ConditionalType;
|
typedef DiscreteConditional ConditionalType;
|
||||||
typedef boost::shared_ptr<DiscreteFactor> shared_ptr;
|
typedef boost::shared_ptr<DiscreteFactor> shared_ptr;
|
||||||
|
|
||||||
|
|
@ -47,23 +50,23 @@ namespace gtsam {
|
||||||
|
|
||||||
/// Construct n-way factor
|
/// Construct n-way factor
|
||||||
DiscreteFactor(const std::vector<Index>& js) :
|
DiscreteFactor(const std::vector<Index>& js) :
|
||||||
IndexFactorOrdered(js) {
|
Base(js) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct unary factor
|
/// Construct unary factor
|
||||||
DiscreteFactor(Index j) :
|
DiscreteFactor(Index j) :
|
||||||
IndexFactorOrdered(j) {
|
Base(boost::assign::cref_list_of<1>(j)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct binary factor
|
/// Construct binary factor
|
||||||
DiscreteFactor(Index j1, Index j2) :
|
DiscreteFactor(Index j1, Index j2) :
|
||||||
IndexFactorOrdered(j1, j2) {
|
Base(boost::assign::cref_list_of<2>(j1)(j2)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// construct from container
|
/// construct from container
|
||||||
template<class KeyIterator>
|
template<class KeyIterator>
|
||||||
DiscreteFactor(KeyIterator beginKey, KeyIterator endKey) :
|
DiscreteFactor(KeyIterator beginKey, KeyIterator endKey) :
|
||||||
IndexFactorOrdered(beginKey, endKey) {
|
Base(beginKey, endKey) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -83,9 +86,8 @@ namespace gtsam {
|
||||||
|
|
||||||
// print
|
// print
|
||||||
virtual void print(const std::string& s = "DiscreteFactor\n",
|
virtual void print(const std::string& s = "DiscreteFactor\n",
|
||||||
const IndexFormatter& formatter
|
const KeyFormatter& formatter = DefaultKeyFormatter) const {
|
||||||
=DefaultIndexFormatter) const {
|
Base::print(s,formatter);
|
||||||
IndexFactorOrdered::print(s,formatter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
@ -100,21 +102,6 @@ namespace gtsam {
|
||||||
|
|
||||||
virtual DecisionTreeFactor toDecisionTreeFactor() const = 0;
|
virtual DecisionTreeFactor toDecisionTreeFactor() const = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* Permutes the factor, but for efficiency requires the permutation
|
|
||||||
* to already be inverted.
|
|
||||||
*/
|
|
||||||
virtual void permuteWithInverse(const Permutation& inversePermutation){
|
|
||||||
IndexFactorOrdered::permuteWithInverse(inversePermutation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply a reduction, which is a remapping of variable indices.
|
|
||||||
*/
|
|
||||||
virtual void reduceWithInverse(const internal::Reduction& inverseReduction) {
|
|
||||||
IndexFactorOrdered::reduceWithInverse(inverseReduction);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
};
|
};
|
||||||
// DiscreteFactor
|
// DiscreteFactor
|
||||||
|
|
|
||||||
|
|
@ -16,28 +16,15 @@
|
||||||
* @author Frank Dellaert
|
* @author Frank Dellaert
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//#define ENABLE_TIMING
|
#include <gtsam/base/timing.h>
|
||||||
#include <gtsam/discrete/DiscreteFactorGraph.h>
|
#include <gtsam/discrete/DiscreteFactorGraph.h>
|
||||||
#include <gtsam/discrete/DiscreteConditional.h>
|
#include <gtsam/discrete/DiscreteConditional.h>
|
||||||
#include <gtsam/inference/EliminationTreeOrdered-inl.h>
|
#include <gtsam/inference/FactorGraph-inst.h>
|
||||||
|
#include <gtsam/inference/Ordering.h>
|
||||||
#include <boost/make_shared.hpp>
|
#include <boost/make_shared.hpp>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
// Explicitly instantiate so we don't have to include everywhere
|
|
||||||
template class FactorGraphOrdered<DiscreteFactor> ;
|
|
||||||
template class EliminationTreeOrdered<DiscreteFactor> ;
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
DiscreteFactorGraph::DiscreteFactorGraph() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
DiscreteFactorGraph::DiscreteFactorGraph(
|
|
||||||
const BayesNetOrdered<DiscreteConditional>& bayesNet) :
|
|
||||||
FactorGraphOrdered<DiscreteFactor>(bayesNet) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
FastSet<Index> DiscreteFactorGraph::keys() const {
|
FastSet<Index> DiscreteFactorGraph::keys() const {
|
||||||
FastSet<Index> keys;
|
FastSet<Index> keys;
|
||||||
|
|
@ -75,27 +62,9 @@ namespace gtsam {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
void DiscreteFactorGraph::permuteWithInverse(
|
|
||||||
const Permutation& inversePermutation) {
|
|
||||||
BOOST_FOREACH(const sharedFactor& factor, factors_) {
|
|
||||||
if(factor)
|
|
||||||
factor->permuteWithInverse(inversePermutation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
void DiscreteFactorGraph::reduceWithInverse(
|
|
||||||
const internal::Reduction& inverseReduction) {
|
|
||||||
BOOST_FOREACH(const sharedFactor& factor, factors_) {
|
|
||||||
if(factor)
|
|
||||||
factor->reduceWithInverse(inverseReduction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr> //
|
std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr> //
|
||||||
EliminateDiscrete(const FactorGraphOrdered<DiscreteFactor>& factors, size_t num) {
|
EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& keys) {
|
||||||
|
|
||||||
// PRODUCT: multiply all factors
|
// PRODUCT: multiply all factors
|
||||||
gttic(product);
|
gttic(product);
|
||||||
|
|
|
||||||
|
|
@ -20,31 +20,37 @@
|
||||||
|
|
||||||
#include <gtsam/discrete/DecisionTreeFactor.h>
|
#include <gtsam/discrete/DecisionTreeFactor.h>
|
||||||
#include <gtsam/discrete/DiscreteBayesNet.h>
|
#include <gtsam/discrete/DiscreteBayesNet.h>
|
||||||
#include <gtsam/inference/FactorGraphOrdered.h>
|
#include <gtsam/inference/FactorGraph.h>
|
||||||
#include <gtsam/base/FastSet.h>
|
#include <gtsam/base/FastSet.h>
|
||||||
#include <boost/make_shared.hpp>
|
#include <boost/make_shared.hpp>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
class DiscreteFactorGraph: public FactorGraphOrdered<DiscreteFactor> {
|
// Forward declarations
|
||||||
|
class Ordering;
|
||||||
|
|
||||||
|
class GTSAM_EXPORT DiscreteFactorGraph: public FactorGraph<DiscreteFactor> {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/** A map from keys to values */
|
/** A map from keys to values */
|
||||||
typedef std::vector<Index> Indices;
|
typedef std::vector<Index> Indices;
|
||||||
typedef Assignment<Index> Values;
|
typedef Assignment<Index> Values;
|
||||||
typedef boost::shared_ptr<Values> sharedValues;
|
typedef boost::shared_ptr<Values> sharedValues;
|
||||||
|
|
||||||
/** Construct empty factor graph */
|
/** Default constructor */
|
||||||
GTSAM_EXPORT DiscreteFactorGraph();
|
DiscreteFactorGraph() {}
|
||||||
|
|
||||||
/** Constructor from a factor graph of GaussianFactor or a derived type */
|
/** Construct from iterator over factors */
|
||||||
template<class DERIVEDFACTOR>
|
template<typename ITERATOR>
|
||||||
DiscreteFactorGraph(const FactorGraphOrdered<DERIVEDFACTOR>& fg) {
|
DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) : Base(firstFactor, lastFactor) {}
|
||||||
push_back(fg);
|
|
||||||
}
|
/** Construct from container of factors (shared_ptr or plain objects) */
|
||||||
|
template<class CONTAINER>
|
||||||
/** construct from a BayesNet */
|
explicit DiscreteFactorGraph(const CONTAINER& factors) : Base(factors) {}
|
||||||
GTSAM_EXPORT DiscreteFactorGraph(const BayesNetOrdered<DiscreteConditional>& bayesNet);
|
|
||||||
|
/** Implicit copy/downcast constructor to override explicit template container constructor */
|
||||||
|
template<class DERIVEDFACTOR>
|
||||||
|
DiscreteFactorGraph(const FactorGraph<DERIVEDFACTOR>& graph) : Base(graph) {}
|
||||||
|
|
||||||
template<class SOURCE>
|
template<class SOURCE>
|
||||||
void add(const DiscreteKey& j, SOURCE table) {
|
void add(const DiscreteKey& j, SOURCE table) {
|
||||||
|
|
@ -68,29 +74,22 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return the set of variables involved in the factors (set union) */
|
/** Return the set of variables involved in the factors (set union) */
|
||||||
GTSAM_EXPORT FastSet<Index> keys() const;
|
FastSet<Index> keys() const;
|
||||||
|
|
||||||
/** return product of all factors as a single factor */
|
/** return product of all factors as a single factor */
|
||||||
GTSAM_EXPORT DecisionTreeFactor product() const;
|
DecisionTreeFactor product() const;
|
||||||
|
|
||||||
/** Evaluates the factor graph given values, returns the joint probability of the factor graph given specific instantiation of values*/
|
/** Evaluates the factor graph given values, returns the joint probability of the factor graph given specific instantiation of values*/
|
||||||
GTSAM_EXPORT double operator()(const DiscreteFactor::Values & values) const;
|
double operator()(const DiscreteFactor::Values & values) const;
|
||||||
|
|
||||||
/// print
|
/// print
|
||||||
GTSAM_EXPORT void print(const std::string& s = "DiscreteFactorGraph",
|
void print(const std::string& s = "DiscreteFactorGraph",
|
||||||
const IndexFormatter& formatter =DefaultIndexFormatter) const;
|
const KeyFormatter& formatter = DefaultKeyFormatter) const;
|
||||||
|
|
||||||
/** Permute the variables in the factors */
|
|
||||||
GTSAM_EXPORT void permuteWithInverse(const Permutation& inversePermutation);
|
|
||||||
|
|
||||||
/** Apply a reduction, which is a remapping of variable indices. */
|
|
||||||
GTSAM_EXPORT void reduceWithInverse(const internal::Reduction& inverseReduction);
|
|
||||||
};
|
};
|
||||||
// DiscreteFactorGraph
|
// DiscreteFactorGraph
|
||||||
|
|
||||||
/** Main elimination function for DiscreteFactorGraph */
|
/** Main elimination function for DiscreteFactorGraph */
|
||||||
GTSAM_EXPORT std::pair<boost::shared_ptr<DiscreteConditional>, DecisionTreeFactor::shared_ptr>
|
GTSAM_EXPORT std::pair<boost::shared_ptr<DiscreteConditional>, DecisionTreeFactor::shared_ptr>
|
||||||
EliminateDiscrete(const FactorGraphOrdered<DiscreteFactor>& factors,
|
EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& keys);
|
||||||
size_t nrFrontals = 1);
|
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
||||||
|
|
@ -61,38 +61,5 @@ namespace gtsam {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
template<class P>
|
|
||||||
void Potentials::remapIndices(const P& remapping) {
|
|
||||||
// Permute the _cardinalities (TODO: Inefficient Consider Improving)
|
|
||||||
DiscreteKeys keys;
|
|
||||||
map<Index, Index> ordering;
|
|
||||||
|
|
||||||
// Get the original keys from cardinalities_
|
|
||||||
BOOST_FOREACH(const DiscreteKey& key, cardinalities_)
|
|
||||||
keys & key;
|
|
||||||
|
|
||||||
// Perform Permutation
|
|
||||||
BOOST_FOREACH(DiscreteKey& key, keys) {
|
|
||||||
ordering[key.first] = remapping[key.first];
|
|
||||||
key.first = ordering[key.first];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Change *this
|
|
||||||
AlgebraicDecisionTree<Index> permuted((*this), ordering);
|
|
||||||
*this = permuted;
|
|
||||||
cardinalities_ = keys.cardinalities();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
void Potentials::permuteWithInverse(const Permutation& inversePermutation) {
|
|
||||||
remapIndices(inversePermutation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
void Potentials::reduceWithInverse(const internal::Reduction& inverseReduction) {
|
|
||||||
remapIndices(inverseReduction);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************* */
|
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
#include <gtsam/discrete/AlgebraicDecisionTree.h>
|
#include <gtsam/discrete/AlgebraicDecisionTree.h>
|
||||||
#include <gtsam/discrete/DiscreteKey.h>
|
#include <gtsam/discrete/DiscreteKey.h>
|
||||||
#include <gtsam/inference/PermutationOrdered.h>
|
|
||||||
|
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
@ -48,10 +47,6 @@ namespace gtsam {
|
||||||
// Safe division for probabilities
|
// Safe division for probabilities
|
||||||
GTSAM_EXPORT static double safe_div(const double& a, const double& b);
|
GTSAM_EXPORT static double safe_div(const double& a, const double& b);
|
||||||
|
|
||||||
// Apply either a permutation or a reduction
|
|
||||||
template<class P>
|
|
||||||
void remapIndices(const P& remapping);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/** Default constructor for I/O */
|
/** Default constructor for I/O */
|
||||||
|
|
@ -73,20 +68,6 @@ namespace gtsam {
|
||||||
|
|
||||||
size_t cardinality(Index j) const { return cardinalities_.at(j);}
|
size_t cardinality(Index j) const { return cardinalities_.at(j);}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Permutes the keys in Potentials
|
|
||||||
*
|
|
||||||
* This permutes the Indices and performs necessary re-ordering of ADD.
|
|
||||||
* This is virtual so that derived types e.g. DecisionTreeFactor can
|
|
||||||
* re-implement it.
|
|
||||||
*/
|
|
||||||
GTSAM_EXPORT virtual void permuteWithInverse(const Permutation& inversePermutation);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply a reduction, which is a remapping of variable indices.
|
|
||||||
*/
|
|
||||||
GTSAM_EXPORT virtual void reduceWithInverse(const internal::Reduction& inverseReduction);
|
|
||||||
|
|
||||||
}; // Potentials
|
}; // Potentials
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue