Cairo Sat Course Timetabling
Cairo Sat Course Timetabling
Mohamed El Halaby
August 30, 2018
Abstract
Due to the good performance of current SAT (satisfiability) and Max-
SAT (maximum satisfiability) solvers, many real-life optimization prob-
lems such as scheduling can be solved by encoding them into Max-SAT. In
this paper we tackle the course timetabling problem of the department of
mathematics, Cairo University by encoding it into Max-SAT. Generating
timetables for the department by hand has proven to be cumbersome and
the generated timetable almost always contains conflicts. We show how
the constraints can be modelled as a Max-SAT instance.
1 Introduction
The satisfiability problem (SAT), which is the problem of deciding if there exists
a truth assignment that satisfies a Boolean formula in conjunctive normal form
(CNF), is a core problem in theoretical computer science because of its central
position in complexity theory. Given a Boolean formula, SAT asks if there is an
assignment to the variables of the formula such that it is satisfied (evaluates to
true). SAT was the first problem proven to be N P-complete by Cook [4]. Each
instance of an N P-complete problem can be translated into an instance of SAT
in polynomial time. This makes it very important to develop fast and efficient
SAT solvers.
An important optimization of SAT is Max-SAT which asks for a truth as-
signment that satisfies the maximum number of clauses of a given CNF formula.
Many theoretical and practical problems can be encoded into SAT and Max-
SAT such as debugging [13], circuits design and scheduling of how an observation
satellite captures photos of Earth [15], course timetabling [2, 10, 9, 7], software
package upgrades [6], routing [16, 11], reasoning [14] and protein structure align-
ment in bioinformatics [12].
This paper addresses the course timetabling problem of the department of
mathematics, faculty of science, Cairo university. Generating a timetable is done
every semester and with the growing number of students and the limited number
of teaching staff, this is becoming a difficult task. An important aspect of this
problem is that students of the department take different courses belonging to
1
different curricula at the same time. This fact complicates the problem because
the number of students taking an arbitrary set of courses is small and generating
a conflict-free schedule becomes harder.
This paper is structured as follows. Section 2 gives basic definitions and back-
ground about SAT and Max-SAT. Section 3 describes our timetabling problem,
problem encoding into partial Max-SAT and weighted Max-SAT and finally an
example that illustrates the workings of the encoding. Section 4 summarizes
our results and discusses future work.
2 Preliminaries
A Boolean variable x can take one of two possible values 0 (false) or 1 (true). A
Wnl is a variable x or its negation ¬x. A clause is a disjunction of literals,
literal
i.e., i=1 li . A CNF formula is a conjunction of clauses [5]. Formally, a CNF
formula F composed of k clauses, where each clause Ci is composed of mi is
defined as
^k
F = Ci
i=1
where
_
mi
Ci = li,j
j=1
2
3 Problem description and encoding
The course timetabling problem deals with the following objects:
• Courses: Each course is associated with one teacher and a number of
students. In our case, each course is scheduled twice a week, once for a
lecture and another for either a section or laboratory work, but we will
consider the lecture and laboratory work of the same course as different
courses for technical purposes as the lecture is given by teachers and the
laboratory work is supervised by teaching assistants.
• Curricula: A curriculum is a set of courses constituting an area of special-
ization.
• Rooms: Courses take place in rooms (lecture rooms or labs), where each
room has an associated capacity. There are two kinds of rooms: lecture
rooms and laboratories.
• Timeslots: A timeslot is a specific day and hour during which a course is
taught.
The course timetabling problem asks for an assignment of courses to rooms and
timeslots in such a way that all the hard constraints are met and satisfy as much
soft constraints as possible.
Because of the large number of students taking courses belonging to differ-
ent curricula, a generated timetable is prone to have conflicts (with two or more
courses taken by a student scheduled at the same time), which is a very impor-
tant problem in the timetabling problem we are tackling. Other formulations
do not take this conflict into account.
Our timetabling approach assumes that the timetable is a per-week schedule
of courses. A week consists of several work days, and each work day is divided
into equal-length timeslots and courses are scheduled into these timeslots.
Many of our timetabling requirements are common for many teaching insti-
tutions, however, some may vary. This allows us to use existing encodings and
timetabling tools.
3
4. ktk,t : curriculum k takes place in timeslot t, which implies that one of the
courses belonging to k takes place in t.
The truth values of these basic variables will determine the whole timetable.
First, we show how to encode correctness requirements, which must all be satis-
fied (i.e., they are hard constraints) for the generated timetable to be accepted.
So, in order for the encoding to be correct, the relation between the variables
must be expressed correctly.
Relationship between ct and cd:
1. If some course c takes place in timeslot t, it also takes place in the day
corresponding to t. So, for each course c and timeslot h, the following
hard clause is needed:
ctc,t ⇒ cdc,day(t)
which is equivalent to1
¬ctc,t ∨ cdc,day(t)
¬ctc,t ∨ ktk1 ,t
..
.
¬ctc,t ∨ ktkn ,t
Now after the relationships between the variables have been encoded, we
encode the constraints of the timetabling problem. Here we omit the details of
the cardinality constraints by denoting them by exactly, at most and at least.
Several attempts have been made to encode the cardinality constraints into CNF
such as [1], [8] and [3].
1 p ⇒ q is equivalent to ¬p ∨ q. From this point forward we will directly write the encoding
4
• Curriculum clashes: Courses belonging to the same curriculum must be
scheduled at different timeslots. Thus, for every two distinct courses c1
and c2 belonging to the same curriculum, and for every timeslot t, the
following hard clause is needed
¬ctc1 ,t ∨ ¬ctc2 ,t
• Room clashes: Every room can have at most one course scheduled to it at
any timeslot. So, for each room r, timeslot t, and pair of distinct courses
c1 and c2 , we have
¬ctc1 ,t ∨ ¬ctc2 ,t ∨ ¬crc1 ,r ∨ ¬crc2 ,r
5
• Room capacity: Every course is preferred to be scheduled into a room
that fits. So, for every course c having nc students and for every room
with capacity cr such that ns > cr , we need the following soft clause
¬crc,r
• Every course must be scheduled to exactly one room. Courses with weekly
laboratory work must be scheduled to a room that is a laboratory. We will
denote regular lecture rooms r1 , . . . , rm and laboratory rooms lab1 , . . . , labn .
1. For each course c with weekly lecture and laboratory work, we need
the following hard clause for the lecture:
2. For each course c with a weekly lecture and a section and no labora-
tory work, we need the following hard clause:
The clauses in the previous two cases are SAT encodings of the cardinality
constraint exactly, which requires that exactly k out of l literals be true.
• Number of lectures: Each course c must be scheduled twice a week; once
for a lecture and another for either a section (e.g., for solving exercises)
or laboratory work (e.g., for programming exercises). So, for every course
c we need the following hard clause
6
2. Timeslot unavailability: Each clause representing a timeslot unavailability
will receive a weight of 10. This constraint is considered less important
than student registration clashes.
3. Room capacity. Each violation of this constraint has a cost of 1 per each
student that does not fit. For example if the capacity of a room r is 50
and the number of students in a course c is 60, then the weight assigned
to (¬crc,r ) is 10.
3.3 Example
The following is a simple example to demonstrate the partial Max-SAT encod-
ing.
• Days: {d1 , d2 , d3 }.
• Timeslots: {t1 , t2 , t3 , t4 , t5 }, where day(t1 ) = d1 , day(t2 ) = d1 , day(t3 ) =
d2 , day(t4 ) = d2 and day(t5 ) = d3 .
• Courses: {CS101, CS202, M 271, CS304, CS305, CS402,
CS408}.
7
• Rooms: {r1 , r2 , lab1, lab2}.
Rooms Capacity
r1 50
r2 100
lab1 50
lab2 100
3.3.1 Clauses
1. Relationship between ct and cd. The clauses are grouped by course.
CS101:
(¬ctCS101,t1 ∨ cdCS101,d1 ), (¬ctCS101,t2 ∨ cdCS101,d1 )
(¬ctCS101,t3 ∨ cdCS101,d2 ), (¬ctCS101,t4 ∨ cdCS101,d2 )
(¬ctCS101,t5 ∨ cdCS101,d3 ), (¬cdCS101,d1 ∨ ctCS101,t1 ∨ ctCS101,t2 )
(¬cdCS101,d2 ∨ ctCS101,t3 ∨ ctCS101,t4 ), (¬cdCS101,d3 ∨ ctCS101,t5 )
CS202:
(¬ctCS202,t1 ∨ cdCS202,d1 ), (¬ctCS202,t2 ∨ cdCS202,d1 )
(¬ctCS202,t3 ∨ cdCS202,d2 ), (¬ctCS202,t4 ∨ cdCS202,d2 )
(¬ctCS202,t5 ∨ cdCS202,d3 ), (¬cdCS202,d1 ∨ ctCS202,t1 ∨ ctCS202,t2 )
(¬cdCS202,d2 ∨ ctCS202,t3 ∨ ctCS202,t4 ), (¬cdCS202,d3 ∨ ctCS202,t5 )
M271:
(¬ctM 271,t1 ∨ cdM 271,d1 ), (¬ctM 271,t2 ∨ cdM 271,d1 )
(¬ctM 271,t3 ∨ cdM 271,d2 ), (¬ctM 271,t4 ∨ cdM 271,d2 )
(¬ctM 271,t5 ∨ cdM 271,d3 ), (¬cdM 271,d1 ∨ ctM 271,t1 ∨ ctM 271,t2 )
(¬cdM 271,d2 ∨ ctM 271,t3 ∨ ctM 271,t4 ), (¬cdM 271,d3 ∨ ctM 271,t5 )
CS304:
(¬ctCS304,t1 ∨ cdCS304,d1 ), (¬ctCS304,t2 ∨ cdCS304,d1 )
(¬ctCS304,t3 ∨ cdCS304,d2 ), (¬ctCS304,t4 ∨ cdCS304,d2 )
(¬ctCS304,t5 ∨ cdCS304,d3 ), (¬cdCS304,d1 ∨ ctCS304,t1 ∨ ctCS304,t2 )
(¬cdCS304,d2 ∨ ctCS304,t3 ∨ ctCS304,t4 ), (¬cdCS304,d3 ∨ ctCS304,t5 )
CS305:
(¬ctCS305,t1 ∨ cdCS305,d1 ), (¬ctCS305,t2 ∨ cdCS305,d1 )
(¬ctCS305,t3 ∨ cdCS305,d2 ), (¬ctCS305,t4 ∨ cdCS305,d2 )
(¬ctCS305,t5 ∨ cdCS305,d3 ), (¬cdCS305,d1 ∨ ctCS305,t1 ∨ ctCS305,t2 )
(¬cdCS305,d2 ∨ ctCS305,t3 ∨ ctCS305,t4 ), (¬cdCS305,d3 ∨ ctCS305,t5 )
CS402:
(¬ctCS402,t1 ∨ cdCS402,d1 ), (¬ctCS402,t2 ∨ cdCS402,d1 )
(¬ctCS402,t3 ∨ cdCS402,d2 ), (¬ctCS402,t4 ∨ cdCS402,d2 )
(¬ctCS402,t5 ∨ cdCS402,d3 ), (¬cdCS402,d1 ∨ ctCS402,t1 ∨ ctCS402,t2 )
(¬cdCS402,d2 ∨ ctCS402,t3 ∨ ctCS402,t4 ), (¬cdCS402,d3 ∨ ctCS402,t5 )
8
CS408:
(¬ctCS408,t1 ∨ cdCS408,d1 ), (¬ctCS408,t2 ∨ cdCS408,d1 )
(¬ctCS408,t3 ∨ cdCS408,d2 ), (¬ctCS408,t4 ∨ cdCS408,d2 )
(¬ctCS408,t5 ∨ cdCS408,d3 ), (¬cdCS408,d1 ∨ ctCS408,t1 ∨ ctCS408,t2 )
(¬cdCS408,d2 ∨ ctCS408,t3 ∨ ctCS408,t4 ), (¬cdCS408,d3 ∨ ctCS408,t5 )
CS202:
(¬ctCS202,t1 ∨ktk2 ,t1 ), (¬ctCS202,t2 ∨ktk2 ,t2 ), (¬ctCS202,t3 ∨ktk2 ,t3 ), (¬ctCS202,t4 ∨
ktk2 ,t4 ), (¬ctCS202,t5 ∨ ktk2 ,t5 )
M271:
(¬ctM 271,t1 ∨ ktk2 ,t1 ), (¬ctM 271,t2 ∨ ktk2 ,t2 ), (¬ctM 271,t3 ∨ ktk2 ,t3 ), (¬ctM 271,t4 ∨
ktk2 ,t4 ), (¬ctM 271,t5 ∨ ktk2 ,t5 )
CS304:
(¬ctCS304,t1 ∨ktk3 ,t1 ), (¬ctCS304,t2 ∨ktk3 ,t2 ), (¬ctCS304,t3 ∨ktk3 ,t3 ), (¬ctCS304,t4 ∨
ktk3 ,t4 ), (¬ctCS304,t5 ∨ ktk3 ,t5 )
CS305:
(¬ctCS305,t1 ∨ktk3 ,t1 ), (¬ctCS305,t2 ∨ktk3 ,t2 ), (¬ctCS305,t3 ∨ktk3 ,t3 ), (¬ctCS305,t4 ∨
ktk3 ,t4 ), (¬ctCS305,t5 ∨ ktk3 ,t5 )
CS402:
(¬ctCS402,t1 ∨ktk4 ,t1 ), (¬ctCS402,t2 ∨ktk4 ,t2 ), (¬ctCS402,t3 ∨ktk4 ,t3 ), (¬ctCS402,t4 ∨
ktk4 ,t4 ), (¬ctCS402,t5 ∨ ktk4 ,t5 )
CS408:
(¬ctCS408,t1 ∨ktk4 ,t1 ), (¬ctCS408,t2 ∨ktk4 ,t2 ), (¬ctCS408,t3 ∨ktk4 ,t3 ), (¬ctCS408,t4 ∨
ktk4 ,t4 ), (¬ctCS408,t5 ∨ ktk4 ,t5 )
3. Curriculum clashes. The clauses are grouped courses of the same curriculum:
CS202 and M271:
(¬ctCS202,t1 ∨¬ctM 271,t1 ), (¬ctCS202,t2 ∨¬ctM 271,t2 ), (¬ctCS202,t3 ∨¬ctM 271,t3 ), (¬ctCS202,t4 ∨
¬ctM 271,t4 ), (¬ctCS202,t5 ∨ ¬ctM 271,t5 )
9
¬ctCS408,t4 ), (¬ctCS402,t5 ∨ ¬ctCS408,t5 )
4. Student registration clashes. The clauses are grouped by each two courses that
can not be scheduled at the same time:
CS101 and M271: (¬ctCS101,t1 ∨¬ctM 271,t1 ), (¬ctCS101,t2 ∨¬ctM 271,t2 ), (¬ctCS101,t3 ∨
¬ctM 271,t3 ), (¬ctCS101,t4 ∨ ¬ctM 271,t4 ), (¬ctCS101,t5 ∨ ¬ctM 271,t5 )
5. Teacher clashes. The clauses are grouped by each pair of courses taught by the
same teacher:
CS202 and CS402:
(¬ctCS202,t1 ∨¬ctCS402,t1 ), (¬ctCS202,t2 ∨¬ctCS402,t2 ), (¬ctCS202,t3 ∨¬ctCS402,t3 ), (¬ctCS202,t4 ∨
¬ctCS402,t4 ), (¬ctCS202,t5 ∨ ¬ctCS402,t5 )
6. Room clashes. We will not write all the clauses since they are too many. Clauses
for one room, six pairs of courses and one timeslot:
r 1 , t1 :
(¬ctCS101,t1 ∨ ¬ctCS202,t1 ∨ ¬crCS101,r1 ∨ ¬crCS202,r1 ),
(¬ctCS101,t1 ∨ ¬ctM 271,t1 ∨ ¬crCS101,r1 ∨ ¬crM 271,r1 ),
(¬ctCS101,t1 ∨ ¬ctCS304,t1 ∨ ¬crCS101,r1 ∨ ¬crCS304,r1 ),
(¬ctCS101,t1 ∨ ¬ctCS305,t1 ∨ ¬crCS101,r1 ∨ ¬crCS305,r1 ),
(¬ctCS101,t1 ∨ ¬ctCS402,t1 ∨ ¬crCS101,r1 ∨ ¬crCS402,r1 ),
(¬ctCS101,t1 ∨ ¬ctCS408,t1 ∨ ¬crCS101,r1 ∨ ¬crCS408,r1 )
(¬ctCS101,t1 ), (¬ctCS101,t2 )
I2 :
(¬ctCS304,t1 ), (¬ctCS305,t1 )
10
8. Room capacity. The clauses are grouped by courses:
CS101: ns = 75
(¬crCS101,r1 ), (¬crCS101,lab1 )
M271: ns = 90
(¬crM 271,r1 ), (¬crM 271,lab1 )
CS304: ns = 74
(¬crCS304,r1 ), (¬crCS304,lab1 )
CS305: ns = 79
(¬crCS305,r1 ), (¬crCS305,lab1 )
CS402: ns = 60
(¬crCS402,r1 ), (¬crCS402,lab1 )
CS408: ns = 55
(¬crCS408,r1 ), (¬crCS408,lab1 )
9. Every course must be scheduled to exactly one room. The clauses are grouped
by courses:
CS101:
11
10. Number of lectures. The clauses are grouped by courses:
CS101:
exactly(2, {ctCS101,t1 , ctCS101,t2 , ctCS101,t3 , ctCS101,t4 ,
ctCS101,t5 })
CS202:
exactly(2, {ctCS202,t1 , ctCS202,t2 , ctCS202,t3 , ctCS202,t4 ,
ctCS202,t5 })
M271:
exactly(2, {ctM 271,t1 , ctM 271,t2 , ctM 271,t3 , ctM 271,t4 ,
ctM 271,t5 })
CS304:
exactly(2, {ctCS304,t1 , ctCS304,t2 , ctCS304,t3 , ctCS304,t4 ,
ctCS304,t5 })
CS305:
exactly(2, {ctCS305,t1 , ctCS305,t2 , ctCS305,t3 , ctCS305,t4 ,
ctCS305,t5 })
CS402:
exactly(2, {ctCS402,t1 , ctCS402,t2 , ctCS402,t3 , ctCS402,t4 ,
ctCS402,t5 })
CS408:
exactly(2, {ctCS408,t1 , ctCS408,t2 , ctCS408,t3 , ctCS408,t4 ,
ctCS408,t5 })
3.3.2 Solution
The following table can be created by feeding the encoding clauses to a Max-
SAT solver and looking at the truth assignments of the ct and the cr variables
in the output model.
Rooms/Timeslots t1 t2 t3 t4 t5
r1 CS305 lect. CS202 lect.
r2 M271 lect. M271 sec. CS101 lect. CS304 lect. CS408 lect.
lab1 CS101 lect. CS202 lab CS305 lab
lab2 CS402 lect. CS304 lab CS402 lab CS408 lab CS101 lect.
12
each constraint can be grouped together to form one CNF formula to be fed to
a Max-SAT solver and generate a solution. The timetable can be created by
checking the output model for the truth assignments of the basic variables of
the encoding. A detailed example has been given to illustrate the workings of
our method.
The encoding of some constraints can be further enhanced such as room
capacity. It is sometimes possible to split the students of a section or a lab-
oratory into two groups if the number of students exceeds the room capacity.
Each group can have its own group of teaching assistants. This idea is not very
applicable for lectures as the number of teachers is small compared to that of
teaching assistants.
We are also interested in the problem of determining the minimum amount
of a certain resource (e.g., rooms, timeslots) necessary to have a conflict-free
timetable.
References
[1] Ignasi Abıo, Robert Nieuwenhuis, Albert Oliveras, and Enric Rodrıguez-
Carbonell. A parametric approach for smaller and better encodings of
cardinality constraints. 2013.
[2] Roberto Ası́n Achá and Robert Nieuwenhuis. Curriculum-based course
timetabling with sat and maxsat. Annals of Operations Research, pages
1–21, 2012.
[3] Olivier Bailleux and Yacine Boufkhad. Efficient cnf encoding of
boolean cardinality constraints. In Principles and Practice of Constraint
Programming–CP 2003, pages 108–122. Springer, 2003.
[4] Stephen A Cook. The complexity of theorem-proving procedures. In Pro-
ceedings of the third annual ACM symposium on Theory of computing,
pages 151–158. ACM, 1971.
[5] Pedro Filipe Medeiros da Silva. Max-sat algorithms for real world instances.
2010.
[6] Mikoláš Janota, Inês Lynce, Vasco Manquinho, and Joao Marques-Silva.
Packup: Tools for package upgradability solving system description. Jour-
nal on Satisfiability, Boolean Modeling and Computation, 8:89–94, 2012.
[7] Filip Maric. Timetabling based on sat encoding: a case study, 2008.
[8] Joao Marques-Silva and Inês Lynce. Towards robust cnf encodings of cardi-
nality constraints. Principles and Practice of Constraint Programming–CP
2007, pages 483–497, 2007.
[9] Elizabeth Montero, Marıa-Cristina Riff, and Leopoldo Altamirano. A pso
algorithm to solve a real course+ exam timetabling problem. In Interna-
tional Conference on Swarm Intelligence, pages 24–1, 2001.
13
[10] Fahima NADER, Mouloud KOUDIL, Karima BENATCHBA, Lotfi AD-
MANE, Said GHAROUT, and Nacer HAMANI. Application of satisfiabil-
ity algorithms to time-table problems. Rapport Interne LMCS, INI, 2004.
[11] G-J Nam, Fadi Aloul, Karem A. Sakallah, and Rob A. Rutenbar. A compar-
ative study of two boolean formulations of fpga detailed routing constraints.
Computers, IEEE Transactions on, 53(6):688–696, 2004.
[12] Wayne Pullan. Protein structure alignment using maximum cliques and
local search. In AI 2007: Advances in Artificial Intelligence, pages 776–
780. Springer, 2007.
[13] Sean Safarpour, Hratch Mangassarian, Andreas Veneris, Mark H Liffiton,
and Karem A Sakallah. Improved design debugging using maximum satis-
fiability. In Formal Methods in Computer Aided Design, 2007. FMCAD’07,
pages 13–19. IEEE, 2007.
[14] Tian Sang, Paul Beame, and Henry Kautz. A dynamic approach to mpe and
weighted max-sat. In Proceedings of the 20th international joint conference
on Artifical intelligence, pages 173–179. Morgan Kaufmann Publishers Inc.,
2007.
[15] Michel Vasquez and Jin-Kao Hao. A logic-constrained knapsack formu-
lation and a tabu algorithm for the daily photograph scheduling of an
earth observation satellite. Computational Optimization and Applications,
20(2):137–157, 2001.
[16] Hui Xu, Rob A Rutenbar, and Karem Sakallah. sub-sat: a formulation
for relaxed boolean satisfiability with applications in routing. Computer-
Aided Design of Integrated Circuits and Systems, IEEE Transactions on,
22(6):814–820, 2003.
14