-
Notifications
You must be signed in to change notification settings - Fork 14
/
robustmdt.py
171 lines (138 loc) · 5.84 KB
/
robustmdt.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
import numpy as np
from sklearn.tree import DecisionTreeClassifier
import pickle as pk
import fixed_env as env
import load_trace
from get_reward import get_reward
S_INFO = 5 # bit_rate, buffer_size, rebuffering_time, bandwidth_measurement, chunk_til_video_end
S_LEN = 8 # take how many frames in the past
A_DIM = 6
MPC_FUTURE_CHUNK_COUNT = 5
ACTOR_LR_RATE = 0.0001
CRITIC_LR_RATE = 0.001
VIDEO_BIT_RATE = [300, 750, 1200, 1850, 2850, 4300] # Kbps
BITRATE_REWARD = [1, 2, 3, 12, 15, 20]
BUFFER_NORM_FACTOR = 10.0
CHUNK_TIL_VIDEO_END_CAP = 48.0
TOTAL_VIDEO_CHUNKS = 48
M_IN_K = 1000.0
REBUF_PENALTY = 4.3 # 1 sec rebuffering -> 3 Mbps
SMOOTH_PENALTY = 1
DEFAULT_QUALITY = 0 # default video quality without agent
RANDOM_SEED = 42
RAND_RANGE = 1000000
SUMMARY_DIR = './results'
LOG_FILE = './results/log_robustmdt'
CHUNK_ON = 0
CHUNK_SWITCH = 1
OPTIMIZED = 2
HORIZON = 5
CHUNK_LEN = 4.0
BITRATE_NUM = 6
CHUNK_COMBO_OPTIONS = []
# past errors in bandwidth
past_errors = []
past_bandwidth_ests = []
class RobustMPCDT:
def __init__(self):
pass
def main(self, args, net_env=None, policy=None):
np.random.seed(RANDOM_SEED)
viper_flag = True
assert len(VIDEO_BIT_RATE) == A_DIM
log_f = LOG_FILE
if net_env is None:
viper_flag = False
all_cooked_time, all_cooked_bw, all_file_names = load_trace.load_trace(args.traces)
net_env = env.Environment(all_cooked_time=all_cooked_time, all_cooked_bw=all_cooked_bw,
all_file_names=all_file_names)
if not viper_flag and args.log:
log_path = LOG_FILE + '_' + net_env.all_file_names[net_env.trace_idx] + '_' + args.qoe_metric
log_file = open(log_path, 'wb')
time_stamp = 0
last_bit_rate = DEFAULT_QUALITY
bit_rate = DEFAULT_QUALITY
s_batch = [np.zeros((S_INFO, S_LEN))]
# a_batch = np.zeros((TOTAL_VIDEO_CHUNKS, 3))
r_batch = []
rollout = []
video_count = 0
reward_sum = 0
in_compute = []
# load dt policy
if policy is None:
with open(args.dt, 'rb') as f:
policy = pk.load(f)
# ========= @ zili: debug ========
# with open('decision_tree_ready/robustmpc_norway_500.pk3', 'rb') as f:
# baseline = pk.load(f)
while True: # serve video forever
delay, sleep_time, buffer_size, rebuf, video_chunk_size, next_video_chunk_sizes, end_of_video, \
video_chunk_remain = net_env.get_video_chunk(bit_rate)
time_stamp += delay # in ms
time_stamp += sleep_time # in ms
reward = get_reward(bit_rate, rebuf, last_bit_rate, args.qoe_metric)
r_batch.append(reward)
reward_sum += reward
last_bit_rate = bit_rate
if args.log:
# log time_stamp, bit_rate, buffer_size, reward
log_file.write(bytes(str(time_stamp / M_IN_K) + '\t' +
str(VIDEO_BIT_RATE[bit_rate]) + '\t' +
str(buffer_size) + '\t' +
str(rebuf) + '\t' +
str(video_chunk_size) + '\t' +
str(delay) + '\t' +
str(reward) + '\n', encoding='utf-8'))
log_file.flush()
# retrieve previous state
if len(s_batch) == 0:
state = [np.zeros((S_INFO, S_LEN))]
else:
state = np.array(s_batch[-1], copy=True)
# dequeue history record
state = np.roll(state, -1, axis=1)
# this should be S_INFO number of terms
state[0, -1] = VIDEO_BIT_RATE[bit_rate] / float(np.max(VIDEO_BIT_RATE)) # last quality
state[1, -1] = buffer_size / BUFFER_NORM_FACTOR
state[2, -1] = rebuf
state[3, -1] = float(video_chunk_size) / float(delay) / M_IN_K # kilo byte / ms
state[4, -1] = np.minimum(video_chunk_remain, CHUNK_TIL_VIDEO_END_CAP) / float(CHUNK_TIL_VIDEO_END_CAP)
# state[5: 10, :] = future_chunk_sizes / M_IN_K / M_IN_K
serialized_state = serial(state)
bit_rate = int(policy.predict([serialized_state])[0])
rollout.append((state, bit_rate, serialized_state))
s_batch.append(state)
# ======== @ zili: debug ========
# if video_chunk_remain > 0:
# a_batch[TOTAL_VIDEO_CHUNKS - video_chunk_remain][0] = bit_rate
# a_batch[TOTAL_VIDEO_CHUNKS - video_chunk_remain][2] = int(baseline.predict([serialized_state])[0])
if end_of_video:
# print(a_batch)
if args.log:
log_file.write(bytes('\n', encoding='utf-8'))
log_file.close()
print("video count", video_count)
last_bit_rate = DEFAULT_QUALITY
bit_rate = DEFAULT_QUALITY # use the default action here
r_batch = []
in_compute = []
if viper_flag:
return rollout
else:
video_count += 1
if video_count >= len(net_env.all_file_names):
break
if args.log:
log_path = log_f + '_' + net_env.all_file_names[net_env.trace_idx] + '_' + args.qoe_metric
log_file = open(log_path, 'wb')
return reward_sum
def serial(state):
serialized_state = []
serialized_state.append(state[0, -1])
serialized_state.append(state[1, -1])
serialized_state.append(state[2, -1])
for i in range(5):
serialized_state.append(state[3, i])
serialized_state.append(state[4, -1])
return serialized_state