Professional Documents
Culture Documents
2. Overview
A Genetic Algorithm is one of many optimization algorithms. Its purpose is to guide a search process to
find a global optimal solution for a problem in a very large search space. It belongs to the class of
evolutionary algorithms and can solve very complex.
On some classes of problems a Genetic Algorithm can not guarantee to find a global optimal solution, but
it may approximate a sufficiently good solution in a relative short time.
One example is the automatic design of antennas for radio communication. There is an infinite amount of
possible shapes for antennas. An evolutionary algorithm can be used to find the (near-)optimal shape of
an antenna with a given requirement. In fact the NASA used Genetic Algorithms to evolve an antenna
design for its Space Technology 5 (ST5) mission which launched in March 2006.
When a problem can be represented as a combination (DNA) of multiple things (Genes) and a function
can be provided that calculates the quality of such combination (Fitness Function), a Genetic Algorithm
may hopefully find the best of all possible combinations - if given enough time! The algorithm may
automatically evolve better solutions from generation to generation with the help of Selection, Crossover
and Mutation.
2.1 Selection
Selection is the most important Phase of GA. All Population is evaluated on the basis of the fitness function.
New population is generated by probabilistic-ally selecting the most fit individual. Some selected individuals are
carried forward (selection).
2
Lab 10: Genetic Algorithm in Artificial Intelligence
2.2 Crossover
2.3 Mutation
2.3.1 Bit Flip Mutation
In this bit flip mutation, we select one or more random bits and flip them. This is used for binary encoded.
3
Lab 10: Genetic Algorithm in Artificial Intelligence
In swap mutation, we select two positions on the chromosome at random, and interchange the values.
This is common in permutation based encoding.
3. Concept Map
A search using domain-specific knowledge. Suppose that we have a way to estimate how close a state is
to the goal, with an evaluation function.
4
Lab 10: Genetic Algorithm in Artificial Intelligence
Tail = 1.
As we have chosen tail so our desired output is tail. Lets toss the coin ten times and we got result something like
this:
0110011010
in the above example we got Tail(1) five times. But what we want is to maximize the possibility of number of tails
(1) in sequence.
Lets apply genetic algorithm so we can increase the possibility of 1’s.
Lets toss the coin 60 times and make sets(chromosome/DNA) of 10 tosses(gene) each. Let ( l ) is the length of each
set and n is the total time toss.
i.e.
n = 60 & l = 10.
S1 = 1 1 1 1 0 1 0 1 0 1
S2 = 0 1 1 1 0 0 0 1 0 1
S3 = 1 1 1 0 1 1 0 1 0 1
S4 = 0 1 0 0 0 1 0 0 1 1
S5 = 1 1 1 0 1 1 1 1 0 1
S6 = 0 1 0 0 1 1 0 0 0 0
now Step 1 is complete (create the initial population). Now we want to maximize number of one’s.
Now lets calculate fitness(number of one’s in each set).
S1 = 1 1 1 1 0 1 0 1 0 1 f(S1) = 7
S2 = 0 1 1 1 0 0 0 1 0 1 f(S2) = 5
S3 = 1 1 1 0 1 1 0 1 0 1 f(S3) = 7
S4 = 0 1 0 0 0 1 0 0 1 1 f(S4) = 4
S5 = 1 1 1 0 1 1 1 1 0 1 f(S5) = 8
S6 = 0 1 0 0 1 1 0 0 0 0 f(S6) = 3
total fitness = 7+5+7+4+8+3 = 34.
5
Lab 10: Genetic Algorithm in Artificial Intelligence
Roulette Wheel
f(S1)
f(S2)
f(S3)
f(S4)
f(S5)
f(S6)
6
Lab 10: Genetic Algorithm in Artificial Intelligence
7
Lab 10: Genetic Algorithm in Artificial Intelligence
By a pretty good solution we simply mean that you can get reasonably good
polynomial that best fits the given data.
Your formula is y = mx + c
Your unknowns are m and c; where m and c are integers
Your representation is the array [m, c]
Your evaluation function for one array is:
For every actual data point (x, y)
Compute ý = mx + c
Find the sum of (y - ý)2 over all x
The sum is your measure of "badness" (larger numbers are worse)
Example: For [5, 7] and the data points (1, 10) and (2, 13):
ý = 5x + 7 = 12 when x is 1
ý = 5x + 7 = 17 when x is 2
(10 - 12)^2 + (13 -17)^2 = 2^2 + 4^2 = 20
If these are the only two data points, the "badness" of [5,7] is 20.
Algorithm:
Your algorithm might be as follows:
Example
Let us solve this problem in detail. Consider that the given points are as follows. (x, y) : {(1,5) (3, 9)}.
We start will the following initial population which are the arrays representing the solutions (m and c).
[2 7][1 3]
8
Lab 10: Genetic Algorithm in Artificial Intelligence
ý = 2x + 7 = 9 when x is 1
ý = 2x + 7 = 13 when x is 3
(5 -9)2 + (9 -13)2 = 42 + 42 = 32
ý = 1x + 3 = 4 when x is 1
ý = 1x + 3 = 6 when x is 3
(5 - 4)2 + (9 - 6)2 = 12 + 32 = 10
In the examples so far, each "Individual" (or "solution") had only one parent. The only way to introduce
variation was through mutation (random changes). In Crossover, each "Individual" (or "solution") has two
parents. Assuming that each organism has just one chromosome, new offspring are produced by forming
a new chromosome from parts of the chromosomes of each parent.
6. Tasks
6.1 Task 1
Implement the Walkthrough Task in Python.
6.2 Links
https://www.tutorialspoint.com/prolog_in_artificial_intelligence/index.asp
http://lpn.swi-prolog.org/lpnpage.php?pageid=online
https://medium.com/analytics-vidhya/understanding-genetic-algorithms-in-the-artificial-intelligence-spectrum-
7021b7cc25e7
https://www.codemiles.com/c-examples/genetic-algorithm-example-t7548.html