SlideShare a Scribd company logo
Practical-4
1 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
AIM: Write a program to solve 8 puzzle problem using the Best First
search algorithm and also find Execution time, completeness of
algorithm, etc.
Consider following steps to create a program in python:
1. Create Enum named “Action” for this problem
2. Create Node class with support of compare node & sort node
3. Choose appropriate heuristic function to solve this problem and create
in Node
class
4. Create BestFirstSearch class with “execution” method
5. Output should be according to given image
6. Print execution time & number of steps needed to reach goal state
7. Don’t use any libraries or packages of python
8. Test Program according to given below test cases
Output 1:
Output 2:
Practical-4
2 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
Output 3 :
Input :-
import time
import enum
import queue
class Action(enum.Enum):
MoveDown = 1
MoveUp = 2
MoveLeft = 3
MoveRight = 4
NoAction = 5
class Node:
def __init__(self, position: [], parent=None,
action=Action.NoAction):
self.position = position
self.action = action
self.parent = parent
self.f = 0
self.h = 0
def __eq__(self, other):
return self.position == other.position
def lt(self, other):
return self.f < other.f
def __repr__(self):
return 'n'.join([str(self.action),
str(self.position[:3]),
Practical-4
3 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
str(self.position[3:6]),
str(self.position[6:])]).replace('[',
'').replace(']', '').replace('.', '').replace('0', '_')
def __h__(self, goal):
return sum([1 if self.position[i] != goal[i] else 0 for i in
range(9)])
def generateHeuristicValue(self, goal):
self.h = self.__h__(goal)
self.f = self.h
def possible_moves(self):
successors = []
i = self.position.index(0)
if i in [0, 1, 2, 3, 4, 5]:
new_board = self.position[:]
new_board[i], new_board[i + 3] = new_board[i + 3],
new_board[i]
successors.append(Node(new_board, self,
Action.MoveDown))
if i in [0, 1, 3, 4, 6, 7]:
new_board = self.position[:]
new_board[i], new_board[i + 1] = new_board[i + 1],
new_board[i]
successors.append(Node(new_board, self,
Action.MoveRight))
if i in [1, 2, 4, 5, 7, 8]:
new_board = self.position[:]
new_board[i], new_board[i - 1] = new_board[i - 1],
new_board[i]
successors.append(Node(new_board, self,
Action.MoveLeft))
if i in [3, 4, 5, 6, 7, 8]:
new_board = self.position[:]
new_board[i], new_board[i - 3] = new_board[i - 3],
new_board[i]
successors.append(Node(new_board, self, Action.MoveUp))
return successors
def best_first_search(initialValues, goalValues):
Open = []
closed = []
initial_node = Node(initialValues, None)
goal_node = Node(goalValues, None)
Practical-4
4 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
Open.append(initial_node)
while len(Open) > 0:
Open.sort(key=lambda x: x.h)
current_node = Open.pop(0)
closed.append(current_node)
if current_node == goal_node:
path = []
temp = current_node
while temp:
path.append(temp)
temp = temp.parent
return path[::-1]
allSuccessors = current_node.possible_moves()
for successor in allSuccessors:
if successor in closed:
continue
successor.generateHeuristicValue(goalValues)
if can_add_to_open(Open, successor) == True:
Open.append(successor)
return None
def can_add_to_open(Open, successor):
for node in Open:
if successor == node and successor.f >= node.f:
return False
return True
def main():
initial = [1, 2, 3, 0, 4, 6, 7, 5, 8]
# initial = [2, 8, 3, 1, 6, 4, 7, 0, 5]
# initial = [3, 0, 7, 2, 8, 1, 6, 4, 5]
goal = [1, 2, 3, 4, 5, 6, 7, 8, 0]
# goal = [1, 2, 3, 8, 0, 4, 7, 6, 5]
# goal = [1, 2, 3, 4, 5, 6, 7, 8, 0]
startTime = time.time()
path = best_first_search(initial, goal)
endTime = time.time()
diffBFSTime = endTime-startTime
print("")
for m in path:
print(m)
print()
print("nSteps to goal: {0}".format(len(path) - 1))
print("Execution Time= "+str(diffBFSTime*1000)+"ms")
print()
Practical-4
5 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
if __name__ == "__main__":
main()
Output :-
Practical-4
6 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
Practical-4
7 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102

