Skip to content

Commit c4acc31

Browse files
committed
Added more comments
1 parent 1694773 commit c4acc31

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,23 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
### JetBrains template
92+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
93+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
94+
.idea/
95+
96+
## File-based project format:
97+
*.iws
98+
99+
## Plugin-specific files:
100+
101+
# IntelliJ
102+
/out/
103+
104+
# Crashlytics plugin (for Android Studio and IntelliJ)
105+
com_crashlytics_export_strings.xml
106+
crashlytics.properties
107+
crashlytics-build.properties
108+
fabric.properties
109+

algorithm/algorithm.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,36 @@ def execute(data):
1111
:return: score of the best solution found
1212
"""
1313

14-
n = 5
14+
n = 4 # nr of bees == solutions
1515
m = 3
1616
e = 1
17-
nep = 4
18-
nsp = 2
19-
ngh = 5
20-
max_iter = 2
21-
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)
22+
max_iter = 8
23+
24+
# n losowych rozwiazan (pszczol)
2225
pool = [AlgorithmUtils.random_solution(data) for _ in range(n)]
2326

2427
for i in range(max_iter):
2528
print("Iter ", i)
2629

30+
# calcuate all solutions and sort them all
2731
solutions_ranking = sorted(pool, key=lambda s: calculate_score(data, s), reverse=True)
32+
# choose 1 elite
2833
elite_solutions = solutions_ranking[0:e]
2934
good_solutions = solutions_ranking[e:m]
3035

3136
pool = []
37+
# choose 1 solution from each elite solutions
3238
for es in elite_solutions:
3339
pool.append(AlgorithmUtils.best_in_neighbourhood(data, es, ngh, nep))
40+
# choose 1 solution from each good solutions
3441
for gs in good_solutions:
3542
pool.append(AlgorithmUtils.best_in_neighbourhood(data, gs, ngh, nsp))
43+
# dla pszczol ktore byly slabe wybieramy dla nich nowe rozwiazania (leca gdzie indziej)
3644
for _ in range(n-m):
3745
pool.append(AlgorithmUtils.random_solution(data))
3846

algorithm/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def random_solution(data):
1919

2020
res = dict()
2121
for ids in range(data.amount_of_cache_servers):
22-
random_video = random.randint(0, len(data.videos_sizes)-1)
22+
random_video = random.randint(0, len(data.videos_sizes)-1) # film to jest indeks na liscie video_sizes
2323
res[ids] = [random_video] if data.videos_sizes[random_video] <= data.cache_size else []
2424
return res
2525

@@ -40,6 +40,11 @@ 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
46+
47+
# lista wszystkich operacji
4348
acceptable_ops = [(s, v)
4449
for s in range(data.amount_of_cache_servers)
4550
for v in range(len(data.videos_sizes))

main.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
from algorithm.algorithm import Algorithm
22
from parsers.input_parser import parse_input
33
from utils.solution_checker import check_solution
4-
4+
import time
55

66
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)
1515
# print(solution)
1616

17+
before = time.time()
1718
score = Algorithm.execute(parse_input(input_file))
18-
print(score)
19+
after = time.time()
20+
21+
print("-----------------------------")
22+
print("Algorithm took time: " + str(round(after-before, 2)) + " s")
23+
print("-----------------------------")
24+
print("Saved time: " + str(round(score / 1000, 2)) + " s")
1925

2026
if __name__ == '__main__':
2127
main()

0 commit comments

Comments
 (0)