0% found this document useful (0 votes)
10 views

GA

Genetic Algorithms (GAs) are optimization techniques inspired by natural selection and genetics, designed to navigate large search spaces for optimal solutions. They involve a population of candidate solutions that evolve through processes like selection, crossover, and mutation, with fitness values determining the likelihood of reproduction. While GAs are efficient for complex problems, they have limitations such as computational expense and lack of guaranteed optimality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

GA

Genetic Algorithms (GAs) are optimization techniques inspired by natural selection and genetics, designed to navigate large search spaces for optimal solutions. They involve a population of candidate solutions that evolve through processes like selection, crossover, and mutation, with fitness values determining the likelihood of reproduction. While GAs are efficient for complex problems, they have limitations such as computational expense and lack of guaranteed optimality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Module 7: Genetic Algorithm

By

Dr. Rajyalakshmi G
Professor
School of
Mechanical
Engineering, VIT,
What is Genetic Algorithm
 Nature has always been a great source of inspiration to all mankind.
 Genetic Algorithms (GAs) are search based algorithms based on the concepts of
natural selection and genetics.
 GAs are a subset of a much larger branch of computation known
as
Evolutionary Computation.
 GAs were developed by John Holland and his students and colleagues at the
University of Michigan, most notably David E. Goldberg and has since been
tried on various optimization problems with a high degree of success.

Genetic algorithms simulate the process of natural selection which means those species who can
adapt to changes in their environment are able to survive and reproduce and go to next
generation.
Introduction – Optimization Problem
Genetic Algorithms are good at taking large, potentially huge search spaces and
navigating them, looking for optimal combinations of things, solutions you might not
otherwise find in a lifetime.
 In GAs, we have a pool or a population of possible solutions to the given problem.

 These solutions then undergo recombination and mutation (like in natural


genetics),
producing new children, and the process is repeated over various generations.

 Each individual (or candidate solution) is assigned a fitness value (based on its objective
function value) and the fitter individuals are given a higher chance to mate and yield more
“fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.

 In this way we keep “evolving” better individuals or solutions over generations, till we reach
a stopping criterion.
 Genetic Algorithms are sufficiently randomized in nature, but they perform much better than
