Skip to content

Commit

Permalink
Merge pull request #1594 from borglab/ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal authored Jul 28, 2023
2 parents 19c3939 + b51ff74 commit 621ef2e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions gtsam/discrete/DiscreteConditional.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ class GTSAM_EXPORT DiscreteConditional
const Signature::Table& table)
: DiscreteConditional(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: DiscreteConditional P(D, {B,E}, table);
*/
DiscreteConditional(const DiscreteKey& key, const DiscreteKeys& parents,
const std::vector<double>& table)
: DiscreteConditional(1, DiscreteKeys{key} & parents,
ADT(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
Expand Down
6 changes: 6 additions & 0 deletions gtsam/discrete/DiscreteKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ namespace gtsam {
return *this;
}

/// Add multiple keys (non-const!)
DiscreteKeys& operator&(const DiscreteKeys& keys) {
this->insert(this->end(), keys.begin(), keys.end());
return *this;
}

/// Print the keys and cardinalities.
void print(const std::string& s = "",
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
Expand Down
5 changes: 5 additions & 0 deletions gtsam/discrete/tests/testDiscreteConditional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ TEST(DiscreteConditional, constructors) {
DiscreteConditional actual2(1, f2);
DecisionTreeFactor expected2 = f2 / *f2.sum(1);
EXPECT(assert_equal(expected2, static_cast<DecisionTreeFactor>(actual2)));

std::vector<double> probs{0.2, 0.5, 0.3, 0.6, 0.4, 0.7, 0.25, 0.55, 0.35, 0.65, 0.45, 0.75};
DiscreteConditional actual3(X, {Y, Z}, probs);
DecisionTreeFactor expected3 = f2;
EXPECT(assert_equal(expected3, static_cast<DecisionTreeFactor>(actual3)));
}

/* ************************************************************************* */
Expand Down
5 changes: 5 additions & 0 deletions gtsam/inference/tests/testOrdering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ TEST(Ordering, AppendVector) {

Ordering expected{X(0), X(1), X(2)};
EXPECT(assert_equal(expected, actual));

actual = Ordering();
Ordering addl{X(0), X(1), X(2)};
actual += addl;
EXPECT(assert_equal(expected, actual));
}

/* ************************************************************************* */
Expand Down
5 changes: 4 additions & 1 deletion gtsam/nonlinear/nonlinear.i
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ class NonlinearFactorGraph {
const gtsam::noiseModel::Base* noiseModel);

// NonlinearFactorGraph
void printErrors(const gtsam::Values& values) const;
void printErrors(const gtsam::Values& values,
const string& str = "NonlinearFactorGraph: ",
const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
double error(const gtsam::Values& values) const;
double probPrime(const gtsam::Values& values) const;
gtsam::Ordering orderingCOLAMD() const;
Expand Down

0 comments on commit 621ef2e

Please sign in to comment.