Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

The Implementation of Simple Genetic Algorithm in a

Simple Card Dividing Game


Lecturer: M. M. Irfan Subakti, SKom, MScEng, MPhil.

Institut Teknologi Sepuluh Nopember


Surabaya, Indonesia
2018
Abstract
Genetic Algorithm is a search heuristic algorithm which is inspired by Charles Darwin’s
theory of natural evolution. This algorithm reflects the process of natural selection where
the fittest or the best individuals are selected for reproduction in order to produce
offspring of a better generation. There are many implementations of Genetic Algorithm in
daily life such as the stochastic optimization techniques in the manufacturing system, the
automotive design especially in both composite materials and aerodynamic shape. This
paper is used to explain, breakdown, and giving the example code using C#
programming language of the internal structure of a Genetic Algorithm.
Introduction
Genetic Algorithm or GA is a search technique used in a computing process which purpose
is to find true or approximate solutions to the optimization and search problems. GA is a
particular class of the evolutionary algorithms which use several method inspired by
evolutionary biology such as inheritance, mutation, selection, and crossover/recombination.
GAs are implemented as a computer program in which population are abstract
representation, commonly known as chromosomes, genotype, or genome, of candidate
solutions, commonly known as individuals, creatures, or phenotypes. Traditionally, the
solutions are represented as 1s and 0s. There are 5 phases of GAs:
1. Initial Population
The processes of GA begins with a set of individuals which is called Population. Each
individual is characterized by a set of parameters known as Gene. A group of genes is
called Chromosomes.

2. Fitness Function/Selection
The fitness function evaluate the fitness of every individuals. The probability of which a
certain individual is selected for reproduction is based on its fitness or evaluation
scores. A pair of individuals are chosen to be the parent to pass their genes to the next
generation. The individuals with high evaluation scores have more chance to be
selected to be the parents.
3. Crossover
Crossover is the most significant phase in a GA. In order to produce a better
individuals in the population, the least fit individual will be replaced by a better
individual which was bred from the population’s members themselves.

4. Mutation
In a certain offsprings, some of their genes can be subjected to a mutation with a low
probability. This implies that some of the bits in the bit string can be flipped.

5. Evolution
The evolution usually starts from a population of randomly generated individuals, and
happens in generations. In each generation, the fitness of every individual in the
population is evaluated, multiple individuals are stochastically selected from the
current population (based on their fitness), and modified (recombined and possibly
mutated) to form a new population. The new population is then used in the next
iteration of the algorithm.
As there are many types of GAs, the author will use a certain GA called Microbial GA.
The Microbial GA works for the population of genotypes, which are designed by a user to
attain his/her goal. Two genotypes are picked up at random, and then their fitness are
evaluated. The genotype that has a higher fitness is the winner, and the other is the loser
(Rank-based Selection). By copying the segment of the winner genotype to the loser
genotypes at the same locus, new offspring is generated, and the loser genotype is
deleted from the population. Unlike basic GAs, in the Microbial GA, only one offspring is
generated at a time, and then the loser genotype is replaced by the new one (Steady
State Method).
Derived from the definition of the Microbial GA, the basic operation of Microbial GA are
as follows:
1. Pick two individuals at random.
2. Compare evaluation scores of both individuals to produce a winner individual(with
higher fitness scores) and a loser individual(with lower fitness scores).
3. With some random probability, the loser will copy some genes from the winner.
4. With some random probability, the loser can mutate. Number 3 and 4 will produce a
new gene.
5. Replace the loser with the new gene.
The diagram processes of Microbial GA is as follows:

Problems
There are a pile of 10 cards
numbered 1 until 10. The task
is to divide the pile into two
piles containing 5 cards each.
The first pile is called the Sum
Pile and the second is the
Product Pile. The sum each
member of the Sum Pile must
equal to 36 and the product
of each member of the
Product Pile must equal to
360.
Solutions
The solution code for the sample problem can be found in
http://bit.ly/IF184981_TK_5116100107_ChristopherAndrew
0. Initialization

Assumes that there are 50 piles(populationSize) of 10 cards(geneLength) with the


sumTarget is 36 and the productTarget is 360.
1. The Initial Population

In this step, the function of initPopulation() gives a random value of 1 and 0 as the
gene to a population member.
2. The Fitness Function / Selection
There are three steps which are done in the fitness function(evaluationOf()):
- Loop through the population member’s gene.
- If the value of the gene is 0, add it to the sumPile
- If the value of the gene is 1, add it to the productPile
- Calculate the totalError of both piles.(the lower the error, the higher the fitness is)

While the selection process takes on the winner and loser selection.
3. Crossover
The crossover phase of Microbial GA only affect the loser. The probability is equal to
the recombinationChance

4. Mutation
There are only a little probability for the loser to mutate. The probability is equal to
the mutationChance

5. Evolution
The evaluation steps are including the Crossover and Mutation steps.
Result
After compiling the code using .NET 4.5, the following is the results:

Proofing:
1. sumPile: 2 + 7 + 8 + 9 + 10 = 36
2. productPile:1 × 3 × 4 × 5 × 6 = 360
Conclusions
GA is used mainly for the searching and optimization problems. The sample problem used
in the paper indicate that even though the problem itself can be solved by
hand(manually), it could take 100s or even 1000s trials of error before getting the final
results.
References
I. Harvey, the Microbial Genetic Algorithm, 1996
I. Harvey, Artificial Evolution: A Continuing SAGA, in Evolutionary Robotics. From Intelligent
Robotics to Artificial Life, Lecture Notes in Computer Science, pp. 94-109, 2001
https://www.codeproject.com/Articles/16286/AI-Simple-Genetic-Algorithm-GA-to-solve-
a-card-pro
https://www.brainz.org/15-real-world-applications-genetic-algorithms/
https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-
code-e396e98d8bf3
http://jkoba.net/jkoba-wiki/index.php?The%20Microbial%20Genetic%20Algorithm

You might also like