Skip to content

Commit

Permalink
discrete error method that returns an ADT
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal committed Nov 11, 2023
1 parent 1121ece commit 4711f58
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 5 deletions.
16 changes: 16 additions & 0 deletions gtsam/discrete/DecisionTreeFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ namespace gtsam {
return error(values.discrete());
}

/* ************************************************************************ */
AlgebraicDecisionTree<Key> DecisionTreeFactor::error() const {
// Get all possible assignments
DiscreteKeys dkeys = discreteKeys();
// Reverse to make cartesian product output a more natural ordering.
DiscreteKeys rdkeys(dkeys.rbegin(), dkeys.rend());
const auto assignments = DiscreteValues::CartesianProduct(rdkeys);

// Construct vector with error values
std::vector<double> errors;
for (const auto& assignment : assignments) {
errors.push_back(error(assignment));
}
return AlgebraicDecisionTree<Key>(dkeys, errors);
}

/* ************************************************************************ */
double DecisionTreeFactor::safe_div(const double& a, const double& b) {
// The use for safe_div is when we divide the product factor by the sum
Expand Down
3 changes: 3 additions & 0 deletions gtsam/discrete/DecisionTreeFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ namespace gtsam {
*/
double error(const HybridValues& values) const override;

/// Compute error for each assignment and return as a tree
AlgebraicDecisionTree<Key> error() const override;

/// @}

private:
Expand Down
15 changes: 10 additions & 5 deletions gtsam/discrete/DiscreteFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

#pragma once

#include <gtsam/base/Testable.h>
#include <gtsam/discrete/AlgebraicDecisionTree.h>
#include <gtsam/discrete/DiscreteValues.h>
#include <gtsam/inference/Factor.h>
#include <gtsam/base/Testable.h>

#include <string>
namespace gtsam {
Expand All @@ -35,7 +36,7 @@ class HybridValues;
*
* @ingroup discrete
*/
class GTSAM_EXPORT DiscreteFactor: public Factor {
class GTSAM_EXPORT DiscreteFactor : public Factor {
public:
// typedefs needed to play nice with gtsam
typedef DiscreteFactor This; ///< This class
Expand Down Expand Up @@ -103,15 +104,19 @@ class GTSAM_EXPORT DiscreteFactor: public Factor {
*/
double error(const HybridValues& c) const override;

/// Multiply in a DecisionTreeFactor and return the result as DecisionTreeFactor
/// Compute error for each assignment and return as a tree
virtual AlgebraicDecisionTree<Key> error() const = 0;

/// Multiply in a DecisionTreeFactor and return the result as
/// DecisionTreeFactor
virtual DecisionTreeFactor operator*(const DecisionTreeFactor&) const = 0;

virtual DecisionTreeFactor toDecisionTreeFactor() const = 0;

/// @}
/// @name Wrapper support
/// @{

/// Translation table from values to strings.
using Names = DiscreteValues::Names;

Expand Down Expand Up @@ -175,4 +180,4 @@ template<> struct traits<DiscreteFactor> : public Testable<DiscreteFactor> {};
std::vector<double> expNormalize(const std::vector<double> &logProbs);


}// namespace gtsam
} // namespace gtsam
5 changes: 5 additions & 0 deletions gtsam/discrete/TableFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ double TableFactor::error(const HybridValues& values) const {
return error(values.discrete());
}

/* ************************************************************************ */
AlgebraicDecisionTree<Key> TableFactor::error() const {
return toDecisionTreeFactor().error();
}

/* ************************************************************************ */
DecisionTreeFactor TableFactor::operator*(const DecisionTreeFactor& f) const {
return toDecisionTreeFactor() * f;
Expand Down
3 changes: 3 additions & 0 deletions gtsam/discrete/TableFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ class GTSAM_EXPORT TableFactor : public DiscreteFactor {
*/
double error(const HybridValues& values) const override;

/// Compute error for each assignment and return as a tree
AlgebraicDecisionTree<Key> error() const override;

/// @}
};

Expand Down
18 changes: 18 additions & 0 deletions gtsam/discrete/tests/testDecisionTreeFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ TEST( DecisionTreeFactor, constructors)
EXPECT_DOUBLES_EQUAL(0.8, f4(x121), 1e-9);
}

/* ************************************************************************* */
TEST(DecisionTreeFactor, Error) {
// Declare a bunch of keys
DiscreteKey X(0,2), Y(1,3), Z(2,2);

// Create factors
DecisionTreeFactor f(X & Y & Z, "2 5 3 6 4 7 25 55 35 65 45 75");

auto errors = f.error();
// regression
AlgebraicDecisionTree<Key> expected(
{X, Y, Z},
vector<double>{-0.69314718, -1.6094379, -1.0986123, -1.7917595,
-1.3862944, -1.9459101, -3.2188758, -4.0073332, -3.5553481,
-4.1743873, -3.8066625, -4.3174881});
EXPECT(assert_equal(expected, errors, 1e-6));
}

/* ************************************************************************* */
TEST(DecisionTreeFactor, multiplication) {
DiscreteKey v0(0, 2), v1(1, 2), v2(2, 2);
Expand Down
5 changes: 5 additions & 0 deletions gtsam_unstable/discrete/AllDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class GTSAM_UNSTABLE_EXPORT AllDiff : public Constraint {
/// Multiply into a decisiontree
DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;

/// Compute error for each assignment and return as a tree
AlgebraicDecisionTree<Key> error() const override {
throw std::runtime_error("AllDiff::error not implemented");
}

/*
* Ensure Arc-consistency by checking every possible value of domain j.
* @param j domain to be checked
Expand Down
5 changes: 5 additions & 0 deletions gtsam_unstable/discrete/BinaryAllDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ class BinaryAllDiff : public Constraint {
const Domains&) const override {
throw std::runtime_error("BinaryAllDiff::partiallyApply not implemented");
}

/// Compute error for each assignment and return as a tree
AlgebraicDecisionTree<Key> error() const override {
throw std::runtime_error("BinaryAllDiff::error not implemented");
}
};

} // namespace gtsam
5 changes: 5 additions & 0 deletions gtsam_unstable/discrete/Domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class GTSAM_UNSTABLE_EXPORT Domain : public Constraint {
}
}

/// Compute error for each assignment and return as a tree
AlgebraicDecisionTree<Key> error() const override {
throw std::runtime_error("Domain::error not implemented");
}

// Return concise string representation, mostly to debug arc consistency.
// Converts from base 0 to base1.
std::string base1Str() const;
Expand Down
5 changes: 5 additions & 0 deletions gtsam_unstable/discrete/SingleValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class GTSAM_UNSTABLE_EXPORT SingleValue : public Constraint {
}
}

/// Compute error for each assignment and return as a tree
AlgebraicDecisionTree<Key> error() const override {
throw std::runtime_error("SingleValue::error not implemented");
}

/// Calculate value
double operator()(const DiscreteValues& values) const override;

Expand Down

0 comments on commit 4711f58

Please sign in to comment.