1. The document describes a program to solve the N-Queen problem using the A* search algorithm with a priority queue. It considers creating classes for queens and states, developing an appropriate heuristic function, and using random, time, and heapq libraries.
2. The key steps are to create Queen and Node classes, define comparison and sorting methods for states, generate a heuristic value using conflict counting, and perform an A* search with a priority queue to find the optimal solution.
3. The program is tested by running it on a given N value and outputting the solution board, execution time, and completeness of the algorithm.
1. Practical-6
1 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
6AIM:- Write a program to solve N-Queen problem using the A*
search
algorithm with Priority Queue and also find Execution time,
completeness
of algorithm, etc.
Consider following steps to create a program in python:
1. Create class queen and state with appropriate member variables
2. Create class state with support of compare state & sort state
3. Create appropriate heuristic function to solve this problem
4. Use random, time, heapq and matplotlib libraries of python
5. Output should be according to given image
Input :-
import random
import time
import math
n = int(input("Enter no of queen:t"))
class Queen:
def __init__(self, row = -1, column = -1):
self.row = row
self.column = column
def __eq__(self, otherQueen):
return self.__cmp__(otherQueen)
def __hash__(self):
return hash(str([self.row, self.column]))
2. Practical-6
2 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
def __cmp__(self, otherQueen):
return self.row == otherQueen.row and self.column == otherQueen.column
class Node:
def __init__(self, parent):
self.state = [Queen() for i in range(n)]
self.parent = parent
if parent:
self.moves = parent.moves + 1
self.h = parent.h
for i in range(n):
self.state[i].row = parent.state[i].row
self.state[i].column = parent.state[i].column
else:
self.moves = 0
for i in range(n):
randomRow = random.randint(0, n-1)
self.place(randomRow, i)
self.h = self.generateHeuristicValue()
def __lt__(self, otherQueen):
3. Practical-6
3 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
return (self.h + self.moves) < (otherQueen.h + otherQueen.moves)
def score(self):
return self.h + self.moves
def place(self, row, column):
if row >=n and column >=n:
return
if self.state[column].row == row and self.state[column].column == column:
return
self.state[column].row = row
self.state[column].column = column
self.h = self.generateHeuristicValue()
def __repr__(self):
returnStr = ""
for i in range(n):
for j in range(n):
if self.state[j].row == i:
returnStr = returnStr + "1 "
else:
returnStr = returnStr + "0 "
returnStr = returnStr + 'n'
returnStr = returnStr + 'n'
return returnStr
def countConflicts(self, row, column):
4. Practical-6
4 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
ct = 0
for i in range(n):
if not(self.state[i].row == row and self.state[i].column == column) and
self.state[i].row == row:
ct +=1
if not (self.state[i].row == row and self.state[i].column == column) and
self.state[i].column == column:
ct +=1
if not(self.state[i].row == row and self.state[i].column == column) and
abs(self.state[i].row - row) == abs(self.state[i].column - column):
ct +=1
return ct
def __cmp__(self, other):
if other == None:
return False
return self.state == other.state
def eq(self, other):
return self.__cmp__(other)
def __lt__(self, other):
return self.score() < other.score()
def generateHeuristicValue(self):
ct = 0
for i in range(n):
ct +=self.countConflicts(self.state[i].row, self.state[i].column)
return ct
def __hash__(self):
5. Practical-6
5 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
return hash(str(self.state))
def generateNextStates(self):
all = []
row = self.moves
for i in range(n):
if not(self.state[i].row == row and self.state[i].column == i):
nextNode = Node(self)
nextNode.place(row, i)
all.append(nextNode)
return all
def aStarAlgorithm(initStates):
openList = []
closedList = []
openList.append(initStates)
while len(openList) > 0:
openList.sort()
currNode = openList.pop(0)
closedList.append(currNode)
if currNode.h == 0:
return currNode
6. Practical-6
6 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
successors = currNode.generateNextStates()
for successor in successors:
if successor in closedList:
continue
openList.append(successor)
if __name__ == "__main__":
initialStates = []
initS = Node(None)
startTime = time.time()
result = aStarAlgorithm(initS)
endTime = time.time()
#result.drawBoard()
print(result)
print(math.trunc((endTime - startTime) * 1000), "ms")
Output :-