0% found this document useful (0 votes)
26 views16 pages

Unit-6 Mla

MLA

Uploaded by

robo 2.0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views16 pages

Unit-6 Mla

MLA

Uploaded by

robo 2.0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

N.B.K.R.

INSTITUTE OF SCIENCE AND TECHNOLOGY::VIDYANAGAR


(AUTONOMOUS)
*****
Department of Computer Science and Engineering
III B.TECH II SEMESTER, March 2024
(R20 Regulations)

Subject: 20CS3201 - MACHINE LEARNING APPLICATIONS


UNIT-VI
Genetic Algorithms: What is Genetic Algorithm, Operators of Genetic Algorithms, How
Genetic Algorithm works with illustrative example, Flow Chart for Genetic Programming,
Implementation of Genetic Algorithm in Python Frameworks, Real Time Applications of
Genetic programming.
1. What is Genetic Algorithm

 Genetic algorithms are based on the ideas of natural selection and genetics.
 This algorithm reflects the process of natural selection where the fittest individuals are
selected for reproduction in order to produce offspring of the next generation.
 It is used to solve optimization problems in machine learning. It is one of the
important algorithms as it helps solve complex problems that would take a long time
to solve.
 Genetic Algorithms are being widely used in different real-world applications, for
example, Designing electronic circuits, code-breaking, image processing, and
artificial creativity.
 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.
 Each generation consist of a population of individuals and each individual represents
a point in search space and possible solution.
 Genetic algorithms are based on an analogy with genetic structure and behaviour of
chromosomes of the population.
Following is the foundation of GAs based on this analogy –
1. Individual in population compete for resources and mate
2. Those individuals who are successful (fittest) then mate to create more offspring than
others
3. Genes from “fittest” parent propagate throughout the generation that is sometimes
parents create offspring which is better than either parent.
4. Thus each successive generation is more suited for their environment.
What is a Genetic Algorithm?
Before understanding the Genetic algorithm, let's first understand basic terminologies to
better understand this algorithm:
o Population: Population is the subset of all possible or probable solutions, which can
solve the given problem.
o Chromosomes: A chromosome is one of the solutions in the population for the given
problem, and the collection of gene generates a chromosome.
o Gene: A chromosome is divided into a different gene, or it is an element of the
chromosome.
o Allele: Allele is the value provided to the gene within a particular chromosome.
o Fitness Function: The fitness function is used to determine the individual's fitness
level in the population. It means the ability of an individual to compete with other
individuals. In every iteration, individuals are evaluated based on their fitness
function.
o Genetic Operators: In a genetic algorithm, the best individual mate to regenerate
offspring better than parents. Here genetic operators play a role in changing the
genetic composition of the next generation.
o Selection
Search space
 The population of individuals is maintained within search space.
 Each individual represents a solution in search space for given problem.
 Each individual is coded as a finite length vector (analogous to chromosome) of
components. These variable components are analogous to Genes.
 Thus a chromosome (individual) is composed of several genes (variable
components).

Fitness Score
 A Fitness Score is given to each individual which shows the ability of an individual
to “compete”.
 The individual having optimal fitness score (or near optimal) are sought.
 The GAs maintains the population of n individuals (chromosome/solutions) along
with their fitness scores.
 The individuals having better fitness scores are given more chance to reproduce than
others. The individuals with better fitness scores are selected who mate and
produce better offspring by combining chromosomes of parents.
 The population size is static so the room has to be created for new arrivals.
 So, some individuals die and get replaced by new arrivals eventually creating new
generation when all the mating opportunity of the old population is exhausted.
 It is hoped that over successive generations better solutions will arrive while least fit
die.
 Each new generation has on average more “better genes” than the individual
(solution) of previous generations.
 Thus, each new generation have better “partial solutions” than previous generations.
 Once the offspring produced having no significant difference from offspring produced
by previous populations, the population is converged.
 The algorithm is said to be converted to a set of solutions for the problem.
2. Operators of Genetic Algorithms
Once the initial generation is created, the algorithm evolves the generation using following
operators:
1. Selection (Reproduction)-
The idea is to give preference to the individuals with good fitness scores and allow them to
pass their genes to successive generations.
 It is the first operator applied on the population.
 It selects the chromosomes from the population of parents to cross over and produce
offspring.
 It is based on evolution theory of “Survival of the fittest” given by Darwin.
There are many techniques for reproduction or selection operator such as-
 Tournament selection
 Ranked position selection
 Steady state selection etc.
