Skip to content

Commit 1038219

Browse files
committed
better random solution
1 parent 516fb6a commit 1038219

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

algorithm/utils.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ class AlgorithmUtils:
77
@staticmethod
88
def random_solution(data):
99
"""
10-
TODO to jest oczywiscie bez sensu i do zmiany
11-
Wbrew pozorom problem generowania losowego rozwiazania nie jest trywialny - pamietajmy, ze bierzemy pod uwage
12-
tylko poprawne rozwiazania i najlepiej by bylo losowac je z rownym prawdopodobienstwem
13-
14-
Sprowadza sie to do takiego problemu:
15-
Z pewnego zbioru [a1, a2, ... ak] wylosować podzbior tak, zeby suma nie przekroczyla danej liczby N
16-
17-
Oczywiscie mozna generowac wszystkie mozliwe zbiory... ale to ma paskudna zlozonosc
10+
Nie jest do doskonaly sposob, ale na nic lepszego nie wpadlismy
1811
"""
1912

2013
res = dict()
21-
for ids in range(data.amount_of_cache_servers):
22-
random_video = random.randint(0, len(data.videos_sizes)-1) # film to jest indeks na liscie video_sizes
23-
res[ids] = [random_video] if data.videos_sizes[random_video] <= data.cache_size else []
14+
avg_video_size = sum(data.videos_sizes)/len(data.videos_sizes)
15+
total_n_videos = round(data.cache_size/avg_video_size)
16+
17+
for s in range(data.amount_of_cache_servers):
18+
if total_n_videos == 0:
19+
res[s] = []
20+
else:
21+
n_videos = random.randint(0, total_n_videos-1) # ile filmow losujemy
22+
res[s] = random.sample(range(len(data.videos_sizes)), n_videos)
23+
while sum([data.videos_sizes[v] for v in res[s]]) > AlgorithmUtils.free_space(data, res, s):
24+
res[s].pop(random.randint(0, len(res[s])-1))
25+
print(res[5])
2426
return res
2527

2628
@staticmethod

0 commit comments

Comments
 (0)