remove constructors that need parents

release/4.3a0
Varun Agrawal 2025-01-04 05:16:21 -05:00
parent bc449c1a45
commit bd30bef1a3
2 changed files with 12 additions and 60 deletions

View File

@ -38,16 +38,15 @@ using std::vector;
namespace gtsam { namespace gtsam {
/* ************************************************************************** */ /* ************************************************************************** */
TableDistribution::TableDistribution(const size_t nrFrontals, TableDistribution::TableDistribution(const TableFactor& f)
const TableFactor& f) : BaseConditional(f.keys().size(),
: BaseConditional(nrFrontals, DecisionTreeFactor(f.discreteKeys(), ADT())), DecisionTreeFactor(f.discreteKeys(), ADT())),
table_(f / (*f.sum(nrFrontals))) {} table_(f / (*f.sum(f.keys().size()))) {}
/* ************************************************************************** */ /* ************************************************************************** */
TableDistribution::TableDistribution( TableDistribution::TableDistribution(
size_t nrFrontals, const DiscreteKeys& keys, const DiscreteKeys& keys, const Eigen::SparseVector<double>& potentials)
const Eigen::SparseVector<double>& potentials) : BaseConditional(keys.size(), keys, DecisionTreeFactor(keys, ADT())),
: BaseConditional(nrFrontals, keys, DecisionTreeFactor(keys, ADT())),
table_(TableFactor(keys, potentials)) {} table_(TableFactor(keys, potentials)) {}
/* ************************************************************************** */ /* ************************************************************************** */
@ -66,11 +65,6 @@ TableDistribution::TableDistribution(const TableFactor& joint,
keys_.insert(keys_.end(), orderedKeys.begin(), orderedKeys.end()); keys_.insert(keys_.end(), orderedKeys.begin(), orderedKeys.end());
} }
/* ************************************************************************** */
TableDistribution::TableDistribution(const Signature& signature)
: BaseConditional(1, DecisionTreeFactor(DiscreteKeys{{1, 1}}, ADT(1))),
table_(TableFactor(signature.discreteKeys(), signature.cpt())) {}
/* ************************************************************************** */ /* ************************************************************************** */
void TableDistribution::print(const string& s, void TableDistribution::print(const string& s,
const KeyFormatter& formatter) const { const KeyFormatter& formatter) const {
@ -107,7 +101,7 @@ DiscreteConditional::shared_ptr TableDistribution::max(
const Ordering& keys) const { const Ordering& keys) const {
auto m = *table_.max(keys); auto m = *table_.max(keys);
return std::make_shared<TableDistribution>(m.discreteKeys().size(), m); return std::make_shared<TableDistribution>(m);
} }
/* ****************************************************************************/ /* ****************************************************************************/
@ -124,8 +118,8 @@ DiscreteConditional::shared_ptr TableDistribution::prune(
size_t maxNrAssignments) const { size_t maxNrAssignments) const {
TableFactor pruned = table_.prune(maxNrAssignments); TableFactor pruned = table_.prune(maxNrAssignments);
return std::make_shared<TableDistribution>( return std::make_shared<TableDistribution>(this->discreteKeys(),
this->nrFrontals(), this->discreteKeys(), pruned.sparseTable()); pruned.sparseTable());
} }
} // namespace gtsam } // namespace gtsam

View File

@ -58,58 +58,16 @@ class GTSAM_EXPORT TableDistribution : public DiscreteConditional {
/// Default constructor needed for serialization. /// Default constructor needed for serialization.
TableDistribution() {} TableDistribution() {}
/// Construct from factor, taking the first `nFrontals` keys as frontals. /// Construct from TableFactor.
TableDistribution(size_t nFrontals, const TableFactor& f); TableDistribution(const TableFactor& f);
/** /**
* Construct from DiscreteKeys and SparseVector, taking the first * Construct from DiscreteKeys and SparseVector, taking the first
* `nFrontals` keys as frontals, in the order given. * `nFrontals` keys as frontals, in the order given.
*/ */
TableDistribution(size_t nFrontals, const DiscreteKeys& keys, TableDistribution(const DiscreteKeys& keys,
const Eigen::SparseVector<double>& potentials); const Eigen::SparseVector<double>& potentials);
/** Construct from signature */
explicit TableDistribution(const Signature& signature);
/**
* Construct from key, parents, and a Signature::Table specifying the
* conditional probability table (CPT) in 00 01 10 11 order. For
* three-valued, it would be 00 01 02 10 11 12 20 21 22, etc....
*
* Example: TableDistribution P(D, {B,E}, table);
*/
TableDistribution(const DiscreteKey& key, const DiscreteKeys& parents,
const Signature::Table& table)
: TableDistribution(Signature(key, parents, table)) {}
/**
* Construct from key, parents, and a vector<double> specifying the
* conditional probability table (CPT) in 00 01 10 11 order. For
* three-valued, it would be 00 01 02 10 11 12 20 21 22, etc....
*
* Example: TableDistribution P(D, {B,E}, table);
*/
TableDistribution(const DiscreteKey& key, const DiscreteKeys& parents,
const std::vector<double>& table)
: TableDistribution(1, TableFactor(DiscreteKeys{key} & parents, table)) {}
/**
* Construct from key, parents, and a string specifying the conditional
* probability table (CPT) in 00 01 10 11 order. For three-valued, it would
* be 00 01 02 10 11 12 20 21 22, etc....
*
* The string is parsed into a Signature::Table.
*
* Example: TableDistribution P(D, {B,E}, "9/1 2/8 3/7 1/9");
*/
TableDistribution(const DiscreteKey& key, const DiscreteKeys& parents,
const std::string& spec)
: TableDistribution(Signature(key, parents, spec)) {}
/// No-parent specialization; can also use DiscreteDistribution.
TableDistribution(const DiscreteKey& key, const std::string& spec)
: TableDistribution(Signature(key, {}, spec)) {}
/** /**
* @brief construct P(X|Y) = f(X,Y)/f(Y) from f(X,Y) and f(Y) * @brief construct P(X|Y) = f(X,Y)/f(Y) from f(X,Y) and f(Y)
* Assumes but *does not check* that f(Y)=sum_X f(X,Y). * Assumes but *does not check* that f(Y)=sum_X f(X,Y).