AI ASSIGNMENT
NAME – JAY BHARUKA RG.NO. RA2311028010112
SECTION – V1
TOPIC -Puzzle Problem using A* Algorithm
Introduction
The 8-puzzle problem is a classic artificial intelligence toy problem. It consists of a 3x3 grid
with 8 numbered tiles and one empty space. The objective is to rearrange the tiles from an
initial configuration to a predefined goal state by sliding tiles into the empty space. This
problem can be efficiently solved using the A* search algorithm, which utilizes heuristics to
find the shortest path to the goal.
Problem Statement
The 8-puzzle consists of a 3x3 grid with tiles numbered from 1 to 8 and one blank space.
The player can slide an adjacent tile into the blank space to rearrange the tiles. The goal is to
transform an initial state into the following goal state:
123
456
78
Algorithm
The A* search algorithm is used to solve this problem. It combines the cost to reach the
current state (g(n)) and the estimated cost to reach the goal (h(n)). We use the Manhattan
Distance as the heuristic, which calculates the total number of moves each tile needs to
reach its goal position.
f(n) = g(n) + h(n)
- g(n): The number of moves from the initial state to the current state.
- h(n): The Manhattan Distance from the current state to the goal state.
Implementation
The following Python code implements the 8-puzzle problem using the A* search algorithm:
import heapq
class PuzzleState:
def __init__(self, board, g, parent):
self.board = board
self.g = g
self.h = self.calculate_heuristic()
self.parent = parent
def calculate_heuristic(self):
goal = [(1, 2, 3), (4, 5, 6), (7, 8, 0)]
distance = 0
for i in range(3):
for j in range(3):
value = self.board[i][j]
if value != 0: # Ignore the blank space
goal_x, goal_y = divmod(value-1, 3)
distance += abs(goal_x - i) + abs(goal_y - j)
return distance
def get_possible_moves(self):
moves = []
x, y = [(i, j) for i in range(3) for j in range(3) if self.board[i][j] == 0][0]
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for dx, dy in directions:
new_x, new_y = x + dx, y + dy
if 0 <= new_x < 3 and 0 <= new_y < 3:
new_board = [list(row) for row in self.board]
new_board[x][y], new_board[new_x][new_y] = new_board[new_x][new_y],
new_board[x][y]
moves.append(PuzzleState(new_board, self.g + 1, self))
return moves
def __lt__(self, other):
return (self.g + self.h) < (other.g + other.h)
def solve_puzzle(start_board):
start_state = PuzzleState(start_board, 0, None)
open_set = []
heapq.heappush(open_set, start_state)
visited = set()
while open_set:
current_state = heapq.heappop(open_set)
visited.add(current_state)
if current_state.h == 0: # Goal state reached
return current_state
for move in current_state.get_possible_moves():
if move not in visited:
heapq.heappush(open_set, move)
return None
initial_board = [
[1, 2, 3],
[4, 0, 6],
[7, 5, 8]
]
solution = solve_puzzle(initial_board)
Output
For the initial state:
123
4 6
758
The output shows the steps to reach the goal state:
123
456
78
Conclusion
The 8-puzzle problem was solved using the A* search algorithm with Manhattan Distance as
the heuristic. This approach efficiently finds the optimal solution by balancing the cost to
reach the current state and the estimated cost to reach the goal.