-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBandwidthEstimator.py
102 lines (90 loc) · 4.08 KB
/
BandwidthEstimator.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch
import numpy as np
from utils.packet_info import PacketInfo
from utils.packet_record import PacketRecord
from deep_rl.actor_critic import ActorCritic
UNIT_M = 1000000
MAX_BANDWIDTH_MBPS = 8
MIN_BANDWIDTH_MBPS = 0.01
LOG_MAX_BANDWIDTH_MBPS = np.log(MAX_BANDWIDTH_MBPS)
LOG_MIN_BANDWIDTH_MBPS = np.log(MIN_BANDWIDTH_MBPS)
def liner_to_log(value):
# from 10kbps~8Mbps to 0~1
value = np.clip(value / UNIT_M, MIN_BANDWIDTH_MBPS, MAX_BANDWIDTH_MBPS)
log_value = np.log(value)
return (log_value - LOG_MIN_BANDWIDTH_MBPS) / (LOG_MAX_BANDWIDTH_MBPS - LOG_MIN_BANDWIDTH_MBPS)
def log_to_linear(value):
# from 0~1 to 10kbps to 8Mbps
value = np.clip(value, 0, 1)
log_bwe = value * (LOG_MAX_BANDWIDTH_MBPS - LOG_MIN_BANDWIDTH_MBPS) + LOG_MIN_BANDWIDTH_MBPS
return np.exp(log_bwe) * UNIT_M
class Estimator(object):
def __init__(self, model_path="./model/pretrained_model.pth", step_time=60):
# model parameters
state_dim = 4
action_dim = 1
# the std var of action distribution
exploration_param = 0.05
# load model
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.model = ActorCritic(state_dim, action_dim, exploration_param, self.device).to(self.device)
self.model.load_state_dict(torch.load(model_path))
# the model to get the input of model
self.packet_record = PacketRecord()
self.packet_record.reset()
self.step_time = step_time
# init
states = [0.0, 0.0, 0.0, 0.0]
torch_tensor_states = torch.FloatTensor(torch.Tensor(states).reshape(1, -1)).to(self.device)
action, action_logprobs, value = self.model.forward(torch_tensor_states)
self.bandwidth_prediction = log_to_linear(action)
self.last_call = "init"
def report_states(self, stats: dict):
'''
stats is a dict with the following items
{
"send_time_ms": uint,
"arrival_time_ms": uint,
"payload_type": int,
"sequence_number": uint,
"ssrc": int,
"padding_length": uint,
"header_length": uint,
"payload_size": uint
}
'''
self.last_call = "report_states"
# clear data
packet_info = PacketInfo()
packet_info.payload_type = stats["payload_type"]
packet_info.ssrc = stats["ssrc"]
packet_info.sequence_number = stats["sequence_number"]
packet_info.send_timestamp = stats["send_time_ms"]
packet_info.receive_timestamp = stats["arrival_time_ms"]
packet_info.padding_length = stats["padding_length"]
packet_info.header_length = stats["header_length"]
packet_info.payload_size = stats["payload_size"]
packet_info.bandwidth_prediction = self.bandwidth_prediction
self.packet_record.on_receive(packet_info)
def get_estimated_bandwidth(self)->int:
if self.last_call and self.last_call == "report_states":
self.last_call = "get_estimated_bandwidth"
# calculate state
states = []
receiving_rate = self.packet_record.calculate_receiving_rate(interval=self.step_time)
states.append(liner_to_log(receiving_rate))
delay = self.packet_record.calculate_average_delay(interval=self.step_time)
states.append(min(delay/1000, 1))
loss_ratio = self.packet_record.calculate_loss_ratio(interval=self.step_time)
states.append(loss_ratio)
latest_prediction = self.packet_record.calculate_latest_prediction()
states.append(liner_to_log(latest_prediction))
# make the states for model
torch_tensor_states = torch.FloatTensor(torch.Tensor(states).reshape(1, -1)).to(self.device)
# get model output
action, action_logprobs, value = self.model.forward(torch_tensor_states)
# update prediction of bandwidth by using action
self.bandwidth_prediction = log_to_linear(action)
return self.bandwidth_prediction