-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_users.py
More file actions
130 lines (98 loc) · 4.38 KB
/
Copy pathgenerate_users.py
File metadata and controls
130 lines (98 loc) · 4.38 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
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
import json, math, os, random
import numpy as np
from shapely.geometry import Polygon, box
from scripts import config
def generate_users():
# ============================================================
# Polygon Region
# ============================================================
polygon = Polygon(config.POLYGON_COORDS)
# ============================================================
# Distance
# ============================================================
def calculate_distance_m(lat1, lon1, lat2, lon2):
R = 6371000
phi1 = math.radians(lat1)
phi2 = math.radians(lat2)
dphi = math.radians(lat2 - lat1)
dlambda = math.radians(lon2 - lon1)
a = (
math.sin(dphi / 2) ** 2 +
math.cos(phi1) *
math.cos(phi2) *
math.sin(dlambda / 2) ** 2
)
return 2 * R * math.atan2(math.sqrt(a), math.sqrt(1 - a))
# ============================================================
# Load Infrastructure
# ============================================================
with open(config.RU_OUTPUT_FILE, "r") as f:
primary_rus = json.load(f)["radio_units"]
with open(config.RU_BACKUP_FILE, "r") as f:
backup_rus = json.load(f)["radio_units_backup"]
with open(config.HOTSPOT_FILE, "r") as f:
hotspots = json.load(f)["hotspots"]
with open(config.DEMAND_POINTS_FILE, "r") as f:
demand_points = json.load(f)["demand_points"]
# ============================================================
# Coverage Check
# ============================================================
def is_within_range(lat1, lon1, lat2, lon2, radius, margin=5):
return calculate_distance_m(lat1, lon1, lat2, lon2) <= (radius + margin)
# ============================================================
# Random User Position Inside Grid Square
# ============================================================
def generate_random_coordinate_in_square(center_lat, center_lon, size):
half_size = size / 2
offset_lat = random.uniform(-half_size, half_size) / 111320
offset_lon = random.uniform(-half_size, half_size) / (111320 * math.cos(math.radians(center_lat)))
return (center_lat + offset_lat, center_lon + offset_lon)
# ============================================================
# Hotspot Detection
# ============================================================
def get_hotspot(lat, lon):
for hotspot in hotspots:
if is_within_range(lat, lon, hotspot["latitude"], hotspot["longitude"], hotspot["size"], margin=0):
return hotspot["demand_id"]
return None
# ============================================================
# Generate Users
# ============================================================
print("Generating users...")
results = []
for dp in demand_points:
for user_id in range(dp["users"]):
user_lat, user_lon = generate_random_coordinate_in_square(
dp["latitude"],
dp["longitude"],
dp["size"]
)
user_info = {
"user_id": f"{dp['demand_id']}_user_{user_id+1}",
"latitude": user_lat,
"longitude": user_lon,
"assigned_ru": [],
"hotspot_RUs": [],
"hotspot_name": get_hotspot(
user_lat,
user_lon
)
}
# Primary RUs
for ru in primary_rus:
if is_within_range(user_lat, user_lon, ru["latitude"], ru["longitude"], ru["range_m"], margin=10):
user_info["assigned_ru"].append(ru["ru_name"])
# Backup RUs
for ru in backup_rus:
if is_within_range(user_lat, user_lon, ru["latitude"], ru["longitude"], ru["range_m"], margin=10):
user_info["hotspot_RUs"].append(ru["ru_name"])
results.append(user_info)
# ============================================================
# Save Users
# ============================================================
with open(config.USER_MAP_FILE, "w") as f:
json.dump(results, f, indent=4)
print(f"Generated {len(results)} users")
print(f"Saved to: {config.USER_MAP_FILE}")
if __name__ == "__main__":
generate_users()