Skip to content

Commit 899512c

Browse files
committed
wincyj komentarzy
1 parent c4acc31 commit 899512c

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

algorithm/algorithm.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,57 @@ class Algorithm:
88
def execute(data):
99

1010
"""
11-
:return: score of the best solution found
11+
Pomaga myslenie o rozwiazaniach jako o kwiatkach. Kazda pszczola siedzi w danym momencie na jednym konkretnym
12+
kwiatku, czyli pszczola jest reprezentacja konkretnego rozwiazania.
13+
14+
n - liczba pszczol, czyli rozwiazan w puli w kazdej iteracji. Ta liczba nigdy sie nie zmieni!
15+
m - liczba rozwiazan dobrych (w tym elitarnych)
16+
e - liczba rozwiazan elitarnych (czyli najlepszych)
17+
nep - kazda pszczola elitarna wybierze nep zblizonych rozwiazan, zeby wybrac jedno najlepsze
18+
jesli wszystkie beda gorsze, zostanie na tym samym kwiatku, na ktorym jest teraz.
19+
nsp - j.w., tylko dla rozwiazan dobrych, a nie elitarnych.
20+
ngh - promien sasiedztwa, czyli jak dalekie rozwiazania rozpatrujemy po elitarnych/dobrych
21+
max_iter - liczba iteracji
1222
"""
1323

14-
n = 4 # nr of bees == solutions
24+
n = 4
1525
m = 3
1626
e = 1
17-
nep = 4 # 1 elite bee goes to 4 different places
18-
# from these 4 places chooses exactly 1
19-
nsp = 2 # 2 good bees goes to 2 different places
20-
# from these 4 places chooses exactly 1
21-
ngh = 4 # max distance from center (e.g. elite solution)
27+
nep = 4
28+
nsp = 2
29+
ngh = 4
2230
max_iter = 8
2331

24-
# n losowych rozwiazan (pszczol)
32+
# inicjalizujemy pule losowymi rozwiazaniami
2533
pool = [AlgorithmUtils.random_solution(data) for _ in range(n)]
2634

2735
for i in range(max_iter):
2836
print("Iter ", i)
2937

30-
# calcuate all solutions and sort them all
38+
# dla kazdego rozwiazania obliczamy wynik - ukladamy je od najlepszych do najgorszych
3139
solutions_ranking = sorted(pool, key=lambda s: calculate_score(data, s), reverse=True)
32-
# choose 1 elite
40+
# najlepsze e rozwiazan to elitarne...
3341
elite_solutions = solutions_ranking[0:e]
42+
# ... a kolejne (m-e) to po prostu dobre
3443
good_solutions = solutions_ranking[e:m]
3544

45+
# tworzymy nowa pule rozwiazan
3646
pool = []
37-
# choose 1 solution from each elite solutions
47+
# kazde elitarne rozwiazanie zastepujemy jednym, najlepszym sposrod nep wybranych z sasiedztwa
48+
# UWAGA: to ze np. nep=4, nie znaczy ze dostajemy 4 nowe pszczoly. Po prostu sprawdzamy 4 rozwiazania, ale
49+
# tylko do najlepszego z nich poleci pszczola. Jedna pszczola tu przyleciala i jedna tu zostanie!
50+
# Nie jest powiedziane, ze ta pszczola bedzie znowu elitarna w kolejnej itreracji - to moze sie zmienic
3851
for es in elite_solutions:
3952
pool.append(AlgorithmUtils.best_in_neighbourhood(data, es, ngh, nep))
40-
# choose 1 solution from each good solutions
53+
# to samo dla dobrych rozwiazan
4154
for gs in good_solutions:
4255
pool.append(AlgorithmUtils.best_in_neighbourhood(data, gs, ngh, nsp))
43-
# dla pszczol ktore byly slabe wybieramy dla nich nowe rozwiazania (leca gdzie indziej)
56+
# dla pszczol ktore byly slabe wybieramy dla nich nowe, calkowicie losowe rozwiazania (leca gdzie indziej)
4457
for _ in range(n-m):
4558
pool.append(AlgorithmUtils.random_solution(data))
4659

60+
# podsumowujac - jesli np. n=5, m=3, e=1, to zawsze na koniec iteracji 1 pszczola pochodzi z obszaru
61+
# elitarnego, 2 z dobrych, a 2 sa losowane od nowa.
62+
4763
print(max(pool, key=lambda s: calculate_score(data, s)))
4864
return calculate_score(data, pool[0])