2. Cross Over
This represents mating between individuals. Two individuals are selected using selection
operator and crossover sites are chosen randomly. Then the genes at these crossover sites are
exchanged thus creating a completely new individual (offspring). For example –
 Population gets enriched with better individuals after reproduction phase.
 Then crossover operator is applied to the mating pool to create better strings.
 Crossover operator makes clones of good strings but does not create new ones.
 By recombining good individuals, the process is likely to create even better
individuals.

Chromosome 1 11011 | 00100110110


Chromosome 2 11011 | 11000011110
Offspring 1 11011 | 11000011110
Offspring 2 11011 | 00100110110
There are other ways how to make crossover, for example we can choose more crossover
points. Crossover can be rather complicated and very depends on encoding of the encoding of
chromosome. Specific crossover made for a specific problem can improve performance of the
genetic algorithm.
3. Mutation-
The key idea is to insert random genes in offspring to maintain the diversity in the population
to avoid premature convergence. For example:
 Mutation is a background operator.
 Mutation of a bit includes flipping it by changing 0 to 1 and vice-versa.
 After crossover, the mutation operator subjects the strings to mutation.
 It facilitates a sudden change in a gene within a chromosome.
 Thus, it allows the algorithm to see for the solution far away from the current ones.
 It guarantees that the search algorithm is not trapped on a local optimum.
 Its purpose is to prevent premature convergence and maintain diversity within the
population.

The whole algorithm can be summarized as –


1) Randomly initialize population’s p
2) Determine fitness of population 3) Until convergence repeat:
a) Select parents from population b) Crossover and generate new population
c) Perform mutation on new population d) Calculate fitness for new population
According to text book:
3.How Genetic Algorithm works with illustrative example

The genetic algorithm works on the evolutionary generational cycle to generate high-quality
solutions. These algorithms use different operations that either enhance or replace the
population to give an improved fit solution.
It basically involves five phases to solve the complex optimization problems, which are given
as below:
o Initialization
o Fitness Assignment
o Selection
o Reproduction
o Termination

1. Initialization
The process of a genetic algorithm starts by generating the set of individuals, which is called
population. Here each individual is the solution for the given problem. An individual contains
or is characterized by a set of parameters called Genes. Genes are combined into a string and
generate chromosomes, which is the solution to the problem. One of the most popular
techniques for initialization is the use of random binary strings.

2. Fitness Assignment
Fitness function is used to determine how fit an individual is? It means the ability of an
individual to compete with other individuals. In every iteration, individuals are evaluated
based on their fitness function. The fitness function provides a fitness score to each
individual. This score further determines the probability of being selected for reproduction.
The high the fitness score, the more chances of getting selected for reproduction.
3. Selection
The selection phase involves the selection of individuals for the reproduction of offspring. All
the selected individuals are then arranged in a pair of two to increase reproduction. Then
these individuals transfer their genes to the next generation.
There are three types of Selection methods available, which are:
o Roulette wheel selection
o Tournament selection
o Rank-based selection
4. Reproduction
After the selection process, the creation of a child occurs in the reproduction step. In this step,
the genetic algorithm uses two variation operators that are applied to the parent population.
The two operators involved in the reproduction phase are given below:
Crossover: The crossover plays a most significant role in the reproduction phase of the
genetic algorithm. In this process, a crossover point is selected at random within the genes.
Then the crossover operator swaps genetic information of two parents from the current
generation to produce a new individual representing the offspring.

The genes of parents are exchanged among themselves until the crossover point is met. These
newly generated offspring are added to the population. This process is also called or
crossover. Types of crossover styles available:
o One point crossover
o Two-point crossover
o Livery crossover
o Inheritable Algorithms crossover
Mutation
The mutation operator inserts random genes in the offspring (new child) to maintain the
diversity in the population. It can be done by flipping some bits in the chromosomes.
Mutation helps in solving the issue of premature convergence and enhances diversification.
The below image shows the mutation process:
Types of mutation styles available,
o Flip bit mutation
o Gaussian mutation
o Exchange/Swap mutation

5. Termination
After the reproduction phase, a stopping criterion is applied as a base for termination. The
algorithm terminates after the threshold fitness solution is reached. It will identify the final
solution as the best solution in the population.

Advantages of Genetic Algorithm


