0% found this document useful (0 votes)
85 views9 pages

Nguyễn Đức Phi Hồng ITITIU 17022- Introduction to artificial intelligence Lab 3

This document discusses implementing a heuristic function to solve the 8-puzzle problem using the A* search algorithm. It provides sample code for the heuristic function that calculates a score for each node based on the positions of tiles relative to the goal state. It then applies the heuristic function to the BFS algorithm from Lab 2 to find solutions from initial to goal states. Three cases with different initial states are evaluated, comparing the path, advantages, disadvantages, complexity, and runtime of A*, BFS, and DFS searches. The heuristic A* search performs best overall, having the same complexity as BFS but running faster than both DFS and BFS for all cases while also consuming less memory than DFS.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views9 pages

Nguyễn Đức Phi Hồng ITITIU 17022- Introduction to artificial intelligence Lab 3

This document discusses implementing a heuristic function to solve the 8-puzzle problem using the A* search algorithm. It provides sample code for the heuristic function that calculates a score for each node based on the positions of tiles relative to the goal state. It then applies the heuristic function to the BFS algorithm from Lab 2 to find solutions from initial to goal states. Three cases with different initial states are evaluated, comparing the path, advantages, disadvantages, complexity, and runtime of A*, BFS, and DFS searches. The heuristic A* search performs best overall, having the same complexity as BFS but running faster than both DFS and BFS for all cases while also consuming less memory than DFS.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab 3: Heuristic function

Name: Nguyễn Đức Phi Hồng


ID: ITITIU17022
Introduction to artificial intelligent

Tutorial videos
+What is Heuristic in AI | Why we use Heuristic:
https://www.youtube.com/watch?v=5F9YzkpnaRw
+ 8 puzzle heuristic function
1) https://www.youtube.com/watch?v=_Sb2_Xlx5Bo
2) https://www.youtube.com/watch?v=_Sb2_Xlx5Bo

Sample codes
1) https://blog.goodaudience.com/solving-8-puzzle-using-a-algorithm-7b509c331288
2) Given sample code of Lab 2;

Procedures

1. Define the Heuristic function


-Algorithm
-Coding
Line 86: MaxFrontier: contain unexplore node
GoalNode: final state to reach
MaxSearchDeep: to prevent node that passes the goal state
Line 89-91: Divine initial to every node for calculate score of each node next.
Line 94-98: Save score we calculated.
Line 100-115: If this is goal state we stop, if not, we continue to move a next path that is
not in boardVisited ,calculate in’s score, and then plus g(x) to 1.
Line 119-127: Give a score for each node from 0 to 8 ( depend on their position to their
goal position)
Line 129-142: Caculate h(x) by plus all score from node 0 to 8
-Result
1 2 5
3 4 8 g=0, h=4, f= g + h=4
6 7 0

1 2 5 1 2 5
3 4 8 3 4 0
6 0 7 6 7 8

g=1, h=5, f=6 g=1, h=3, f=4


1 2 0
3 4 5
1 2 5 6 7 8
3 0 4
6 7 8
1 0 2
3 4 5
6 7 8 g=2, h=2, f=4
g=2, h=4, f=6

g=3, h=1, f=4


0 1 2
3 4 5
6 7 8

g=4, h=0, f=4 g=4, h=2, f=6


2.Apply the heuristic function to implement
the BFS (Lab 2) to find the solution the init. to
the end state.
3.Evaluation
Compare at least 3 method: heuristic (ast), bft , dfs( run time , path ,complexity ,adv,
div …) for at least 3 various cases of init.state.
BFS:

Case Path Adv Dis Complexity Runtime


#1 ['Up', 'Up', 'Left', 'Left'] Run faster O(4^4) 0.00031785
[1,2,5,3,4,8,6,7,0] than DFS
#2 'Left', 'Up', 'Up', 'Left', Consume less Run slower O(4^26) 3.32510930
[8,6,4,2,1,3,5,7,0] 'Down', 'Right', 'Down', memory than than DFS
'Left', 'Up', 'Right', 'Right', DFS
'Up', 'Left', 'Left', 'Down',
'Right', 'Right', 'Up', 'Left',
'Down', 'Down', 'Right', 'Up',
'Left', 'Up', 'Left'
#3 'Down', 'Right', 'Up', 'Up', Consume less Run slower O(4^20) 1.02962956
[6,1,8,4,0,2,7,3,5] 'Left', 'Down', 'Right', memory than
1 4 2
than DFS
'Down', 'Left', 'Up', 'Left', DFS 3 0 5
'Up', 'Right', 'Right', 'Down', 6 7 8
'Down', 'Left', 'Left', 'Up',
'Up'

DFS: Too long can’t take picture


Case Path Adv Dis Complexity Runtime
#1 'Up', 'Up', 'Left', 'Left' Run slower O(4^4) 3.24088437
[1,2,5,3,4,8,6,7,0] than BFS
#2 Very long Run faster Consume O(4^9612) 0.17663588
[8,6,4,2,1,3,5,7,0] than BFS more
memory
than BFS
#3 Very long Run faster Consume O(4^46142) 0.98581995
[6,1,8,4,0,2,7,3,5] than BFS less
memory
than BFS

AST:

Case Path Adv Dis Complexity Runtime


#1 'Up', 'Up', 'Left', 'Left' Run faster O(4^4) 0.00013471
[1,2,5,3,4,8,6,7,0] than DFS and
BFS
#2 'Left', 'Up', 'Up', 'Left', Run faster O(4^26) 0.23243244
[8,6,4,2,1,3,5,7,0] 'Down', 'Right', 'Down', than DFS and
'Left', 'Up', 'Right', 'Right', BFS
'Up', 'Left', 'Left', 'Down', Consume less
'Right', 'Right', 'Up', 'Left', memory than
'Down', 'Down', 'Right', 'Up', DFS
'Left', 'Up', 'Left'
#3 'Down', 'Right', 'Up', 'Up', Run faster O(4^20) 0.07090627
[6,1,8,4,0,2,7,3,5] 'Left', 'Down', 'Right', than DFS and
'Down', 'Left', 'Up', 'Left', BFS
'Up', 'Right', 'Right', 'Down', Consume less
'Down', 'Left', 'Left', 'Up', memory than
'Up' DFS
Conclusion: Heuristic (AST) have the same complexity as BFS but far more efficient than DFS
and BFS in any kind of case

You might also like