testMFAS.cpp
Go to the documentation of this file.
1 
8 #include <gtsam/sfm/MFAS.h>
9 
11 
12 using namespace std;
13 using namespace gtsam;
14 
24 // edges in the graph - last edge from node 3 to 0 is an outlier
25 vector<MFAS::KeyPair> edges = {make_pair(3, 2), make_pair(0, 1), make_pair(3, 1),
26  make_pair(1, 2), make_pair(0, 2), make_pair(3, 0)};
27 // nodes in the graph
28 KeyVector nodes = {Key(0), Key(1), Key(2), Key(3)};
29 // weights from projecting in direction-1 (bad direction, outlier accepted)
30 vector<double> weights1 = {2, 1.5, 0.5, 0.25, 1, 0.75};
31 // weights from projecting in direction-2 (good direction, outlier rejected)
32 vector<double> weights2 = {0.5, 0.75, -0.25, 0.75, 1, 0.5};
33 
34 // helper function to obtain map from keypairs to weights from the
35 // vector representations
36 map<MFAS::KeyPair, double> getEdgeWeights(const vector<MFAS::KeyPair> &edges,
37  const vector<double> &weights) {
38  map<MFAS::KeyPair, double> edgeWeights;
39  for (size_t i = 0; i < edges.size(); i++) {
40  edgeWeights[edges[i]] = weights[i];
41  }
42  return edgeWeights;
43 }
44 
45 // test the ordering and the outlierWeights function using weights2 - outlier
46 // edge is rejected when projected in a direction that gives weights2
47 TEST(MFAS, OrderingWeights2) {
48  MFAS mfas_obj(getEdgeWeights(edges, weights2));
49 
50  KeyVector ordered_nodes = mfas_obj.computeOrdering();
51 
52  // ground truth (expected) ordering in this example
53  KeyVector gt_ordered_nodes = {0, 1, 3, 2};
54 
55  // check if the expected ordering is obtained
56  for (size_t i = 0; i < ordered_nodes.size(); i++) {
57  EXPECT_LONGS_EQUAL(gt_ordered_nodes[i], ordered_nodes[i]);
58  }
59 
60  map<MFAS::KeyPair, double> outlier_weights = mfas_obj.computeOutlierWeights();
61 
62  // since edge between 3 and 0 is inconsistent with the ordering, it must have
63  // positive outlier weight, other outlier weights must be zero
64  for (auto &edge : edges) {
65  if (edge == make_pair(Key(3), Key(0)) ||
66  edge == make_pair(Key(0), Key(3))) {
67  EXPECT_DOUBLES_EQUAL(outlier_weights[edge], 0.5, 1e-6);
68  } else {
69  EXPECT_DOUBLES_EQUAL(outlier_weights[edge], 0, 1e-6);
70  }
71  }
72 }
73 
74 // test the ordering function and the outlierWeights method using
75 // weights1 (outlier edge is accepted when projected in a direction that
76 // produces weights1)
77 TEST(MFAS, OrderingWeights1) {
78  MFAS mfas_obj(getEdgeWeights(edges, weights1));
79 
80  KeyVector ordered_nodes = mfas_obj.computeOrdering();
81 
82  // "ground truth" expected ordering in this example
83  KeyVector gt_ordered_nodes = {3, 0, 1, 2};
84 
85  // check if the expected ordering is obtained
86  for (size_t i = 0; i < ordered_nodes.size(); i++) {
87  EXPECT_LONGS_EQUAL(gt_ordered_nodes[i], ordered_nodes[i]);
88  }
89 
90  map<MFAS::KeyPair, double> outlier_weights = mfas_obj.computeOutlierWeights();
91 
92  // since edge between 3 and 0 is inconsistent with the ordering, it must have
93  // positive outlier weight, other outlier weights must be zero
94  for (auto &edge : edges) {
95  EXPECT_DOUBLES_EQUAL(outlier_weights[edge], 0, 1e-6);
96  }
97 }
98 
99 /* ************************************************************************* */
100 int main() {
101  TestResult tr;
102  return TestRegistry::runAllTests(tr);
103 }
104 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::MFAS
Definition: MFAS.h:51
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
TestHarness.h
edges
vector< MFAS::KeyPair > edges
Definition: testMFAS.cpp:25
main
int main()
Definition: testMFAS.cpp:100
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
weights2
vector< double > weights2
Definition: testMFAS.cpp:32
weights1
vector< double > weights1
Definition: testMFAS.cpp:30
gtsam::MFAS::computeOutlierWeights
std::map< KeyPair, double > computeOutlierWeights() const
Computes the outlier weights of the graph. We define the outlier weight of a edge to be zero if the e...
Definition: MFAS.cpp:141
MFAS.h
MFAS class to solve Minimum Feedback Arc Set graph problem.
EXPECT_DOUBLES_EQUAL
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
TestResult
Definition: TestResult.h:26
nodes
KeyVector nodes
Definition: testMFAS.cpp:28
gtsam
traits
Definition: SFMdata.h:40
std
Definition: BFloat16.h:88
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
TEST
TEST(MFAS, OrderingWeights2)
Definition: testMFAS.cpp:47
gtsam::MFAS::computeOrdering
KeyVector computeOrdering() const
Computes the 1D MFAS ordering of nodes in the graph.
Definition: MFAS.cpp:124
getEdgeWeights
map< MFAS::KeyPair, double > getEdgeWeights(const vector< MFAS::KeyPair > &edges, const vector< double > &weights)
Definition: testMFAS.cpp:36


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:56