Skip to content

Commit d4b8c0b

Browse files
committed
a bit more efficient
1 parent 0d7773c commit d4b8c0b

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

algorithm/algorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def execute(data):
2222
nep = 4
2323
nsp = 2
2424
ngh = 1
25-
max_iter = 11
25+
max_iter = 9
2626

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

3232
solutions_ranking = sorted(pool, key=lambda s: calculate_score(data, s), reverse=True)
3333
elite_solutions = solutions_ranking[0:e]

algorithm/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ def random_neighbour(data, solution, radius):
4343
def best_in_neighbourhood(data, solution, radius, number_of_bees):
4444
neighbours = []
4545
for _ in range(number_of_bees):
46-
neighbours.append(AlgorithmUtils.random_neighbour(data, solution, radius))
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
46+
neighbour = AlgorithmUtils.random_neighbour(data, solution, radius)
47+
neighbours.append((neighbour, calculate_score(data, neighbour)))
48+
best_neighbour = max(neighbours, key=lambda s: calculate_score(data, s[0]))
49+
return best_neighbour[0] if best_neighbour[1] > calculate_score(data, solution) else solution
4950

5051
@staticmethod
5152
def free_space(data, server, solution):

main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
from utils.solution_checker import check_solution
21
from algorithm.algorithm import Algorithm
32
from parsers.input_parser import parse_input
4-
from utils.solution_checker import calculate_score
53

64

75
def main():
8-
input_file = "files/example1.in"
6+
# input_file = "files/videos_worth_spreading.in"
7+
# input_file = "files/kittens.in"
8+
# input_file = "files/example1.in"
9+
input_file = "files/trending_today.in"
10+
911
# solution_file = "files/example1.out"
1012
# solution = check_solution(input_file, solution_file)
1113
# print(solution)
1214

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

1718
if __name__ == '__main__':

0 commit comments

Comments
 (0)