def simpy_medium(env): def listen(packet): assert packet.payload == TEST_MESSAGE1 TEST_MESSAGE1 = "TEST1" TEST_MESSAGE2 = "TEST2" t = TransmissionMedium(env) d1 = Device(1, env, [20]) d2 = Device(2, env, [20]) d1.on_receive += listen t.add_device(d1) t.add_device(d2) # this one should be successful d1.send(TEST_MESSAGE1, d1.MTU) yield env.timeout(1.1) assert t.is_busy(d1) == False d1.sleep() # this should be sucessful as well d1.send(TEST_MESSAGE2, 2 * d1.MTU) yield env.timeout(1.1) assert t.is_busy(d1) == True yield env.timeout(1.1) assert t.is_busy(d1) == False # collision d1.send(TEST_MESSAGE1, 2 * d1.MTU) yield env.timeout(1) d2.wake_up() # collision d2.send(TEST_MESSAGE1, 2 * d2.MTU) yield env.timeout(1.1) assert t.is_busy(d2) == True yield env.timeout(1.1) assert t.is_busy(d2) == False
def _run(self, env, pr): if self.use_seed: seeds = [i for i in range(self.num_nodes + 1)] numpy.random.seed(0) random.seed(0) else: seeds = [random.randint(0, self.num_nodes * 1000) for i in range(self.num_nodes + 1)] special_args = {"seed": seeds[0]} name = self.log_prefix + str(pr) t = TransmissionMedium(env, name) t.add_logger(name) bs = create_basestation(self.protocol_type, 0, env, "default.json", special_args) t.add_device(bs) nodes = [] for i in range(self.num_nodes): special_arg = {"total": self.num_nodes, "scheduled_time": i, "seed": seeds[i]} n = create_node(self.protocol_type, i, env, "default.json", special_arg) nodes.append(n) t.add_device(n) rate = pr * len(nodes) dummy_payload = "Test" while True: num_of_trans = numpy.random.poisson(rate) nodes_to_trans = random.sample(nodes, num_of_trans) for n in nodes_to_trans: n.send(dummy_payload, n.MTU) sleep_time = numpy.random.uniform(0.5, 1.5) yield env.timeout(sleep_time)
def test_LPDQ(): env = simpy.Environment() rates = [30] slot_t = 0.1 feedback_t = 0.1 random_list = range(10) t = TransmissionMedium(env) bs = LPDQBaseStation(0, env, 0, 3, rates=rates, jitter_range=0.0001, feedback_t=feedback_t, slot_t=slot_t) t.add_device(bs) nodes = [] TOTAL = 10 for i in range(1, TOTAL + 1): node = LPDQNode(i, env, feedback_t=feedback_t, slot_t=slot_t, m=3, rates=rates, guard=0.01, seed=i, jitter_range=0.001) t.add_device(node) nodes.append(node) env.process(simpy_LPDQ(env, nodes, bs)) env.run(until=20)
def _run(self, env, simulation_id): # load configure json with open("river/{0}.json".format(self.bs_name)) as f: configure = json.load(f) num_nodes = len(configure) if self.use_seed: seeds = [i for i in range(num_nodes)] numpy.random.seed(0) random.seed(0) else: seeds = [ random.randint(0, num_nodes * 1000) for i in range(num_nodes + 1) ] special_args = {"seed": seeds[0]} name = self.log_prefix + str(simulation_id) # add a PHY layer layer = BPSK(None) # no threshold so far t = TransmissionMedium(env, name, layer=layer) t.add_logger(name) bs = create_basestation(self.protocol_type, 0, env, configure[0], special_args) t.add_device(bs) nodes = [] for i in range(1, len(configure)): config = configure[i] special_arg = { "total": len(configure) - 1, "scheduled_time": i, "seed": seeds[i] } n = create_node(self.protocol_type, i, env, config, special_arg) nodes.append(n) t.add_device(n) dummy_payload = "Test" while True: for n in nodes: size = random.randrange(24, 48) # random payload size n.send(dummy_payload, size) sleep_time = 3 * 60 / 0.2 # each tick is 0.2 seconds TODO: switch to Realistic Environment yield env.timeout(sleep_time)
def test_path_loss(): rates = [20] env = simpy.Environment() d1 = Device(env=env, id=0, rates=[20], lat=0, lng=0) d2 = Device(env=env, id=1, rates=[20], lat=0.1, lng=0.1) layer = PHYLayer(120, 10000, 1) # 10 KHz bandwidth. won't be used in the simulation t = TransmissionMedium(env, layer=layer) t.add_device(d1) t.add_device(d2) def on_receive1(packet): assert packet.id == 0 def on_receive2(packet): assert False # should not receive d2.on_receive += on_receive1 def sim_test(): d1.send("test", size=10) yield env.timeout(1) d2.on_receive -= on_receive1 d2.on_receive += on_receive2 # test no receive t.layer.threshold = 10 d1.send("test", size=10) yield env.timeout(1) env.process(sim_test()) env.run(until=10)
def test_LORA(): env = simpy.Environment() rates = [30] t = TransmissionMedium(env) bs = LORABaseStation(0, env, rates) t.add_device(bs) nodes = [] TOTAL = 3 for i in range(1, TOTAL + 1): node = LORANode(i, env, rates, guard=0.01, seed=i, jitter_range=0.001) t.add_device(node) nodes.append(node) env.process(simpy_LORA(env, nodes, bs)) env.run(until=40)
def _run(self, env, pr): if self.use_seed: seeds = [i for i in range(self.num_nodes + 1)] numpy.random.seed(0) random.seed(0) else: seeds = [random.randint(0, self.num_nodes * 1000) for i in range(self.num_nodes + 1)] special_args = {"seed": seeds[0]} name = self.log_prefix + str(pr) with open("100.json") as f: config = json.load(f) layer = PHYLayer(120, 10000, 1) # 10 KHz bandwidth. won't be used in the simulation t = TransmissionMedium(env, name, layer=layer) t.add_logger(name) bs = create_basestation(self.protocol_type, 0, env, config, special_args) t.add_device(bs) nodes = [] for i in range(self.num_nodes): special_arg = {"total": self.num_nodes, "scheduled_time": i, "seed": seeds[i]} n = create_node(self.protocol_type, i, env, config, special_arg) nodes.append(n) t.add_device(n) rate = pr * len(nodes) dummy_payload = "Test" ADJUST_FACTOR = 4 # this is for DQN adjustment load = rate if self.protocol_type != 3 else rate / config["N"] * ADJUST_FACTOR while True: num_of_trans = numpy.random.poisson(load) nodes_to_trans = random.sample(nodes, num_of_trans) for n in nodes_to_trans: n.send(dummy_payload, int(n.MTU / ADJUST_FACTOR)) sleep_time = numpy.random.uniform(0, 2) yield env.timeout(sleep_time)
def simpy_bs(env): MESSAGE1 = "TEST1" MESSAGE2 = "TEST2" def process(packet): # only receives test1 becaus we let test2 collides with other packets assert packet.payload == MESSAGE1 bs = Device(env = env, id =1, rates=[20]) bs.process = process # simply way to override the method d1 = Device(2, env, rates=[20]) d2 = Device(3, env, rates=[20]) t = TransmissionMedium(env) t.add_device(bs) t.add_device(d1) t.add_device(d2) d1.send(MESSAGE1, 1 * d1.MTU) yield env.timeout(1.1) # both message should be dropped d2.send(MESSAGE2, 2 * d2.MTU) yield env.timeout(1.1) d1.send(MESSAGE2, 1 * d1.MTU) assert t.is_busy(d1) == True
def test_DQN_path_loss(): env = simpy.Environment() rates = [30] slot_t = 0.5 feedback_t = 0.5 N = 16 bs_seed = 0 m = 4 layer = PHYLayer( 120, 10000, 1) # the details doesn't matter as random mock will be used here t = TransmissionMedium(env, layer=layer) bs = DQNBaseStation(0, env, N, bs_seed, m, rates=rates, jitter_range=0.0001, feedback_t=feedback_t, slot_t=slot_t) t.add_device(bs) node = DQNNode(1, env, N=N, feedback_t=feedback_t, slot_t=slot_t, m=m, rates=rates, guard=0.01, seed=1, jitter_range=0.001) t.add_device(node) node.random = RandomMock([0]) env.process(path_loss_DQN(env, node, bs)) env.run(until=150)
def test_DQN(): env = simpy.Environment() rates = [30] slot_t = 0.5 feedback_t = 0.5 N = 16 random_list = range(10) bs_seed = 0 m = 4 t = TransmissionMedium(env) bs = DQNBaseStation(0, env, N, bs_seed, m, rates=rates, jitter_range=0.0001, feedback_t=feedback_t, slot_t=slot_t) t.add_device(bs) nodes = [] TOTAL = 10 for i in range(1, TOTAL + 1): node = DQNNode(i, env, N=N, feedback_t=feedback_t, slot_t=slot_t, m=m, rates=rates, guard=0.01, seed=i, jitter_range=0.001) t.add_device(node) nodes.append(node) env.process(simpy_DQN(env, nodes, bs)) env.run(until=150)
def test_LPDQ_loss(): env = simpy.Environment() rates = [30] slot_t = 0.1 feedback_t = 0.1 # need to set up transmission medium for this one to simulate the path loss layer = PHYLayer( 120, 10000, 1) # the details doesn't matter as random mock will be used here t = TransmissionMedium(env, layer=layer) bs = LPDQBaseStation(0, env, 0, 3, rates=rates, jitter_range=0.0001, feedback_t=feedback_t, slot_t=slot_t) t.add_device(bs) bs.random = RandomMock([0]) node = LPDQNode(1, env, feedback_t=feedback_t, slot_t=slot_t, m=3, rates=rates, guard=0.01, seed=1, jitter_range=0.001) node.random = RandomMock([0, 1, 0]) t.add_device(node) env.process(LPDQ_path_loss(env, node, bs)) env.run(until=10)
def setup_sim(self, name): env = simpy.Environment() t = TransmissionMedium(env, name) t.add_logger(name) return env, t