SlideShare a Scribd company logo
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 files
Nitesh Dubey
 
Implemeting queue in python.pptx
Implemeting queue in python.pptxImplemeting queue in python.pptx
Implemeting queue in python.pptx
KennyDon5
 
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
 
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
rajatxyz
 
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
 
GE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_NotesGE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_Notes
Asst.prof M.Gokilavani
 
科特林λ學
科特林λ學科特林λ學
科特林λ學
彥彬 洪
 
Python Cheat Sheet Presentation Learning
Python Cheat Sheet Presentation LearningPython Cheat Sheet Presentation Learning
Python Cheat Sheet Presentation Learning
Naseer-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).pdf
srxerox
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
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
Odoo
 
support vector regression
support vector regressionsupport vector regression
support vector regression
Akhilesh 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 Schlawack
Fwdays
 
fds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdffds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdf
GaneshPawar819187
 
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
AnishaJ7
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
ICADCMLTPC
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contd
raksharao
 
Αλγόριθμοι
Αλγόριθμοι Αλγόριθμοι
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
Eelco 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

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
SakkaravarthiShanmug
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
GauravCar
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
riddhimaagrawal986
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 

Recently uploaded (20)

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 

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