0% found this document useful (0 votes)
13 views7 pages

AI (Exp 4)

Uploaded by

dev606033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

AI (Exp 4)

Uploaded by

dev606033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UG Program in Artificial Intelligence & Data Science

Experiment 4

Experiment No. - 4

Date of Performance: 05/08/24

Date of Submission: 12/08/24

Program Execution/ Viva

formation/correction/ Answer to

ethical practices Documentation Timely sample Experiment Sign with


Date
(07) (02) Submission questions Total (15)

(03) (03)

5.1 Aim:

To implement the A* search algorithm to solve the 8-puzzle problem and find the optimal solution for a
given initial state and goal state.

5.2 Course Outcome (CO):

CO 2: Apply the most suitable search strategy and represent a natural language description of statements
in logic and apply the inference rules to design problem-solving agents.

5.3 Problem Statement:

Given the 8-puzzle problem, implement the A* search algorithm to solve the puzzle. The 8-puzzle
problem consists of an initial configuration and a goal configuration. The objective is to rearrange the
tiles from the initial configuration to match the goal configuration using the fewest moves possible. Use
the Manhattan distance heuristic for this problem.

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-26


UG Program in Artificial Intelligence & Data Science

5.4 Related Theory:

The 8-Puzzle Problem is a classic AI problem that consists of a 3x3 grid with eight numbered tiles and
one empty space. The goal is to rearrange the tiles from a given start configuration to a goal
configuration by sliding the tiles into the empty space.

A Search Algorithm*:

 A* is an informed search algorithm that finds the shortest path from a start node to a goal node by
combining the advantages of both Depth-First Search (DFS) and Breadth-First Search (BFS).
 It uses a heuristic function h(n) (which estimates the cost from the current node to the goal) and a cost
function g(n) (the actual cost from the start node to the current node).
 The total cost function f(n) = g(n) + h(n) is used to prioritize nodes during the search.

Manhattan Distance Heuristic:

 The Manhattan distance heuristic calculates the sum of the vertical and horizontal distances of each tile
from its goal position. This heuristic ensures that A* searches optimally and efficiently for this type of
puzzle.

Example:

Initial Configuration:

1 2 3
4 0 6
7 5 8

(Where 0 represents the empty tile)

Goal Configuration:

1 2 3
4 5 6
7 8 0

Step-by-Step Solution using A* Algorithm:

In A*, we use the following components:

 g(n): Cost to reach the current state from the start state (number of moves so far).
 h(n): Heuristic estimate to reach the goal state. We'll use the Manhattan distance heuristic, which
calculates the sum of the distances each tile needs to move to reach its goal position.
 f(n) = g(n) + h(n): Total cost function used to prioritize nodes in the open list.

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-27


UG Program in Artificial Intelligence & Data Science

1. Initial State:
1 2 3
4 0 6
7 5 8

 g(n): 0 (Initial state, no moves yet)


 h(n): Calculate the Manhattan distance:
o Tile 1 is already in the correct position (distance = 0).
o Tile 2 is already in the correct position (distance = 0).
o Tile 3 is already in the correct position (distance = 0).
o Tile 4 is already in the correct position (distance = 0).
o Tile 5 is one position away from its goal (distance = 1).
o Tile 6 is already in the correct position (distance = 0).
o Tile 7 is already in the correct position (distance = 0).
o Tile 8 is one position away from its goal (distance = 1).
o The empty space 0 doesn't contribute to the heuristic.
o h(n) = 1 + 1 = 2
 f(n) = g(n) + h(n) = 0 + 2 = 2

2. Move Empty Tile Left:


1 0 3
4 2 6
7 5 8

 g(n): 1 (One move from the start)


 h(n):
o Tile 1 is already in the correct position (distance = 0).
o Tile 2 is one position away from its goal (distance = 1).
o Tile 3 is already in the correct position (distance = 0).
o Tile 4 is already in the correct position (distance = 0).
o Tile 5 is one position away from its goal (distance = 1).
o Tile 6 is already in the correct position (distance = 0).
o Tile 7 is already in the correct position (distance = 0).
o Tile 8 is one position away from its goal (distance = 1).
o h(n) = 1 + 1 + 1 = 3
 f(n) = g(n) + h(n) = 1 + 3 = 4

