SlideShare a Scribd company logo
1 of 6
Download to read 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 :-

More Related Content

Similar to 19012011102_Nayan Oza_Practical-6_AI.pdf

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 filesNitesh Dubey
 
Implemeting queue in python.pptx
Implemeting queue in python.pptxImplemeting queue in python.pptx
Implemeting queue in python.pptxKennyDon5
 
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 .pdfudit652068
 
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.pdfrajatxyz
 
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.pdfarishmarketing21
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
科特林λ學
科特林λ學科特林λ學
科特林λ學彥彬 洪
 
Python Cheat Sheet Presentation Learning
Python Cheat Sheet Presentation LearningPython Cheat Sheet Presentation Learning
Python Cheat Sheet Presentation LearningNaseer-ul-Hassan Rehman
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdfsrxerox
 
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 upOdoo
 
support vector regression
support vector regressionsupport vector regression
support vector regressionAkhilesh Joshi
 
"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 SchlawackFwdays
 
fds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdffds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdfGaneshPawar819187
 
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.pptAnishaJ7
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdfICADCMLTPC
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contdraksharao
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 

Similar to 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
 
科特林λ學
科特林λ學科特林λ學
科特林λ學
 
Python Cheat Sheet Presentation Learning
Python Cheat Sheet Presentation LearningPython Cheat Sheet Presentation Learning
Python Cheat Sheet Presentation Learning
 
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
 

Recently uploaded

Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2T.D. Shashikala
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfEr.Sonali Nasikkar
 
Attraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptxAttraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptxkarthikeyanS725446
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...Roi Lipman
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDrGurudutt
 
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptxROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptxGagandeepKaur617299
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGKOUSTAV SARKAR
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...ShivamTiwari995432
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Prakhyath Rai
 
ChatGPT Prompt Engineering for project managers.pdf
ChatGPT Prompt Engineering for project managers.pdfChatGPT Prompt Engineering for project managers.pdf
ChatGPT Prompt Engineering for project managers.pdfqasastareekh
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisDr.Costas Sachpazis
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfMadan Karki
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdfKamal Acharya
 
Teachers record management system project report..pdf
Teachers record management system project report..pdfTeachers record management system project report..pdf
Teachers record management system project report..pdfKamal Acharya
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxCHAIRMAN M
 
Quiz application system project report..pdf
Quiz application system project report..pdfQuiz application system project report..pdf
Quiz application system project report..pdfKamal Acharya
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxRashidFaridChishti
 
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfBURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfKamal Acharya
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesRashidFaridChishti
 
Introduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsIntroduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsKineticEngineeringCo
 

Recently uploaded (20)

Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
Attraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptxAttraction and Repulsion type Moving Iron Instruments.pptx
Attraction and Repulsion type Moving Iron Instruments.pptx
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
 
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptxROAD CONSTRUCTION PRESENTATION.PPTX.pptx
ROAD CONSTRUCTION PRESENTATION.PPTX.pptx
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
ChatGPT Prompt Engineering for project managers.pdf
ChatGPT Prompt Engineering for project managers.pdfChatGPT Prompt Engineering for project managers.pdf
ChatGPT Prompt Engineering for project managers.pdf
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdf
 
Teachers record management system project report..pdf
Teachers record management system project report..pdfTeachers record management system project report..pdf
Teachers record management system project report..pdf
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Quiz application system project report..pdf
Quiz application system project report..pdfQuiz application system project report..pdf
Quiz application system project report..pdf
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfBURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 
Introduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsIntroduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and Applications
 

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