o The parallel capabilities of genetic algorithms are best.
o It helps in optimizing various problems such as discrete functions, multi-objective
problems, and continuous functions.
o It provides a solution for a problem that improves over time.
o A genetic algorithm does not need derivative information.
o Genetic algorithms provide solutions or answers that improve over a period.
o It comes as the best choice for a wide variety of optimization problems.
o It has a large and broad space for solutions search ability.
Limitations of Genetic Algorithms
 Genetic algorithms are not efficient algorithms for solving simple problems.
 It does not guarantee the quality of the final solution to a problem.
 Repetitive calculation of fitness values may generate some computational challenges.
According to Text book:
9.3 AN ILLUSTRATIVE EXAMPLE
The algorithm used by GABIL is exactly the algorithm described in Table 9.1.
The specific instantiation of the GA algorithm in GABIL can be summarized as follows:
Representation: Each hypothesis in GABIL corresponds to a disjunctive set of propositional
rules, encoded. In particular, the hypothesis space of rule preconditions consists of a
conjunction of constraints on a fixed set of attributes, as described in that earlier section. To
represent a set of rules, the bit-string representations of individual rules are concatenated. To
illustrate, consider a hypothesis space in which rule preconditions are conjunctions of
constraints over two Boolean attributes, a1 and a2. The rule postcondition is described by a
single bit that indicates the predicted value of the target attribute c. Thus, the hypothesis
consisting of the two rules.

Genetic operators: GABIL uses the standard mutation operator of Table 9.2, in which a
single bit is chosen at random and replaced by its complement. The crossover operator that it
uses is a fairly standard extension to the two-point crossover operator described in Table 9.2.
In particular, to accommodate the variable-length bit strings that encode rule sets, and to
constrain the system so that crossover occurs only between like sections of the bit strings that
encode rules, the following approach is taken. To perform a crossover operation on two
parents, two crossover points are first chosen at random in the first parent string. Let dl (dz)
denotes the distance from the leftmost (rightmost) of these two crossover points to the rule
boundary immediately to its left. The crossover points in the second parent are now randomly
chosen, subject to the constraint that they must have the same dl and d2 value. For example,
if the two parent strings are
Fitness function: The fitness of each hypothesized rule set is based on its classification
accuracy over the training data. In particular, the function used to measure fitness is

4. Flow Chart for Genetic Programming


According to Text Book:

5. Implementation of Genetic Algorithm in Python Frameworks


...
# initial population of random bitstring
pop = [randint(0, 2, n_bits).tolist() for _ in range(n_pop)]
...
# enumerate generations
for gen in range(n_iter):
...
...
# evaluate all candidates in the population
scores = [objective(c) for c in pop]
# tournament selection
def selection(pop, scores, k=3):
# first random selection
selection_ix = randint(len(pop))
for ix in randint(0, len(pop), k-1):
# check if better (e.g. perform a tournament)
if scores[ix] < scores[selection_ix]:
selection_ix = ix
return pop[selection_ix]
...
# select parents
selected = [selection(pop, scores) for _ in range(n_pop)]
# crossover two parents to create two children
def crossover(p1, p2, r_cross):
# children are copies of parents by default
c1, c2 = p1.copy(), p2.copy()
# check for recombination
if rand() < r_cross:
# select crossover point that is not on the end of the string
pt = randint(1, len(p1)-2)
# perform crossover
c1 = p1[:pt] + p2[pt:]
c2 = p2[:pt] + p1[pt:]
return [c1, c2]
# mutation operator
def mutation(bitstring, r_mut):
for i in range(len(bitstring)):
# check for a mutation
if rand() < r_mut:
# flip the bit
bitstring[i] = 1 - bitstring[i]
...
# create the next generation
children = list()
for i in range(0, n_pop, 2):
# get selected parents in pairs
p1, p2 = selected[i], selected[i+1]
# crossover and mutation
for c in crossover(p1, p2, r_cross):
# mutation
mutation(c, r_mut)
# store for next generation
children.append(c)
# genetic algorithm
def genetic_algorithm(objective, n_bits, n_iter, n_pop, r_cross, r_mut):
# initial population of random bitstring
pop = [randint(0, 2, n_bits).tolist() for _ in range(n_pop)]
# keep track of best solution
best, best_eval = 0, objective(pop[0])
# enumerate generations
for gen in range(n_iter):
# evaluate all candidates in the population
scores = [objective(c) for c in pop]
# check for new best solution
for i in range(n_pop):
if scores[i] < best_eval:
best, best_eval = pop[i], scores[i]
print(">%d, new best f(%s) = %.3f" % (gen, pop[i], scores[i]))
# select parents
selected = [selection(pop, scores) for _ in range(n_pop)]
# create the next generation
children = list()
for i in range(0, n_pop, 2):
# get selected parents in pairs
p1, p2 = selected[i], selected[i+1]
# crossover and mutation
for c in crossover(p1, p2, r_cross):
# mutation
mutation(c, r_mut)
# store for next generation
children.append(c)
# replace population
pop = children
return [best, best_eval]

