0% found this document useful (0 votes)
30 views

Time Table Generation Projects in Java

Uploaded by

Sowmiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Time Table Generation Projects in Java

Uploaded by

Sowmiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction

Making a class schedule is one of those NP hard problems. The problem can be solved using a
heuristic search algorithm to find the optimal solution, but it only works for simple cases. For
more complex inputs and requirements, finding a considerably good solution can take a while, or
it may be impossible. This is where genetic algorithms come in to the game. In this article, I
assume that you are familiar with the basic concepts of genetic algorithms, and I won't describe
them in detail because it has been done so ma n y times before.

B ack g ro u n d
When you make a class schedule, you must take into consideration many requirements (number
of professors, students, classes and classrooms, size of classroom, laboratory equipment in
classroom, and many others). These requirements can b e divided into several groups b y their
importance. Hard requirements (if you break one of these, then the schedule is infeasible):


A class can be placed only in a spare classroom.

No professor or student group can have more then one class at a time.

A classroom must have enough seats to accommodate all students.

To place a class in a classroom, the classroom must have laboratory equipment
(computers, in our case) if the class requires it.

Some soft requirements (can be broken, bu t the schedule is still feasible):


Preferred time of class by professors.

Preferred classroom by professors.

Distribution (in time or space) of classes for student groups or professors.

Hard and soft requirements, of course, depend on the situation. In this example, only hard
requirements are implemented. Let's start by explaining the objects which makes a class
schedule.

Objects of Class Schedule


Professor

The P r o f e s s o r class has an ID and the name of the professor. It also contains a list of classes
that a professor teaches.

Students G r o u p

The St u d e n t s Gr o u p class has an ID and the name of the student group, as well as the number of
students (size of group). It also contains a list of classes that the group attends.
Classroom

The Room class has an ID and the name of the classroom, as well as the number of seats and
information about equipment (computers). If the classroom has computers, it is expected that
there is a computer for each seat. IDs are generated internally and automatically.

Course

The Co u r s e class has an ID and the name of the course.

Class

