/
main.py
executable file
·109 lines (75 loc) · 2.58 KB
/
main.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
#!/usr/bin/python
import os
from model.cache import Cache
from model.candidate import Candidate
from model.endpoint import Endpoint
from model.request import Request
from model.video import Video
from solution import find_solution, write_solution
from sys import argv
def debug(str):
if os.getenv('DEBUG', False):
print(str)
def intTuple(str):
return map( int, str.split(' ') )
def parseInputs():
script, filename = argv
txt = open(filename)
lines = txt.read().split('\n')
first_line = lines[0]
(no_videos, no_endpoints, no_requests, no_caches, cache_size) = intTuple(first_line)
video_size = intTuple(lines[1])
debug("Video %d" % no_videos)
debug("Videos size : ")
debug( video_size )
videos = []
for i in range(no_videos):
videos.append(Video(i, video_size[i]))
debug(videos)
caches = []
for i in range(no_caches):
caches.append(Cache(i, cache_size, []))
debug(caches)
idx = 2
# loop endpoint
endpoints = []
endpoint_id = 0
while( no_endpoints > 0 ):
(latency_to_dc, no_caches) = intTuple(lines[idx])
idx = idx + 1
latency_to_caches = dict()
for i in range(no_caches):
(cache_server, latency) = intTuple(lines[idx])
latency_to_caches[cache_server] = latency
idx = idx + 1
en = Endpoint(endpoint_id, latency_to_caches, latency_to_dc)
endpoints.append(en)
endpoint_id = endpoint_id + 1
no_endpoints = no_endpoints - 1
debug(endpoints)
requests = []
while(no_requests > 0):
(video_id, endpoint_id, reqs) = intTuple(lines[idx])
requests.append( Request(video_id, endpoint_id, reqs) )
idx = idx + 1
no_requests = no_requests - 1
debug(requests)
idx = 2
return (caches, endpoints, requests, videos)
def generate_candidates(requests, endpoints, videos):
candidates = []
for r in requests:
ep = endpoints[r.endpoint_id]
for cache_id in ep.cache_map.keys():
latency_saving = ep.data_center_latency - ep.cache_map[cache_id]
reward = 1.0 * r.n_requests * latency_saving / videos[r.video_id].size
candidate = Candidate(r.video_id, r.endpoint_id, reward, cache_id)
candidates.append(candidate)
return candidates
def main():
(caches, endpoints, requests, videos) = parseInputs()
candidates = generate_candidates(requests, endpoints, videos)
assigned_caches = find_solution(candidates, caches, videos)
write_solution(assigned_caches)
# your program here
main()