testFindSeparator.cpp
Go to the documentation of this file.
1 /*
2  * testFindSeparator.cpp
3  *
4  * Created on: Nov 23, 2010
5  * Author: nikai
6  * Description: unit tests for FindSeparator
7  */
8 
9 #include <boost/assign/std/list.hpp> // for operator +=
10 #include <boost/assign/std/set.hpp> // for operator +=
11 #include <boost/assign/std/vector.hpp> // for operator +=
12 using namespace boost::assign;
13 #include <boost/make_shared.hpp>
15 
18 
19 using namespace std;
20 using namespace gtsam;
21 using namespace gtsam::partition;
22 
23 /* ************************************************************************* */
24 // x0 - x1 - x2
25 // l3 l4
27 {
29  graph.push_back(boost::make_shared<GenericFactor2D>(0, NODE_POSE_2D, 3, NODE_LANDMARK_2D));
30  graph.push_back(boost::make_shared<GenericFactor2D>(2, NODE_POSE_2D, 4, NODE_LANDMARK_2D));
31  graph.push_back(boost::make_shared<GenericFactor2D>(0, NODE_POSE_2D, 1, NODE_POSE_2D));
32  graph.push_back(boost::make_shared<GenericFactor2D>(1, NODE_POSE_2D, 2, NODE_POSE_2D));
33  std::vector<size_t> keys; keys += 0, 1, 2, 3, 4;
34 
35  WorkSpace workspace(5);
36  boost::optional<MetisResult> actual = separatorPartitionByMetis<GenericGraph2D>(graph, keys,
37  workspace, true);
38 
39  CHECK(actual.is_initialized());
40  vector<size_t> A_expected; A_expected += 0, 3; // frontal
41  vector<size_t> B_expected; B_expected += 2, 4; // frontal
42  vector<size_t> C_expected; C_expected += 1; // separator
43  CHECK(A_expected == actual->A);
44  CHECK(B_expected == actual->B);
45  CHECK(C_expected == actual->C);
46 }
47 
48 /* ************************************************************************* */
49 // x1 - x2 - x3, variable not used x0, x4, l7
50 // l5 l6
51 TEST ( Partition, separatorPartitionByMetis2 )
52 {
54  graph.push_back(boost::make_shared<GenericFactor2D>(1, NODE_POSE_2D, 5, NODE_LANDMARK_2D));
55  graph.push_back(boost::make_shared<GenericFactor2D>(3, NODE_POSE_2D, 6, NODE_LANDMARK_2D));
56  graph.push_back(boost::make_shared<GenericFactor2D>(1, NODE_POSE_2D, 2, NODE_POSE_2D));
57  graph.push_back(boost::make_shared<GenericFactor2D>(2, NODE_POSE_2D, 3, NODE_POSE_2D));
58  std::vector<size_t> keys; keys += 1, 2, 3, 5, 6;
59 
60  WorkSpace workspace(8);
61  boost::optional<MetisResult> actual = separatorPartitionByMetis<GenericGraph2D>(graph, keys,
62  workspace, true);
63 
64  CHECK(actual.is_initialized());
65  vector<size_t> A_expected; A_expected += 1, 5; // frontal
66  vector<size_t> B_expected; B_expected += 3, 6; // frontal
67  vector<size_t> C_expected; C_expected += 2; // separator
68  CHECK(A_expected == actual->A);
69  CHECK(B_expected == actual->B);
70  CHECK(C_expected == actual->C);
71 }
72 
73 /* *************************************************************************/
74 // x0 - x1 - x2 - x3
75 TEST ( Partition, edgePartitionByMetis )
76 {
78  graph.push_back(boost::make_shared<GenericFactor3D>(0, 1, 0, NODE_POSE_3D, NODE_POSE_3D));
79  graph.push_back(boost::make_shared<GenericFactor3D>(1, 2, 1, NODE_POSE_3D, NODE_POSE_3D));
80  graph.push_back(boost::make_shared<GenericFactor3D>(2, 3, 2, NODE_POSE_3D, NODE_POSE_3D));
81  std::vector<size_t> keys; keys += 0, 1, 2, 3;
82 
83  WorkSpace workspace(6);
84  boost::optional<MetisResult> actual = edgePartitionByMetis<GenericGraph3D>(graph, keys,
85  workspace, true);
86 
87  CHECK(actual.is_initialized());
88  vector<size_t> A_expected; A_expected += 0, 1; // frontal
89  vector<size_t> B_expected; B_expected += 2, 3; // frontal
90  vector<size_t> C_expected; // separator
91 // for(const size_t a: actual->A)
92 // cout << a << " ";
93 // cout << endl;
94 // for(const size_t b: actual->B)
95 // cout << b << " ";
96 // cout << endl;
97 
98  CHECK(A_expected == actual->A || A_expected == actual->B);
99  CHECK(B_expected == actual->B || B_expected == actual->A);
100  CHECK(C_expected == actual->C);
101 }
102 
103 /* *************************************************************************/
104 // x0 - x1 - x2 - x3 - x4
105 TEST ( Partition, edgePartitionByMetis2 )
106 {
108  graph.push_back(boost::make_shared<GenericFactor3D>(0, 1, 0, NODE_POSE_3D, NODE_POSE_3D, 1));
109  graph.push_back(boost::make_shared<GenericFactor3D>(1, 2, 1, NODE_POSE_3D, NODE_POSE_3D, 1));
110  graph.push_back(boost::make_shared<GenericFactor3D>(2, 3, 2, NODE_POSE_3D, NODE_POSE_3D, 20));
111  graph.push_back(boost::make_shared<GenericFactor3D>(3, 4, 3, NODE_POSE_3D, NODE_POSE_3D, 1));
112  std::vector<size_t> keys; keys += 0, 1, 2, 3, 4;
113 
114  WorkSpace workspace(6);
115  boost::optional<MetisResult> actual = edgePartitionByMetis<GenericGraph3D>(graph, keys,
116  workspace, true);
117  CHECK(actual.is_initialized());
118  vector<size_t> A_expected; A_expected += 0, 1; // frontal
119  vector<size_t> B_expected; B_expected += 2, 3, 4; // frontal
120  vector<size_t> C_expected; // separator
121  CHECK(A_expected == actual->A);
122  CHECK(B_expected == actual->B);
123  CHECK(C_expected == actual->C);
124 }
125 
126 /* ************************************************************************* */
127 // x0 - x1 - x2
128 // l3 l4
129 TEST ( Partition, findSeparator )
130 {
132  graph.push_back(boost::make_shared<GenericFactor2D>(0, NODE_POSE_2D, 3, NODE_LANDMARK_2D));
133  graph.push_back(boost::make_shared<GenericFactor2D>(2, NODE_POSE_2D, 4, NODE_LANDMARK_2D));
134  graph.push_back(boost::make_shared<GenericFactor2D>(0, NODE_POSE_2D, 1, NODE_POSE_2D));
135  graph.push_back(boost::make_shared<GenericFactor2D>(1, NODE_POSE_2D, 2, NODE_POSE_2D));
136  std::vector<size_t> keys; keys += 0, 1, 2, 3, 4;
137 
138  WorkSpace workspace(5);
139  int minNodesPerMap = -1;
140  bool reduceGraph = false;
141  int numSubmaps = findSeparator<GenericGraph2D>(graph, keys, minNodesPerMap, workspace,
142  false, boost::none, reduceGraph, 0, 0);
143  LONGS_EQUAL(2, numSubmaps);
144  LONGS_EQUAL(5, workspace.partitionTable.size());
145  LONGS_EQUAL(1, workspace.partitionTable[0]);
146  LONGS_EQUAL(0, workspace.partitionTable[1]);
147  LONGS_EQUAL(2, workspace.partitionTable[2]);
148  LONGS_EQUAL(1, workspace.partitionTable[3]);
149  LONGS_EQUAL(2, workspace.partitionTable[4]);
150 }
151 
152 /* ************************************************************************* */
153 // x1 - x2 - x3, variable not used x0, x4, l7
154 // l5 l6
155 TEST ( Partition, findSeparator2 )
156 {
158  graph.push_back(boost::make_shared<GenericFactor2D>(1, NODE_POSE_2D, 5, NODE_LANDMARK_2D));
159  graph.push_back(boost::make_shared<GenericFactor2D>(3, NODE_POSE_2D, 6, NODE_LANDMARK_2D));
160  graph.push_back(boost::make_shared<GenericFactor2D>(1, NODE_POSE_2D, 2, NODE_POSE_2D));
161  graph.push_back(boost::make_shared<GenericFactor2D>(2, NODE_POSE_2D, 3, NODE_POSE_2D));
162  std::vector<size_t> keys; keys += 1, 2, 3, 5, 6;
163 
164  WorkSpace workspace(8);
165  int minNodesPerMap = -1;
166  bool reduceGraph = false;
167  int numSubmaps = findSeparator<GenericGraph2D>(graph, keys, minNodesPerMap, workspace,
168  false, boost::none, reduceGraph, 0, 0);
169  LONGS_EQUAL(2, numSubmaps);
170  LONGS_EQUAL(8, workspace.partitionTable.size());
171  LONGS_EQUAL(-1,workspace.partitionTable[0]);
172  LONGS_EQUAL(1, workspace.partitionTable[1]);
173  LONGS_EQUAL(0, workspace.partitionTable[2]);
174  LONGS_EQUAL(2, workspace.partitionTable[3]);
175  LONGS_EQUAL(-1,workspace.partitionTable[4]);
176  LONGS_EQUAL(1, workspace.partitionTable[5]);
177  LONGS_EQUAL(2, workspace.partitionTable[6]);
178  LONGS_EQUAL(-1,workspace.partitionTable[7]);
179 }
180 
181 /* *************************************************************************/
187 TEST ( Partition, findSeparator3_with_reduced_camera )
188 {
190  for (int j=1; j<=8; j++)
191  graph.push_back(boost::make_shared<GenericFactor3D>(25, j));
192  for (int j=1; j<=16; j++)
193  graph.push_back(boost::make_shared<GenericFactor3D>(26, j));
194  for (int j=9; j<=24; j++)
195  graph.push_back(boost::make_shared<GenericFactor3D>(27, j));
196  for (int j=17; j<=24; j++)
197  graph.push_back(boost::make_shared<GenericFactor3D>(28, j));
198 
199  std::vector<size_t> keys;
200  for(int i=1; i<=28; i++)
201  keys.push_back(i);
202 
203  vector<Symbol> int2symbol;
204  int2symbol.push_back(Symbol('x',0)); // dummy
205  for(int i=1; i<=24; i++)
206  int2symbol.push_back(Symbol('l',i));
207  int2symbol.push_back(Symbol('x',25));
208  int2symbol.push_back(Symbol('x',26));
209  int2symbol.push_back(Symbol('x',27));
210  int2symbol.push_back(Symbol('x',28));
211 
212  WorkSpace workspace(29);
213  bool reduceGraph = true;
214  int numIsland = findSeparator(graph, keys, 3, workspace, false, int2symbol, reduceGraph, 0, 0);
215  LONGS_EQUAL(2, numIsland);
216 
217  partition::PartitionTable& partitionTable = workspace.partitionTable;
218  for (int j=1; j<=8; j++)
219  LONGS_EQUAL(1, partitionTable[j]);
220  for (int j=9; j<=16; j++)
221  LONGS_EQUAL(0, partitionTable[j]);
222  for (int j=17; j<=24; j++)
223  LONGS_EQUAL(2, partitionTable[j]);
224  LONGS_EQUAL(1, partitionTable[25]);
225  LONGS_EQUAL(1, partitionTable[26]);
226  LONGS_EQUAL(2, partitionTable[27]);
227  LONGS_EQUAL(2, partitionTable[28]);
228 }
229 
230 /* ************************************************************************* */
231 int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
232 /* ************************************************************************* */
#define CHECK(condition)
Definition: Test.h:109
int main()
static int runAllTests(TestResult &result)
TEST(Partition, separatorPartitionByMetis)
Definition: Half.h:150
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:166
NonlinearFactorGraph graph
std::vector< sharedGenericFactor2D > GenericGraph2D
Definition: GenericGraph.h:49
std::vector< sharedGenericFactor3D > GenericGraph3D
Definition: GenericGraph.h:97
boost::optional< MetisResult > separatorPartitionByMetis(const GenericGraph &graph, const std::vector< size_t > &keys, WorkSpace &workspace, bool verbose)
#define LONGS_EQUAL(expected, actual)
Definition: Test.h:135
traits
Definition: chartTesting.h:28
std::vector< int > PartitionTable
int findSeparator(const GenericGraph &graph, const std::vector< size_t > &keys, const int minNodesPerMap, WorkSpace &workspace, bool verbose, const boost::optional< std::vector< Symbol > > &int2symbol, const bool reduceGraph, const int minNrConstraintsPerCamera, const int minNrConstraintsPerLandmark)
const KeyVector keys
std::ptrdiff_t j
boost::optional< MetisResult > edgePartitionByMetis(const GenericGraph &graph, const std::vector< size_t > &keys, WorkSpace &workspace, bool verbose)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:35