More Related Content

Similar to 19012011102_Nayan Oza_Practical-4_AI.pdf

Insertion sort
Insertion sortInsertion sort
Insertion sort
Abdelrahman Saleh
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
Vijaykota11
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
Mahyuddin8
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
mareeswari15
 
Functional Stream Processing with Scalaz-Stream
Functional Stream Processing with Scalaz-StreamFunctional Stream Processing with Scalaz-Stream
Functional Stream Processing with Scalaz-Stream
Adil Akhter
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
Intro C# Book
 
Write the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdfWrite the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdf
arihanthtoysandgifts
 
Dreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and TricksDreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and Tricks
Daniel Ballinger
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
Khirulnizam Abd Rahman
 
08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx
cursdjango
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Paulo Sergio Lemes Queiroz
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Soumen Santra
 
The Final Programming Project
The Final Programming ProjectThe Final Programming Project
The Final Programming Project
Sage Jacobs
 
Please help me get this Mind - tap homework to validateProgramming.pdf
Please help me get this Mind - tap homework to validateProgramming.pdfPlease help me get this Mind - tap homework to validateProgramming.pdf
Please help me get this Mind - tap homework to validateProgramming.pdf
amarrex323
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
Twig tips and tricks
Twig tips and tricksTwig tips and tricks
Twig tips and tricks
Javier Eguiluz
 
Python presentation
Python presentationPython presentation
Python presentation
Julia437584
 
Recursion.ppt
Recursion.pptRecursion.ppt
Recursion.ppt
ssuser53af97
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
IHTMINSTITUTE
 

