-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_simulation_gui.py
232 lines (184 loc) · 8.03 KB
/
new_simulation_gui.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from lifi import LifiAccessPoint
from wifi import WiFiAccessPoint
from propose import ProposedMethod
from conventional import ConventionalMethod
from new_mobility import Mobility
from user import User
from blockage import check_blockage, generate_random_variable
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
from tkinter import Tk, Canvas, Label, Button
# Simulation parameters
ROOM_WIDTH = 5.0
ROOM_HEIGHT = 5.0
USER_HEIGHT = 0.8
GRID_SIZE = 0.1
X_GRID = [round(i * GRID_SIZE, 1) for i in range(int(ROOM_WIDTH / GRID_SIZE) + 1)]
Y_GRID = [round(i * GRID_SIZE, 1) for i in range(int(ROOM_HEIGHT / GRID_SIZE) + 1)]
WIFI_AP = WiFiAccessPoint(ap_id='W', ap_position=(2.5, 2.5, 5), transmit_power=1e-3, noise_psd=10**(-174/10), bandwidth=20e6, sigma=10)
LIFI_APS = [
LifiAccessPoint(x=1.25, y=1.25),
LifiAccessPoint(x=1.25, y=3.75),
LifiAccessPoint(x=3.75, y=3.75),
LifiAccessPoint(x=3.75, y=1.25)
]
MOBILITY_CONFIG = {
'type': 'random_walk',
'step_size': 0.5,
'room_x': ROOM_WIDTH,
'room_y': ROOM_HEIGHT
}
NUMBER_OF_USERS = 10
# Lists to store throughput for each user
conventional_throughputs = []
proposed_throughputs = []
# Create lists to store user positions for plotting
user_positions_x = [[] for _ in range(NUMBER_OF_USERS)]
user_positions_y = [[] for _ in range(NUMBER_OF_USERS)]
# Create Tkinter window
root = Tk()
root.title("Wireless Communication Simulation")
root.geometry("500x500")
graph = Tk()
graph.title("Graph")
graph.geometry("900x900")
# Create Matplotlib figure
fig, ax = plt.subplots(figsize=(4, 4))
# Create canvas for Matplotlib figure
canvas = FigureCanvasTkAgg(fig, master=graph)
canvas_widget = canvas.get_tk_widget()
canvas_widget.grid(row=0, column=0, padx=10, pady=10)
# Create labels and buttons
label_user = Label(root, text="User")
label_user.grid(row=0, column=1, padx=10, pady=5)
label_router = Label(root, text="Router")
label_router.grid(row=0, column=2, padx=10, pady=5)
label_wifi_snr = Label(root, text="WiFi SINR")
label_wifi_snr.grid(row=0, column=3, padx=10, pady=5)
label_lifi_snr = Label(root, text="LiFi SINR")
label_lifi_snr.grid(row=0, column=4, padx=10, pady=5)
button_next = Button(root, text="Next", command=lambda: update_simulation())
button_next.grid(row=0, column=5, padx=10, pady=5)
button_stop = Button(root, text="Stop", command=lambda: handleDestroy())
button_stop.grid(row=0, column=0, padx=10, pady=5)
# Lists to store labels for each user
user_labels = []
router_labels = []
wifi_snr_labels = []
lifi_snr_labels = []
# Initialize the users and the simulation
users = [Mobility(np.random.uniform(0, ROOM_WIDTH), np.random.uniform(0, ROOM_HEIGHT), MOBILITY_CONFIG) for _ in range(NUMBER_OF_USERS)]
# Create labels for each user
for i in range(NUMBER_OF_USERS):
user_label = Label(root, text=f"User {i + 1}")
user_label.grid(row=i+1, column=1, padx=10, pady=5)
user_labels.append(user_label)
router_label = Label(root, text="")
router_label.grid(row=i+1, column=2, padx=10, pady=5)
router_labels.append(router_label)
wifi_snr_label = Label(root, text="")
wifi_snr_label.grid(row=i+1, column=3, padx=10, pady=5)
wifi_snr_labels.append(wifi_snr_label)
lifi_snr_label = Label(root, text="")
lifi_snr_label.grid(row=i+1, column=4, padx=10, pady=5)
lifi_snr_labels.append(lifi_snr_label)
def handleDestroy():
root.destroy()
graph.destroy()
# Function to calculate the SNRs and connect the user to the best router
def calculate_snrs(user):
wifi_snrs = []
lifi_snrs = []
wifi_proportion_time = 0
lifi_propotion_time = 0
best_router = ""
blockage_prob = check_blockage(mean_occurrence_rate=1/10)
# Calculate WiFi channel gain
u = User('U', (user.x, user.y, USER_HEIGHT))
wifi_channel_gain = WIFI_AP.calculate_channel_gain(u, fc=2.4e9)
wifi_snr = WIFI_AP.calculate_snr(wifi_channel_gain)
# Calculate LiFi channel gains
blockage_present = [generate_random_variable(blockage_prob) for _ in LIFI_APS]
lifi_snr = []
for i in range(len(LIFI_APS)):
if blockage_present[i] == 1:
print(f"Blockage present from LiFi AP {i + 1}")
lifi_snr.append(10 * np.log10(LIFI_APS[i].signal_to_noise_ratio_nlos(user.x, user.y)))
else:
lifi_snr.append(10 * np.log10(LIFI_APS[i].signal_to_noise_ratio(user.x, user.y)))
print(f"WiFi SNR: {wifi_snr} dB, LiFi SNRs: {lifi_snr}")
if wifi_snr > max(lifi_snr):
best_router = 'H_W'
wifi_snrs.append(wifi_snr)
wifi_proportion_time += 1
else:
best_router = max([(lifi_snr[i], f'H_L{i + 1}') for i in range(len(lifi_snr))], key=lambda x: x[0])[1]
lifi_snrs.append(max(lifi_snr))
lifi_propotion_time += 1
# Connect the user to the best router based on the decision
if best_router == 'H_W':
print("Connecting to WiFi (W)")
else:
# Extract the router number from the key (e.g., 'H_L1' -> 'L1')
router_number = best_router.split('_')[1]
print(f"Connecting to LiFi ({router_number})")
return wifi_snr, max(lifi_snr), best_router, wifi_snrs, lifi_snrs, wifi_proportion_time, lifi_propotion_time
def update_simulation():
for user_index in range(NUMBER_OF_USERS):
# Move the user
user = users[user_index]
user.move()
# Store user positions for plotting
user_positions_x[user_index].append(users[user_index].x)
user_positions_y[user_index].append(users[user_index].y)
print(f"User {user_index + 1} Position: ({users[user_index].x:.2f}, {users[user_index].y:.2f})")
# Calculate SNRs
wifi_snr, lifi_snr, best_router, wifi_snrs, lifi_snrs, wifi_proportion_time, lifi_propotion_time = calculate_snrs(user)
# Update the labels
user_labels[user_index].config(text=f"User {user_index + 1}")
router_labels[user_index].config(text=best_router)
wifi_snr_labels[user_index].config(text=f"{wifi_snr:.2f} dB")
lifi_snr_labels[user_index].config(text=f"{lifi_snr:.2f} dB")
# Calculate average throughput
wifi_avg_throughput_proposed = ProposedMethod(0, wifi_snrs, wifi_proportion_time).avg_throughput()
lifi_avg_throughput_proposed = ProposedMethod(1, lifi_snrs, lifi_propotion_time).avg_throughput()
wifi_avg_throughput_conventional = ConventionalMethod(0, wifi_snrs, wifi_proportion_time).avg_throughput()
lifi_avg_throughput_conventional = ConventionalMethod(1, lifi_snrs, lifi_propotion_time).avg_throughput()
print("Conventional Average Throughput (Mbps): ", (wifi_avg_throughput_conventional + lifi_avg_throughput_conventional)/3000)
print("Proposed Average Throughput (Mbps): ", (wifi_avg_throughput_proposed + lifi_avg_throughput_proposed)/4000)
# Append the throughput for this user to the list
conventional_throughputs.append((wifi_avg_throughput_conventional + lifi_avg_throughput_conventional) / 3000)
proposed_throughputs.append((wifi_avg_throughput_proposed + lifi_avg_throughput_proposed) / 4000)
# Plot user movements
ax.clear()
for i in range(NUMBER_OF_USERS):
ax.plot(user_positions_x[i], user_positions_y[i], label=f'User {i+1}')
ax.set_title('User Movements in the Room')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.legend()
ax.grid(True)
canvas.draw()
# Start the Tkinter main loop
root.mainloop()
# Calculate overall average throughput across all users
avg_conventional_throughput = np.mean(conventional_throughputs)
avg_proposed_throughput = np.mean(proposed_throughputs)
print("="*20)
print("")
print("")
print("")
print("")
print("Conventional Overall Average Throughput (Mbps): ", avg_conventional_throughput)
print("Proposed Overall Average Throughput (Mbps): ", avg_proposed_throughput)
# Plot throughput
# plt.figure(figsize=(8, 8))
# plt.plot(conventional_throughputs, label='Conventional')
# plt.plot(proposed_throughputs, label='Proposed')
# plt.title('Average Throughput')
# plt.xlabel('Iteration')
# plt.ylabel('Throughput (Mbps)')
# plt.legend()
# plt.grid(True)
# plt.show()