Co u r s e Cl a s s holds a reference to the course to which the class belongs, a reference to the
professor who teaches, and a list of student groups that attend the class. It also stores how many
seats (sum of student groups' sizes) are needed in the classroom, if the class requires computers
in the classroom, and the duration of the class (in hours).

C h ro m o so m e
The first thing we should consider when we deal with a genetic algorithm is how to represent our
solution in such a way that it is feasible for genetic operations such as crossover and mutation.
Also, we should know how to specify how good our solution is. In other words, we should be
able to calculate the fitness value of our solution.

Representation

How can we represent the chromosome for a class schedule? Well, we need a slot (time-space
slot) for each hour (we assume that time is in one hour granules), for every room, every day.
Also, we assume that classes cannot begin before 9am, and should finish before or at 9pm (12
hours total), and working days are from Monday to Friday (5 days total). We can use an
s t d : : v e c t o r with a size 1 2 * 5 * n u mb e r _ o f _ r o o ms . The slot should be an s t d : : l i s t because
during the execution of our algorithm, we allow multiple classes during the same time-space slot.
There is an additional hash map which is used to obtain the first time-space slot at which a class
begins (its position in vector) from the address of the class' object. Each hour of a class has a
separate entry in the vector, but there is only one entry per class in the hash map. For instance, if a
class starts at 1pm and lasts for three hours, it has entries in the 1pm, 2pm, and 3pm slots.
Figure 1 - Chromosome Representation

Chromosomes are represented by the Sc h e d u l e class, and it stores the representation of a class
schedule in these two attributes:

Collapse |
/ / Ti me - s p a c e s l ot s , o n e e n t r y r e p r e s e n t o n e h o u r i n o n e
c l a s s r oom v e c t or <l i s t <Cou r s e Cl a s s * >> _ s l o t s ;

/ / Cl a s s t a b l e f o r c h r o mo s o me
/ / U s e d t o d e t e r mi ne f i r s t t i me - s p a c e s l o t u s e d b y
c l a s s h a s h _ ma p <Co u r s e Cl a s s * , i nt > _ c l a s s e s ;

Additionally, the chromosome should store its fitness value and the parameters which are used
by genetic operations.

The fitness value is stored here:

Collapse
/ / Fi t ne s s v a l u e o f
c h r o mo s o me f l o a t _ f i t n e s s ;

/ / Fl a gs o f c l a s s r e q ui r ome nt s
s a t i s f a c t i o n v e c t o r <b oo l > _ c r i t e r i a ;

Chromosome parameters:

Collapse
/ / Nu mb e r o f c r o s s o v e r p o i n t s o f pa r e nt ' s c l a s s t a b l e s
i n t _n u mbe r Of Cr os s ove r Po i nt s ;

/ / Nu mb e r o f c l a s s e s t h a t i s moved r a n d o ml y b y s i n g l e mut a t i on
o p e r a t i o n i n t _mut a t i onSi z e ;
/ / Pr o b a b i l i t y t h a t c r o s s o v e r wi l l
o c c u r e i n t _ c r o s s o v e r Pr o b a b i l i t y ;

/ / Pr o b a b i l i t y t h a t mu t a t i o n wi l l
o c c u r e i n t _ mu t a t i o n Pr o b a b i l i t y ;

Fitness

Now we need to assign a fitness value to the chromosome. As I previously said, only hard
requirements are used to calculate the fitness of a class schedule. This is how we do it:


Each class can have 0 to 5 points.
• If a class uses a spare classroom, we increment its score.
• If a class requires computers and it is located in the classroom with them, or it doesn't
require them, we increment the score of the class.

If a class is located in a classroom with enough available seats, guess what, we increment
its score.

If a professor has no other classes at the time, we increment the class's score once again.

The last thing that we check is if any of the student groups that attend the class has any
other class at the same time, and if they don't, we increment the score of the class.

If a class breaks a rule at any time-space slot that it occupies, its score is not incremented
for that rule.

The total score of a class schedule is the sum of points of all classes.

The fitness value is calculated as s c h e d u l e _ s c o r e / ma x i mu m_ s c o r e , and
ma x i mu m_ s c o r e is numb e r _of _c l a s s e s *5 .

The fitness values are represented by single-precision floating point numbers (f l oa t ) in the
range 0 to 1.

Crossover

A crossover operation combines data in the hash maps of two parents, and then it creates a vector
of slots according to the content of the new hash map. A crossover 'splits' hash maps of both
parents in parts of random size. The number of parts is defined by the number of crossover points
(plus one) in the chromosome's parameters. Then, it alternately copies parts form parents to the
new chromosome, and forms a new vector of slots.
Figure 2 - Crossover operation
Collapse
/ / Pe r f o r me s c r o s s o v e r
operation using t o
c h r o mo s o me s
/ / and r e t u r n s p o i n t e r
to offspring
Sc h e d u l e *
Cr os s ove r ( c ons t
Sc h e d u l e & p a r e n t 2 )
const;

Mutation

A mutation operation is very simple. It just takes a class randomly and moves it to another
randomly chosen slot. The nmber of classes which are going to be moved in a single operation is
defined by the mutation size in the chromosome's parameters.
Algorithm
Collapse
/ / Pe r f o r ms mu t a t i o n o n
The genetic algorithm is fairly simple. For each generation, it performs two basic operations:
c h r o mo s o me v o i d Mu t a t i o n ( ) ;

1. Randomly selects N pairs of parents from the current population and produces N new
chromosomes by performing a crossover operation on the pair of parents.
2. Randomly selects N chromosomes from the current population and replaces them with
new ones. The algorithm doesn't select chromosomes for replacement if it is among the
best chromosomes in the population.

And, these two operations are repeated until the best chromosome reaches a fitness value equal to
1 (meaning that all classes in the schedule meet the requirement). As mentioned before, the
genetic algorithm keeps track of the M best chromosomes in the population, and guarantees that
they are not going to be replaced while they are among the best chromosomes.

Collapse
/ / Ge n e t i c
a l gor i t hm c l a s s
Al go r i t h m
{

private:

// Popul a t i on o f c h r o mo s o me s
v e c t o r <Sc h e d u l e * > _ c h r o mo s o me s ;

// I n i d i c a t e s wh e a h t e r c h r o mo s o me
belongs to
// b e s t c h r o mo s o me g r o u p
ve c t o r <bo o l > _be s t Fl a gs ;

// I n d i c e s o f b e s t c h r o mo s o me s
ve c t or <i nt > _ b e s t Ch r o mo s o me s ;

// Nu mb e r o f b e s t c h r o mo s o me s
c u r r e n t l y saved i n
// b e s t c h r o mo s o me g r o u p
i n t _c ur r e nt Be s t Si z e ;

// Nu mb e r o f c h r o mo s o me s
wh i c h a r e r e p l a c e d i n
// each g e n e r a t i o n by o f f s p r i n g
i n t _r e pl a c e ByGe ne r a t i on;

// Poi nt e r t o a l gor i t hm o b s e r v e r
Sc h e d u l e Ob s e r v e r * _ o b s e r v e r ;

// Pr ot ot yp e o f c h r o mo s o me s i n p o p u l a t i o n
Sc h e d u l e * _ p r o t o t y p e ;

// Cur r e nt g e n e r a t i o n i n t
_c ur r e nt Ge ne r a t i on;

// St a t e o f e x e c u t i o n o f a l gor i t hm
Al gor i t hmSt a t e _ s t a t e ;

// Sy n c h r o n i z a t i o n o f a l g o r i t h m' s s t a t e
CCr i t i c a l Se c t i o n _ s t a t e Se c t ;

// Poi nt e r t o g l o b a l i n s t a n c e o f a l gor i t hm
s t a t i c Al g o r i t h m* _ i n s t a n c e ;

// Sync h r oni z a t i on o f c r e a t i o n a n d
destruction
// of global instance
s t a t i c CCr i t i c a l Se c t i on _i ns t a nc e Se c t ;

public:

/ / Re t ur ns r e f e r e n c e t o g l o b a l i n s t a n c e o f
a l gor i t hm s t a t i c Al g o r i t h m& Ge t I ns t a nc e ( ) ;

/ / F r e e s me mo r y u s e d b y g l o v a l
i n s t a n c e s t a t i c v o i d Fr e e I ns t a nc e ( ) ;

/ / I n i t i a l i z e s g e n e t i c a l gor i t hm
Al gor i t hm( i nt
n u mb e r Of Ch r o mo s o me s ,
int
r e pl a c e ByGe ne r a t i on, i n t
t r a c kBe s t ,
Sc h e d u l e * pr ot ot ype ,
Sc h e d u l e Ob s e r v e r * o b s e r v e r ) ;

/ / Frees used resources


~Al gor i t hm( ) ;

/ / St a r t s a n d e x e c u t e s
a l gor i t hm v o i d St a r t ( ) ;

/ / St ops e x e c u t i o n o f
a l go r ut hm v o i d St op( ) ;

/ / Re t u r ns p o i n t e r t o b e s t c h r o mo s o me s i n
p o p u l a t i o n Sc h e d u l e * Ge t Be s t Ch r o mo s o me ( ) c o n s t ;

/ / Re t u r n s c u r r e n t g e n e r a t i o n
i n l i n e i n t Ge t Cur r e nt Ge n e r a t i on( ) const { return
_c ur r e nt Ge ne r a t i on; }

/ / Re t u r n s p o i n t e t o a l g o r i t h m' s o b s e r v e r
i n l i n e Sc h e d u l e Ob s e r v e r * Ge t Ob s e r ve r ( ) c o n s t
{ return _observer; }

private:

/ / Tr i e s t o a d d c h r o mo s o me s i n b e s t c h r o mo s o me
g r o u p v o i d Ad d To Be s t ( i n t c h r o mo s o me I n d e x ) ;

/ / Re t ur n s TRUE i f c h r o mo s o me b e l o n g s t o b e s t c h r o mo s o me
g r o u p b o o l I s I nBe s t ( i nt c h r o mo s o me I n d e x ) ;

/ / Cl e a r s b e s t c h r o mo s o me
Observing
g r o u p v o i d Cl e a r Be s t ( ) ;

};
The Sc h e d u l e Ob s e r v e r class handles the events that are triggered by the genetic algorithm. This
class sends messages to the view window of the application. Also, you can block the caller's
thread until the execution of the algorithm is not finished or stopped, by calling the Wa i t Eve nt ( )
method.
Collapse
/ / Ha n d l e s e v e n t t h a t i s r a i s e d
/ / when a l gor i t hm f i n d s new b e s t c h r o mo s o me
v o i d Ne wBe s t Ch r o mo s o me ( c o n s t Sc h e d u l e & n e wCh r o mo s o me ) ;

/ / Ha n d l e s e v e n t t h a t i s r a i s e d when s t a t e
/ / o f e x e c u t i o n o f a l gor i t hm i s c h a n g e d
v o i d Evol ut i on St a t e Ch a nge d( Al go r i t hmSt a t e ne wSt a t e ) ;

/ / Bl o c k cal l er ' s t h r e a d u n t i l a l gor i t hm f i n i s h e s


e x e c u t i o n i n l i n e v o i d Wa i t Eve nt ( ) / / . . .

If you plan to change the Ne wBe s t Ch r o mo s o me method, keep in mind that if you want to keep
the best chromosome to display it, you must make a hard copy (by using the Ma k e Co p y method of
the Sc h e d u l e class), because the algorithm can delete that chromosome in the next generation.

Configuration
Configuration File

Types of objects:

1. professor ( # p r o f tag) - describes a professor.


2. course ( # c o u r s e tag) - describes a course.
3. room (# r o o m tag) - describes a room.
4. group ( # g r o u p tag) - describes a students group.
5. course class (#c l a s s tag) - describes a class, and binds the professor, course, and
students group.

Each object begins with its tag and finishes with the # e n d tag, all tags must be in separate lines.
In the body of an object, each line contains only one key and value pair (attribute) separated by
an = character. Each attribute should be specified just one time, except for the group attribute in
the # g r o u p object which can have multiple group attributes. Tag and key names are case
sensitive. Here is a list of the objects' attributes:

1. # p r o f
o i d (number, required) - ID of the professor.
o name (string, required) - name of the professor.
2. # c o u r s e
o i d (number, required) - ID of the course.
o name (string, required) - name of the course.
3. # r o o m
o name (string, required) - name of the room.
o s i z e (number, required) - number of seats in the room.
o l a b (boolean, optional) - indicates if the room is a lab (has computers); if not
specified, the default value is false.
4. # g r o u p
o i d (number, required) - ID of the students group.
o name (string, required) - name of the students group.
o s i z e (number, required) - number of students in the group.
5. # c l a s s
o p r o f e s s o r (number, required) - ID of a professor; binds a professor to a class.
o c o u r s e (number, required) - ID of a course; binds a course to a class.
o g r o u p (number, required) - ID of a students group; binds the students group to a
class; each class can be bound to multiple students groups.
o d u r a t i o n (number, optional) - duration of class (in hours); if not specified, the
default value is 1.
o l a b (boolean, optional) - if the class requires computers in a room; if not
specified, the default value is false.

Note that the professor, students group, and course objects must be defined before they are
bound to a course class object.

Example of a Configuration File

Collapse
#prof
id = 1
name = J o h n
#end Smi t h

#course
id = 1
name = I n t r o d u c t i o n t o Pr o g r a mmi n g
#end

#r oom
name = R1
lab = true
s i z e = 24
#end

#group
id = 1
name = 1O1
s i z e = 19
#end

#class
professor
= 1
course = 1
duration =
2
group = 1
group = 2
#end

#class
professor
= 1
course = 1
duration = 3
group = 1
lab = true
#end

#class
professor
= 1
course = 1
duration =
3
group = 2
lab = true
#end

Parsing a
Configuration

Parsing of a configuration file is done by the Conf i gur a t i on class. The Pa r s e Fi l e method
opens and parses a configuration file. It searches for object tags and calls the appropriate method
for a parsing object. The Pa r s e Fi l e method also clears a previously parsed object.

Collapse
public:
v o i d Pa r s e Fi l e ( c ha r * f i l e Na me ) ;

private:

P r o f e s s o r * Pa r s e Pr of e s s or ( i f s t r e a m& f i l e ) ;
St u d e n t s Gr o u p * Pa r s e St ud e n t s Gr oup( i f s t r e a m&
f i l e ) ; Co u r s e * Pa r s e Cou r s e ( i f s t r e a m& f i l e ) ;
Room* Pa r s e Ro o m( i f s t r e a m& f i l e ) ;
Co u r s e Cl a s s * Pa r s e Cour s e Cl a s s ( i f s t r e a m& f i l e ) ;

To parse a file:

Collapse
Conf i gur a t i o
n: : Ge t I ns t a
nc e ( ) . Pa r s e F
i l e ( " Ga Sc h e
d ul e . c f g" ) ;

ha s h_ma p<i nt , Professor*> _professors;


Parsed objects are kept in
St a
u dhash
e n t s map
Gr o uexcept
p * > for course classes, so they can be accessed easily
and fast.
ha s h_ma p<i nt , _s t ude nt Gr oups ; Co u r s e * >
_courses;
ha s h_ma p<i nt , Room*> _r o o ms ;
Collapse
l i s t <Cour s e Cl a s s *> _c our s e Cl a s s e s ;
private:
ha s h_ma p<i nt ,
The Conf i gur a t i on class also contains the methods for retrieving the parsed information and
objects.
Collapse
public:
i n l i n e P r o f e s s o r * Ge t Pr of e s s o r ByI d( i nt
i n l i n e i d ) / / . . . i n t Ge t Nu mb e r Of Pr o f e s s o r s ( )
const / / . . .
i n l i n e St u d e n t s Gr o u p * Ge t St ud e nt s Gr oup ByI d( i nt
i d ) / / . . . i n l i n e i n t Ge t Nu mb e r Of St u d e n t Gr o u p s ( )
const / / . . .

i n l i n e Co u r s e * Ge t Cou r s e ByI d ( i n t id) / / . . .


i n l i n e i n t Ge t Nu mb e r Of Co u r s e s ( )
const / / . . .

i n l i n e Room* Ge t Ro o mBy I d ( i n t i d ) / / . . .
i n l i n e i n t Ge t Nu mb e r Of Ro o ms ( )
const / / . . .

i n l i n e c o n s t l i s t <Cou r s e Cl a s s * >& Ge t Cou r s e Cl a s s e s ( )


c o n s t / / . . . i n l i n e i n t Ge t Nu mb e r Of Co u r s e Cl a s s e s ( ) c o n s t / / . . .

You might also like