def test_completion_detection(): """ Tests that the completeness of an embedding is accurately detected in a simple example. """ infra = InfrastructureNetwork() # One source, one sink, one relay. # Enough transmit power so that it doesn't need to be taken into account nsource = infra.add_source( pos=(0, 0), # transmit power should not block anything in this example transmit_power_dbm=100, ) _nrelay = infra.add_intermediate(pos=(0, 1), transmit_power_dbm=100) nsink = infra.set_sink(pos=(1, 1), transmit_power_dbm=100) overlay = OverlayNetwork() esource = ENode(overlay.add_source(), nsource) esink = ENode(overlay.set_sink(), nsink) overlay.add_link(esource.block, esink.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[(esource.block, esource.node)] ) assert not embedding.is_complete() embedding.take_action(esource, esink, 0) assert embedding.is_complete()
def test_parallel_receive_impossible(): """ Tests that receiving from two sender nodes at the same time is impossible """ infra = InfrastructureNetwork() nsource1 = infra.add_source(pos=(0, 0), transmit_power_dbm=30) nsource2 = infra.add_source(pos=(3, 0), transmit_power_dbm=30) nsink = infra.set_sink(pos=(2, 0), transmit_power_dbm=30) overlay = OverlayNetwork() esource1 = ENode(overlay.add_source(), nsource1) esource2 = ENode(overlay.add_source(), nsource2) esink = ENode(overlay.set_sink(), nsink) # two incoming connections to sink overlay.add_link(esource1.block, esink.block) overlay.add_link(esource2.block, esink.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[ (esource1.block, esource1.node), (esource2.block, esource2.node), ], ) # Try to send two signals to sink at the same timeslot. This should # fail, as either one signal should overshadow the other. embedding.take_action(esource1, esink, 0) assert not embedding.take_action(esource2, esink, 0)
def test_trivial_possibilities(): """ Tests that a single reasonable option is correctly generated in a trivial case. """ infra = InfrastructureNetwork() # Two nodes, 1m apart. The transmitting node has a # transmit_power_dbm # power of 30dBm (similar to a regular router) which should easily # cover the distance of 1m without any noise. source_node = infra.add_source(pos=(0, 0), transmit_power_dbm=0.1) infra.set_sink(pos=(1, 0), transmit_power_dbm=0) overlay = OverlayNetwork() source_block = overlay.add_source() sink_block = overlay.set_sink() overlay.add_link(source_block, sink_block) embedding = PartialEmbedding( infra, overlay, source_mapping=[(source_block, source_node)] ) # can only embed B2 into N2 assert len(embedding.possibilities()) == 1
def test_self_loop_does_not_interfere(): """Tests self-loop does not interfere with other connections""" infra = InfrastructureNetwork() nso1 = infra.add_source(name="nso1", pos=(0, 0), transmit_power_dbm=30) nso2 = infra.add_source(name="nso2", pos=(0, 1), transmit_power_dbm=30) nsi = infra.set_sink(name="nsi", pos=(2, 0), transmit_power_dbm=30) overlay = OverlayNetwork() bso1 = overlay.add_source(name="bso1", datarate=5, requirement=0) bso2 = overlay.add_source(name="bso2", datarate=5, requirement=0) bin_ = overlay.add_intermediate(name="bin", datarate=5, requirement=0) bsi = overlay.set_sink(name="bsi", datarate=5, requirement=0) overlay.add_link(bso1, bin_) overlay.add_link(bin_, bsi) overlay.add_link(bso2, bsi) embedding = PartialEmbedding( infra, overlay, source_mapping=[(bso1, nso1), (bso2, nso2)] ) eso1 = ENode(bso1, nso1) eso2 = ENode(bso2, nso2) ein = ENode(bin_, nsi) esi = ENode(bsi, nsi) assert embedding.take_action(eso1, ein, 0) # self loop at node esi, ts 1 assert embedding.take_action(ein, esi, 1) # can still send to that node at the same ts assert embedding.take_action(eso2, esi, 1)
def test_invalidating_earlier_choice_impossible(): """ Tests that an action that would invalidate an earlier action is impossible. """ infra = InfrastructureNetwork() # Two sources, one sink. Equal distance from both sources to sink. # One source with moderate transmit power (but enough to cover the # distance, one source with excessive transmit power. # transmit_power_dbm # power of 30dBm (similar to a regular router) which should easily # cover the distance of 1m without any noise. source_node_silent = infra.add_source( pos=(0, 0), transmit_power_dbm=20, name="Silent" ) source_node_screamer = infra.add_source( pos=(3, 0), transmit_power_dbm=100, name="Screamer" ) node_sink = infra.set_sink(pos=(1, 3), transmit_power_dbm=0, name="Sink") overlay = OverlayNetwork() esource_silent = ENode(overlay.add_source(), source_node_silent) esource_screamer = ENode(overlay.add_source(), source_node_screamer) esink = ENode(overlay.set_sink(), node_sink) overlay.add_link(esource_silent.block, esink.block) overlay.add_link(esource_screamer.block, esink.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[ (esource_silent.block, esource_silent.node), (esource_screamer.block, esource_screamer.node), ], ) action_to_be_invalidated = (esource_screamer, esink, 0) # make sure the action is an option in the first place assert action_to_be_invalidated in embedding.possibilities() # embed the link from the silent node to the sink embedding.take_action(esource_silent, esink, 0) # first assert that action would be valid by itself screamer_sinr = embedding.known_sinr(source_node_screamer, node_sink, 0) assert screamer_sinr > 2.0 new_possibilities = embedding.possibilities() # but since the action would make the first embedding invalid (a # node cannot receive two signals at the same time), it should still # not be possible assert action_to_be_invalidated not in new_possibilities # since there are no options left in the first timeslot, there are # now exactly 2 (screamer -> silent as relay, screamer -> sink # embedded) options left in the newly created second timeslot assert len(new_possibilities) == 2
def test_path_loss(): """ Tests that an embedding over impossible distances is recognized as invalid. """ infra = InfrastructureNetwork() # Two nodes, 1km apart. The transmitting node has a transmission # power of 1dBm (=1.26mW). With a path loss over 1km of *at least* # 30dBm, less than ~-30dBm (approx. 10^-3 = 0.001mW = 1uW) arrives # at the target. That is a very optimistic approximation and is not # nearly enough to send any reasonable signal. source_node = infra.add_source(pos=(0, 0), transmit_power_dbm=1) infra.set_sink(pos=(1000, 0), transmit_power_dbm=0) overlay = OverlayNetwork() source_block = overlay.add_source() sink_block = overlay.set_sink() overlay.add_link(source_block, sink_block) embedding = PartialEmbedding( infra, overlay, source_mapping=[(source_block, source_node)] ) assert len(embedding.possibilities()) == 0
def test_timeslots_dynamically_created(): """Tests the dynamic creation of new timeslots as needed""" infra = InfrastructureNetwork() nso1 = infra.add_source( name="nso1", pos=(0, 0), # transmits so loudly that no other node can realistically # transmit in the same timeslot transmit_power_dbm=1000, ) nso2 = infra.add_source(name="nso2", pos=(1, 0), transmit_power_dbm=1000) nsi = infra.set_sink(name="nsi", pos=(1, 1), transmit_power_dbm=1000) overlay = OverlayNetwork() bso1 = overlay.add_source(name="bso1") bso2 = overlay.add_source(name="bso2") bsi = overlay.set_sink(name="bsi") eso1 = ENode(bso1, nso1) esi = ENode(bsi, nsi) overlay.add_link(bso1, bsi) overlay.add_link(bso2, bsi) embedding = PartialEmbedding( infra, overlay, source_mapping=[(bso1, nso1), (bso2, nso2)] ) # nothing used yet assert embedding.used_timeslots == 0 # it would be possible to create a new timeslot and embed either # link in it (2) or go to a relay from either source (2) assert len(embedding.possibilities()) == 4 # Take an action. nosurce1 will transmit so strongly that nso2 # cannot send at the same timelot assert embedding.take_action(eso1, esi, 0) # timeslot 0 is now used assert embedding.used_timeslots == 1 # New options (for creating timeslot 1) were created accordingly. # The second source could now still send to the other source as a # relay or to to the sink directly, it will just have to do it in a # new timeslot. assert len(embedding.possibilities()) == 2
def test_broadcast_possible(): """Tests that broadcast is possible despite SINR constraints""" infra = InfrastructureNetwork() # One source, one sink, one intermediate nsource = infra.add_source(pos=(0, 0), transmit_power_dbm=30) ninterm = infra.add_intermediate(pos=(1, 2), transmit_power_dbm=30) nsink = infra.set_sink(pos=(2, 0), transmit_power_dbm=30) overlay = OverlayNetwork() esource = ENode(overlay.add_source(), nsource) einterm = ENode(overlay.add_intermediate(), ninterm) esink = ENode(overlay.set_sink(), nsink) # fork overlay.add_link(esource.block, einterm.block) overlay.add_link(esource.block, esink.block) # make complete overlay.add_link(einterm.block, esink.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[(esource.block, esource.node)] ) # Broadcast from source to sink and intermediate sinr_before = embedding.known_sinr(esource.node, esink.node, timeslot=0) assert embedding.take_action(esource, esink, 0) # Easiest way to test this, easy to change if internals change. # pylint: disable=protected-access power_at_sink = embedding.infra.power_at_node( esink.node, frozenset(embedding._nodes_sending_in[0]) ) assert embedding.take_action(esource, einterm, 0) # Make sure the broadcasting isn't counted twice new_power = embedding.infra.power_at_node( esink.node, frozenset(embedding._nodes_sending_in[0]) ) assert new_power == power_at_sink # Make sure the broadcasts do not interfere with each other assert sinr_before == embedding.known_sinr( esource.node, esink.node, timeslot=0 )
def test_connection_within_node_always_possible(): """Tests that a node cannot send and receive at the same time""" infra = InfrastructureNetwork() nso = infra.add_source(name="nso", pos=(0, 0), transmit_power_dbm=30) nsi = infra.set_sink(name="nsi", pos=(2, 0), transmit_power_dbm=30) overlay = OverlayNetwork() bso = overlay.add_source(name="bso", datarate=0, requirement=0) bin_ = overlay.add_intermediate(name="bin", datarate=0, requirement=0) bsi = overlay.set_sink(name="bsi", datarate=0, requirement=0) overlay.add_link(bso, bin_) overlay.add_link(bin_, bsi) embedding = PartialEmbedding(infra, overlay, source_mapping=[(bso, nso)]) eso = ENode(bso, nso) ein = ENode(bin_, nsi) esi = ENode(bsi, nsi) assert embedding.take_action(eso, ein, 0) # even though nsi is already receiving in ts 0 assert embedding.take_action(ein, esi, 0)
def test_half_duplex(): """Tests that a node cannot send and receive at the same time""" infra = InfrastructureNetwork() nso = infra.add_source(name="nso", pos=(0, 0), transmit_power_dbm=30) ni = infra.add_intermediate(name="ni", pos=(1, 0), transmit_power_dbm=30) nsi = infra.set_sink(name="nsi", pos=(2, 0), transmit_power_dbm=30) overlay = OverlayNetwork() # links have no datarate requirements, so SINR concerns don't apply bso = overlay.add_source(name="bso", datarate=0) bsi = overlay.set_sink(name="bsi", datarate=0) overlay.add_link(bso, bsi) embedding = PartialEmbedding(infra, overlay, source_mapping=[(bso, nso)]) eso = ENode(bso, nso) esi = ENode(bsi, nsi) ein = ENode(bso, ni, bsi) print(embedding.possibilities()) assert embedding.take_action(eso, ein, 0) assert not embedding.take_action(ein, esi, 0)
def test_count_timeslots_multiple_sources(): """Tests correct counting behaviour with multiple sources""" infra = InfrastructureNetwork() nsource1 = infra.add_source(pos=(0, -1), transmit_power_dbm=30) nsource2 = infra.add_source(pos=(0, 1), transmit_power_dbm=30) nsink = infra.set_sink(pos=(1, 0), transmit_power_dbm=30) overlay = OverlayNetwork() esource1 = ENode(overlay.add_source(), nsource1) esource2 = ENode(overlay.add_source(), nsource2) esink = ENode(overlay.set_sink(), nsink) overlay.add_link(esource1.block, esink.block) overlay.add_link(esource2.block, esink.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[ (esource1.block, esource1.node), (esource2.block, esource2.node), ], ) assert not embedding.is_complete() assert embedding.used_timeslots == 0 assert embedding.take_action(esource1, esink, 0) assert not embedding.is_complete() assert embedding.used_timeslots == 1 assert embedding.take_action(esource2, esink, 1) assert embedding.is_complete() assert embedding.used_timeslots == 2
def test_same_connection_not_possible_twice(): """Tests that the same connection cannot be taken twice""" infra = InfrastructureNetwork() N2 = infra.add_source(name="N2", pos=(2.3, 2.2), transmit_power_dbm=26.9) N3 = infra.add_intermediate(name="N3", pos=(0, 4), transmit_power_dbm=11) _N1 = infra.set_sink(name="N1", pos=(9.4, 9.5), transmit_power_dbm=26.1) overlay = OverlayNetwork() B2 = overlay.add_source(name="B2") B3 = overlay.add_intermediate(name="B3") B1 = overlay.set_sink(name="B1") overlay.add_link(B2, B1) overlay.add_link(B2, B3) overlay.add_link(B3, B1) embedding = PartialEmbedding(infra, overlay, source_mapping=[(B2, N2)]) eso = ENode(B2, N2) ein = ENode(B2, N3, B1) assert embedding.take_action(eso, ein, 0) # this connection has already been taken assert not embedding.take_action(eso, ein, 1)
def test_all_viable_options_offered(): """ Tests that all manually verified options are offered in a concrete example. """ infra = InfrastructureNetwork() # Two sources, one sink, one intermediate, one relay # Enough transmit power so that it doesn't need to be taken into account nso1 = infra.add_source( pos=(0, 0), # transmit power should not block anything in this example transmit_power_dbm=100, name="nso1", ) nso2 = infra.add_source(pos=(1, 0), transmit_power_dbm=100, name="nso2") _nrelay = infra.add_intermediate( pos=(0, 1), transmit_power_dbm=100, name="nr" ) _ninterm = infra.add_intermediate( pos=(2, 0), transmit_power_dbm=100, name="ni" ) _nsink = infra.set_sink(pos=(1, 1), transmit_power_dbm=100, name="nsi") overlay = OverlayNetwork() bso1 = overlay.add_source(name="bso1") bso2 = overlay.add_source(name="bso2") bsi = overlay.set_sink(name="bsi") bin_ = overlay.add_intermediate(name="bin") eso1 = ENode(bso1, nso1) eso2 = ENode(bso2, nso2) # source1 connects to the sink over the intermediate source2 # connects both to the sink and to source1. overlay.add_link(bso1, bin_) overlay.add_link(bin_, bsi) overlay.add_link(bso2, bsi) overlay.add_link(bso2, bso1) embedding = PartialEmbedding( infra, overlay, source_mapping=[(bso1, eso1.node), (bso2, eso2.node)] ) # source1 can connect to the intermediate, which could be embedded # in any node (5). It could also connect to any other node as a # relay (4) -> 9. source2 can connect to the sink (1) or the other # source (1). It could also connect to any other node as a relay for # either of its two links (2 * 3) -> 8 No timeslot is used yet, so # there is just one timeslot option. assert len(embedding.possibilities()) == 9 + 8
def test_no_unnecessary_options(): """ Tests that no unnecessary connections are offered. """ infra = InfrastructureNetwork() # Two sources, one sink. Equal distance from both sources to sink. # One source with moderate transmit power (but enough to cover the # distance, one source with excessive transmit power. # transmit_power_dbm # power of 30dBm (similar to a regular router) which should easily # cover the distance of 1m without any noise. source_node = infra.add_source( pos=(0, 0), transmit_power_dbm=30, name="Source" ) sink_node = infra.set_sink(pos=(1, 3), transmit_power_dbm=0, name="Sink") overlay = OverlayNetwork() esource = ENode(overlay.add_source(), source_node) esink = ENode(overlay.set_sink(), sink_node) overlay.add_link(esource.block, esink.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[(esource.block, esource.node)] ) assert len(embedding.possibilities()) == 1 # embed the sink embedding.take_action(esource, esink, 0) # Now it would still be *feasible* according to add a connection to # the relay in the other timeslot. It shouldn't be possible however, # since all outgoing connections are already embedded. assert len(embedding.possibilities()) == 0
def test_count_timeslots_parallel(): """Tests correct counting behaviour with parallel connections""" infra = InfrastructureNetwork() # One source, one sink, two intermediates nsource = infra.add_source( pos=(0, 0), transmit_power_dbm=30, name="nsource" ) ninterm1 = infra.add_intermediate( pos=(1, 2), transmit_power_dbm=30, name="ninterm1" ) ninterm2 = infra.add_intermediate( pos=(1, -2), transmit_power_dbm=30, name="ninterm2" ) nsink = infra.set_sink(pos=(2, 0), transmit_power_dbm=30, name="nsink") overlay = OverlayNetwork() esource = ENode(overlay.add_source(name="bsource"), nsource) einterm1 = ENode(overlay.add_intermediate(name="binterm1"), ninterm1) einterm2 = ENode(overlay.add_intermediate(name="binterm2"), ninterm2) esink = ENode(overlay.set_sink(name="bsink"), nsink) # fork overlay.add_link(esource.block, einterm1.block) overlay.add_link(esource.block, einterm2.block) overlay.add_link(einterm1.block, esink.block) overlay.add_link(einterm2.block, esink.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[(esource.block, esource.node)] ) assert not embedding.is_complete() assert embedding.used_timeslots == 0 assert embedding.take_action(esource, einterm1, 0) assert embedding.take_action(esource, einterm2, 0) assert not embedding.is_complete() assert embedding.used_timeslots == 1 assert embedding.take_action(einterm1, esink, 1) assert not embedding.is_complete() assert embedding.used_timeslots == 2 assert embedding.take_action(einterm2, esink, 2) assert embedding.is_complete() assert embedding.used_timeslots == 3
def test_count_timeslots_loop(): """Tests reasonable counting behaviour with loops""" infra = InfrastructureNetwork() # One source, one sink, two intermediates nsource = infra.add_source(pos=(0, 0), transmit_power_dbm=30, name="nso") ninterm1 = infra.add_intermediate( pos=(2, 1), transmit_power_dbm=5, name="ni1" ) ninterm2 = infra.add_intermediate( pos=(0, -1), transmit_power_dbm=5, name="ni2" ) nsink = infra.set_sink(pos=(2, 0), transmit_power_dbm=30, name="nsi") overlay = OverlayNetwork() esource = ENode(overlay.add_source(name="bso"), nsource) einterm1 = ENode(overlay.add_intermediate(name="bi1"), ninterm1) einterm2 = ENode(overlay.add_intermediate(name="bi2"), ninterm2) esink = ENode(overlay.set_sink(name="bsi"), nsink) overlay.add_link(esource.block, einterm1.block) overlay.add_link(einterm1.block, esink.block) overlay.add_link(esink.block, einterm2.block) overlay.add_link(einterm2.block, esource.block) embedding = PartialEmbedding( infra, overlay, source_mapping=[(esource.block, esource.node)] ) assert not embedding.is_complete() assert embedding.used_timeslots == 0 assert embedding.take_action(esource, einterm1, 0) assert not embedding.is_complete() assert embedding.used_timeslots == 1 assert embedding.take_action(einterm1, esink, 1) assert not embedding.is_complete() assert embedding.used_timeslots == 2 assert embedding.take_action(esink, einterm2, 2) assert not embedding.is_complete() assert embedding.used_timeslots == 3 assert embedding.take_action(einterm2, esource, 1) assert embedding.is_complete() assert embedding.used_timeslots == 3
def parse_infra( nodes_file, sink_source_mapping, positions_file, source_seed, transmit_power_dbm, ): """Reads an infrastructure definition in MARVELO format from csvs""" # read the files names = [] capacities = [] positions = [] for (_id, name, capacity) in csv_to_list(nodes_file): names.append(name) capacities.append(float(capacity)) for csvline in csv_to_list(positions_file, sep=";"): positions.append([float(pos) for pos in csvline[1:]]) # the positions are saved in a weird format, probably a mistake when # saving positions = np.transpose(positions) specs = list(zip(names, capacities, positions)) (sink_idx, source_idx) = sink_source_mapping[(source_seed, len(specs))] if source_idx == sink_idx: return None # make sure source is always first, sink always last specs[0], specs[source_idx] = specs[source_idx], specs[0] specs[-1], specs[sink_idx] = specs[sink_idx], specs[-1] # construct the infrastructure from the gathered info infra = InfrastructureNetwork(bandwidth=1, noise_floor_dbm=-30) for (name, capacity, pos) in specs[:1]: infra.add_source(pos, transmit_power_dbm, capacity, name) for (name, capacity, pos) in specs[1:-1]: infra.add_intermediate(pos, transmit_power_dbm, capacity, name) for (name, capacity, pos) in specs[-1:]: infra.set_sink(pos, transmit_power_dbm, capacity, name) return infra
def random_infrastructure(self, num_sources: int, rand): """Generates a randomized infrastructure""" assert num_sources > 0 infra = InfrastructureNetwork() rand_node_args = lambda: { "pos": self.pos_dist(rand), "transmit_power_dbm": self.power_dist(rand), "capacity": self.capacity_dist(rand), } infra.set_sink(**rand_node_args()) for _ in range(num_sources): infra.add_source(**rand_node_args()) for _ in range(self.interm_nodes_dist(rand)): infra.add_intermediate(**rand_node_args()) return infra