Made cartesian product static, and added specialization in DiscreteValues. Added markdown and html methods for the latter.
parent
3ea5aed26e
commit
c1561dba02
|
@ -50,7 +50,8 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
// Print the UGM distribution
|
// Print the UGM distribution
|
||||||
cout << "\nUGM distribution:" << endl;
|
cout << "\nUGM distribution:" << endl;
|
||||||
auto allPosbValues = cartesianProduct(Cathy & Heather & Mark & Allison);
|
auto allPosbValues =
|
||||||
|
DiscreteValues::CartesianProduct(Cathy & Heather & Mark & Allison);
|
||||||
for (size_t i = 0; i < allPosbValues.size(); ++i) {
|
for (size_t i = 0; i < allPosbValues.size(); ++i) {
|
||||||
DiscreteFactor::Values values = allPosbValues[i];
|
DiscreteFactor::Values values = allPosbValues[i];
|
||||||
double prodPot = graph(values);
|
double prodPot = graph(values);
|
||||||
|
|
|
@ -19,9 +19,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
|
@ -43,8 +43,6 @@ namespace gtsam {
|
||||||
bool equals(const Assignment& other, double tol = 1e-9) const {
|
bool equals(const Assignment& other, double tol = 1e-9) const {
|
||||||
return (*this == other);
|
return (*this == other);
|
||||||
}
|
}
|
||||||
}; //Assignment
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get Cartesian product consisting all possible configurations
|
* @brief Get Cartesian product consisting all possible configurations
|
||||||
|
@ -58,11 +56,11 @@ namespace gtsam {
|
||||||
* variables with each having cardinalities 4, we get 4096 possible
|
* variables with each having cardinalities 4, we get 4096 possible
|
||||||
* configurations!!
|
* configurations!!
|
||||||
*/
|
*/
|
||||||
template<typename L>
|
template <typename Derived = Assignment<L>>
|
||||||
std::vector<Assignment<L> > cartesianProduct(
|
static std::vector<Derived> CartesianProduct(
|
||||||
const std::vector<std::pair<L, size_t>>& keys) {
|
const std::vector<std::pair<L, size_t>>& keys) {
|
||||||
std::vector<Assignment<L> > allPossValues;
|
std::vector<Derived> allPossValues;
|
||||||
Assignment<L> values;
|
Derived values;
|
||||||
typedef std::pair<L, size_t> DiscreteKey;
|
typedef std::pair<L, size_t> DiscreteKey;
|
||||||
for (const DiscreteKey& key : keys)
|
for (const DiscreteKey& key : keys)
|
||||||
values[key.first] = 0; // Initialize from 0
|
values[key.first] = 0; // Initialize from 0
|
||||||
|
@ -72,15 +70,14 @@ namespace gtsam {
|
||||||
for (j = 0; j < keys.size(); j++) {
|
for (j = 0; j < keys.size(); j++) {
|
||||||
L idx = keys[j].first;
|
L idx = keys[j].first;
|
||||||
values[idx]++;
|
values[idx]++;
|
||||||
if (values[idx] < keys[j].second)
|
if (values[idx] < keys[j].second) break;
|
||||||
break;
|
|
||||||
// Wrap condition
|
// Wrap condition
|
||||||
values[idx] = 0;
|
values[idx] = 0;
|
||||||
}
|
}
|
||||||
if (j == keys.size())
|
if (j == keys.size()) break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return allPossValues;
|
return allPossValues;
|
||||||
}
|
}
|
||||||
|
}; // Assignment
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include <gtsam/base/FastSet.h>
|
#include <gtsam/base/FastSet.h>
|
||||||
|
|
||||||
#include <boost/make_shared.hpp>
|
#include <boost/make_shared.hpp>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -150,9 +151,9 @@ namespace gtsam {
|
||||||
for (auto& key : keys()) {
|
for (auto& key : keys()) {
|
||||||
pairs.emplace_back(key, cardinalities_.at(key));
|
pairs.emplace_back(key, cardinalities_.at(key));
|
||||||
}
|
}
|
||||||
// Reverse to make cartesianProduct output a more natural ordering.
|
// Reverse to make cartesian product output a more natural ordering.
|
||||||
std::vector<std::pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
|
std::vector<std::pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
|
||||||
const auto assignments = cartesianProduct(rpairs);
|
const auto assignments = DiscreteValues::CartesianProduct(rpairs);
|
||||||
|
|
||||||
// Construct unordered_map with values
|
// Construct unordered_map with values
|
||||||
std::vector<std::pair<DiscreteValues, double>> result;
|
std::vector<std::pair<DiscreteValues, double>> result;
|
||||||
|
@ -212,7 +213,7 @@ namespace gtsam {
|
||||||
auto assignment = kv.first;
|
auto assignment = kv.first;
|
||||||
for (auto& key : keys()) {
|
for (auto& key : keys()) {
|
||||||
size_t index = assignment.at(key);
|
size_t index = assignment.at(key);
|
||||||
ss << Translate(names, key, index) << "|";
|
ss << DiscreteValues::Translate(names, key, index) << "|";
|
||||||
}
|
}
|
||||||
ss << kv.second << "|\n";
|
ss << kv.second << "|\n";
|
||||||
}
|
}
|
||||||
|
@ -244,7 +245,7 @@ namespace gtsam {
|
||||||
auto assignment = kv.first;
|
auto assignment = kv.first;
|
||||||
for (auto& key : keys()) {
|
for (auto& key : keys()) {
|
||||||
size_t index = assignment.at(key);
|
size_t index = assignment.at(key);
|
||||||
ss << "<th>" << Translate(names, key, index) << "</th>";
|
ss << "<th>" << DiscreteValues::Translate(names, key, index) << "</th>";
|
||||||
}
|
}
|
||||||
ss << "<td>" << kv.second << "</td>"; // value
|
ss << "<td>" << kv.second << "</td>"; // value
|
||||||
ss << "</tr>\n";
|
ss << "</tr>\n";
|
||||||
|
|
|
@ -180,22 +180,17 @@ DecisionTreeFactor::shared_ptr DiscreteConditional::likelihood(
|
||||||
return likelihood(values);
|
return likelihood(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
void DiscreteConditional::solveInPlace(DiscreteValues* values) const {
|
void DiscreteConditional::solveInPlace(DiscreteValues* values) const {
|
||||||
// TODO: Abhijit asks: is this really the fastest way? He thinks it is.
|
// TODO(Abhijit): is this really the fastest way? He thinks it is.
|
||||||
ADT pFS = Choose(*this, *values); // P(F|S=parentsValues)
|
ADT pFS = Choose(*this, *values); // P(F|S=parentsValues)
|
||||||
|
|
||||||
// Initialize
|
// Initialize
|
||||||
DiscreteValues mpe;
|
DiscreteValues mpe;
|
||||||
double maxP = 0;
|
double maxP = 0;
|
||||||
|
|
||||||
DiscreteKeys keys;
|
|
||||||
for(Key idx: frontals()) {
|
|
||||||
DiscreteKey dk(idx, cardinality(idx));
|
|
||||||
keys & dk;
|
|
||||||
}
|
|
||||||
// Get all Possible Configurations
|
// Get all Possible Configurations
|
||||||
const auto allPosbValues = cartesianProduct(keys);
|
const auto allPosbValues = frontalAssignments();
|
||||||
|
|
||||||
// Find the MPE
|
// Find the MPE
|
||||||
for (const auto& frontalVals : allPosbValues) {
|
for (const auto& frontalVals : allPosbValues) {
|
||||||
|
@ -295,20 +290,20 @@ size_t DiscreteConditional::sample() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
vector<Assignment<Key>> DiscreteConditional::frontalAssignments() const {
|
vector<DiscreteValues> DiscreteConditional::frontalAssignments() const {
|
||||||
vector<pair<Key, size_t>> pairs;
|
vector<pair<Key, size_t>> pairs;
|
||||||
for (Key key : frontals()) pairs.emplace_back(key, cardinalities_.at(key));
|
for (Key key : frontals()) pairs.emplace_back(key, cardinalities_.at(key));
|
||||||
vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
|
vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
|
||||||
return cartesianProduct(rpairs);
|
return DiscreteValues::CartesianProduct(rpairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
vector<Assignment<Key>> DiscreteConditional::allAssignments() const {
|
vector<DiscreteValues> DiscreteConditional::allAssignments() const {
|
||||||
vector<pair<Key, size_t>> pairs;
|
vector<pair<Key, size_t>> pairs;
|
||||||
for (Key key : parents()) pairs.emplace_back(key, cardinalities_.at(key));
|
for (Key key : parents()) pairs.emplace_back(key, cardinalities_.at(key));
|
||||||
for (Key key : frontals()) pairs.emplace_back(key, cardinalities_.at(key));
|
for (Key key : frontals()) pairs.emplace_back(key, cardinalities_.at(key));
|
||||||
vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
|
vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
|
||||||
return cartesianProduct(rpairs);
|
return DiscreteValues::CartesianProduct(rpairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
|
@ -358,7 +353,7 @@ std::string DiscreteConditional::markdown(const KeyFormatter& keyFormatter,
|
||||||
for (const auto& a : frontalAssignments) {
|
for (const auto& a : frontalAssignments) {
|
||||||
for (auto&& it = beginFrontals(); it != endFrontals(); ++it) {
|
for (auto&& it = beginFrontals(); it != endFrontals(); ++it) {
|
||||||
size_t index = a.at(*it);
|
size_t index = a.at(*it);
|
||||||
ss << Translate(names, *it, index);
|
ss << DiscreteValues::Translate(names, *it, index);
|
||||||
}
|
}
|
||||||
ss << "|";
|
ss << "|";
|
||||||
}
|
}
|
||||||
|
@ -377,7 +372,7 @@ std::string DiscreteConditional::markdown(const KeyFormatter& keyFormatter,
|
||||||
ss << "|";
|
ss << "|";
|
||||||
for (auto&& it = beginParents(); it != endParents(); ++it) {
|
for (auto&& it = beginParents(); it != endParents(); ++it) {
|
||||||
size_t index = a.at(*it);
|
size_t index = a.at(*it);
|
||||||
ss << Translate(names, *it, index) << "|";
|
ss << DiscreteValues::Translate(names, *it, index) << "|";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ss << operator()(a) << "|";
|
ss << operator()(a) << "|";
|
||||||
|
@ -413,7 +408,7 @@ string DiscreteConditional::html(const KeyFormatter& keyFormatter,
|
||||||
ss << "<th>";
|
ss << "<th>";
|
||||||
for (auto&& it = beginFrontals(); it != endFrontals(); ++it) {
|
for (auto&& it = beginFrontals(); it != endFrontals(); ++it) {
|
||||||
size_t index = a.at(*it);
|
size_t index = a.at(*it);
|
||||||
ss << Translate(names, *it, index);
|
ss << DiscreteValues::Translate(names, *it, index);
|
||||||
}
|
}
|
||||||
ss << "</th>";
|
ss << "</th>";
|
||||||
}
|
}
|
||||||
|
@ -429,7 +424,7 @@ string DiscreteConditional::html(const KeyFormatter& keyFormatter,
|
||||||
ss << " <tr>";
|
ss << " <tr>";
|
||||||
for (auto&& it = beginParents(); it != endParents(); ++it) {
|
for (auto&& it = beginParents(); it != endParents(); ++it) {
|
||||||
size_t index = a.at(*it);
|
size_t index = a.at(*it);
|
||||||
ss << "<th>" << Translate(names, *it, index) << "</th>";
|
ss << "<th>" << DiscreteValues::Translate(names, *it, index) << "</th>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ss << "<td>" << operator()(a) << "</td>"; // value
|
ss << "<td>" << operator()(a) << "</td>"; // value
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include <boost/make_shared.hpp>
|
#include <boost/make_shared.hpp>
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
|
@ -182,10 +183,10 @@ class GTSAM_EXPORT DiscreteConditional
|
||||||
void sampleInPlace(DiscreteValues* parentsValues) const;
|
void sampleInPlace(DiscreteValues* parentsValues) const;
|
||||||
|
|
||||||
/// Return all assignments for frontal variables.
|
/// Return all assignments for frontal variables.
|
||||||
std::vector<Assignment<Key>> frontalAssignments() const;
|
std::vector<DiscreteValues> frontalAssignments() const;
|
||||||
|
|
||||||
/// Return all assignments for frontal *and* parent variables.
|
/// Return all assignments for frontal *and* parent variables.
|
||||||
std::vector<Assignment<Key>> allAssignments() const;
|
std::vector<DiscreteValues> allAssignments() const;
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
/// @name Wrapper support
|
/// @name Wrapper support
|
||||||
|
|
|
@ -25,14 +25,4 @@ using namespace std;
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
string DiscreteFactor::Translate(const Names& names, Key key, size_t index) {
|
|
||||||
if (names.empty()) {
|
|
||||||
stringstream ss;
|
|
||||||
ss << index;
|
|
||||||
return ss.str();
|
|
||||||
} else {
|
|
||||||
return names.at(key)[index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include <gtsam/inference/Factor.h>
|
#include <gtsam/inference/Factor.h>
|
||||||
#include <gtsam/base/Testable.h>
|
#include <gtsam/base/Testable.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
class DecisionTreeFactor;
|
class DecisionTreeFactor;
|
||||||
|
@ -90,10 +91,7 @@ public:
|
||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
/// Translation table from values to strings.
|
/// Translation table from values to strings.
|
||||||
using Names = std::map<Key, std::vector<std::string>>;
|
using Names = DiscreteValues::Names;
|
||||||
|
|
||||||
/// Translate an integer index value for given key to a string.
|
|
||||||
static std::string Translate(const Names& names, Key key, size_t index);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Render as markdown table
|
* @brief Render as markdown table
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
|
||||||
|
* Atlanta, Georgia 30332-0415
|
||||||
|
* All Rights Reserved
|
||||||
|
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
|
||||||
|
|
||||||
|
* See LICENSE for the license information
|
||||||
|
|
||||||
|
* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file DiscreteValues.cpp
|
||||||
|
* @date January, 2022
|
||||||
|
* @author Frank Dellaert
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <gtsam/discrete/DiscreteValues.h>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
using std::cout;
|
||||||
|
using std::endl;
|
||||||
|
using std::string;
|
||||||
|
using std::stringstream;
|
||||||
|
|
||||||
|
namespace gtsam {
|
||||||
|
|
||||||
|
void DiscreteValues::print(const string& s,
|
||||||
|
const KeyFormatter& keyFormatter) const {
|
||||||
|
cout << s << ": ";
|
||||||
|
for (auto&& kv : *this)
|
||||||
|
cout << "(" << keyFormatter(kv.first) << ", " << kv.second << ")";
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
string DiscreteValues::Translate(const Names& names, Key key, size_t index) {
|
||||||
|
if (names.empty()) {
|
||||||
|
stringstream ss;
|
||||||
|
ss << index;
|
||||||
|
return ss.str();
|
||||||
|
} else {
|
||||||
|
return names.at(key)[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string DiscreteValues::markdown(const KeyFormatter& keyFormatter,
|
||||||
|
const Names& names) const {
|
||||||
|
stringstream ss;
|
||||||
|
|
||||||
|
// Print out header and separator with alignment hints.
|
||||||
|
ss << "|Variable|value|\n|:-:|:-:|\n";
|
||||||
|
|
||||||
|
// Print out all rows.
|
||||||
|
for (const auto& kv : *this) {
|
||||||
|
ss << "|" << keyFormatter(kv.first) << "|"
|
||||||
|
<< Translate(names, kv.first, kv.second) << "|\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string DiscreteValues::html(const KeyFormatter& keyFormatter,
|
||||||
|
const Names& names) const {
|
||||||
|
stringstream ss;
|
||||||
|
|
||||||
|
// Print out preamble.
|
||||||
|
ss << "<div>\n<table class=\'DiscreteValues\'>\n <thead>\n";
|
||||||
|
|
||||||
|
// Print out header row.
|
||||||
|
ss << " <tr><th>Variable</th><th>value</th></tr>\n";
|
||||||
|
|
||||||
|
// Finish header and start body.
|
||||||
|
ss << " </thead>\n <tbody>\n";
|
||||||
|
|
||||||
|
// Print out all rows.
|
||||||
|
for (const auto& kv : *this) {
|
||||||
|
ss << " <tr>";
|
||||||
|
ss << "<th>" << keyFormatter(kv.first) << "</th><td>\'"
|
||||||
|
<< Translate(names, kv.first, kv.second) << "</td>";
|
||||||
|
ss << "</tr>\n";
|
||||||
|
}
|
||||||
|
ss << " </tbody>\n</table>\n</div>";
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
} // namespace gtsam
|
|
@ -18,8 +18,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <gtsam/discrete/Assignment.h>
|
#include <gtsam/discrete/Assignment.h>
|
||||||
|
#include <gtsam/discrete/DiscreteKey.h>
|
||||||
#include <gtsam/inference/Key.h>
|
#include <gtsam/inference/Key.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
/** A map from keys to values
|
/** A map from keys to values
|
||||||
|
@ -34,25 +37,57 @@ namespace gtsam {
|
||||||
*/
|
*/
|
||||||
class DiscreteValues : public Assignment<Key> {
|
class DiscreteValues : public Assignment<Key> {
|
||||||
public:
|
public:
|
||||||
|
using Base = Assignment<Key>; // base class
|
||||||
|
|
||||||
using Assignment::Assignment; // all constructors
|
using Assignment::Assignment; // all constructors
|
||||||
|
|
||||||
// Define the implicit default constructor.
|
// Define the implicit default constructor.
|
||||||
DiscreteValues() = default;
|
DiscreteValues() = default;
|
||||||
|
|
||||||
// Construct from assignment.
|
// Construct from assignment.
|
||||||
DiscreteValues(const Assignment<Key>& a) : Assignment<Key>(a) {}
|
explicit DiscreteValues(const Base& a) : Base(a) {}
|
||||||
|
|
||||||
void print(const std::string& s = "",
|
void print(const std::string& s = "",
|
||||||
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
|
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
|
||||||
std::cout << s << ": ";
|
|
||||||
for (const typename Assignment::value_type& keyValue : *this)
|
static std::vector<DiscreteValues> CartesianProduct(
|
||||||
std::cout << "(" << keyFormatter(keyValue.first) << ", "
|
const DiscreteKeys& keys) {
|
||||||
<< keyValue.second << ")";
|
return Base::CartesianProduct<DiscreteValues>(keys);
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @name Wrapper support
|
||||||
|
/// @{
|
||||||
|
|
||||||
|
/// Translation table from values to strings.
|
||||||
|
using Names = std::map<Key, std::vector<std::string>>;
|
||||||
|
|
||||||
|
/// Translate an integer index value for given key to a string.
|
||||||
|
static std::string Translate(const Names& names, Key key, size_t index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Output as a markdown table.
|
||||||
|
*
|
||||||
|
* @param keyFormatter function that formats keys.
|
||||||
|
* @param names translation table for values.
|
||||||
|
* @return string markdown output.
|
||||||
|
*/
|
||||||
|
std::string markdown(const KeyFormatter& keyFormatter,
|
||||||
|
const Names& names) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Output as a html table.
|
||||||
|
*
|
||||||
|
* @param keyFormatter function that formats keys.
|
||||||
|
* @param names translation table for values.
|
||||||
|
* @return string html output.
|
||||||
|
*/
|
||||||
|
std::string html(const KeyFormatter& keyFormatter, const Names& names) const;
|
||||||
|
|
||||||
|
/// @}
|
||||||
};
|
};
|
||||||
|
|
||||||
// traits
|
// traits
|
||||||
template<> struct traits<DiscreteValues> : public Testable<DiscreteValues> {};
|
template <>
|
||||||
|
struct traits<DiscreteValues> : public Testable<DiscreteValues> {};
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
@ -101,10 +101,10 @@ TEST(DiscreteBayesTree, ThinTree) {
|
||||||
auto R = self.bayesTree->roots().front();
|
auto R = self.bayesTree->roots().front();
|
||||||
|
|
||||||
// Check whether BN and BT give the same answer on all configurations
|
// Check whether BN and BT give the same answer on all configurations
|
||||||
auto allPosbValues =
|
auto allPosbValues = DiscreteValues::CartesianProduct(
|
||||||
cartesianProduct(keys[0] & keys[1] & keys[2] & keys[3] & keys[4] &
|
keys[0] & keys[1] & keys[2] & keys[3] & keys[4] & keys[5] & keys[6] &
|
||||||
keys[5] & keys[6] & keys[7] & keys[8] & keys[9] &
|
keys[7] & keys[8] & keys[9] & keys[10] & keys[11] & keys[12] & keys[13] &
|
||||||
keys[10] & keys[11] & keys[12] & keys[13] & keys[14]);
|
keys[14]);
|
||||||
for (size_t i = 0; i < allPosbValues.size(); ++i) {
|
for (size_t i = 0; i < allPosbValues.size(); ++i) {
|
||||||
DiscreteValues x = allPosbValues[i];
|
DiscreteValues x = allPosbValues[i];
|
||||||
double expected = self.bayesNet.evaluate(x);
|
double expected = self.bayesNet.evaluate(x);
|
||||||
|
|
|
@ -164,8 +164,8 @@ TEST_UNSAFE(DiscreteMarginals, truss2) {
|
||||||
graph.add(key[2] & key[3] & key[4], "1 2 3 4 5 6 7 8");
|
graph.add(key[2] & key[3] & key[4], "1 2 3 4 5 6 7 8");
|
||||||
|
|
||||||
// Calculate the marginals by brute force
|
// Calculate the marginals by brute force
|
||||||
auto allPosbValues =
|
auto allPosbValues = DiscreteValues::CartesianProduct(
|
||||||
cartesianProduct(key[0] & key[1] & key[2] & key[3] & key[4]);
|
key[0] & key[1] & key[2] & key[3] & key[4]);
|
||||||
Vector T = Z_5x1, F = Z_5x1;
|
Vector T = Z_5x1, F = Z_5x1;
|
||||||
for (size_t i = 0; i < allPosbValues.size(); ++i) {
|
for (size_t i = 0; i < allPosbValues.size(); ++i) {
|
||||||
DiscreteValues x = allPosbValues[i];
|
DiscreteValues x = allPosbValues[i];
|
||||||
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
|
||||||
|
* Atlanta, Georgia 30332-0415
|
||||||
|
* All Rights Reserved
|
||||||
|
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
|
||||||
|
|
||||||
|
* See LICENSE for the license information
|
||||||
|
|
||||||
|
* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* testDiscreteValues.cpp
|
||||||
|
*
|
||||||
|
* @date Jan, 2022
|
||||||
|
* @author Frank Dellaert
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <CppUnitLite/TestHarness.h>
|
||||||
|
#include <gtsam/base/Testable.h>
|
||||||
|
#include <gtsam/discrete/DiscreteValues.h>
|
||||||
|
#include <gtsam/discrete/Signature.h>
|
||||||
|
|
||||||
|
#include <boost/assign/std/map.hpp>
|
||||||
|
using namespace boost::assign;
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace gtsam;
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
// Check markdown representation with a value formatter.
|
||||||
|
TEST(DiscreteValues, markdownWithValueFormatter) {
|
||||||
|
DiscreteValues values;
|
||||||
|
values[12] = 1; // A
|
||||||
|
values[5] = 0; // B
|
||||||
|
string expected =
|
||||||
|
"|Variable|value|\n"
|
||||||
|
"|:-:|:-:|\n"
|
||||||
|
"|B|-|\n"
|
||||||
|
"|A|One|\n";
|
||||||
|
auto keyFormatter = [](Key key) { return key == 12 ? "A" : "B"; };
|
||||||
|
DiscreteValues::Names names{{12, {"Zero", "One", "Two"}}, {5, {"-", "+"}}};
|
||||||
|
string actual = values.markdown(keyFormatter, names);
|
||||||
|
EXPECT(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
// Check html representation with a value formatter.
|
||||||
|
TEST(DiscreteValues, htmlWithValueFormatter) {
|
||||||
|
DiscreteValues values;
|
||||||
|
values[12] = 1; // A
|
||||||
|
values[5] = 0; // B
|
||||||
|
string expected =
|
||||||
|
"<div>\n"
|
||||||
|
"<table class='DiscreteValues'>\n"
|
||||||
|
" <thead>\n"
|
||||||
|
" <tr><th>Variable</th><th>value</th></tr>\n"
|
||||||
|
" </thead>\n"
|
||||||
|
" <tbody>\n"
|
||||||
|
" <tr><th>B</th><td>'-</td></tr>\n"
|
||||||
|
" <tr><th>A</th><td>'One</td></tr>\n"
|
||||||
|
" </tbody>\n"
|
||||||
|
"</table>\n"
|
||||||
|
"</div>";
|
||||||
|
auto keyFormatter = [](Key key) { return key == 12 ? "A" : "B"; };
|
||||||
|
DiscreteValues::Names names{{12, {"Zero", "One", "Two"}}, {5, {"-", "+"}}};
|
||||||
|
string actual = values.html(keyFormatter, names);
|
||||||
|
EXPECT(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
int main() {
|
||||||
|
TestResult tr;
|
||||||
|
return TestRegistry::runAllTests(tr);
|
||||||
|
}
|
||||||
|
/* ************************************************************************* */
|
Loading…
Reference in New Issue