-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwireless_models.py
More file actions
104 lines (85 loc) · 2.98 KB
/
Copy pathwireless_models.py
File metadata and controls
104 lines (85 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import simpy
import numpy as np
from dataclasses import dataclass
@dataclass
class Packet:
user_id: int
traffic_type: str
size_bits: int
arrival_time: float
@dataclass
class UserStats:
generated_packets: int = 0
served_packets: int = 0
dropped_packets: int = 0
served_bits: int = 0
total_delay: float = 0.0
def jains_fairness_index(throughputs):
x = np.array(throughputs, dtype=float)
if np.sum(x) == 0:
return 0.0
return (np.sum(x) ** 2) / (len(x) * np.sum(x ** 2) + 1e-12)
def normalized_throughput(total_bits_served, link_capacity_bps, interval_s):
if interval_s <= 0:
return 0.0
return min(total_bits_served / (link_capacity_bps * interval_s), 1.0)
class PoissonTraffic:
def __init__(self, env, ap, user_id, rate_pps, packet_size_bits):
self.env = env
self.ap = ap
self.user_id = user_id
self.rate_pps = rate_pps
self.packet_size_bits = packet_size_bits
def run(self):
while True:
inter_arrival = np.random.exponential(1.0 / self.rate_pps)
yield self.env.timeout(inter_arrival)
pkt = Packet(
user_id=self.user_id,
traffic_type="Poisson",
size_bits=self.packet_size_bits,
arrival_time=self.env.now
)
self.ap.enqueue_packet(pkt)
class OnOffTraffic:
def __init__(self, env, ap, user_id, on_rate_pps, packet_size_bits, mean_on=2.0, mean_off=1.0):
self.env = env
self.ap = ap
self.user_id = user_id
self.on_rate_pps = on_rate_pps
self.packet_size_bits = packet_size_bits
self.mean_on = mean_on
self.mean_off = mean_off
def run(self):
while True:
on_duration = np.random.exponential(self.mean_on)
off_duration = np.random.exponential(self.mean_off)
end_on = self.env.now + on_duration
while self.env.now < end_on:
inter_arrival = np.random.exponential(1.0 / self.on_rate_pps)
yield self.env.timeout(inter_arrival)
pkt = Packet(
user_id=self.user_id,
traffic_type="ON/OFF",
size_bits=self.packet_size_bits,
arrival_time=self.env.now
)
self.ap.enqueue_packet(pkt)
yield self.env.timeout(off_duration)
class PeriodicTraffic:
def __init__(self, env, ap, user_id, period_s, packet_size_bits):
self.env = env
self.ap = ap
self.user_id = user_id
self.period_s = period_s
self.packet_size_bits = packet_size_bits
def run(self):
while True:
yield self.env.timeout(self.period_s)
pkt = Packet(
user_id=self.user_id,
traffic_type="Periodic",
size_bits=self.packet_size_bits,
arrival_time=self.env.now
)
self.ap.enqueue_packet(pkt)