-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy.py
103 lines (91 loc) · 4.07 KB
/
greedy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import logging
import numpy as np
from load_file import load_libraries
from main import score
import numpy as np
import random
def order_libraries(libraries, scores_of_books, num_of_days, random_beam=0):
ordered_libraries = []
available_libraries_indices = set(range(len(libraries)))
excluded_books = set()
num_of_remaining_days = num_of_days
best_score = 1
book_indices_by_score = np.argsort(scores_of_books)
book_orders = np.empty(book_indices_by_score.size, np.uint)
book_orders[book_indices_by_score] = np.arange(book_indices_by_score.size)
while available_libraries_indices and num_of_remaining_days and best_score:
scores_of_libraries = {}
books_of_libraries = {}
if random_beam:
indices = random.sample(
available_libraries_indices,
min(random_beam, len(available_libraries_indices)))
else:
indices = available_libraries_indices
for i in indices:
_, signup_days, books_per_day, list_of_books = libraries[i]
scores_of_libraries[i], books_of_libraries[i] = score_library(list_of_books,
signup_days,
books_per_day,
excluded_books,
scores_of_books,
num_of_remaining_days,
book_indices_by_score,
book_orders)
best_library = max(scores_of_libraries, key=scores_of_libraries.get)
best_score = scores_of_libraries[best_library]
if best_score == 0:
break
logging.debug({'library': best_library, 'score': scores_of_libraries[best_library]})
ordered_libraries.append((best_library,
books_of_libraries[best_library]))
_, signup_days, books_per_day, list_of_books = libraries[best_library]
excluded_books.update(list_of_books)
num_of_remaining_days -= signup_days
available_libraries_indices.remove(best_library)
return ordered_libraries
def score_library(list_of_books,
signup_days,
books_per_day,
excluded_books,
scores_of_books,
num_of_days,
book_indices_by_score,
book_orders):
books = set(list_of_books) - set(excluded_books)
books_indices = book_indices_by_score[book_orders[list(books)]]
days_for_scanning = num_of_days - signup_days
num_books_to_scan = books_per_day * days_for_scanning
book_indices_to_scan = books_indices[:num_books_to_scan]
scores_cut = scores_of_books[book_indices_to_scan]
score = np.sum(scores_cut)
return score, book_indices_to_scan
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', type=str)
args = parser.parse_args()
n_books, n_libs, n_days, scores_of_books, libs = load_libraries(args.file)
scores_of_books_dict = dict(enumerate(scores_of_books))
# print(n_books)
# print(n_libs)
# print(n_days)
# print(book_scores)
# print(libs[:10])
# lib = libs[0]
# n = lib[0]
# signup_days = lib[1]
# bs_per_day = lib[2]
# bs = lib[3]
# score = score_library(bs, signup_days, bs_per_day, [], scores_of_books, n_days)
# print(score)
library_signup_times = [lib[1] for lib in libs]
library_ship_capacities = [lib[2] for lib in libs]
solution = order_libraries(libs, scores_of_books_dict, n_days)
print(solution)
print(scores_of_books)
scores_of_books = np.asarray(scores_of_books, dtype=np.uint)
s = score(solution, n_days, scores_of_books,
library_signup_times,
library_ship_capacities)
print(s)