Practical-5
1 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
AIM:- Write a program to solve 8 puzzle problem using the A*
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 AStarSearch 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.
Input :-
import time
import enum
class Action(enum.Enum):
MoveDown = 1
MoveUp = 2
MoveLeft = 3
MoveRight = 4
NoAction = 0
class Node:
def __init__(self, position: [], parent: None, action: Action.NoAction):
self.position = position
self.h = 0
self.f = 0
self.g = 0
self.depth = 0
Practical-5
2 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
self.parent = parent
self.action = action
if parent:
self.depth = self.parent.depth + 1
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]), str(self.position[3:6]),
str(self.position[6:9])]).replace("[","").replace(']','').replace(',', '').replace('0', '_')
def _h(self, goal):
return sum([1 if self.position[i] != goal.position[i] else 0 for i in range(8)])
def generatevalue(self, goal):
self.h = self._h(goal)
self.g = self.depth
self.f = self.h + self.g
def possibleMoves(self):
successors = []
i = self.position.index(0)
# move down
if i in [3, 4, 5, 6, 7, 8]:
new_value = self.position[:]
new_value[i], new_value[i - 3] = new_value[i - 3], new_value[i]
successors.append(Node(new_value, self, Action.MoveDown))
# move top
if i in [0, 1, 2, 3, 4, 5]:
new_value = self.position[:]
new_value[i], new_value[i + 3] = new_value[i + 3], new_value[i]
successors.append(Node(new_value, self, Action.MoveUp))
Practical-5
3 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
# move left
if i in [0, 1, 3, 4, 6, 7]:
new_value = self.position[:]
new_value[i], new_value[i + 1] = new_value[i + 1], new_value[i]
successors.append(Node(new_value, self, Action.MoveLeft))
# move right
if i in [1, 2, 4, 5, 7, 8]:
new_value = self.position[:]
new_value[i], new_value[i - 1] = new_value[i - 1], new_value[i]
successors.append(Node(new_value, self, Action.MoveRight))
return successors
def A_Star(initial_node, Goal_Node):
Open_list = []
Close_list = []
Open_list.append(initial_node)
while len(Open_list) > 0:
Open_list.sort()
currentNode = Open_list.pop(0)
Close_list.append(currentNode)
if currentNode == Goal_Node:
path = []
while currentNode != initial_node:
path.append(currentNode)
currentNode = currentNode.parent
return path[::-1]
all_successors = currentNode.possibleMoves()
for i in all_successors:
if i in Close_list:
continue
i.generatevalue(Goal_Node)
Practical-5
4 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
if can_add_to_open(Open_list, i):
Open_list.append(i)
def can_add_to_open(Open_list, successors):
for i in Open_list:
if i == successors:
if successors.f >= i.f:
return False
else:
Open_list.remove(i)
return True
return True
initial_node = Node([1,2,3,0,4,6,7,5,8], None, Action.NoAction)
currentNode = initial_node
Goal_Node = Node([1,2,3,4,5,6,7,8,0], None, Action.NoAction)
# initial_node = Node([3,0,7,2,8,1,6,4,5], None, Action.NoAction)
# currentNode = initial_node
# Goal_Node = Node([1,2,3,4,5,6,7,8,0], None, Action.NoAction)
# initial_node = Node([2, 8, 3, 1, 6, 4, 7, 0, 5], None, Action.NoAction)
# currentNode = initial_node
# Goal_Node = Node([1, 2, 3, 8, 0, 4, 7, 6, 5], None, Action.NoAction)
print(Goal_Node)
start_time = time.time()
path = A_Star(initial_node, Goal_Node)
End_time = time.time()
for i in path:
print(i)
Practical-5
5 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
print("Path Cost=", len(path) - 1)
print("Execution time=", (End_time - start_time) * 1000, "ms")
Output :-
Output 1:-
Output 2:
Practical-5
6 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102
Output 3 :
Practical-5
7 | P a g e
Name:- Nayan Oza
Enrollment No :- 19012011102

