undo TableFactor changes
parent
cb3c35b81a
commit
3a78499d36
|
@ -56,45 +56,9 @@ TableFactor::TableFactor(const DiscreteKeys& dkeys,
|
||||||
sort(sorted_dkeys_.begin(), sorted_dkeys_.end());
|
sort(sorted_dkeys_.begin(), sorted_dkeys_.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************ */
|
|
||||||
TableFactor::TableFactor(const DiscreteKeys& dkeys,
|
|
||||||
const DecisionTree<Key, double>& dtree)
|
|
||||||
: TableFactor(dkeys, DecisionTreeFactor(dkeys, dtree)) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Compute the correct ordering of the leaves in the decision tree.
|
|
||||||
*
|
|
||||||
* This is done by first taking all the values which have modulo 0 value with
|
|
||||||
* the cardinality of the innermost key `n`, and we go up to modulo n.
|
|
||||||
*
|
|
||||||
* @param dt The DecisionTree
|
|
||||||
* @return std::vector<double>
|
|
||||||
*/
|
|
||||||
std::vector<double> ComputeLeafOrdering(const DiscreteKeys& dkeys,
|
|
||||||
const DecisionTreeFactor& dt) {
|
|
||||||
std::vector<double> probs = dt.probabilities();
|
|
||||||
std::vector<double> ordered;
|
|
||||||
|
|
||||||
size_t n = dkeys[0].second;
|
|
||||||
|
|
||||||
for (size_t k = 0; k < n; ++k) {
|
|
||||||
for (size_t idx = 0; idx < probs.size(); ++idx) {
|
|
||||||
if (idx % n == k) {
|
|
||||||
ordered.push_back(probs[idx]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ordered;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************ */
|
|
||||||
TableFactor::TableFactor(const DiscreteKeys& dkeys,
|
|
||||||
const DecisionTreeFactor& dtf)
|
|
||||||
: TableFactor(dkeys, ComputeLeafOrdering(dkeys, dtf)) {}
|
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
TableFactor::TableFactor(const DiscreteConditional& c)
|
TableFactor::TableFactor(const DiscreteConditional& c)
|
||||||
: TableFactor(c.discreteKeys(), c) {}
|
: TableFactor(c.discreteKeys(), c.probabilities()) {}
|
||||||
|
|
||||||
/* ************************************************************************ */
|
/* ************************************************************************ */
|
||||||
Eigen::SparseVector<double> TableFactor::Convert(
|
Eigen::SparseVector<double> TableFactor::Convert(
|
||||||
|
|
|
@ -144,12 +144,6 @@ class GTSAM_EXPORT TableFactor : public DiscreteFactor {
|
||||||
TableFactor(const DiscreteKey& key, const std::vector<double>& row)
|
TableFactor(const DiscreteKey& key, const std::vector<double>& row)
|
||||||
: TableFactor(DiscreteKeys{key}, row) {}
|
: TableFactor(DiscreteKeys{key}, row) {}
|
||||||
|
|
||||||
/// Constructor from DecisionTreeFactor
|
|
||||||
TableFactor(const DiscreteKeys& keys, const DecisionTreeFactor& dtf);
|
|
||||||
|
|
||||||
/// Constructor from DecisionTree<Key, double>/AlgebraicDecisionTree
|
|
||||||
TableFactor(const DiscreteKeys& keys, const DecisionTree<Key, double>& dtree);
|
|
||||||
|
|
||||||
/** Construct from a DiscreteConditional type */
|
/** Construct from a DiscreteConditional type */
|
||||||
explicit TableFactor(const DiscreteConditional& c);
|
explicit TableFactor(const DiscreteConditional& c);
|
||||||
|
|
||||||
|
@ -181,7 +175,7 @@ class GTSAM_EXPORT TableFactor : public DiscreteFactor {
|
||||||
/// Calculate error for DiscreteValues `x`, is -log(probability).
|
/// Calculate error for DiscreteValues `x`, is -log(probability).
|
||||||
double error(const DiscreteValues& values) const;
|
double error(const DiscreteValues& values) const;
|
||||||
|
|
||||||
/// multiply two TableFactors
|
/// multiple two TableFactors
|
||||||
TableFactor operator*(const TableFactor& f) const {
|
TableFactor operator*(const TableFactor& f) const {
|
||||||
return apply(f, Ring::mul);
|
return apply(f, Ring::mul);
|
||||||
};
|
};
|
||||||
|
|
|
@ -132,16 +132,6 @@ TEST(TableFactor, constructors) {
|
||||||
// Manually constructed via inspection and comparison to DecisionTreeFactor
|
// Manually constructed via inspection and comparison to DecisionTreeFactor
|
||||||
TableFactor expected(X & Y, "0.5 0.4 0.2 0.5 0.6 0.8");
|
TableFactor expected(X & Y, "0.5 0.4 0.2 0.5 0.6 0.8");
|
||||||
EXPECT(assert_equal(expected, f4));
|
EXPECT(assert_equal(expected, f4));
|
||||||
|
|
||||||
// Test for 9=3x3 values.
|
|
||||||
DiscreteKey V(0, 3), W(1, 3);
|
|
||||||
DiscreteConditional conditional5(V | W = "1/2/3 5/6/7 9/10/11");
|
|
||||||
TableFactor f5(conditional5);
|
|
||||||
// GTSAM_PRINT(f5);
|
|
||||||
TableFactor expected_f5(
|
|
||||||
X & Y,
|
|
||||||
"0.166667 0.277778 0.3 0.333333 0.333333 0.333333 0.5 0.388889 0.366667");
|
|
||||||
EXPECT(assert_equal(expected_f5, f5, 1e-6));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
|
|
Loading…
Reference in New Issue