Using Genetic Algorithms to Schedule Timetables | by Vijini Mallawaarachchi | Towards Data Science

Genetic algorithms are a type of evolutionary algorithms that are inspired by the process of natural selection. I have already introduced genetic algorithms in my previous article, Introduction to Genetic Algorithms — Including Example Code. Go check it out if you haven’t already!
towardsdatascience.com
Recently I noticed that one reply I had left for a response about scheduling timetables using genetic algorithms has been really useful to many. Hence, I thought of expanding that reply into an article so that it will be of more use to the readers. Let’s get started.
Quoting from my previous article,
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
If you want to know more about genetic algorithms, you can read my article Introduction to Genetic Algorithms — Including Example Code where I have explained every phase with examples.
To summarise, the 5 main phases of a genetic algorithm are,
Initial populationFitness functionSelectionCrossoverMutation
Initial population
Fitness function
Selection
Crossover
Mutation
The following figure denotes the flow chart of how a general genetic algorithm works.
In timetabling, we have to allocate time for the activities we have planned and coordinate resources in an orderly way so that we can obtain our intended results without having to violate any constraints. For example, a school timetable would coordinate students, teachers, classrooms, subjects and time slots. An exam timetable would coordinate students, invigilators, subjects, exam halls and time slots. A public transport timetable would coordinate transport methods (buses, trains, etc.), routes, arrival times and departure times so that passengers can plan their trips.
A very popular scenario where genetic algorithms can be applied is in the process of scheduling timetables.
Consider you are trying to come up with a weekly timetable for classes in a college for different groups of students. We have to arrange classes and come up with a timetable so that there are no clashes between classes. Here, our task is to search for the optimum timetable schedule.
An individual is characterized by a set of parameters (variables) known as genes. Genes are joined into a string to form a chromosome (solution). We model our solutions as chromosomes.
In our example scenario, chromosomes will be different lecture sessions for students taking different modules. Consider that we have to coordinate student groups, modules, lecture halls, the days of the week and time. You can represent a lecture session as,
<Module, Student Group, Day, Location, Lecture Time>
You can encode the lecture session as a binary pattern to a chromosome.
You can give binary values for each value in each entity. You can change the encoding pattern as you wish. Given below is an example way you can encode the lecture session.
Get the list of modules and assign binary values.
Data Mining - 0000,Machine Learning - 0001,Biology - 0010,...
Get the list of student groups and give binary values.
STG0 - 00000, STG1 - 00001, STG2 - 00010, STG3 - 00011,...
Similarly, you can come up with coding schemes for every entity in the lecture session.
Given below is a sample encoding of a lecture session.
<Data Mining, STG3, Monday, Hall D, 8.00AM>Data Mining - 0000STG3 - 00011Monday - 000Hall D - 10108.00AM - 1000Chromosome - 00000001100010101000
Individual bits are called genes. This chromosome has 20 genes.
Different student groups take different classes within a week. Hence, you have to come up with different class combinations and create the initial population. You can decide upon the size of the population (number of classes).
<Data Mining, STG3, Monday, Hall A, 8.00AM><Machine Learning, STG2, Tuesday, Hall B, 8.00AM><Computational Biology, STG8, Tuesday, Hall A, 10.00AM>...
You have to encode these classes into chromosomes as mentioned before.
As an example, you can formulate the evaluation function as the inverse of the number of class conflicts for student groups. Lesser the number of conflicts, more fit the lecture session is. You can pick a suitable evaluation function as you wish.
Now you can perform crossover and mutation operations to maximize the fitness value for each lecture session.
You can terminate the process when the population has reached the maximum fitness value. In our example, that is when the lecture sessions have a minimum number of conflicts.
The method I have described in this article is very basic so that you can get a general idea about how to model the timetabling problem. If you want to read more, you can read many research articles where new and improved methods have been proposed to solve the timetabling problem using genetic algorithms. Some articles which I came across are listed below.
Time table scheduling using Genetic Algorithms employing guided mutation
Modified genetic algorithm for high school time-table scheduling with fuzzy time window
Solving time-table scheduling problem by novel chromosome representation using Genetic algorithm
Genetic Algorithm Analysis using the Graph Coloring Method for Solving the University Timetable Problem
An Improved Genetic Algorithm for Flexible Job Shop Scheduling Problem
A novel educational timetabling solution through recursive genetic algorithms
The genetic algorithm is not the only way we can solve the timetabling problem. There are many more evolutionary algorithms such as ant algorithm and bees algorithm which are population-based search algorithms.
Hope you got a basic idea about a real-world problem where we can use genetic algorithms to solve it. Let me know your thoughts in the responses section.
Don’t forget to check out this awesome implementation of genetic algorithms with visualizations of the gene pool in each generation at https://github.com/memento/GeneticAlgorithm by mem ento.
github.com
Thank you for reading!