def test_shortest_path(self): configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "topology") from_nid = TopologyId(1) to_nid = TopologyId(15) topology_result = configuration.shortest_path(from_nid, to_nid) expected_result = ([1, 12, 13, 14, 15], [1, 2, 13, 14, 15], [1, 2, 3, 14, 15], [1, 2, 3, 4, 15]) self.assertIn(topology_result, expected_result) configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "randomised") from_nid = configuration.topology.t2o(from_nid) to_nid = configuration.topology.t2o(to_nid) randomised_result = configuration.shortest_path(from_nid, to_nid) expected_result = tuple( list(map(configuration.topology.t2o, result)) for result in expected_result) self.assertIn(randomised_result, expected_result) self.assertEqual(topology_result, map(configuration.topology.o2t, randomised_result))
def _average_duty_cycle(columns, cached_cols, constants): t2o = constants["configuration"].topology.t2o return columns["DutyCycle"].apply(lambda x: np.mean([ d for (nid, d) in x.items() if t2o(TopologyId(nid)) not in constants["configuration"].sink_ids ]))
def __init__(self, topology, source_ids, sink_ids, space_behind_sink): super().__init__() self.topology = topology self.sink_ids = { topology.t2o(TopologyId(sink_id)) for sink_id in sink_ids } self.source_ids = { topology.t2o(TopologyId(source_id)) for source_id in source_ids } self.space_behind_sink = space_behind_sink if any(sink_id.nid < 0 for sink_id in self.sink_ids): raise RuntimeError("All sink ids must be positive") if any(source_id.nid < 0 for source_id in self.source_ids): raise RuntimeError("All source ids must be positive") if any(sink_id not in topology.nodes for sink_id in self.sink_ids): raise RuntimeError( f"The a sink id {self.sink_ids} is not present in the available node ids {topology.nodes}" ) if any(source_id not in topology.nodes for source_id in self.source_ids): raise RuntimeError( f"The a source id {self.source_ids} is not present in the available node ids {topology.nodes}" ) if len(self.sink_ids) == 0: raise RuntimeError( "There must be at least one sink in the configuration") if len(self.source_ids) == 0: raise RuntimeError( "There must be at least one source in the configuration") self._dist_matrix = None self._dist_matrix_meters = None self._predecessors = None self._build_connectivity_matrix()
def test_ssd_topology(self): configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "topology") source_id = TopologyId(0) self.assertIn(source_id, configuration.source_ids) self.assertEqual(configuration.ssd(source_id), 10) with self.assertRaises(RuntimeError): configuration.ssd(1)
def test_node_distance(self): configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "topology") from_nid = TopologyId(1) to_nid = TopologyId(15) topology_result = configuration.node_distance(from_nid, to_nid) expected_result = 4 self.assertEqual(topology_result, expected_result) configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "randomised") from_nid = configuration.topology.t2o(from_nid) to_nid = configuration.topology.t2o(to_nid) randomised_result = configuration.node_distance(from_nid, to_nid) self.assertEqual(randomised_result, expected_result) self.assertEqual(topology_result, randomised_result)
def test_ssd_randomised(self): configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "randomised") source_id = TopologyId(0) adjusted_source_id = configuration.topology.t2o(source_id) self.assertIn(adjusted_source_id, configuration.source_ids) self.assertEqual(configuration.ssd(adjusted_source_id), 10) # Get another node id that is not the source with self.assertRaises(RuntimeError): configuration.ssd((adjusted_source_id + 1) % configuration.size())
def get_node_id(self, topo_node_id_str): """Gets the topology node id from a node id string. This value could either be the topology node id as an integer, or it could be an attribute of the topology or configuration (e.g., 'sink_id').""" try: topo_node_id = TopologyId(int(topo_node_id_str)) # Check valid node ord_node_id = self.topology.t2o(topo_node_id) if ord_node_id not in self.topology.nodes: raise RuntimeError( f"The node id {topo_node_id} is not a valid node id") return topo_node_id except ValueError: attr_sources = (self, self.topology) for attr_source in attr_sources: if hasattr(attr_source, topo_node_id_str): ord_node_id = getattr(attr_source, topo_node_id_str) return self.topology.o2t(ord_node_id) # For sink_id and source_id look for plurals of them # Then make sure there is only one to choose from if hasattr(attr_source, topo_node_id_str + "s"): ord_node_ids = getattr(attr_source, topo_node_id_str + "s") if len(ord_node_ids) != 1: raise RuntimeError( f"Unable to get a {topo_node_id_str} because there is not only one of them." ) ord_node_id = next(iter(ord_node_ids)) return self.topology.o2t(ord_node_id) choices = [ x for a in (self, self.topology) for x in dir(a) if not callable(getattr(a, x)) and not x.startswith("_") ] import difflib close = difflib.get_close_matches(topo_node_id_str, choices, n=5) if len(close) == 0: close = choices raise RuntimeError( f"No way to work out node from '{topo_node_id_str}', did you mean one of {close}." )
def test_node_distance_meters(self): configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "topology") from_nid = TopologyId(1) to_nid = TopologyId(15) topology_result = configuration.node_distance_meters(from_nid, to_nid) from_coords = np.array((0 * 4.5, 1 * 4.5), dtype=np.float64) to_coords = np.array((1 * 4.5, 4 * 4.5), dtype=np.float64) expected_result = euclidean2_2d(from_coords, to_coords) self.assertEqual(topology_result, expected_result) configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "randomised") from_nid = configuration.topology.t2o(from_nid) to_nid = configuration.topology.t2o(to_nid) randomised_result = configuration.node_distance_meters( from_nid, to_nid) self.assertEqual(randomised_result, expected_result) self.assertEqual(topology_result, randomised_result)
def test_ssd_meters(self): configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "topology") source = TopologyId(0) topology_result = configuration.ssd_meters(source) from_coords = np.array((0 * 4.5, 0 * 4.5), dtype=np.float64) to_coords = np.array((5 * 4.5, 5 * 4.5), dtype=np.float64) expected_result = euclidean2_2d(from_coords, to_coords) self.assertEqual(topology_result, expected_result) configuration = simulator.Configuration.create_specific( 'SourceCorner', 11, 4.5, "randomised") source = configuration.topology.t2o(source) randomised_result = configuration.ssd_meters(source) self.assertEqual(randomised_result, expected_result) self.assertEqual(topology_result, randomised_result)