Working on converting discrete

release/4.3a0
Richard Roberts 2013-08-02 22:09:45 +00:00
parent be0b27a003
commit 667facdbce
11 changed files with 112 additions and 234 deletions

View File

@ -45,14 +45,14 @@ namespace gtsam {
/* ************************************************************************* */
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,
const IndexFormatter& formatter) const {
cout << s;
IndexFactorOrdered::print("IndexFactor:",formatter);
Base::print("Factor:",formatter);
Potentials::print("Potentials:",formatter);
}

View File

@ -128,26 +128,6 @@ namespace gtsam {
*/
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

View File

@ -18,47 +18,41 @@
#include <gtsam/discrete/DiscreteBayesNet.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/foreach.hpp>
namespace gtsam {
// Explicitly instantiate so we don't have to include everywhere
template class BayesNetOrdered<DiscreteConditional> ;
/* ************************************************************************* */
void add_front(DiscreteBayesNet& bayesNet, const Signature& s) {
bayesNet.push_front(boost::make_shared<DiscreteConditional>(s));
void DiscreteBayesNet::add(const Signature& s) {
push_back(boost::make_shared<DiscreteConditional>(s));
}
/* ************************************************************************* */
void add(DiscreteBayesNet& bayesNet, const Signature& s) {
bayesNet.push_back(boost::make_shared<DiscreteConditional>(s));
}
/* ************************************************************************* */
double evaluate(const DiscreteBayesNet& bn, const DiscreteConditional::Values & values) {
double DiscreteBayesNet::evaluate(const DiscreteConditional::Values & values) {
// evaluate all conditionals and multiply
double result = 1.0;
BOOST_FOREACH(DiscreteConditional::shared_ptr conditional, bn)
BOOST_FOREACH(DiscreteConditional::shared_ptr conditional, *this)
result *= (*conditional)(values);
return result;
}
/* ************************************************************************* */
DiscreteFactor::sharedValues optimize(const DiscreteBayesNet& bn) {
DiscreteFactor::sharedValues DiscreteBayesNet::optimize() {
// solve each node in turn in topological sort order (parents first)
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);
return result;
}
/* ************************************************************************* */
DiscreteFactor::sharedValues sample(const DiscreteBayesNet& bn) {
DiscreteFactor::sharedValues DiscreteBayesNet::sample() {
// sample each node in turn in topological sort order (parents first)
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);
return result;
}

View File

@ -17,30 +17,50 @@
#pragma once
#include <gtsam/discrete/DiscreteConditional.h>
#include <gtsam/inference/FactorGraph.h>
#include <vector>
#include <map>
#include <boost/shared_ptr.hpp>
#include <gtsam/inference/BayesNetOrdered.h>
#include <gtsam/discrete/DiscreteConditional.h>
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 */
GTSAM_EXPORT void add(DiscreteBayesNet&, const Signature& s);
/** Add a DiscreteCondtional */
void add(const Signature& s);
/** Add a DiscreteCondtional in front, when listing parents first*/
GTSAM_EXPORT void add_front(DiscreteBayesNet&, const Signature& s);
//** evaluate for given Values */
double evaluate(const DiscreteConditional::Values& values);
//** evaluate for given Values */
GTSAM_EXPORT double evaluate(const DiscreteBayesNet& bn, const DiscreteConditional::Values & values);
/** Optimize function for back-substitution. */
DiscreteFactor::sharedValues optimize();
/** Optimize function for back-substitution. */
GTSAM_EXPORT DiscreteFactor::sharedValues optimize(const DiscreteBayesNet& bn);
/** Do ancestral sampling */
GTSAM_EXPORT DiscreteFactor::sharedValues sample(const DiscreteBayesNet& bn);
/** Do ancestral sampling */
DiscreteFactor::sharedValues sample();
};
} // namespace

View File

@ -35,38 +35,34 @@ using namespace std;
namespace gtsam {
/* ******************************************************************************** */
DiscreteConditional::DiscreteConditional(const size_t nrFrontals,
const DecisionTreeFactor& f) :
IndexConditionalOrdered(f.keys(), nrFrontals), Potentials(
f / (*f.sum(nrFrontals))) {
}
DiscreteConditional::DiscreteConditional(const size_t nrFrontals, const DecisionTreeFactor& f) :
BaseFactor(f / (*f.sum(nrFrontals))), BaseConditional(nrFrontals) {}
/* ******************************************************************************** */
DiscreteConditional::DiscreteConditional(const DecisionTreeFactor& joint,
const DecisionTreeFactor& marginal) :
IndexConditionalOrdered(joint.keys(), joint.size() - marginal.size()), Potentials(
ISDEBUG("DiscreteConditional::COUNT") ? joint : joint / marginal) {
DiscreteConditional::DiscreteConditional(const DecisionTreeFactor& joint, const DecisionTreeFactor& marginal) :
BaseFactor(ISDEBUG("DiscreteConditional::COUNT") ? joint : joint / marginal),
BaseConditional(joint.size() - marginal.size())
{
if (ISDEBUG("DiscreteConditional::DiscreteConditional"))
cout << (firstFrontalKey()) << endl; //TODO Print all keys
}
/* ******************************************************************************** */
DiscreteConditional::DiscreteConditional(const Signature& signature) :
IndexConditionalOrdered(signature.indices(), 1), Potentials(
signature.discreteKeysParentsFirst(), signature.cpt()) {
}
BaseFactor(signature.discreteKeysParentsFirst(), signature.cpt()),
BaseConditional(1) {}
/* ******************************************************************************** */
void DiscreteConditional::print(const std::string& s, const IndexFormatter& formatter) const {
std::cout << s << std::endl;
IndexConditionalOrdered::print(s, formatter);
void DiscreteConditional::print(const std::string& s, const KeyFormatter& formatter) const {
BaseConditional::print(s, formatter);
Potentials::print(s);
}
/* ******************************************************************************** */
bool DiscreteConditional::equals(const DiscreteConditional& other, double tol) const {
return IndexConditionalOrdered::equals(other, tol)
&& Potentials::equals(other, tol);
return BaseFactor::equals(other, tol)
&& BaseConditional::equals(other, tol)
&& Potentials::equals(other, tol);
}
/* ******************************************************************************** */
@ -195,13 +191,6 @@ namespace gtsam {
return 0;
}
/* ******************************************************************************** */
void DiscreteConditional::permuteWithInverse(const Permutation& inversePermutation){
IndexConditionalOrdered::permuteWithInverse(inversePermutation);
Potentials::permuteWithInverse(inversePermutation);
}
/* ******************************************************************************** */
} // namespace

View File

@ -20,7 +20,7 @@
#include <gtsam/discrete/DecisionTreeFactor.h>
#include <gtsam/discrete/Signature.h>
#include <gtsam/inference/IndexConditionalOrdered.h>
#include <gtsam/inference/Conditional.h>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
@ -30,13 +30,16 @@ namespace gtsam {
* Discrete Conditional Density
* Derives from DecisionTreeFactor
*/
class GTSAM_EXPORT DiscreteConditional: public IndexConditionalOrdered, public Potentials {
class GTSAM_EXPORT DiscreteConditional :
public DecisionTreeFactor,
public Conditional<DecisionTreeFactor, DiscreteConditional>
{
public:
// typedefs needed to play nice with gtsam
typedef DiscreteFactor FactorType;
typedef boost::shared_ptr<DiscreteConditional> shared_ptr;
typedef IndexConditionalOrdered Base;
typedef DiscreteConditional This;
typedef DecisionTreeFactor BaseFactor;
typedef Conditional<DecisionTreeFactor, DiscreteConditional> BaseConditional;
typedef boost::shared_ptr<This> shared_ptr;
/** A map from keys to values */
typedef Assignment<Index> Values;
@ -56,8 +59,7 @@ namespace gtsam {
DiscreteConditional(const Signature& signature);
/** construct P(X|Y)=P(X,Y)/P(Y) from P(X,Y) and P(Y) */
DiscreteConditional(const DecisionTreeFactor& joint,
const DecisionTreeFactor& marginal);
DiscreteConditional(const DecisionTreeFactor& joint, const DecisionTreeFactor& marginal);
/**
* Combine several conditional into a single one.
@ -75,7 +77,7 @@ namespace gtsam {
/// GTSAM-style print
void print(const std::string& s = "Discrete Conditional: ",
const IndexFormatter& formatter = DefaultIndexFormatter) const;
const KeyFormatter& formatter = DefaultKeyFormatter) const;
/// GTSAM-style equals
bool equals(const DiscreteConditional& other, double tol = 1e-9) const;
@ -89,11 +91,6 @@ namespace gtsam {
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 */
ADT choose(const Assignment<Index>& parentsValues) const;
@ -121,11 +118,6 @@ namespace gtsam {
/// sample in place, stores result in partial solution
void sampleInPlace(Values& parentsValues) const;
/**
* Permutes both IndexConditional and Potentials.
*/
void permuteWithInverse(const Permutation& inversePermutation);
/// @}
};

View File

@ -19,7 +19,9 @@
#pragma once
#include <gtsam/discrete/Assignment.h>
#include <gtsam/inference/IndexFactorOrdered.h>
#include <gtsam/inference/Factor.h>
#include <boost/assign/list_of.hpp>
namespace gtsam {
@ -30,12 +32,13 @@ namespace gtsam {
* Base class for discrete probabilistic factors
* The most general one is the derived DecisionTreeFactor
*/
class GTSAM_EXPORT DiscreteFactor: public IndexFactorOrdered {
class GTSAM_EXPORT DiscreteFactor : public Factor {
public:
// typedefs needed to play nice with gtsam
typedef DiscreteFactor This;
typedef Factor Base;
typedef DiscreteConditional ConditionalType;
typedef boost::shared_ptr<DiscreteFactor> shared_ptr;
@ -47,23 +50,23 @@ namespace gtsam {
/// Construct n-way factor
DiscreteFactor(const std::vector<Index>& js) :
IndexFactorOrdered(js) {
Base(js) {
}
/// Construct unary factor
DiscreteFactor(Index j) :
IndexFactorOrdered(j) {
Base(boost::assign::cref_list_of<1>(j)) {
}
/// Construct binary factor
DiscreteFactor(Index j1, Index j2) :
IndexFactorOrdered(j1, j2) {
Base(boost::assign::cref_list_of<2>(j1)(j2)) {
}
/// construct from container
template<class KeyIterator>
DiscreteFactor(KeyIterator beginKey, KeyIterator endKey) :
IndexFactorOrdered(beginKey, endKey) {
Base(beginKey, endKey) {
}
public:
@ -83,9 +86,8 @@ namespace gtsam {
// print
virtual void print(const std::string& s = "DiscreteFactor\n",
const IndexFormatter& formatter
=DefaultIndexFormatter) const {
IndexFactorOrdered::print(s,formatter);
const KeyFormatter& formatter = DefaultKeyFormatter) const {
Base::print(s,formatter);
}
/// @}
@ -100,21 +102,6 @@ namespace gtsam {
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

View File

@ -16,28 +16,15 @@
* @author Frank Dellaert
*/
//#define ENABLE_TIMING
#include <gtsam/base/timing.h>
#include <gtsam/discrete/DiscreteFactorGraph.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>
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> 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> //
EliminateDiscrete(const FactorGraphOrdered<DiscreteFactor>& factors, size_t num) {
EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& keys) {
// PRODUCT: multiply all factors
gttic(product);

View File

@ -20,31 +20,37 @@
#include <gtsam/discrete/DecisionTreeFactor.h>
#include <gtsam/discrete/DiscreteBayesNet.h>
#include <gtsam/inference/FactorGraphOrdered.h>
#include <gtsam/inference/FactorGraph.h>
#include <gtsam/base/FastSet.h>
#include <boost/make_shared.hpp>
namespace gtsam {
class DiscreteFactorGraph: public FactorGraphOrdered<DiscreteFactor> {
// Forward declarations
class Ordering;
class GTSAM_EXPORT DiscreteFactorGraph: public FactorGraph<DiscreteFactor> {
public:
/** A map from keys to values */
typedef std::vector<Index> Indices;
typedef Assignment<Index> Values;
typedef boost::shared_ptr<Values> sharedValues;
/** Construct empty factor graph */
GTSAM_EXPORT DiscreteFactorGraph();
/** Constructor from a factor graph of GaussianFactor or a derived type */
template<class DERIVEDFACTOR>
DiscreteFactorGraph(const FactorGraphOrdered<DERIVEDFACTOR>& fg) {
push_back(fg);
}
/** construct from a BayesNet */
GTSAM_EXPORT DiscreteFactorGraph(const BayesNetOrdered<DiscreteConditional>& bayesNet);
/** Default constructor */
DiscreteFactorGraph() {}
/** Construct from iterator over factors */
template<typename ITERATOR>
DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) : Base(firstFactor, lastFactor) {}
/** Construct from container of factors (shared_ptr or plain objects) */
template<class CONTAINER>
explicit DiscreteFactorGraph(const CONTAINER& factors) : Base(factors) {}
/** Implicit copy/downcast constructor to override explicit template container constructor */
template<class DERIVEDFACTOR>
DiscreteFactorGraph(const FactorGraph<DERIVEDFACTOR>& graph) : Base(graph) {}
template<class SOURCE>
void add(const DiscreteKey& j, SOURCE table) {
@ -68,29 +74,22 @@ public:
}
/** 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 */
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*/
GTSAM_EXPORT double operator()(const DiscreteFactor::Values & values) const;
double operator()(const DiscreteFactor::Values & values) const;
/// print
GTSAM_EXPORT void print(const std::string& s = "DiscreteFactorGraph",
const IndexFormatter& formatter =DefaultIndexFormatter) 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);
void print(const std::string& s = "DiscreteFactorGraph",
const KeyFormatter& formatter = DefaultKeyFormatter) const;
};
// DiscreteFactorGraph
/** Main elimination function for DiscreteFactorGraph */
GTSAM_EXPORT std::pair<boost::shared_ptr<DiscreteConditional>, DecisionTreeFactor::shared_ptr>
EliminateDiscrete(const FactorGraphOrdered<DiscreteFactor>& factors,
size_t nrFrontals = 1);
EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& keys);
} // namespace gtsam

View File

@ -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

View File

@ -19,7 +19,6 @@
#include <gtsam/discrete/AlgebraicDecisionTree.h>
#include <gtsam/discrete/DiscreteKey.h>
#include <gtsam/inference/PermutationOrdered.h>
#include <boost/shared_ptr.hpp>
#include <set>
@ -48,10 +47,6 @@ namespace gtsam {
// Safe division for probabilities
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:
/** Default constructor for I/O */
@ -73,20 +68,6 @@ namespace gtsam {
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
} // namespace gtsam