algorithm/utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def random_neighbour(data, solution, radius):
3131
Potencjalnie mozemy wyladowac w odleglosci radius lub mniejszej (w szczegolnosci wrocic do solution) - to chyba
3232
nie szkodzi?
3333
Nie ma tez gwarancji, ze sie nie powtorza - to chyba nie szkodzi?
34-
Prawdopodobienstwo wylosowania kazdego rozwiazania niekoniecznie jest rowne. Ale nie wiem jak inaczej to zrobic.
34+
Prawdopodobienstwo wylosowania kazdego rozwiazania niekoniecznie jest rowne. Ale watpie czy da sie lepiej.
3535
3636
TODO or not TODO? Zostawiamy to, czy ktos ma lepszy pomysl? Ja nie. :)
3737
"""
@@ -40,15 +40,18 @@ def random_neighbour(data, solution, radius):
4040
if radius == 0:
4141
return res
4242

43-
# TODO - optimize free_space here
44-
# I guess free_space is invoked many times in list comprehension
45-
# we could previously preapre a list of free_spaces for each server
43+
# licze to wczesniej, zeby nie robic tego wielokrotnie w list comprehension
44+
free_space_on_server = \
45+
[AlgorithmUtils.free_space(data, solution, s) for s in range(data.amount_of_cache_servers)]
4646

47-
# lista wszystkich operacji
47+
# generujemy liste moliwych do wykonania elementarnych modyfikacji rozwiazania
48+
# sa to operacje dwoch typow: dodaj albo usun
49+
# dodac mozemy te filmy, ktore sie smieszcza
50+
# usunac mozemy te, ktore sa aktualnie w rozwiazaniu
4851
acceptable_ops = [(s, v)
4952
for s in range(data.amount_of_cache_servers)
5053
for v in range(len(data.videos_sizes))
51-
if v in res[s] or data.videos_sizes[v] <= AlgorithmUtils.free_space(data, solution, s)]
54+
if v in res[s] or data.videos_sizes[v] <= free_space_on_server[s]]
5255

5356
op = random.choice(acceptable_ops)
5457
s = op[0]

main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def main():
77
# input_file = "files/videos_worth_spreading.in"
88
# input_file = "files/kittens.in"
99
# input_file = "files/example1.in"
10-
# input_file = "files/me_at_the_zoo.in"
11-
input_file = "files/trending_today.in"
10+
input_file = "files/me_at_the_zoo.in"
11+
# input_file = "files/trending_today.in"
1212

1313
# solution_file = "files/example1.out"
1414
# solution = check_solution(input_file, solution_file)
@@ -19,7 +19,7 @@ def main():
1919
after = time.time()
2020

2121
print("-----------------------------")
22-
print("Algorithm took time: " + str(round(after-before, 2)) + " s")
22+
print("Algorithm took: " + str(round(after-before, 2)) + " s")
2323
print("-----------------------------")
2424
print("Saved time: " + str(round(score / 1000, 2)) + " s")
2525

parsers/input_parser.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ def parse_input(path_to_file: str):
88
Parses input file.
99
1010
:param path_to_file: path to input file as string
11-
:return: tuple consisting of 8 values: amount of videos (int), amount of endpoints (int),
12-
amount of request descriptions (int), amount of cache servers (int),
13-
cache servers size (int), videos sizes (dictionary consisting of pairs:
14-
video id (int), video size (int) )
15-
dict of endpoints (consisting of pairs: endpoint_id, endpoint),
16-
list of requests (list of RequestInfo)
11+
:return: Data objet containing a complete description of input (see datarepresentation/data.py)
1712
"""
1813
amount_of_videos, amount_of_endpoints, amount_of_request_descriptions, \
1914
amount_of_cache_servers, cache_size = -1, -1, -1, -1, -1
@@ -54,9 +49,6 @@ def parse_input(path_to_file: str):
5449

5550
return Data(endpoints, videos_sizes, amount_of_cache_servers, requests, cache_size)
5651

57-
# return (amount_of_videos, amount_of_endpoints, amount_of_request_descriptions,
58-
# amount_of_cache_servers, cache_size, videos_sizes, endpoints, requests)
59-
6052

6153
def __parse_general_data(line_content: list):
6254
return tuple(int(x) for x in line_content)

0 commit comments

Comments
 (0)