Pre-compute asia things

release/4.3a0
Frank Dellaert 2025-01-27 10:09:40 -05:00
parent d53b48cf80
commit 2da5d764f1
1 changed files with 15 additions and 19 deletions

View File

@ -20,18 +20,21 @@
#include <gtsam/base/Testable.h> #include <gtsam/base/Testable.h>
#include <gtsam/discrete/DiscreteSearch.h> #include <gtsam/discrete/DiscreteSearch.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#include "AsiaExample.h" #include "AsiaExample.h"
using namespace gtsam; using namespace gtsam;
// Create Asia Bayes net, FG, and Bayes tree once
namespace asia {
using namespace asia_example;
static const DiscreteBayesNet bayesNet = createAsiaExample();
static const DiscreteFactorGraph factorGraph(bayesNet);
static const DiscreteValues mpe = factorGraph.optimize();
static const Ordering ordering{D, X, B, E, L, T, S, A};
static const DiscreteBayesTree bayesTree =
*factorGraph.eliminateMultifrontal(ordering);
} // namespace asia
/* ************************************************************************* */ /* ************************************************************************* */
TEST(DiscreteBayesNet, EmptyKBest) { TEST(DiscreteBayesNet, EmptyKBest) {
DiscreteBayesNet net; // no factors DiscreteBayesNet net; // no factors
@ -44,9 +47,7 @@ TEST(DiscreteBayesNet, EmptyKBest) {
/* ************************************************************************* */ /* ************************************************************************* */
TEST(DiscreteBayesNet, AsiaKBest) { TEST(DiscreteBayesNet, AsiaKBest) {
using namespace asia_example; const DiscreteSearch search(asia::bayesNet);
const DiscreteBayesNet asia = createAsiaExample();
const DiscreteSearch search(asia);
// Ask for the MPE // Ask for the MPE
auto mpe = search.run(); auto mpe = search.run();
@ -56,8 +57,7 @@ TEST(DiscreteBayesNet, AsiaKBest) {
EXPECT_DOUBLES_EQUAL(1.236627, std::fabs(mpe[0].error), 1e-5); EXPECT_DOUBLES_EQUAL(1.236627, std::fabs(mpe[0].error), 1e-5);
// Check it is equal to MPE via inference // Check it is equal to MPE via inference
const DiscreteFactorGraph asiaFG(asia); EXPECT(assert_equal(asia::mpe, mpe[0].assignment));
EXPECT(assert_equal(mpe[0].assignment, asiaFG.optimize()));
// Ask for top 4 solutions // Ask for top 4 solutions
auto solutions = search.run(4); auto solutions = search.run(4);
@ -82,11 +82,7 @@ TEST(DiscreteBayesTree, EmptyTree) {
/* ************************************************************************* */ /* ************************************************************************* */
TEST(DiscreteBayesTree, AsiaTreeKBest) { TEST(DiscreteBayesTree, AsiaTreeKBest) {
using namespace asia_example; DiscreteSearch search(asia::bayesTree);
DiscreteFactorGraph asiaFG(createAsiaExample());
const Ordering ordering{D, X, B, E, L, T, S, A};
DiscreteBayesTree::shared_ptr bt = asiaFG.eliminateMultifrontal(ordering);
DiscreteSearch search(*bt);
// Ask for MPE // Ask for MPE
auto mpe = search.run(); auto mpe = search.run();
@ -96,7 +92,7 @@ TEST(DiscreteBayesTree, AsiaTreeKBest) {
EXPECT_DOUBLES_EQUAL(1.236627, std::fabs(mpe[0].error), 1e-5); EXPECT_DOUBLES_EQUAL(1.236627, std::fabs(mpe[0].error), 1e-5);
// Check it is equal to MPE via inference // Check it is equal to MPE via inference
EXPECT(assert_equal(mpe[0].assignment, asiaFG.optimize())); EXPECT(assert_equal(asia::mpe, mpe[0].assignment));
// Ask for top 4 solutions // Ask for top 4 solutions
auto solutions = search.run(4); auto solutions = search.run(4);