0% found this document useful (0 votes)
4 views17 pages

AI Lecture 8

Uploaded by

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

AI Lecture 8

Uploaded by

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

.

Artificial
Intelligence
Lecture 8
 Basic Genetic Algorithm
 Solution to a Few Problems using
GA
 Eight Queens Problem

Instrutor : Hafiz Mueez Ameen


Solution of
N Queens Problem

Using math lab


Queen moves
in Chess
Board
Problem statement:
• Place 'n' queens on a chess board in
such a way that no queen attacks any
other queen using genetic algorithm.
• Initial state: queens are randomly
placed in each column with no queen
in same row.

• The Target: Find the greatest number


of correct solutions.
We have mainly written this program for S queen problem but
we can try for other values or 'n' but recalculate the max (to
update the solution matrix) we can try using different
generations and populations. When we increased the number
of Individuals In the populations and the number of generations,
the probability or finding the largest number of correct solutions
increased.
Encoding of Chromosomes:
• Every (Chess
chromosome board) by one
is represent array
dimension of 8
elements (gene), every gene
represent the place of one
queen, the index of the
element within the array
represents the value of the
column in Chess board and
the stored value represents
the row in Chess board.
2
7
3
Evaluation function
• Fitness value for every chromosome found from checking
every queen (gene) with the next queens
• if there is no crossing compere the fitness value by one and so
on.

max_fitness = ( ( ( n- 1) * n) / 2)
=((7 * 8 )/ 2)=
28
Evaluation function
For every
two queues
does not equal the if
between thetherow difference add
differencebetween
fitness function.
the one tocolumns
the
1 2 3 4 5 6 7
1
2 1
3 1 1
4 1 1 0
5 0 1 1 1
6 1 1 1 1 0
7 0 1 1 1 0 1 2 7 3 4 6 5 8 1
8 1 0 1 1 1 1 1
Fitness= 22
5 5 4 4 1 2 1
Selection
• We sort the chromosomes in the Generation by
the fitness value .then select the half of the
generation with highest value of fitness function
and duplicate it
• neglect the half with a lowest value of fitness
function.
Cross over
One Point cross over
Ex:random cross over point=3

Crou OYer C t O U polM


point

..,.,,,,131s11l1lsl214161 ..... 1 1 1 s 7 8 2 4

-- -- --
6
3
9arf'llt2

8
4

2
6 1

7
3S P>rtntl
3
---
6 8 2 7

---
1

•• "' I1 Is I1 I3 Is I 2 1 4 1 6 1 CNlwenl I I ---


I I I
4
s 6 3
S
I I zI I
S 4

1 I
....,,,1 3 5 1 4 6 8 2 7

O.lwenl I I i 81 I
! 2 6 4
17 I I
S
Mutation
Random point Random point
1 2

Before l1lsl6131sl214171

After I1I8I2I3 I5I6 I4 I7I


No flr.tlt'Ufot M y •
Ye
m.tltlm-t.lll'llrH s
No Yes

• · +
« C o p ytoc.hromosome
solution
Sorting the generation rom the highest value
of fitnessfunction tot e Joweront

Duplicate tht' higher half of the generationand


nc lect the lowest

Cross overevery two paren·ts to random point

Mutationrandom geneIn random children's

= +1

Print solution array


En
d

Constructing
1 2 3 4 5 6 7 8
8-queens problem have
9 10 11 12 13 14 15 16
4,426,165,368 17 18 19 20 21 22 23 24

25 26 27 28 29 30 31 32
(i.e., 64C8) possible 33 34 35 36 37 38 39 40
arrangements 41 42 43 44 45 46 47 48

it is a brute-force 49 so 51 52 53 54 55 56

computational 57 58 59 60 61 62 63 64

• by applying a simple rule that constrains each queen to a


single column (or row) 16,777,216 (that is, 8 8 ) possible
combinations.
• Generating permutations further reduces the possibilities
to just 40,320 (that is, 81), which are then checked for
diagonal attacks.
Counting solutions
The eight queens has 92 solutions
If solutions that differ only by the symmetry operations of
rotation and reflection of the board are counted as one, the
puzzle has

12 solutions
These are called

fundamental solutions
representatives of each are shown below.
11x8 + 1x4 = 92

(where the 8 is derived from four 90" rotational positions andtheir reflections, and the 4 is
derived from two 1so•rotational positions and their reflections).
fundamental solutions

..
....
..... . .. . ,.

.. ·•- ..
:;.- ;- .,

...
• • t • • ' • "

11'"11

' .

.-·
••II.•■
" •■" •■ 'II ' ■■■'
;•• • ■■:■ ■II.,•
' ••
·• ■ ■ l ! I .
■II■■'
11 :• ti
.... a J 1 ,

...
._ '
.......
.,
·•

..•
·•
• II ■ •
••
. . . .. '
·. :

.
'
fundamental
1 1 1
2 0 0
3 0 0
4 1 2
5 2 10
6 1 4
7 6 40
8 12 92
9 46 352
10 92 724
........... ....................
24 28,439,272,956,934 227,514,171,973,736
25 275,986,683,743,434 2,207,893,435,808,352
26 2,789,712,466,510,289 22,317,699,616,364,044
27 29,363,495,934,315,694 234,907,967,154,122,528

You might also like