-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathUtilities.py
More file actions
49 lines (36 loc) · 1.4 KB
/
Utilities.py
File metadata and controls
49 lines (36 loc) · 1.4 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
import sys
import numpy as np
from tqdm import tqdm
from collections import namedtuple
Endpoint = namedtuple('Endpoint', ['id', 'lat', 'con'])
Request = namedtuple('Request', ['vid', 'eid', 'n'])
Scoring = namedtuple('Scoring', ['vid', 'cid', 'score'])
def csv_print(mat, fmt='%.5f'):
np.savetxt(sys.stdout.buffer, mat, fmt=fmt, newline="\n")
def sort_array_with_id(arr):
"""
Sort array ascending and keep track of ids.
:param arr: array with values
:return: array with tuples (id, val)
"""
tuple_arr = [(id, arr[id]) for id in range(len(arr))]
return sorted(tuple_arr, key=lambda t: t[1])
def compute_solution_score(videos_on_cache, requests, endpoints):
score = 0.0
total_req = 0
for req in tqdm(requests, desc="Computing score"):
# find caches that are connected to eid and have vid
_lats = [endpoints[req.eid].lat]
# check if video vid is on cache
for _ci, _c in enumerate(videos_on_cache):
if req.vid in _c:
# check if endpoint eid is connected to cache
for _cc in endpoints[req.eid].con:
if _ci == _cc[0]:
_lats.append(_cc[1])
min_lat = np.min(_lats)
diff_lat = endpoints[req.eid].lat - min_lat
saved_lad = req.n * diff_lat
score += saved_lad
total_req += req.n
return np.floor(score * 1000.0 / total_req)