3. Move Empty Tile Down:


1 2 3
4 5 6
7 0 8

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-28


UG Program in Artificial Intelligence & Data Science

 g(n): 2 (Two moves from the start)


 h(n):
o Tile 1 is already in the correct position (distance = 0).
o Tile 2 is already in the correct position (distance = 0).
o Tile 3 is already in the correct position (distance = 0).
o Tile 4 is already in the correct position (distance = 0).
o Tile 5 is already in the correct position (distance = 0).
o Tile 6 is already in the correct position (distance = 0).
o Tile 7 is already in the correct position (distance = 0).
o Tile 8 is one position away from its goal (distance = 1).
o h(n) = 1
 f(n) = g(n) + h(n) = 2 + 1 = 3

4. Move Empty Tile Right (Goal State):


1 2 3
4 5 6
7 8 0

 g(n): 3 (Three moves from the start)


 h(n):
o All tiles are in their correct positions, so the heuristic value is 0.
o h(n) = 0
 f(n) = g(n) + h(n) = 3 + 0 = 3

Solution Summary:

The sequence of moves that solves the 8-puzzle problem is:

1. Move the empty tile left.


2. Move the empty tile down.
3. Move the empty tile right (reaches the goal state).

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-29


UG Program in Artificial Intelligence & Data Science

5.5 Program Listing and Output:

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-30


UG Program in Artificial Intelligence & Data Science

5.5 Procedure:

1. Understand the Problem:


o Analyze the initial and goal configurations of the 8-puzzle. The objective is to rearrange the tiles
by sliding them into the empty space (denoted as 0).
2. Implement the Manhattan Distance Heuristic:
o Write a function to calculate the Manhattan distance of each tile from its goal position.
3. Define Neighbor States:
o Implement a function that generates all possible neighbor states by sliding tiles into the empty
space.
4. Implement the A Search Algorithm*:
o Use a priority queue (heap) to store nodes with their total cost f(n) = g(n) + h(n) and
perform the search.
5. Run the Program:
o Test the program with the given initial and goal configurations and observe the solution path.
6. Analyze the Output:
o Verify that the program produces the correct sequence of moves leading from the initial state to
the goal state.

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-31


UG Program in Artificial Intelligence & Data Science

5.6 Conclusion:

The implementation of the A* search algorithm to solve the 8-puzzle problem demonstrates an effective
approach to finding optimal solutions for rearranging tiles from an initial configuration to a specified goal
configuration. By utilizing the Manhattan distance heuristic, which quantifies the distance each tile is from its
target position, the algorithm efficiently guides the search process.
A* combines the advantages of both breadth-first and depth-first search methods, ensuring that it explores paths
in a way that minimizes total movement cost while also considering heuristic estimates. This results in a
systematic exploration of possible states, ultimately leading to the discovery of the optimal path with the fewest
moves.
The A* algorithm's reliance on admissible heuristics like Manhattan distance ensures that it never overestimates
the cost to reach the goal, making it both optimal and complete for solving the 8-puzzle problem. This
characteristic is crucial in AI applications where finding the most efficient solution is paramount.

5.7 Questions:

1. What is the 8-puzzle problem, and how does it represent a classic search problem?
2. Explain the A* search algorithm. Why is it considered optimal?
3. How is the Manhattan distance heuristic used in the A* search algorithm?
4. What are the advantages of using A* over other search algorithms like BFS and DFS for this problem?
5. Implement the 8-puzzle problem with a different heuristic (e.g., misplaced tiles) and compare the
performance with A* using the Manhattan distance heuristic.
6. How does the choice of heuristic affect the efficiency of the A* search?
7. Write a PROLOG program to solve the 8-puzzle problem using depth-first search (DFS).
8. What are the limitations of A* in solving larger versions of the 8-puzzle, such as the 15-puzzle?
9. Why is A* both complete and optimal for solving this problem?
10. How would the solution change if the puzzle size was increased from 8 tiles to 15 tiles?

Artificial Intelligence Lab (ADLR0503) A.Y. 2024-25 1-32

You might also like