Skip to content

Commit 0d7773c

Browse files
committed
return solution
1 parent 67fbb97 commit 0d7773c

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

algorithm/algorithm.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def execute(data):
2727
pool = [AlgorithmUtils.random_solution(data) for _ in range(n)]
2828

2929
for i in range(max_iter):
30-
print("Iter ", i)
30+
# print("Iter ", i)
3131

32-
solutions_ranking = sorted(pool, key=lambda s: calculate_score(data, s))
32+
solutions_ranking = sorted(pool, key=lambda s: calculate_score(data, s), reverse=True)
3333
elite_solutions = solutions_ranking[0:e]
3434
good_solutions = solutions_ranking[e:m]
3535

36-
AlgorithmUtils.print_solution_pool(solutions_ranking)
36+
# AlgorithmUtils.print_solution_pool(solutions_ranking)
3737

3838
pool = []
3939
for es in elite_solutions:
@@ -42,3 +42,6 @@ def execute(data):
4242
pool.append(AlgorithmUtils.best_in_neighbourhood(data, gs, ngh, nsp))
4343
for _ in range(n-m):
4444
pool.append(AlgorithmUtils.random_solution(data))
45+
46+
print(pool[0])
47+
return calculate_score(data, pool[0])

algorithm/utils.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class AlgorithmUtils:
88
def random_solution(data):
99
res = dict()
1010
for ids in range(data.amount_of_cache_servers):
11-
res[ids] = [0]
11+
random_video = random.randint(0, len(data.videos_sizes)-1)
12+
res[ids] = [random_video] if data.videos_sizes[random_video] <= data.cache_size else []
1213
return res
1314

1415
@staticmethod
@@ -18,7 +19,7 @@ def print_solution_pool(pool):
1819
@staticmethod
1920
def random_neighbour(data, solution, radius):
2021

21-
res = solution.copy()
22+
res = AlgorithmUtils.solution_copy(solution)
2223
if radius == 0:
2324
return res
2425

@@ -43,9 +44,19 @@ def best_in_neighbourhood(data, solution, radius, number_of_bees):
4344
neighbours = []
4445
for _ in range(number_of_bees):
4546
neighbours.append(AlgorithmUtils.random_neighbour(data, solution, radius))
46-
best_neighbour = min(neighbours, key=lambda s: calculate_score(data, s))
47-
return best_neighbour if calculate_score(data, best_neighbour) < calculate_score(data, solution) else solution
47+
best_neighbour = max(neighbours, key=lambda s: calculate_score(data, s))
48+
return best_neighbour if calculate_score(data, best_neighbour) > calculate_score(data, solution) else solution
4849

4950
@staticmethod
5051
def free_space(data, server, solution):
51-
return 999
52+
res = data.cache_size
53+
for v in solution[server]:
54+
res -= data.videos_sizes[v]
55+
return res
56+
57+
@staticmethod
58+
def solution_copy(solution):
59+
res = dict()
60+
for s in solution:
61+
res[s] = solution[s][:]
62+
return res

main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
from utils.solution_checker import check_solution
22
from algorithm.algorithm import Algorithm
33
from parsers.input_parser import parse_input
4+
from utils.solution_checker import calculate_score
45

56

67
def main():
78
input_file = "files/example1.in"
8-
solution_file = "files/example1.out"
9-
solution = check_solution(input_file, solution_file)
10-
print(solution)
9+
# solution_file = "files/example1.out"
10+
# solution = check_solution(input_file, solution_file)
11+
# print(solution)
1112

1213
algo = Algorithm(5, 3, 1, 4, 2, 1, 11)
13-
algo.execute(parse_input(input_file))
14+
score = algo.execute(parse_input(input_file))
15+
print(score)
1416

1517
if __name__ == '__main__':
1618
main()

0 commit comments

Comments
 (0)