19012011102_Nayan Oza_Practical-5_AI.pdf

  • 1.
    Practical-5 1 | Pa g e Name:- Nayan Oza Enrollment No :- 19012011102 AIM:- Write a program to solve 8 puzzle problem using the A* 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 AStarSearch 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. Input :- import time import enum class Action(enum.Enum): MoveDown = 1 MoveUp = 2 MoveLeft = 3 MoveRight = 4 NoAction = 0 class Node: def __init__(self, position: [], parent: None, action: Action.NoAction): self.position = position self.h = 0 self.f = 0 self.g = 0 self.depth = 0
  • 2.
    Practical-5 2 | Pa g e Name:- Nayan Oza Enrollment No :- 19012011102 self.parent = parent self.action = action if parent: self.depth = self.parent.depth + 1 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]), str(self.position[3:6]), str(self.position[6:9])]).replace("[","").replace(']','').replace(',', '').replace('0', '_') def _h(self, goal): return sum([1 if self.position[i] != goal.position[i] else 0 for i in range(8)]) def generatevalue(self, goal): self.h = self._h(goal) self.g = self.depth self.f = self.h + self.g def possibleMoves(self): successors = [] i = self.position.index(0) # move down if i in [3, 4, 5, 6, 7, 8]: new_value = self.position[:] new_value[i], new_value[i - 3] = new_value[i - 3], new_value[i] successors.append(Node(new_value, self, Action.MoveDown)) # move top if i in [0, 1, 2, 3, 4, 5]: new_value = self.position[:] new_value[i], new_value[i + 3] = new_value[i + 3], new_value[i] successors.append(Node(new_value, self, Action.MoveUp))
  • 3.
    Practical-5 3 | Pa g e Name:- Nayan Oza Enrollment No :- 19012011102 # move left if i in [0, 1, 3, 4, 6, 7]: new_value = self.position[:] new_value[i], new_value[i + 1] = new_value[i + 1], new_value[i] successors.append(Node(new_value, self, Action.MoveLeft)) # move right if i in [1, 2, 4, 5, 7, 8]: new_value = self.position[:] new_value[i], new_value[i - 1] = new_value[i - 1], new_value[i] successors.append(Node(new_value, self, Action.MoveRight)) return successors def A_Star(initial_node, Goal_Node): Open_list = [] Close_list = [] Open_list.append(initial_node) while len(Open_list) > 0: Open_list.sort() currentNode = Open_list.pop(0) Close_list.append(currentNode) if currentNode == Goal_Node: path = [] while currentNode != initial_node: path.append(currentNode) currentNode = currentNode.parent return path[::-1] all_successors = currentNode.possibleMoves() for i in all_successors: if i in Close_list: continue i.generatevalue(Goal_Node)
  • 4.
    Practical-5 4 | Pa g e Name:- Nayan Oza Enrollment No :- 19012011102 if can_add_to_open(Open_list, i): Open_list.append(i) def can_add_to_open(Open_list, successors): for i in Open_list: if i == successors: if successors.f >= i.f: return False else: Open_list.remove(i) return True return True initial_node = Node([1,2,3,0,4,6,7,5,8], None, Action.NoAction) currentNode = initial_node Goal_Node = Node([1,2,3,4,5,6,7,8,0], None, Action.NoAction) # initial_node = Node([3,0,7,2,8,1,6,4,5], None, Action.NoAction) # currentNode = initial_node # Goal_Node = Node([1,2,3,4,5,6,7,8,0], None, Action.NoAction) # initial_node = Node([2, 8, 3, 1, 6, 4, 7, 0, 5], None, Action.NoAction) # currentNode = initial_node # Goal_Node = Node([1, 2, 3, 8, 0, 4, 7, 6, 5], None, Action.NoAction) print(Goal_Node) start_time = time.time() path = A_Star(initial_node, Goal_Node) End_time = time.time() for i in path: print(i)
  • 5.
    Practical-5 5 | Pa g e Name:- Nayan Oza Enrollment No :- 19012011102 print("Path Cost=", len(path) - 1) print("Execution time=", (End_time - start_time) * 1000, "ms") Output :- Output 1:- Output 2:
  • 6.
    Practical-5 6 | Pa g e Name:- Nayan Oza Enrollment No :- 19012011102 Output 3 :
  • 7.
    Practical-5 7 | Pa g e Name:- Nayan Oza Enrollment No :- 19012011102