Genetic Algorithm: Understanding The Gene Using Java

Link Copied To Clipboard !

gene-in-genetic-algorithm Algorithms

Genetic Algorithm is the popular evolutionary algorithm based on biological genetics and natural selection. It is typically used in the problems where the goal is to find an optimal best possible solution by performing some genetic operations like crossover and mutation over fittest and selected parents of existing population.

The common entities used in genetic algorithm are gene, chromosome, individual and population. The whole population is then subjected to the genetic operations like crossover and mutation.

What is a gene ?

In biological terms, a gene can be defined as a basic unit consisting of sequence of nucleotide in DNA or RNA that encodes the synthesis of a gene product i.e.RNA or protein.

In genetic algorithm, a gene is an atomic entity having some genetic properties. Gene is what makes an individual differ from others. Sequence of genes are combined in specified manner to form a chromosome which basically orderly packaged form of genes.

Let’s take an example of very simple binary genetic algorithm. The algorithm performs sequence of genetic operations until it makes an individual having chromosome with all genes 1 i.e. if we take chromosome size of 20, the aim is to find an individual chromosome with all 20 genes having value 1 (11111111111111111111). Here we have represented a gene which can have value of either 1 or 0. So, in this example, a gene class contains information about the numeric value it represents. The code below shows the typical gene we talked about.

package np.com.bsubash.ga.binary;

/**
 * @author subash
 * @since Mar 27, 2021
 */
public class Gene {
	private int number;

	public Gene(int number) {
		this.number = number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public int getNumber() {
		return number;
	}

	@Override
	public String toString() {
		return String.valueOf(this.getNumber());
	}
}

So this was the gene for binary genetic algorithm. To clarify more about gene, let’s take another example of class routine scheduling. In class routine scheduling, our aim is to find the best possible, non conflicting time tables from given information like teachers, subjects, rooms, time slots etc. Here, an individual represents one unique solution of problem and an individual contains chromosome built up by sequence of genes. So, a gene, in this case would be a single class schedule containing information about room for that class, teacher for that class, subject for that class and time slot for that class. Again, to clarify more, let’s see the code:


package np.com.bsubash.ga.classroutine;

/**
 * This is a gene class. It represents a unique scheduled time slot for specific
 * class i.e. specific room, specific time slot and specific teacher of
 * corresponding subject.
 * 
 * @author subash
 * @since Apr 29, 2021
 */
public class Gene {
	private Room room;
	private TimeSlot timeSlot;
	private Teacher teacher;

	public Gene(Room room, TimeSlot timeSlot, Teacher teacher) {
		this.room = room;
		this.timeSlot = timeSlot;
		this.teacher = teacher;
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public TimeSlot getTimeSlot() {
		return timeSlot;
	}

	public void setTimeSlot(TimeSlot timeSlot) {
		this.timeSlot = timeSlot;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

}

That was it. Hope you cleared your mind. Enjoy coding and solving the toughest problems. Good Luck !

You can find the whole source code on GitHub here => SOURCE CODE


You May Also Like