-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_gen.py
102 lines (93 loc) · 3.45 KB
/
data_gen.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
import datetime
import os
from itertools import combinations
from functools import reduce
import sys
from random import shuffle
import numpy as np
record = None
server_num, client_num, time_len = 0, 0, 0
pressure = 0.6
qos_lim = 0
qos = None
dist_matrix = None
def ask(msg: str, default: int):
inputed = input(msg)
if inputed.strip():
return int(inputed.strip())
return default
def read_input():
global server_num, client_num, time_len, pressure
server_num = ask('input server number (default 100):', 100)
client_num = ask('input client number (default 10):', 10)
time_len = ask('input time length (default 100):', 100)
inp = input('input pressure 0.01-0.99 (default 0.6):')
if inp.strip():
pressure = float(inp.strip())
def distribute_server():
global record, qos_lim, qos
qos_lim = np.random.randint(200, 500)
qos = np.ceil(np.random.normal(qos_lim, 50, size=(server_num, client_num))).astype('int32')
mask = np.random.randn()
for t_idx in range(time_len):
dis_bd = np.random.randint(500, 300000, size=(server_num, client_num)) * (qos < qos_lim)
mask = np.random.randn(server_num, client_num) > 0.35
dis_bd = dis_bd * mask
record[t_idx] = dis_bd
def gen_client_name(n: int):
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
out = list(combinations(alphabet, 2))
out = [ reduce(lambda x, y: x+y, each) for each in out ]
out += [ i for i in alphabet ]
shuffle(out)
out = out[:n]
out.sort()
return out
def gen_server_name(n: int):
cand = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789'
out = list(combinations(cand, 2))
out = [ reduce(lambda x, y: x+y, each) for each in out ]
shuffle(out)
out = out[:n]
out.sort()
return out
def output(path: str):
curr_time = datetime.datetime(2021, 11, 1, 0, 0)
time_sep = datetime.timedelta(minutes=5)
sname = gen_server_name(server_num)
cname = gen_client_name(client_num)
with open(os.path.join(path, 'demand.csv'), 'w') as f:
f.write('mtime,' + ','.join(cname) + '\r\n')
for t_idx in range(time_len):
time_str = curr_time.strftime("%Y-%m-%dT%H:%M")
c_demand = record[t_idx].sum(axis=0)
c_demand = [ str(i) for i in c_demand ]
f.write(time_str + ',' + ','.join(c_demand) + '\r\n')
curr_time += time_sep
with open(os.path.join(path, 'site_bandwidth.csv'), 'w') as f:
f.write('site_name,bandwidth\r\n')
for s_idx in range(server_num):
bd_used = record.sum(axis=-1)[:, s_idx].max()
bd_upper = np.ceil(bd_used / pressure).astype('int32')
f.write(f'{sname[s_idx]},{bd_upper}\r\n')
with open(os.path.join(path, 'qos.csv'), 'w') as f:
f.write('site_name,' + ','.join(cname) + '\r\n')
for s_idx in range(server_num):
s = sname[s_idx]
qos_list = qos[s_idx]
qos_list = [ str(i) for i in qos_list ]
qos_str = ','.join(qos_list)
f.write(f'{s},{qos_str}\r\n')
with open(os.path.join(path, 'config.ini'), 'w') as f:
f.write('[config]\r\n')
f.write(f'qos_constraint={qos_lim}\r\n')
if __name__ == '__main__':
read_input()
record = np.zeros((time_len, server_num, client_num), dtype=np.int32)
distribute_server()
if len(sys.argv) == 1:
output('pressure_data')
else:
try: os.mkdir(sys.argv[1])
except: pass
output(sys.argv[1])