random local search (in which we just try various random solutions, keeping track of the best so
Advantages of GAs
GAs have various advantages which have made them immensely popular. These
include −

 Does not require any derivative information (which may not be available for many real-world
problems).
 Is faster and more efficient as compared to the traditional methods.
 Has very good parallel capabilities.
 Optimizes both continuous and discrete functions and also multi-objective problems.
 Provides a list of “good” solutions and not just a single solution.
 Always gets an answer to the problem, which gets better over the time.
 Useful when the search space is very large and there are a large number of
parameters involved.
Limitations of GAs
Like any technique, GAs also suffer from a few limitations. These include −

GAs are not suited for all problems, especially problems which are simple
and for which derivative information is available.
Fitness value is calculated repeatedly which might be
computationally expensive for some problems.
Being stochastic, there are no guarantees on the optimality or the quality of
the solution.
If not implemented properly, the GA may not converge to the
optimal solution.
GA – Motivation
Genetic Algorithms have the ability to deliver a “good-enough” solution “fast-
enough”. This makes genetic algorithms attractive for use in solving optimization
problems.

The reasons why GAs are needed are as follows

 Solving Difficult Problems

 In computer science, there is a large set of problems, which are NP-Hard. What
this essentially means is that, even the most powerful computing systems
take a very long time (even years!) to solve that problem.
 In such a scenario, GAs prove to be an efficient tool to provide usable near-

optimal solutions in a short amount of time.


Basic Terminology
Population − It is a subset of all the possible (encoded) solutions to the given problem. The
population for a GA is analogous to the population for human beings except that instead of
human beings, we have Candidate Solutions representing human beings.

Chromosomes − A chromosome is one such solution to the given problem.


Gene − A gene is one element position of a chromosome.
Allele − It is the value a gene takes for a particular chromosome.
Basic Terminology
Genotype − Genotype is the population in the computation space. In the computation
space, the solutions are represented in a way which can be easily understood and manipulated
using a computing system.

Phenotype − Phenotype is the population in the actual real world solution space in which
solutions are represented in a way they are represented in real world situations.

Decoding and Encoding − For simple problems, the phenotype and genotype spaces are the
same. However, in most of the cases, the phenotype and genotype spaces are different.
Decoding is a process of transforming a solution from the
genotype to the phenotype space, while encoding is a
process of transforming from the phenotype to genotype
space.
Decoding should be fast as it is carried out
repeatedly in aGAduring
thefitnessvaluecalculation.
Basic Terminology
Fitness Function − A fitness function simply
defined is a function which takes the solution as
input and produces the suitability of the
solution as the output.

In some cases, the fitness function and the


objective function may be the same, while in
others it might be different based on the
problem.

Genetic Operators − These alter the


genetic composition of the offspring.
These include crossover, mutation,
selection, etc.
Basic Terminology
Five phases are considered in a genetic algorithm.
1.Initial population
2.Fitness function
3.Selection
4.Crossover
5.Mutation

Initial
Population
The process begins with a set of individuals which is
called a Population. Each individual is a solution to the
problem you want to solve.
An individual is characterized by a set of parameters
(variables) known as Genes. Genes are joined into a string
to form a Chromosome (solution).
In a genetic algorithm, the set of genes of an individual
is represented using a string, in terms of an alphabet.
Usually, binary values are used (string of 1s and 0s). We
say that we encode the genes in a chromosome.
Basic Terminology
Fitness Function

The fitness function determines how fit an individual is (the ability of an


individual to compete with other individuals). It gives a fitness score to each
individual. The probability that an individual will be selected for
reproduction is based on its fitness score.

Selection

The idea of selection phase is to select the fittest individuals


and let them pass their genes to the next generation.

Two pairs of individuals (parents) are selected based on


their fitness scores. Individuals with high fitness have more
chance to be selected for reproduction.
Basic Terminology
Crossover

Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated,
a crossover point is chosen at random from within the genes.
For example, consider the crossover point to be 3 as shown Single Point Crossover: A point on both
below.
parents’ chromosomes is picked randomly
Offspring are created by exchanging the and designated a ‘crossover point’. Bits to
genes of parents among themselves until the right of that point are exchanged
the crossover point is reached.
between the two parent chromosomes.
Two-Point Crossover: Two
crossover points are picked
randomly
parent from
chromosomes. the
The bits in between
the two points are swapped between the
The new offspring are added to the population. parent organisms.
Uniform Crossover: In a uniform
crossover, typically, each bit is chosen
from either parent with equal probability.
Basic Terminology
Mutation

In certain new offspring formed, some of their genes can be subjected to a


mutation with a low random probability. This implies that some of the bits
in the bit string can be flipped..

Mutation occurs to maintain diversity


within the population and prevent
premature convergence.

Termination

The algorithm terminates if the population has converged (does not


produce offspring which are significantly different from the previous
generation). Then it is said that the genetic algorithm has provided a set
of solutions to our problem.
import
java.util.Random; //Do selection
/ demo.selection();
**
*
* @author //Do crossover
Vijini demo.crossover();
*/
//Main class //Do mutation under a random probability
public class if (rn.nextInt()%7 < 5) {
SimpleDemoGA { demo.mutation();
Population population = new }
Population(); Individual fittest;
Individual //Add fittest offspring to population
secondFittest; int demo.addFittestOffspring();
generationCount = 0;
public static void main(String[] //Calculate new fitness value
args) { demo.population.calculateFitness();
Random rn = new
Random(); System.out.println("Generation: " + demo.generationCount + " Fittest: "
SimpleDemoGA demo = new + demo.population.fittest);
SimpleDemoGA(); }
//Initialize population
demo.population.initializePopulation System.out.println("\nSolution found in generation " +
(10); demo.generationCount);
//Calculate fitness of each System.out.println("Fitness:
individual "+demo.population.getFittest().fitness); System.out.print("Genes: ");
demo.population.calculateFitne for (int i = 0; i < 5; i++) {
ss();
System.out.println("Generation: " + demo.generationCount System.out.print(demo.population.getFittest().genes[i]);
+ " Fittest: }
" + demo.population.fittest);
//While population gets an individual with System.out.println("");
//Selection //Flip values at the mutation point
void selection() { if (fittest.genes[mutationPoint] == 0) {
fittest.genes[mutationPoint] = 1;
//Select the most fittest individual } else {
fittest = population.getFittest(); fittest.genes[mutationPoint] = 0;
}
//Select the second most fittest individual
secondFittest = population.getSecondFittest(); mutationPoint = rn.nextInt(population.individuals[0].geneLength);
}
if (secondFittest.genes[mutationPoint] == 0) {
//Crossover secondFittest.genes[mutationPoint] = 1;
void crossover() { } else {
Random rn = new Random(); secondFittest.genes[mutationPoint] = 0;
}
//Select a random crossover point }
int crossOverPoint = rn.nextInt(population.individuals[0].geneLength);
//Get fittest offspring
//Swap values among parents Individual getFittestOffspring() {
for (int i = 0; i < crossOverPoint; i++) { if (fittest.fitness > secondFittest.fitness) {
int temp = fittest.genes[i]; return fittest;
fittest.genes[i] = secondFittest.genes[i]; }
secondFittest.genes[i] = temp; return secondFittest;
}
}

} //Replace least fittest individual from most fittest offspring


void addFittestOffspring() {
//Mutation
void mutation() { //Update fitness values of offspring
Random rn = new Random(); fittest.calcFitness();
secondFittest.calcFitness();
//Select a random mutation point
int mutationPoint = rn.nextInt(population.individuals[0].geneLength); //Get index of least fit individual
int leastFittestIndex = population.getLeastFittestIndex();
//Replace least fittest individual from most fittest offspring //Get the fittest individual
population.individuals[leastFittestIndex] = getFittestOffspring(); public Individual getFittest() {
} int maxFit =
Integer.MIN_VALUE;
} int maxFitIndex = 0;
for (int i = 0; i < individuals.length; i++) {
if (maxFit <= individuals[i].fitness) {
//Individual class maxFit = individuals[i].fitness;
class Individual { maxFitIndex = i;
}
int fitness = 0; }
int[] genes = new int[5]; fittest = individuals[maxFitIndex].fitness;
int geneLength = 5; return individuals[maxFitIndex];
}
public Individual() { //Get the second most fittest individual
Random rn = new Random(); public Individual getSecondFittest() {
int maxFit1 = 0;
//Set genes randomly for each individual int maxFit2 = 0;
for (int i = 0; i < genes.length; i++) { for (int i = 0; i < individuals.length; i+
genes[i] = Math.abs(rn.nextInt() % 2); +) {
} if (individuals[i].fitness > individuals[maxFit1].fitness)
{ maxFit2 = maxFit1;
fitness = 0; maxFit1 = i;
} } else if (individuals[i].fitness > individuals[maxFit2].fitness)
{ maxFit2 = i;
//Calculate fitness }
public void calcFitness() { }
return individuals[maxFit2];
fitness = 0; } //Get index of least fittest individual
for (int i = 0; i < 5; i++) { public int getLeastFittestIndex() {
if (genes[i] == 1) { int minFitVal = Integer.MAX_VALUE;
++fitness; int minFitIndex = 0;
} for (int i = 0; i < individuals.length; i++)
} { if (minFitVal >= individuals[i].fitness)
} {
minFitVal = individuals[i].fitness;
} minFitIndex = i;
}
//Population class }
class Population { return minFitIndex;
}
int popSize = 10; //Calculate fitness of each individual
Individual[] individuals = new Individual[10]; public void calculateFitness() {
int fittest = 0;
for (int i = 0; i < individuals.length; i++) {
//Initialize population individuals[i].calcFitness();
public void initializePopulation(int size) }
{ for (int i = 0; i < individuals.length; i++) getFittest();
{ }
individuals[i] = new Individual();
Basic Steps in GA
Applications of GA

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy