This repository has been archived by the owner on Aug 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
323 lines (274 loc) · 8.81 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from io import open
from time import process_time
from copy import deepcopy
from pprint import PrettyPrinter
from json import load
from random import shuffle
from dateutil import parser
# SETTINGS
FILE = "rozvrh.json"
NO_730 = True
NO_915 = True
RANDOMIZE = False
# INIT and CONSTS
START = 0
EMPTY = " "
PP = PrettyPrinter(indent=2)
# Normalizes data from the json
def create_json(inp):
struct = {}
# Builds an object with the event information
def fmt(ev, class_form):
time_old = parser.parse(ev["starts_at"])
time_new = (time_old.weekday(), time_old.hour, time_old.minute)
return {
"cls": ev["links"]["course"], # Class name
"tpe": class_form, # Type of the cass
"beg": [time_new], # Day and start time
"rom": ev["links"]["room"], # Classroom
"cap": ev["capacity"], # Capacity
"par": ev["parallel"], # Parallel number
}
# Divide lectures tutorials and laboratories
for key in inp:
struct.setdefault(key, {
"lecture": [fmt(ev, "lecture") for ev in
filter(lambda it: it["event_type"] == "lecture", inp[key]["events"])],
"tutorial": [fmt(ev, "tutorial") for ev in
filter(lambda it: it["event_type"] == "tutorial", inp[key]["events"])],
"laboratory": [fmt(ev, "laboratory") for ev in
filter(lambda it: it["event_type"] == "laboratory", inp[key]["events"])],
})
# Combine classes for same parallels into one
for sub in struct:
for clType in struct[sub]:
for ev in struct[sub][clType]:
times = ev["beg"]
for i in struct[sub][clType]:
if ev["par"] == i["par"] and ev["beg"][0] != i["beg"][0]:
times.append(i["beg"][0])
struct[sub][clType].remove(i)
ev["beg"] = times
return struct
# Flatten normalized JSON
def flatten_data(struct):
flattened = []
flattened_combinations = 1
for sbj in struct:
for class_type in struct[sbj]:
if len(struct[sbj][class_type]) > 0:
flattened.append(struct[sbj][class_type])
flattened_combinations *= len(flattened[-1])
return flattened, flattened_combinations
# Creates a new instance of the time table
def create_table():
return {
0: [ # MON
EMPTY, # 7:30
EMPTY, # 9:15
EMPTY, # 11:00
EMPTY, # 12:45
EMPTY, # 14:30
EMPTY, # 16:15
EMPTY # 18:00
],
1: [ # TUE
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY
],
2: [ # WED
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY
],
3: [ # THU
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY
],
4: [ # FRI
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY,
EMPTY
]
}
# Converts time from format (weekday, hour, minutes) to an index in the time table
def time_to_index(trp):
if trp[1] == 7 and trp[2] == 30:
i = 0
elif trp[1] == 9 and trp[2] == 15:
i = 1
elif trp[1] == 11 and trp[2] == 0:
i = 2
elif trp[1] == 12 and trp[2] == 45:
i = 3
elif trp[1] == 14 and trp[2] == 30:
i = 4
elif trp[1] == 16 and trp[2] == 15:
i = 5
elif trp[1] == 18 and trp[2] == 0:
i = 6
else:
# Hack for BI-CS1 classes to make things simple. No need to attend these anyway.
if trp[1] == 13 and trp[2] == 30:
return time_to_index((trp[0], 14, 30))
elif trp[1] == 11 and trp[2] == 45:
return time_to_index((trp[0], 12, 45))
raise ValueError("Invalid class time!", trp)
return trp[0], i
# Calc score for a given table, lower is better
def score_table(tbl):
scr = 0
mtp = 0
# Calc total time spent in school
for day in tbl:
i = 0
for hour in tbl[day]:
if hour == EMPTY:
i += 1
else:
break
j = len(tbl[day])
for hour in reversed(tbl[day]):
if hour == EMPTY:
j -= 1
else:
break
if j - i > 0:
scr += (j - i) * 10
mtp += 1
total = scr * mtp
# Scoring of free days, days with lectures only and so on
for day in tbl:
# Free days
if sum(c == EMPTY for c in tbl[day]) == len(tbl[day]):
total -= 20
# Days with only lectures
count = 0
lectures_only = True
for hour in tbl[day]:
if hour == EMPTY:
continue
if hour[-1] == "l":
count += 1
else:
lectures_only = False
break
if lectures_only:
if count == 1:
total -= 10
else:
total -= 5
# NO730 or NO915
if NO_730 or NO_915:
for day in tbl:
if tbl[day][0] != EMPTY:
total += 5
elif tbl[day][1] == EMPTY and sum(c == EMPTY for c in tbl[day]) > 0:
total -= 1
return total
# Recursively try all possible combinations
def try_new(res, arr, tbl_old, flat_combinations):
# For each event in the first parallel-class-type-thing array
for ev in arr[0]:
# Create a new time table to try this stuff on
tbl = deepcopy(tbl_old)
added_time = False
tested_times = []
# For each class in such parallel
for tme in ev["beg"]:
tb_id = time_to_index(tme)
# Can this class be added to the time table?
if tbl[tb_id[0]][tb_id[1]] != EMPTY:
added_time = False
break
# Yep, add the class to the time table
added_time = True
tested_times.append(tme)
tbl[tb_id[0]][tb_id[1]] = ev["cls"][-3:] + ("t" if ev["tpe"] == "tutorial" else
"l" if ev["tpe"] == "lecture" else
"b")
# Was the parallel-class-type-thing added to the time table?
if added_time:
# Current parallel-class-type-thing was added successfully, lets move to the next one
arr_new = deepcopy(arr)
arr_new = arr_new[1:]
# Check if there are more parallel-class-type-things left
if len(arr_new) == 0:
# Score the table and append to the results
score = score_table(tbl)
res.append((score, tbl))
# Print info about the progress
if len(res) % 1000 == 0:
# How long did it take from the start?
tmr = process_time() - START
# Percentage progress, in reality only around 1 % is checked
proc = (len(res) / flat_combinations) * 100
print("{0:.5f} - {1:.3f} - {2}".format(proc, tmr, len(res)))
else:
# There are, let's try them
try_new(res, arr_new, tbl, flat_combinations)
# Print results
def print_res(res, tmr):
res.sort(key=lambda tup: tup[0])
all_low = list(filter(lambda x: x[0] == res[0][0], res))
print("Found", len(res), "possible timetables")
print("Execution stopped after", tmr, "seconds")
print("Results:")
print("-Best:")
print(res[1])
print("-Worst:")
print(res[-1])
print("-All best (", len(all_low), "):")
PP.pprint(all_low)
# Main
def main(start):
# Load JSON
file = open(FILE, "r")
data = load(file)
# Normalize JSON
work = create_json(data)
table = create_table()
# Flatten data
flat, flat_combinations = flatten_data(work)
# Sort or randomize input order
for f in flat:
if RANDOMIZE:
shuffle(f)
else:
f.sort(key=lambda x: x["beg"][0])
if RANDOMIZE:
shuffle(flat)
else:
flat.sort(key=lambda x: x[0]["cls"])
print("Data parsed after", start, "seconds")
print("Maximum combinations:", flat_combinations)
print("[% of combinations tried] - [time spent in seconds] - [number of total results]")
res = []
start = process_time()
try:
try_new(res, flat, table, flat_combinations)
except KeyboardInterrupt:
end = process_time()
print_res(res, end - start)
else:
end = process_time()
print_res(res, end - start)
main(START)