You are on page 1of 5

COMSATS University Islamabad Lahore Campus

Assignment III – FALL 2020


Course Title: Artificial Intelligence Course Code: CSC462 Credit Hours: 3(2,1)
Course
Dr. Atifa Athar Programme Name: BS Computer Science
Instructor/s:
Semester: 7th Batch: FA17 Section: B Date: 15-12-2020
Due on 22-12-2020 Maximum Marks: 10
Name:
Registration no#
 No late submissions will be accepted.
 All assignments are required to be submitted using attached template only.

Question No. 1 Marks: 10

Solve 8-queen Problem using Genetic Algorithm.


Consider minimal conflict among queens as the Fitness
function
Solution
Introduction:
8-queen problem is a very well-known problem in chess in which only 8 queen
pieces are distributed on the board with no other pieces. All 8 queens on the board
must be in positions from where they cannot attack any other queen horizontally,
vertically or diagonally. It means that no 2 queens can be in a same row, same
column or same diameter.
Steps to solve this problem
To solve this problem, we have to follow the following 7 steps to get the possible
solution.
1. Devise a way to represent each box in the Chess-Board
In this step, you have to represent the location of each box on the Chess-
Board with a number ranging from 1-8. To store the location of the queens,
we can use an array of size 8 starting from 1. Index of the array will
represent the Column while the value at that index will represent the row.
Example:

The above state can be represented as


[3,8,4,7,1,6,2,5]
2. Generating random Chess-board sequences
In this step, you have to generate few random states

Here
S1 = [1,6,2,5,7,4,8,2]
S2 = [6,1,5,2,8,3,7,4]
S3 = [4,1,5,8,2,7,3,6]
S4 = [8,7,6,5,2,3,4,1]
3. Applying a fitness function.
In this step, you have to apply fitness function i.e. Minimal conflict between
queens, on all the random states you have generated.
Fitness of State = Number of Non-attacking pairs
States Fitness Fitness Percentage
S1 27 29%
S2 28 30%
S3 28 30%
S4 10 11%

4. Select two states as parents for mating on the basis of their


fitness
In this step, we are using Scholastic Universal Sampling (SUS) to select 2
states for mating purposes. In this method, fitness percentages are
represented in the form of a pie-charm/wheel and the wheel is turned. The
arrows are pointing towards states and those states are selected as parents for
mating purpose.

Fitness Percentage

S1 S2 S3 S4

5. Crossover of parents to meet new generations.


For example, S2 and S4 are selected as parents for mating.
S2 = [6,1,5,2,8,3,7,4]
S4 = [8,7,6,5,2,3,4,1]
Let’s take Cut-off index = 4
After Cross-over, we will get two new states S5 and S6 i.e.
S5 = [8,7,6,5,8,3,7,4]
S6 = [6,1,5,2,2,3,4,1]
6. Mutation of newly generated states
In this step, you have to change any 1 value of both states randomly to avoid
monotony and we can find solution easily. Values in red are mutated.
S5 = [8,7,6,5,8,3,7,4]
S6 = [6,1,5,2,2,3,4,1]
After Mutation,
S5 = [8,7,6,1,8,3,7,4]
S6 = [6,1,5,2,9,3,4,1]
7. Repeat until solution is reached.
You have to repeat all 7 steps until you reach the best possible solution. The
best possible solution is the one in which no queen is attacking any other
queen and the Fitness of that state is 28. Then that state or sequence of
Queens will be the best solution.
S2 and S3 are the best possible sequences of queens as no queen attack any
other queen in both states.

You might also like