Swarm-SLAM  1.0.0
C-SLAM Framework
test_broker.py
Go to the documentation of this file.
1 import unittest
2 
3 from cslam.broker import Broker
4 from cslam.algebraic_connectivity_maximization import AlgebraicConnectivityMaximization, EdgeInterRobot
5 from cslam.loop_closure_sparse_matching import LoopClosureSparseMatching
6 from test_algebraic_connectivity import build_multi_robot_graph
7 import random
8 import numpy as np
9 import networkx as nx
10 import scipy
11 from collections import namedtuple
12 import math
13 
14 
15 def build_graph_and_extract_selection(nb_poses, nb_candidate_edges, max_nb_robots,
16  robot_id, nb_candidates_to_choose):
17  """Build a graph and perform selection based on algebraic connectivity
18  """
19  fixed_edges_list, candidate_edges_list = build_multi_robot_graph(
20  nb_poses, nb_candidate_edges, max_nb_robots)
21 
22  params = {}
23  params['robot_id'] = robot_id
24  params['max_nb_robots'] = max_nb_robots
25  params['frontend.similarity_threshold'] = 0.0 # Doesn't change anything
26  params['frontend.sensor_type'] = 'stereo'
27  params["frontend.enable_sparsification"] = True
28  params["evaluation.enable_sparsification_comparison"] = False
29  lcsm = LoopClosureSparseMatching(params)
30  lcsm.candidate_selector.set_graph(fixed_edges_list, candidate_edges_list)
31 
32  # Solve the initial graph
33  is_robot_considered = {}
34  for i in range(max_nb_robots):
35  is_robot_considered[i] = True
36  return lcsm.select_candidates(nb_candidates_to_choose,
37  is_robot_considered,
38  greedy_initialization=False)
39 
40 
41 def verif_broker(unittest_framework, nb_poses, nb_candidate_edges, max_nb_robots,
42  robot_id, nb_candidates_to_choose, use_vertex_cover):
43  """Test broker based on params
44  """
45  # Build graph
46  selection = build_graph_and_extract_selection(nb_poses, nb_candidate_edges,
47  max_nb_robots, robot_id,
48  nb_candidates_to_choose)
49  unittest_framework.assertEqual(
50  len(selection), min(nb_candidate_edges, nb_candidates_to_choose))
51  neighbors_in_range_list = range(max_nb_robots)
52 
53  # Brokerage
54  broker = Broker(selection, neighbors_in_range_list)
55  components = broker.brokerage(use_vertex_cover)
56 
57  # Extract vertices
58  initial_vertices = set()
59  duplicates = []
60  for e in selection:
61  v0 = (e.robot0_id, e.robot0_keyframe_id)
62  if v0 in initial_vertices:
63  duplicates.append(v0)
64  initial_vertices.add(v0)
65  v1 = (e.robot1_id, e.robot1_keyframe_id)
66  if v1 in initial_vertices:
67  duplicates.append(v1)
68  initial_vertices.add(v1)
69  # Sanity check
70  unittest_framework.assertEqual(
71  len(initial_vertices) + len(duplicates),
72  min(nb_candidate_edges, nb_candidates_to_choose) * 2)
73 
74  vertices = []
75  for c in components:
76  for v in c:
77  vertices.append(v)
78 
79  # Check that we do not send more vertices than the trivial solution
80  # Trivial solution: sending one vertex per edge
81  # Upper Bounds:
82  unittest_framework.assertLessEqual(
83  len(vertices), min(nb_candidate_edges, nb_candidates_to_choose))
84  if use_vertex_cover and max_nb_robots == 2:
85  unittest_framework.assertLessEqual(
86  len(vertices), math.ceil(float(len(initial_vertices)) / 2))
87  # Lower Bounds:
88  unittest_framework.assertGreaterEqual(len(vertices), 1)
89 
90  # Check no duplicates
91  for i in range(len(vertices)):
92  for j in range(len(vertices)):
93  if i != j:
94  unittest_framework.assertNotEqual(vertices[i], vertices[j])
95 
96  # Check that all edges are covered
97  for e in selection:
98  v0 = (e.robot0_id, e.robot0_keyframe_id)
99  v1 = (e.robot1_id, e.robot1_keyframe_id)
100  v0_in = v0 in vertices
101  v1_in = v1 in vertices
102  unittest_framework.assertTrue(v0_in or v1_in)
103 
104 
105 class TestBroker(unittest.TestCase):
106  """Unit tests for communication broker
107  """
108 
110  """Simple dialog strategy
111  """
112  # Parameters
113  robot_id = 0
114  nb_poses = 100
115  nb_candidate_edges = 50
116  max_nb_robots = 2
117  nb_candidates_to_choose = 30
118  use_vertex_cover = False
119 
120  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
121  nb_candidates_to_choose, use_vertex_cover)
122 
123  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
124  nb_candidate_edges, use_vertex_cover)
125 
126  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
127  robot_id, nb_candidates_to_choose * 10, use_vertex_cover)
128 
129  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
130  robot_id, nb_candidate_edges * 10, use_vertex_cover)
131 
132  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
133  2 * nb_candidate_edges, use_vertex_cover)
134 
136  """Vertex cover strategy
137  """
138  # Parameters
139  robot_id = 0
140  nb_poses = 100
141  nb_candidate_edges = 50
142  max_nb_robots = 2
143  nb_candidates_to_choose = 30
144  use_vertex_cover = True
145 
146  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
147  nb_candidates_to_choose, use_vertex_cover)
148 
149  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
150  nb_candidate_edges, use_vertex_cover)
151 
152  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
153  robot_id, nb_candidates_to_choose * 10, use_vertex_cover)
154 
155  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
156  robot_id, nb_candidate_edges * 10, use_vertex_cover)
157 
158  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
159  2 * nb_candidate_edges, use_vertex_cover)
160 
162  """Simple dialog strategy
163  """
164  # Parameters
165  robot_id = 1
166  nb_poses = 100
167  nb_candidate_edges = 200
168  max_nb_robots = 5
169  nb_candidates_to_choose = 100
170  use_vertex_cover = False
171 
172  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
173  nb_candidates_to_choose, use_vertex_cover)
174 
175  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
176  nb_candidate_edges, use_vertex_cover)
177 
178  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
179  robot_id, nb_candidates_to_choose * 10, use_vertex_cover)
180 
181  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
182  robot_id, nb_candidate_edges * 10, use_vertex_cover)
183 
184  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
185  2 * nb_candidate_edges, use_vertex_cover)
186 
188  """Vertex cover strategy
189  """
190  # Parameters
191  robot_id = 2
192  nb_poses = 100
193  nb_candidate_edges = 200
194  max_nb_robots = 5
195  nb_candidates_to_choose = 100
196  use_vertex_cover = True
197 
198  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
199  nb_candidates_to_choose, use_vertex_cover)
200 
201  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
202  nb_candidate_edges, use_vertex_cover)
203 
204  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
205  robot_id, nb_candidates_to_choose * 10, use_vertex_cover)
206 
207  verif_broker(self, nb_poses * 10, nb_candidate_edges * 10, max_nb_robots,
208  robot_id, nb_candidate_edges * 10, use_vertex_cover)
209 
210  verif_broker(self, nb_poses, nb_candidate_edges, max_nb_robots, robot_id,
211  2 * nb_candidate_edges, use_vertex_cover)
212 
214  # Build graph
215  fixed_edges_list = []
216  candidate_edges_list = []
217  fixed_weight = 1.0
218  candidate_edges_list.append(EdgeInterRobot(0, 1, 1, 1, fixed_weight))
219  candidate_edges_list.append(EdgeInterRobot(0, 1, 1, 2, fixed_weight))
220  candidate_edges_list.append(EdgeInterRobot(0, 1, 1, 3, fixed_weight))
221  candidate_edges_list.append(EdgeInterRobot(0, 1, 1, 4, fixed_weight))
222  candidate_edges_list.append(EdgeInterRobot(0, 2, 1, 5, fixed_weight))
223 
224  robot_id = 0
225  nb_poses = 100
226  nb_candidate_edges = 5
227  max_nb_robots = 2
228  nb_candidates_to_choose = 5
229  use_vertex_cover = True
230 
231  params = {}
232  params['robot_id'] = robot_id
233  params['max_nb_robots'] = max_nb_robots
234  params['frontend.similarity_threshold'] = 0.0 # Doesn't change anything
235  params['frontend.sensor_type'] = 'stereo'
236  params["frontend.enable_sparsification"] = True
237  params["evaluation.enable_sparsification_comparison"] = False
238  lcsm = LoopClosureSparseMatching(params)
239  lcsm.candidate_selector.set_graph(fixed_edges_list, candidate_edges_list)
240 
241  # Solve the initial graph
242  is_robot_considered = {}
243  for i in range(max_nb_robots):
244  is_robot_considered[i] = True
245  selection = lcsm.select_candidates(nb_candidates_to_choose,
246  is_robot_considered,
247  greedy_initialization=False)
248 
249  self.assertEqual(
250  len(selection), min(nb_candidate_edges, nb_candidates_to_choose))
251  neighbors_in_range_list = range(max_nb_robots)
252 
253  # Brokerage
254  broker = Broker(selection, neighbors_in_range_list)
255  components = broker.brokerage(use_vertex_cover)
256 
257  nb_components = len(components)
258  nb_vertices_transmitted = 0
259 
260  for component in components:
261  for v in component:
262  nb_vertices_transmitted += 1
263 
264  self.assertEqual(nb_components, 2)
265  self.assertEqual(nb_vertices_transmitted, 2)
266 
267 
268 
269 
270 if __name__ == "__main__":
271  unittest.main()
def test_vertex_cover_5robots(self)
Definition: test_broker.py:187
def test_simple_dialog_2robots(self)
Definition: test_broker.py:109
def test_manual_vertex_cover(self)
Definition: test_broker.py:213
def test_simple_dialog_5robots(self)
Definition: test_broker.py:161
def test_vertex_cover_2robots(self)
Definition: test_broker.py:135
def build_multi_robot_graph(nb_poses, nb_candidate_edges, max_nb_robots)
def build_graph_and_extract_selection(nb_poses, nb_candidate_edges, max_nb_robots, robot_id, nb_candidates_to_choose)
Definition: test_broker.py:16
def verif_broker(unittest_framework, nb_poses, nb_candidate_edges, max_nb_robots, robot_id, nb_candidates_to_choose, use_vertex_cover)
Definition: test_broker.py:42