6. Real Time Applications of Genetic programming

Genetic algorithms find use in various real-world applications. In this segment, we have
elaborated on some ‌areas that utilize the genetic algorithms in machine learning.
1. Neural networks
Genetic programming in machine learning finds great applications for neural networks in
machine learning. We use it for ‌genetic optimization in neural networks or use cases like
inheriting qualities of neurons, neural network pipeline optimization, finding the best fit set
of parameters for a given neural network, and others.
2. Data mining and clustering
Data mining and clustering use genetic algorithms to find out the centre point of the clusters
with an optimal error rate given to its great searching capability for an optimal value. It is
renowned as an unsupervised learning process in machine learning, where we categorize the
data based on the characteristics of the data points.
3. Image processing
Image processing tasks, such as image segmentation, are one of the major use cases of
genetic optimization. However, genetic algorithms can also be used in different areas of
image analysis to resolve complex optimization problems.
4. Wireless sensor network
A wireless sensor network refers to one which includes dedicated and especially dispersed
centers that maintain the record of the physical conditions of an environment. It further
passes the record created to a central storage system.
WSN utilizes genetic machine learning to stimulate the sensors. We can optimize and even
customize all the operational stages with the help of the fitness function from genetic
algorithms in wireless sensor networks.
5. Traveling salesman problem (TSP)
TSP or traveling salesman problem is one of the real-life combinatorial optimization
problems that were solved using genetic optimization. It helps in finding an optimal way in a
given map with the distance between two points and with the routes to be covered by the
salesman.
Several iterations take place that generate offspring solutions after every iteration to inherit
the qualities of parent solutions. In this way we don’t get the solution only one time that
offers you the opportunity to choose the best route structure. It finds application in real-time
processes like planning, manufacturing and logistics.
6. Vehicle routing problems
One of the generalizations of the travelling salesman problem discussed above is the vehicle
routing problem. Genetic algorithms help in finding the optimal weight of goods to be
delivered through the optimal set of delivery routes. Factors such as depot points, wait,
distance are taken into consideration while solving such problems if they have any kind of
restrictions. Moreover, the genetic algorithm approach is competitive in terms of solution
quality and time with simulated annealing algorithms and tabu search.
7. Mechanical engineering design
Genetic algorithms find application in many designing procedures of mechanical
components. For instance, consider the following genetic algorithm example where the
aircraft wing design is a kind of designing problem that takes multiple disciplines into
consideration. It requires improvement in the ratio of left to drag for a complex wing.
The fitness function in genetic optimization is flexible to considerations that come as a
demand for a particular design.
8. Manufacturing system
The manufacturing arena includes various examples of the cost function. Based on the same,
the need to find an optimal set of parameters for such functions poses a problem.
Genetic optimization performs this task of using the optimized set of parameters to minimize
the cost function. It also finds application in product manufacturing to achieve optimum
production plans by considering dynamic conditions like capacity, inventories, or material
quality.
9. Financial markets
A variety of issues can be solved using genetic optimization in the financial market. It helps
in finding an optimal combination of parameters that can affect the trades or market rules.
You can also find out the near-optimal value for the optimal set of parameters.
10. Medical science
Medical signs have an array of use cases for genetic optimization. The areas of predictive
analysis include protein prediction, RNA structure prediction, operon prediction, and others.
Other processes such as protein folding, gene expression profiling analysis, bioinformatics
multiple sequence alignment, or some of the process alignment that uses genetic optimisation.
11. Task scheduling
Genetic machine learning algorithms are used to derive optimal schedules that satisfy certain
constraints related to a problem.
For instance: assume that you have to prepare the schedule for the semester exams for
university. The genetic algorithm will find the best optimal schedule for the university
considering all the constraints like the number of classrooms, the number of students, the
total number of courses and subjects, and others.
12. Economics
Economics is the study of resource utilization in the production, distribution, and complete
consumption of goods and services over a time period. The genetic algorithm creates models
of demand and supply that derive asset pricing, game theory, and others.
13. Robotics
Robotics comprises the construction, design, and working of the autonomous robot. Genetic
algorithms contribute to the robotics field by providing the necessary insight into the
decisions made by the robot. It generates optimal routes for the robot so that it can use the
least amount of resources to get to the desired position.

Prepared by Mrs.C.Sonika

You might also like