-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimetabler.py
354 lines (318 loc) · 14.8 KB
/
timetabler.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from course import Course
from room import Room
from slot import Slot
from clause import Clause
from faculty import Faculty
from literal import Literal
from pysat.formula import WCNF
from pysat.examples.rc2 import RC2
from pysat.solvers import Solver
from itertools import combinations
import copy
class TimeTabler:
"""
1 ... c*s
c*s + (1 ... c.r)
"""
def __init__(self):
self.courses = []
self.courses_to_int = {}
self.CNF = WCNF()
self.slots = []
self.faculties = []
self.students = []
self.rooms = []
self.room_to_index = {}
self.clauses = []
self.clauses_converted = 0
self.set1 = []
self.slot_to_index = {}
self.faculty_code_map = {}
def add_course(self, course):
assert isinstance(course, Course), "course must be an Course type"
assert course.code not in self.courses_to_int.keys(), "course code already exist"
self.courses_to_int[course.code] = len(self.courses)
self.courses.append(course)
return True
def add_course_pair(self, course_pair):
assert course_pair[0] in self.courses_to_int.keys(), "incorrect course code"
assert course_pair[1] in self.courses_to_int.keys(), "incorrect course code"
self.set1.append(course_pair)
return True
def add_faculty(self, faculty):
assert isinstance(faculty, Faculty), "Faculty must be an Faculty type"
self.faculties.append(faculty)
self.faculty_code_map[faculty.code] = faculty.preference
return True
def add_room(self, room):
assert isinstance(room, Room), TypeError("room must be a Room type")
assert room.name not in self.room_to_index.keys(), "room already exists"
self.room_to_index[room.name] = len(self.rooms)
self.rooms.append(room)
return True
def add_slots(self, slot):
assert isinstance(slot, Slot), "slot must be an slot type"
self.slot_to_index[slot.day * 100 + slot.start_time] = len(self.slots)
self.slots.append(slot)
return True
def add_clause(self, clause):
assert isinstance(clause, Clause), "Clause must be an Clause type"
self.clauses.append(clause)
return True
def generate_cnf(self):
if len(self.clauses) <= self.clauses_converted:
return True
temp = self.clauses_converted
for i in range(temp, len(self.clauses)):
clause = self.clauses[i]
if clause.at_most is not None:
self.at_most(clause, clause.at_most)
if clause.at_least is not None:
self.at_least(clause, clause.at_least)
if clause.at_most is None and clause.at_least is None:
self.add_cnf(clause)
self.clauses_converted = len(self.clauses)
return True
def add_cnf(self, clause):
converted_clause = [self.convert_literal(literal) for literal in clause.literals]
if clause.weight == -1:
self.CNF.append(converted_clause)
return True
self.CNF.append(converted_clause, weight=clause.weight)
return True
def at_most(self, clause, k, at_least=False):
for combination in combinations(clause.literals, k + 1):
new_cls = Clause(-1)
for lit in combination:
new_lit = copy.deepcopy(lit)
new_lit.Not ^= (True ^ at_least)
new_cls.add_literal(new_lit)
self.add_cnf(new_cls)
def at_least(self, clause, k):
n = len(clause.literals)
self.at_most(clause, max(n - k, 0), at_least=True)
def convert_literal(self, literal):
if literal.Type == 1:
return (((literal.course - 1) * len(self.slots)) + literal.room_slot) * (-1 if literal.Not else 1)
else:
return ((len(self.courses) * len(self.slots)) + (
((literal.course - 1) * len(self.rooms)) + literal.room_slot)) * (
-1 if literal.Not else 1)
def room_clash_constraint(self):
"""
~Cc1Ss | ~Cc2Ss | ~Cc1Rr | ~Cc2Rr
:return:
"""
for c1 in range(len(self.courses)):
for c2 in range(len(self.courses)):
if c1 != c2:
for s in range(len(self.slots)):
for r in range(len(self.rooms)):
clause = Clause(-1)
clause.add_literal(Literal(1, c1 + 1, s + 1, True))
clause.add_literal(Literal(1, c2 + 1, s + 1, True))
clause.add_literal(Literal(2, c1 + 1, r + 1, True))
clause.add_literal(Literal(2, c2 + 1, r + 1, True))
self.add_clause(clause)
def course_room_assignment(self):
"""
exactly one(CcRr) given r.size >= c.size
and
~CcRr given r.size < c.size
:return:
"""
for c in range(len(self.courses)):
clause = Clause(-1, at_most=1, at_least=1)
for r in range(len(self.rooms)):
if self.courses[c].size <= self.rooms[r].size:
clause.add_literal(Literal(2, c + 1, r + 1, False))
elif self.courses[c].size > self.rooms[r].size:
clause2 = Clause(-1)
clause2.add_literal(Literal(2, c + 1, r + 1, True))
self.add_clause(clause2)
# exactly one of all these literals true
self.add_clause(clause)
def slot_assignment(self):
"""
Morning slots before 2 and Evening after 2
and
Number of slots assigned == c.no_of_lectures
:return:
"""
for c in range(len(self.courses)):
clause = Clause(-1, at_most=self.courses[c].number_of_lectures,
at_least=self.courses[c].number_of_lectures)
for s in range(len(self.slots)):
if self.courses[c].timing == "morning" and self.slots[s].start_time <= 5:
clause.add_literal(Literal(1, c + 1, s + 1, False))
elif self.courses[c].timing == "evening" and self.slots[s].start_time > 6:
clause.add_literal(Literal(1, c + 1, s + 1, False))
self.add_clause(clause)
def practical_slot_assignment(self):
"""
Morning slots before 2 and Evening after 2
and
Number of slots assigned == c.no_of_practicals
:return:
"""
for c in range(len(self.courses)):
clause = Clause(-1, at_most=self.courses[c].number_of_practicals*2,
at_least=self.courses[c].number_of_practicals*2)
for s in range(len(self.slots)):
if self.courses[c].timing == "morning" and self.slots[s].start_time>6:
clause.add_literal(Literal(1, c + 1, s + 1, False))
elif self.courses[c].timing == "evening" and self.slots[s].start_time <= 5:
clause.add_literal(Literal(1, c + 1, s + 1, False))
self.add_clause(clause)
def course_clash_constraint(self):
"""
~Cc1Ss | ~Cc2Ss
:return:
"""
for c in self.set1:
for s in range(len(self.slots)):
clause = Clause(-1)
clause.add_literal(Literal(1, self.courses_to_int[c[0]] + 1, s + 1, True))
clause.add_literal(Literal(1, self.courses_to_int[c[1]] + 1, s + 1, True))
self.add_clause(clause)
def preference_constraint(self):
"""
CcSs
:return:
"""
for c in self.courses:
for f in c.faculty:
for s in self.faculty_code_map[f]:
var = s[0] * 100 + s[1]
clause = Clause(1)
clause.add_literal(Literal(1, self.courses_to_int[c.code] + 1, self.slot_to_index[var] + 1, False))
self.add_clause(clause)
def lecture_per_day_constraint(self):
"""
atmost 1 of all CcSs (s belongs to a particular day)
:return:
"""
for c in range(len(self.courses)):
for d in range(1, 6):
clause = Clause(-1, at_most=1)
for s in range(len(self.slots)):
if(self.slots[s].day==d):
if self.courses[c].timing == "morning" and self.slots[s].start_time <= 5:
clause.add_literal(Literal(1, c + 1, s + 1, False))
elif self.courses[c].timing == "evening" and self.slots[s].start_time > 6:
clause.add_literal(Literal(1, c + 1, s + 1, False))
self.add_clause(clause)
def practical_per_day_constraint(self):
"""
atmost 2 of all CcSs (s belongs to a particular day)
:return:
"""
for c in range(len(self.courses)):
for d in range(1, 6):
clause = Clause(-1, at_most=2*self.courses[c].number_of_practicals)
for s in range(len(self.slots)):
if(self.slots[s].day==d):
if self.courses[c].timing == "morning" and self.slots[s].start_time > 6:
clause.add_literal(Literal(1, c + 1, s + 1, False))
elif self.courses[c].timing == "evening" and self.slots[s].start_time <= 5:
clause.add_literal(Literal(1, c + 1, s + 1, False))
self.add_clause(clause)
# not working else working
# for c in range(len(self.courses)):
# for d in range(1, 6):
# clause = Clause(1, at_least=2*self.courses[c].number_of_practicals)
# for s in range(len(self.slots)):
# if(self.slots[s].day==d):
# if self.courses[c].timing == "morning" and self.slots[s].start_time > 6:
# clause.add_literal(Literal(1, c + 1, s + 1, False))
# elif self.courses[c].timing == "evening" and self.slots[s].start_time <= 5:
# clause.add_literal(Literal(1, c + 1, s + 1, False))
# self.add_clause(clause)
def continuous_practical_assignment(self):
"""
atmost 2 of all CcSs (s belongs to a particular day)
:return:
"""
for c in range(len(self.courses)):
for d in range(1, 6):
for s in range(len(self.slots)):
clause = Clause(-1)
if(self.slots[s].day==d):
if self.courses[c].timing == "morning" and self.slots[s].start_time==7:
clause = Clause(-1)
clause.add_literal(Literal(1, c + 1, s + 1, True))
clause.add_literal(Literal(1, c + 1, s + 3, True))
self.add_clause(clause)
clause = Clause(-1)
clause.add_literal(Literal(1, c + 1, s + 2, True))
clause.add_literal(Literal(1, c + 1, s + 4, True))
self.add_clause(clause)
clause = Clause(-1)
clause.add_literal(Literal(1, c + 1, s + 1, True))
clause.add_literal(Literal(1, c + 1, s + 4, True))
self.add_clause(clause)
elif self.courses[c].timing == "evening" and self.slots[s].start_time==1:
clause = Clause(-1)
clause.add_literal(Literal(1, c + 1, s + 1, True))
clause.add_literal(Literal(1, c + 1, s + 3, True))
self.add_clause(clause)
clause = Clause(-1)
clause.add_literal(Literal(1, c + 1, s + 2, True))
clause.add_literal(Literal(1, c + 1, s + 4, True))
self.add_clause(clause)
clause = Clause(-1)
clause.add_literal(Literal(1, c + 1, s + 1, True))
clause.add_literal(Literal(1, c + 1, s + 4, True))
self.add_clause(clause)
def courses_in_same_type(self):
for c in range(len(self.courses)):
for s in range(len(self.slots)):
co = self.courses[c]
sl = self.slots[s]
if co.type.intersection(sl.type) != co.type:
clause = Clause(1000)
clause.add_literal(Literal(1, c + 1, s + 1, True))
self.add_clause(clause)
def slots_enforced(self):
for c in range(len(self.courses)):
for sl in self.courses[c].slots:
clause = Clause(-1)
s = self.slot_to_index[sl.day * 100 + sl.start_time]
clause.add_literal(Literal(1, c + 1, s + 1, False))
self.add_clause(clause)
def room_enforced(self):
for c in range(len(self.courses)):
if self.courses[c].room_assigned():
if self.courses[c].room.name in self.room_to_index.keys():
r = self.room_to_index[self.courses[c].room.name]
clause = Clause(-1)
clause.add_literal(Literal(2, c + 1, r + 1, False))
self.add_clause(clause)
def solve(self):
print("generating formula")
self.generate_cnf()
print("formula generated")
# result = {course:{slots:[],room:[]}}
rc2 = RC2(self.CNF)
print("solving MAXSAT")
model = rc2.compute()
print("MAXSAT solved")
if model is None:
print("All hard clauses not satisfied")
return None
result = {}
for c in range(len(self.courses)):
slots = []
for s in range(len(self.slots)):
pos = self.convert_literal(Literal(1, c + 1, s + 1, False))
if len(model) >= pos:
if model[pos - 1] > 0:
slots.append(self.slots[s])
room = []
for r in range(len(self.rooms)):
pos = self.convert_literal(Literal(2, c + 1, r + 1, False))
if len(model) >= pos:
if model[pos - 1] > 0:
room.append(self.rooms[r])
result[self.courses[c].code] = {"slots": slots, "room": room}
return result, model, rc2