Similar to 19012011102_Nayan Oza_Practical-4_AI.pdf (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
Functional Stream Processing with Scalaz-Stream
Functional Stream Processing with Scalaz-StreamFunctional Stream Processing with Scalaz-Stream
Functional Stream Processing with Scalaz-Stream
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Write the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdfWrite the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdf
 
Dreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and TricksDreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and Tricks
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
 
08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPU
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
The Final Programming Project
The Final Programming ProjectThe Final Programming Project
The Final Programming Project
 
Please help me get this Mind - tap homework to validateProgramming.pdf
Please help me get this Mind - tap homework to validateProgramming.pdfPlease help me get this Mind - tap homework to validateProgramming.pdf
Please help me get this Mind - tap homework to validateProgramming.pdf
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Twig tips and tricks
Twig tips and tricksTwig tips and tricks
Twig tips and tricks
 
Python presentation
Python presentationPython presentation
Python presentation
 
Recursion.ppt
Recursion.pptRecursion.ppt
Recursion.ppt
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
 

Recently uploaded

IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
mahaffeycheryld
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
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
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
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
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
ycwu0509
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
Prakhyath Rai
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
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
 
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
 
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
 

Recently uploaded (20)

IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
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
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
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
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
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...
 
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...
 
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...
 

19012011102_Nayan Oza_Practical-4_AI.pdf

  • 1. Practical-4 1 | P a g e Name:- Nayan Oza Enrollment No :- 19012011102 AIM: Write a program to solve 8 puzzle problem using the Best First search algorithm and also find Execution time, completeness of algorithm, etc. Consider following steps to create a program in python: 1. Create Enum named “Action” for this problem 2. Create Node class with support of compare node & sort node 3. Choose appropriate heuristic function to solve this problem and create in Node class 4. Create BestFirstSearch class with “execution” method 5. Output should be according to given image 6. Print execution time & number of steps needed to reach goal state 7. Don’t use any libraries or packages of python 8. Test Program according to given below test cases Output 1: Output 2:
  • 2. Practical-4 2 | P a g e Name:- Nayan Oza Enrollment No :- 19012011102 Output 3 : Input :- import time import enum import queue class Action(enum.Enum): MoveDown = 1 MoveUp = 2 MoveLeft = 3 MoveRight = 4 NoAction = 5 class Node: def __init__(self, position: [], parent=None, action=Action.NoAction): self.position = position self.action = action self.parent = parent self.f = 0 self.h = 0 def __eq__(self, other): return self.position == other.position def lt(self, other): return self.f < other.f def __repr__(self): return 'n'.join([str(self.action), str(self.position[:3]),
  • 3. Practical-4 3 | P a g e Name:- Nayan Oza Enrollment No :- 19012011102 str(self.position[3:6]), str(self.position[6:])]).replace('[', '').replace(']', '').replace('.', '').replace('0', '_') def __h__(self, goal): return sum([1 if self.position[i] != goal[i] else 0 for i in range(9)]) def generateHeuristicValue(self, goal): self.h = self.__h__(goal) self.f = self.h def possible_moves(self): successors = [] i = self.position.index(0) if i in [0, 1, 2, 3, 4, 5]: new_board = self.position[:] new_board[i], new_board[i + 3] = new_board[i + 3], new_board[i] successors.append(Node(new_board, self, Action.MoveDown)) if i in [0, 1, 3, 4, 6, 7]: new_board = self.position[:] new_board[i], new_board[i + 1] = new_board[i + 1], new_board[i] successors.append(Node(new_board, self, Action.MoveRight)) if i in [1, 2, 4, 5, 7, 8]: new_board = self.position[:] new_board[i], new_board[i - 1] = new_board[i - 1], new_board[i] successors.append(Node(new_board, self, Action.MoveLeft)) if i in [3, 4, 5, 6, 7, 8]: new_board = self.position[:] new_board[i], new_board[i - 3] = new_board[i - 3], new_board[i] successors.append(Node(new_board, self, Action.MoveUp)) return successors def best_first_search(initialValues, goalValues): Open = [] closed = [] initial_node = Node(initialValues, None) goal_node = Node(goalValues, None)
  • 4. Practical-4 4 | P a g e Name:- Nayan Oza Enrollment No :- 19012011102 Open.append(initial_node) while len(Open) > 0: Open.sort(key=lambda x: x.h) current_node = Open.pop(0) closed.append(current_node) if current_node == goal_node: path = [] temp = current_node while temp: path.append(temp) temp = temp.parent return path[::-1] allSuccessors = current_node.possible_moves() for successor in allSuccessors: if successor in closed: continue successor.generateHeuristicValue(goalValues) if can_add_to_open(Open, successor) == True: Open.append(successor) return None def can_add_to_open(Open, successor): for node in Open: if successor == node and successor.f >= node.f: return False return True def main(): initial = [1, 2, 3, 0, 4, 6, 7, 5, 8] # initial = [2, 8, 3, 1, 6, 4, 7, 0, 5] # initial = [3, 0, 7, 2, 8, 1, 6, 4, 5] goal = [1, 2, 3, 4, 5, 6, 7, 8, 0] # goal = [1, 2, 3, 8, 0, 4, 7, 6, 5] # goal = [1, 2, 3, 4, 5, 6, 7, 8, 0] startTime = time.time() path = best_first_search(initial, goal) endTime = time.time() diffBFSTime = endTime-startTime print("") for m in path: print(m) print() print("nSteps to goal: {0}".format(len(path) - 1)) print("Execution Time= "+str(diffBFSTime*1000)+"ms") print()
  • 5. Practical-4 5 | P a g e Name:- Nayan Oza Enrollment No :- 19012011102 if __name__ == "__main__": main() Output :-
  • 6. Practical-4 6 | P a g e Name:- Nayan Oza Enrollment No :- 19012011102
  • 7. Practical-4 7 | P a g e Name:- Nayan Oza Enrollment No :- 19012011102