forked from NDresevic/timetable-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·216 lines (183 loc) · 8.52 KB
/
utils.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
import json
import random
from costs import check_hard_constraints, subjects_order_cost, empty_space_groups_cost, empty_space_teachers_cost, \
free_hour
from model import Class, Classroom, Data
def load_data(file_path, teachers_empty_space, groups_empty_space, subjects_order):
"""
Loads and processes input data, initialises helper structures.
:param file_path: path to file with input data
:param teachers_empty_space: dictionary where key = name of the teacher, values = list of rows where it is in
:param groups_empty_space: dictionary where key = group index, values = list of rows where it is in
:param subjects_order: dictionary where key = (name of the subject, index of the group), value = [int, int, int]
where ints represent start times (row in matrix) for types of classes P, V and L respectively. If start time is -1
it means that that subject does not have that type of class.
:return: Data(groups, teachers, classes, classrooms)
"""
with open(file_path) as file:
data = json.load(file)
# classes: dictionary where key = index of a class, value = class
classes = {}
# classrooms: dictionary where key = index, value = classroom name
classrooms = {}
# teachers: dictionary where key = teachers' name, value = index
teachers = {}
# groups: dictionary where key = name of the group, value = index
groups = {}
class_list = []
for cl in data['Casovi']:
new_group = cl['Grupe']
new_teacher = cl['Nastavnik']
# initialise for empty space of teachers
if new_teacher not in teachers_empty_space:
teachers_empty_space[new_teacher] = []
new = Class(new_group, new_teacher, cl['Predmet'], cl['Tip'], cl['Trajanje'], cl['Ucionica'])
# add groups
for group in new_group:
if group not in groups:
groups[group] = len(groups)
# initialise for empty space of groups
groups_empty_space[groups[group]] = []
# add teacher
if new_teacher not in teachers:
teachers[new_teacher] = len(teachers)
class_list.append(new)
# shuffle mostly because of teachers
random.shuffle(class_list)
# add classrooms
for cl in class_list:
classes[len(classes)] = cl
# every class is assigned a list of classrooms he can be in as indexes (later columns of matrix)
for type in data['Ucionice']:
for name in data['Ucionice'][type]:
new = Classroom(name, type)
classrooms[len(classrooms)] = new
# every class has a list of groups marked by its index, same for classrooms
for i in classes:
cl = classes[i]
classroom = cl.classrooms
index_classrooms = []
# add classrooms
for index, c in classrooms.items():
if c.type == classroom:
index_classrooms.append(index)
cl.classrooms = index_classrooms
class_groups = cl.groups
index_groups = []
for name, index in groups.items():
if name in class_groups:
# initialise order of subjects
if (cl.subject, index) not in subjects_order:
subjects_order[(cl.subject, index)] = [-1, -1, -1]
index_groups.append(index)
cl.groups = index_groups
return Data(groups, teachers, classes, classrooms)
def set_up(num_of_columns):
"""
Sets up the timetable matrix and dictionary that stores free fields from matrix.
:param num_of_columns: number of classrooms
:return: matrix, free
"""
w, h = num_of_columns, 60 # 5 (workdays) * 12 (work hours) = 60
matrix = [[None for x in range(w)] for y in range(h)]
free = []
# initialise free dict as all the fields from matrix
for i in range(len(matrix)):
for j in range(len(matrix[i])):
free.append((i, j))
return matrix, free
def show_timetable(matrix):
"""
Prints timetable matrix.
"""
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# print heading for classrooms
for i in range(len(matrix[0])):
if i == 0:
print('{:17s} C{:6s}'.format('', '0'), end='')
else:
print('C{:6s}'.format(str(i)), end='')
print()
d_cnt = 0
h_cnt = 0
for i in range(len(matrix)):
day = days[d_cnt]
hour = hours[h_cnt]
print('{:10s} {:2d} -> '.format(day, hour), end='')
for j in range(len(matrix[i])):
print('{:6s} '.format(str(matrix[i][j])), end='')
print()
h_cnt += 1
if h_cnt == 12:
h_cnt = 0
d_cnt += 1
print()
def write_solution_to_file(matrix, data, filled, filepath, groups_empty_space, teachers_empty_space, subjects_order):
"""
Writes statistics and schedule to file.
"""
f = open('solution_files/sol_' + filepath, 'w')
f.write('-------------------------- STATISTICS --------------------------\n')
cost_hard = check_hard_constraints(matrix, data)
if cost_hard == 0:
f.write('\nHard constraints satisfied: 100.00 %\n')
else:
f.write('Hard constraints NOT satisfied, cost: {}\n'.format(cost_hard))
f.write('Soft constraints satisfied: {:.02f} %\n\n'.format(subjects_order_cost(subjects_order)))
empty_groups, max_empty_group, average_empty_groups = empty_space_groups_cost(groups_empty_space)
f.write('TOTAL empty space for all GROUPS and all days: {}\n'.format(empty_groups))
f.write('MAX empty space for GROUP in day: {}\n'.format(max_empty_group))
f.write('AVERAGE empty space for GROUPS per week: {:.02f}\n\n'.format(average_empty_groups))
empty_teachers, max_empty_teacher, average_empty_teachers = empty_space_teachers_cost(teachers_empty_space)
f.write('TOTAL empty space for all TEACHERS and all days: {}\n'.format(empty_teachers))
f.write('MAX empty space for TEACHER in day: {}\n'.format(max_empty_teacher))
f.write('AVERAGE empty space for TEACHERS per week: {:.02f}\n\n'.format(average_empty_teachers))
f_hour = free_hour(matrix)
if f_hour != -1:
f.write('Free term -> {}\n'.format(f_hour))
else:
f.write('NO hours without classes.\n')
groups_dict = {}
for group_name, group_index in data.groups.items():
if group_index not in groups_dict:
groups_dict[group_index] = group_name
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
f.write('\n--------------------------- SCHEDULE ---------------------------')
for class_index, times in filled.items():
c = data.classes[class_index]
groups = ' '
for g in c.groups:
groups += groups_dict[g] + ', '
f.write('\n\nClass {}\n'.format(class_index))
f.write('Teacher: {} \nSubject: {} \nGroups:{} \nType: {} \nDuration: {} hour(s)'
.format(c.teacher, c.subject, groups[:len(groups) - 2], c.type, c.duration))
room = str(data.classrooms[times[0][1]])
f.write('\nClassroom: {:2s}\nTime: {}'.format(room[:room.rfind('-')], days[times[0][0] // 12]))
for time in times:
f.write(' {}'.format(hours[time[0] % 12]))
f.close()
def show_statistics(matrix, data, subjects_order, groups_empty_space, teachers_empty_space):
"""
Prints statistics.
"""
cost_hard = check_hard_constraints(matrix, data)
if cost_hard == 0:
print('Hard constraints satisfied: 100.00 %')
else:
print('Hard constraints NOT satisfied, cost: {}'.format(cost_hard))
print('Soft constraints satisfied: {:.02f} %\n'.format(subjects_order_cost(subjects_order)))
empty_groups, max_empty_group, average_empty_groups = empty_space_groups_cost(groups_empty_space)
print('TOTAL empty space for all GROUPS and all days: ', empty_groups)
print('MAX empty space for GROUP in day: ', max_empty_group)
print('AVERAGE empty space for GROUPS per week: {:.02f}\n'.format(average_empty_groups))
empty_teachers, max_empty_teacher, average_empty_teachers = empty_space_teachers_cost(teachers_empty_space)
print('TOTAL empty space for all TEACHERS and all days: ', empty_teachers)
print('MAX empty space for TEACHER in day: ', max_empty_teacher)
print('AVERAGE empty space for TEACHERS per week: {:.02f}\n'.format(average_empty_teachers))
f_hour = free_hour(matrix)
if f_hour != -1:
print('Free term ->', f_hour)
else:
print('NO hours without classes.')