Anda di halaman 1dari 26

# GENETIC ALGORITHM

M.Sivagami
APSG SCSE
VIT CC
www.ramperi.org

Agenda

## What is genetic algorithm?

History of genetic algorithm
Where to use Genetic Algorithm?
GA methodology
Application of GA
Implementation of GA
Genetic operators
Role of operators
Strength and weakness of Genetic Algorithm

## What is Genetic Algorithm?

Genetic algorithms are heuristic search
algorithm based on the evolutionary ideas
of natural selection and genetics
Heuristic is a method might not always
find the best solution but is guaranteed to
find solution in a reasonable time

History of GA
John Holland and his students-in 1960s1970s in University of Michigan
Book on GA Adaption in natural and
artificial systems by holland in 1975

## Where to use GA?

Complexity and size of the problem is high
We know the solution but dont know how to get it
Optimization and search problems.
- NP Complete problem
- Time tabling
- Scheduling
- Encryption and code breaking

Applications of GA

Engineering design
Automotive design
Computer gaming
Robotics
Evolvable hardware
Optimized telecommunication Routing
Encryption and code breaking
Biometric Inventions
Financial Investment
Marketing

## Simple Genetic Algorithm

produce an initial population of individuals
evaluate the fitness of all individuals
while termination condition not met do
select fitter individuals for reproduction
recombine between individuals
mutate individuals
evaluate the fitness of the modified individuals
generate a new population
End while

Implementation

Java JGAP,JGAL
Python
Lisp
MATLAB
c/c++

## Search the String

int count1 =0;
int main()
{
srand(unsigned(time(NULL)));
ga_vector pop_alpha, pop_beta;
init_population(pop_alpha, pop_beta);
for (int i=0; i<GA_MAXITER; i++) {
calc_fitness(pop_alpha);
sort_by_fitness(pop_alpha);
// sort them
print_best(pop_alpha); // print the best one
count1++;
if(pop_alpha[0].fitness == 0)
{cout << "number of iterations ->"<< count1;getch(); break;}
mate(pop_alpha,pop_beta);
swap(pop_alpha,pop_beta);
}
return 0;
}

## Intialize the population

void init_population(ga_vector &population, ga_vector &buffer )
{
int tsize = GA_TARGET.size();
for (int i=0; i<GA_POPSIZE; i++) {
ga_struct citizen;
citizen.fitness = 0;
citizen.str.erase();
for (int j=0; j<tsize; j++)
citizen.str += (rand() % 90) + 32;
population.push_back(citizen);
}
buffer.resize(GA_POPSIZE);
}

Fitness calculation
void calc_fitness(ga_vector &population)
{
string target = GA_TARGET;
int tsize = target.size();
unsigned int fitness;
for (int i=0; i<GA_POPSIZE; i++) {
fitness = 0;
for (int j=0; j<tsize; j++) {
fitness += abs(int(population[i].str[j] - target[j]));
}
population[i].fitness = fitness;
}
}

Selection
void elitism(ga_vector &population,
ga_vector &buffer, int esize )
{
for (int i=0; i<esize; i++) {
buffer[i].str = population[i].str;
buffer[i].fitness = population[i].fitness;
}
}

Sorting
bool fitness_sort(ga_struct x, ga_struct y)
{ return (x.fitness < y.fitness); }
inline void sort_by_fitness(ga_vector
&population)
{ sort(population.begin(), population.end(),
fitness_sort); }

Mutation
void mutate(ga_struct &member)
{
int tsize = GA_TARGET.size();
int ipos = rand() % tsize;
int delta = (rand() % 90) + 32;
member.str[ipos] = ((member.str[ipos] + delta)% 122);

Crossover
void mate(ga_vector &population, ga_vector &buffer)
{
int esize = GA_POPSIZE * GA_ELITRATE;
int tsize = GA_TARGET.size(), spos, i1, i2;
elitism(population, buffer, esize);
// Mate the rest
for (int i=esize; i<GA_POPSIZE; i++) {
i1 = rand() % (GA_POPSIZE / 2);
i2 = rand() % (GA_POPSIZE / 2);
spos = rand() % tsize;
buffer[i].str = population[i1].str.substr(0, spos) +
population[i2].str.substr(spos, tsize - spos);
if (rand() < GA_MUTATION) mutate(buffer[i]);
}
}

## Printing the best

inline void print_best(ga_vector &gav)
{ cout << "Best: " << gav[0].str << " (" <<
gav[0].fitness << ")" << endl; }

## Replacing the new population

inline void swap(ga_vector &population,
ga_vector &buffer)
{ ga_vector& temp = population; population =
buffer; buffer = temp; }

Solution representation

Binary digits
Integer
Real number
Character array
Any data structure tree in GP

Terminating criteria
A pre-determined number of generations
or time has elapsed
A satisfactory solution has been achieved
No improvement in solution quality has
taken place for a pre-determined number
of generations

Genetic Operators
Selection
Cross over
Mutation

Selection Operator
Random based selection
Fitness based selection

Crossover operator

## 1 point cross over

2 point cross over
Multipoint crossover
Cut and splicing (for variable length)
Uniform crossover

Mutation

Flip flop
Boundary
Non-Uniform
Uniform
Gaussian

Role of operators
High and low mutation
Selection is important
Selection and crossover tend to
converge on a good but sub optimal
solution
Selection and mutation create a parallel
hill climbing algorithm

Criticisms
Fitness function for complex problems
(structural optimisization)-expensive fitness function
evaluation and it takes long time several hours to
several days
Terminating criteria is not clear- it is only comparison
with other solution
Operating on dynamic data set begin to converge early
on towards solution which may no longer valid for later
data
Solutions high mutation probability
- comma strategy new parents are selected
only from offspring

Strength of GA
Fast comparatively with exhaustive search
algorithm
GA searches from a population of points
not from a single point. Hill climbing)
- No free lunch theorem