SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
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]))
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):
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):
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):
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
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 :-

Mais conteúdo relacionado

Semelhante a 19012011102_Nayan Oza_Practical-6_AI.pdf

I have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdfI have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdf
udit652068
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
arishmarketing21
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
ICADCMLTPC
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
Eelco Visser
 

Semelhante a 19012011102_Nayan Oza_Practical-6_AI.pdf (20)

design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
Implemeting queue in python.pptx
Implemeting queue in python.pptxImplemeting queue in python.pptx
Implemeting queue in python.pptx
 
I have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdfI have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdf
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdf
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
GE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_NotesGE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_Notes
 
科特林λ學
科特林λ學科特林λ學
科特林λ學
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Building l10n Payroll Structures from the Ground up
Building l10n Payroll Structures from the Ground upBuilding l10n Payroll Structures from the Ground up
Building l10n Payroll Structures from the Ground up
 
support vector regression
support vector regressionsupport vector regression
support vector regression
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
fds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdffds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdf
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contd
 
Αλγόριθμοι
Αλγόριθμοι Αλγόριθμοι
Αλγόριθμοι
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 

Último

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 

Último (20)

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 

19012011102_Nayan Oza_Practical-6_AI.pdf

  • 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 :-