How to define a Fitness Function in a Genetic Algorithm? | by Vijini Mallawaarachchi | Towards Data Science

In my previous article, I have explained the basics about Genetic Algorithms. After it was published, I got many requests to discuss more about the Fitness Function and Evaluation Strategies. In this article, we will discuss about fitness functions and how to come up with a fitness function for a given problem.
Fitness Function (also known as the Evaluation Function) evaluates how close a given solution is to the optimum solution of the desired problem. It determines how fit a solution is.
In genetic algorithms, each solution is generally represented as a string of binary numbers, known as a chromosome. We have to test these solutions and come up with the best set of solutions to solve a given problem. Each solution, therefore, needs to be awarded a score, to indicate how close it came to meeting the overall specification of the desired solution. This score is generated by applying the fitness function to the test, or results obtained from the tested solution.
The following requirements should be satisfied by any fitness function.
The fitness function should be clearly defined. The reader should be able to clearly understand how the fitness score is calculated.The fitness function should be implemented efficiently. If the fitness function becomes the bottleneck of the algorithm, then the overall efficiency of the genetic algorithm will be reduced.The fitness function should quantitatively measure how fit a given solution is in solving the problem.The fitness function should generate intuitive results. The best/worst candidates should have best/worst score values.
The fitness function should be clearly defined. The reader should be able to clearly understand how the fitness score is calculated.
The fitness function should be implemented efficiently. If the fitness function becomes the bottleneck of the algorithm, then the overall efficiency of the genetic algorithm will be reduced.
The fitness function should quantitatively measure how fit a given solution is in solving the problem.
The fitness function should generate intuitive results. The best/worst candidates should have best/worst score values.
Each problem has its own fitness function. The fitness function that should be used depends on the given problem. Coming up with a fitness function for the given problem is the hardest part when it comes to formulating a problem using genetic algorithms.
There is no hard and fast rule that a particular function should be used in a particular problem. However, certain functions have been adopted by data scientists regarding certain types of problems.
Typically, for classification tasks where supervised learning is used, error measures such as Euclidean distance and Manhattan distance have been widely used as the fitness function.
For optimization problems, basic functions such as sum of a set of calculated parameters related to the problem domain can be used as the fitness function.
Let’s go through a few example problems and their related fitness functions.
Consider the example given below. I have used this simple example mostly for the ease of understanding.
Given a set of 5 genes, which can hold one of the binary values 0 and 1, we have to come up with the sequence having all 1s. So we have to maximize the number of 1s as much as possible. This can be considered as an optimization problem. Hence, the fitness function is considered as the number of 1s present in the genome. If there are five 1s, then it is having maximum fitness and solves our problem. If there are no 1s, then if has the minimum fitness.
The code given below shows how the fitness function is being implemented to calculate the fitness score.
A very famous scenario where genetic algorithms can be used is the process of making timetables or timetable scheduling.
Consider you are trying to come up with a weekly timetable for classes in a college for a particular batch. 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.
Since there should be no collisions among classes we should minimize the number of students having class conflicts. You can formulate the fitness function as the inverse of the number of students with class conflicts. Lesser the number of students with class conflicts, more fit the class is.
Consider three variables x, y and z. The problem is to find the best set of values for x, y and z so that their total value is equal to a value t.
x + y + z = t
We have to reduce the sum x+y+z from deviating from t, i.e. |x + y + z — t| should be zero. Hence the fitness function can be considered as the inverse of |x + y + z - t|.
Fitness function = 1/|x + y + z - t|
These are a few examples of applications where genetic algorithms are used and how to come up with their fitness functions. I have used these simple examples for the ease of understanding. When modeling complex real world problems, we may not get such simple fitness functions.
The fitness function has to be something that measures how good your solution is. In particular, it should be able to handle whatever the available solutions generated and has to show the right way to improve them.
For example, a fitness function that is zero unless the answer is right is not good, because it doesn’t help you get an idea of how close the solution is to the right answer. Also, a fitness function that increases as solutions get better, but doesn’t identify the best solution is not so good either, because your population will improve up to a certain point and then get stuck.
You have to play around with the problem, look in different ways and think about what kind of function you can use to check how good your solution is. You want a function that gives low values for bad solutions and high values for good solutions. With practice, you will eventually get better at defining a fitness function for a given problem.
Hope you got a basic idea on how to define a fitness function for a given problem where genetic algorithms are used for solving.
Thanks for reading...