Beruflich Dokumente
Kultur Dokumente
Tushar Malhotra
totobogy@gmail.com
1
The OIPR’s (Open Intellectual Property Rights) for this paper are held by Tushar Malhotra. However, the
Publishing Rights are held by the EFY Group.
ABSTRACT Diagram 1: Progression of Solution Population
This paper introduces and compares Genetic
Algorithms as a search and optimization technique
against other traditional methods. It demonstrates
Evaluation
the implementation, working and utility of a Generation and
Genetic Algorithm by simulation of a “String based on some
reinsertion of
Guessing Game”. A simplistic statistical objective
new Population
performance analysis of the algorithm is also function
discussed.
INTRODUCTION
Should the ghost of Darwin happen to visit the people
involved with AI and Algorithm research today, he
would be pleasantly surprised to see his proposed
principles being applied in all sorts of interesting
applications! Selection of
Genetic Algorithms are a subdomain of Evolutionary Reproduction, individuals for
Algorithms which simulate Evolution – the way living Mutation, breeding to
beings evolve into forms which are more successful Variation to propagate next
and adaptable to their environments. The main and produce better generation
often cited principles involved are offsprings
Survival of the Fittest
Natural Selection
GA’s simulate the evolution in the following manner – A variety of algorithms/procedures are available to
We start from an initial set of possible solutions to a choose from at each step of the evolution, depending
given problem and allow these solutions to “evolve” or on the specific problem at hand. For details on the
improve over successive generations, trying to arrive various aspects of and techniques available at each
at the exact or optimal solution. stage of a Genetic Algorithm, readers are referred to
Like in nature, we go by the assumption that the [1].
solutions will improve, in general, with each
successive generation under the pressures of
selection and reproduction. HOW DO GENETIC ALGORITHMS DIFFER FROM THE
TRADITIONAL SEARCH AND OPTIMIZATION TECHNIQUES?
(See [1])
A typical Genetic (Evolutionary) Algorithm broadly
consists of the following stages:-
1. Coding of Solutions and determination of Genetic algorithms are most useful for
Solution Search-Space optimization problems with a very large solution-
2. Initialization of Population search space such that the use of conventional
3. Evaluation based on some objective function search techniques to locate the optimal solution is
4. Selection of individuals to reproduce and rendered inefficient.
propagate the next generation The optimization is achieved by the use of some
5. Reproduction, Variation, Alteration to produce objective evaluation function. Thus no
better offspring solutions derivative information is required.
6. Generation and reinsertion of new population A typical GA provides a number of potentially
Steps 3 through 6 are repeated till the desired results optimal or near optimal solutions to the given
are obtained. This is depicted in the Diagram 1 shown problem. This is especially useful in case of the
alongside. optimization problems which do not have a single
optimal solution (e.g. scheduling problems like
Activity selection; Shortest paths, Minimum
spanning trees etc.) in these cases a genetic
algorithm facilitates determination of various
alternative solutions simultaneously.
Owing to the above reason, Genetic Algorithms
are inherently Anytime Algorithms. That is they
can be intercepted at any time to yield a selection
of fittest solutions reached so far. This
characteristic is of much practical importance. Because any conventional algorithm would necessarily
Genetic Algorithms deal with a population of prove inefficient as shown above, I devise the
potential solutions instead of individual solutions. following GA to beat you in the game.
Thus they are typical candidates for parallel
processing due to their inherent parallel search 1. Population Initialization
nature.
I choose a population size, N (N = 100, say) and
I now demonstrate the implementation and working of generate the N initial individuals (here strings of
a Genetic Algorithm through a simple problem and its letters of the English alphabet) randomly. For
solution. I will explain why the problem is a suitable simplicity, I keep the population size fixed for each
candidate to deserve a GA solution, implement the generation, which means at least some of the
solution and analyze its working. The discussion will individuals of each generation must perish.
serve as an illustrative example for understanding the
key concepts involved. 2. Fitness Evaluation of a Generation
10 9.53
9.278
8.874
8.492 9.64 9.79
8.008 9.03 9.05
8.91
8 8.49 8.63
7.17
Avg. Score of generation
7.51 7.72
6.182
7.08
6
6.03
4.604
4 4.54
2.208
2
2.27
0
0 4 8 12 16 20 24 28 32 36 40 44 48
population size 100
Generations
population size 500
Figure 1(a)
12
10.12 11.66
9.734
10
8.724 10.17 10.24 10.35
Avg. Score of generation
9.51
8 7.346 8.94
7.82
6 5.468
6.6
5.1
4
2.722
2
2.46
0
0 4 8 12 16 20 24 28 32 39
population size 100
Generations
population size 500
Figure 1(b)
Sample Output with Population Size 6
Crossover Operation
Enter a seed value: 6767
Enter the string(8 characters without space) to be
constructed: zebrafry Single Point
Generation: 0
0 hsirfcna 4 Parents
1 wuctpqzy 3
2 mmhnkrbx 2
3 aqlnbjqw 2
4 rdeuvcrs 4
5 pycygnob 2 Children
Maximum possible score of any individual: 16
Average score of generation 0 : 2.83333
/* A string guessing game Author: Tushar Malhotra totobogy@ieee.org. This file was created using Bloodshed Dev-C++ 4.9.8.0*/
/* INCLUDES */
//Population size (MUST be greater than 5 or you are calling for trouble)
#define PSIZE 100
#define LEN 8 //string lrngth
#define MAX (LEN*2) //max possible score of any individual
//Prototypes
void evaluate(char*,char[][LEN+1],int*,int*); //evaluates a generation
void select(char[][LEN+1],int*,char*); //selects top N/2 individuals for reproduction
void crossover(char*,char*,char[][LEN+1],int); //performs single and double point crossover
void mutate(char[][LEN+1]); //mutates genes(letters) arbitrarily
int main()
{
//Variable definitions
char solstr[LEN+1];
char fittest[LEN+1] = "\0";
int gen = 0;
char pop[PSIZE][LEN+1];
int score[PSIZE];
double totalscore;
int rank[PSIZE]; //rank i is the string pop[rank[i]]
int i,j; //loop variables
char ch;
for(j=0;j<LEN;j++)
pop[i][j] = sspace[(rand() % 26)];
pop[i][j] = '\0';
srand(1); //reintialize srand()
}
while(strcmp(solstr,fittest) != 0)
{
//new generation starts
gen++;
totalscore = 0;
for(i=0;i<PSIZE;i++)
totalscore += score[i];
printf("\n\nAverage score of generation %d of my guesses: %f",gen,totalscore/PSIZE);
printf("\n(This statistic is based on the scores you have assigned to my guesses.\nIt shows how good or bad I'm doing!)");
printf("\n\nThe Fittest individual of this generation is: %s",fittest);
printf("\n\nTop 5 fittest individuals of generation %d are:-",gen);
for(i=0;i<5;i++)
printf("\nRank %d is %s having the score %d",i+1,pop[rank[i]],score[rank[i]]);
if(ch != 'y')
break;
if(score[rank[0]] == MAX)
printf("\n\nI guessed the word! Watch out, I can read your mind now!");
getch();
return 0;
}
if(j == k)
{
score[i] +=2;
tscore[i] +=2;
}
else
{
score[i]++;
tscore[i]++;
}
break; //break and lookup next char
}
}}}
for(i=0;i<PSIZE;i++)
rank[i] = i;
for(i=0;i<PSIZE;i++)
{
for(j=0;j<PSIZE-i-1;j++)
{
if(tscore[j]<tscore[j+1])
{
tmp = rank[j];
rank[j] = rank[j+1];
rank[j+1] = tmp;
tmp = tscore[j];
tscore[j] = tscore[j+1];
tscore[j+1] = tmp;
}
}
}
}//end of function
for(i=0;i<PSIZE/2;i++)
strcpy(newpop[i],pop[rank[i]]);
//Reproduction by crossover of pairs - single pt/double point. Yields 2 children per pair.
//Elitism - best preserved.
for(i=0;i<PSIZE;i+=2)
crossover(newpop[rand()%(PSIZE/2)],newpop[rand()%(PSIZE/2)],newpop,((PSIZE/2)+(i%(PSIZE/2))-1));
}//end of function
//Random Mutations
void mutate(char newpop[PSIZE][LEN+1])
{
int i,j,k1,k2;
char tmp;
seed *= rand();
srand(seed);
End of Code
REFERENCES