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

AI Assiggnment Afrar

Uploaded by

mohamedshazlan6
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 views9 pages

AI Assiggnment Afrar

Uploaded by

mohamedshazlan6
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/ 9

Artificial Intelligence

Acknowledgment

I would like to express my sincere gratitude to my professor, Mr. Pushpika Prasad for his
guidance and support throughout this assignment. He provided me with valuable feedback and
suggestions that helped me improve my work and learn new skills.
Contents

Acknowledgment ............................................................................................................................ 2
1. Correct initial state................................................................................................................... 4
2. Python code ............................................................................................................................. 4
3. Executing the python code ...................................................................................................... 7
4. Final result ............................................................................................................................... 8
5. explanation of the chosen heuristic function ........................................................................... 9
1. Correct initial state
In this assignment, the initial state is not working properly; it is looping. Therefore, I
edited the code as shown below.

1 2 3

8 0 4

5 6 7

2. Python code

# Import heapq to use a priority queue


import heapq

# Define the start state and the goal state


start_state = [1, 2, 3, 8, 0, 4, 5, 6, 7]
goal_state = [1, 2, 3, 4, 5, 6, 7, 8, 0]

# Define the possible actions and the transition function


actions = ["up", "down", "left", "right"]

def transition(state, action):


# Find the index of the empty space
i = state.index(0)
# Copy the state as a list
new_state = state[:]
# Swap the empty space with the adjacent tile according to the action
if action == "up" and i > 2:
new_state[i], new_state[i-3] = new_state[i-3], new_state[i]
elif action == "down" and i < 6:
new_state[i], new_state[i+3] = new_state[i+3], new_state[i]
elif action == "left" and i % 3 > 0:
new_state[i], new_state[i-1] = new_state[i-1], new_state[i]
elif action == "right" and i % 3 < 2:
new_state[i], new_state[i+1] = new_state[i+1], new_state[i]
# Return the new state
return new_state
# Define the path cost function
def path_cost(path):
# The cost is the length of the path
return len(path)

# Define the heuristic function (Manhattan distance)


def heuristic(state):
# Initialize the distance to zero
distance = 0
# Loop through each tile in the state
for i in range(9):
# If the tile is not the empty space
if state[i] != 0:
# Find the row and column of the tile in the state
row = i // 3
col = i % 3
# Find the row and column of the tile in the goal state
goal_row = (state[i] - 1) // 3
goal_col = (state[i] - 1) % 3
# Add the absolute difference of the rows and columns to the distance
distance += abs(row - goal_row) + abs(col - goal_col)
# Return the distance
return distance

# Define the A* algorithm


def astar(start, goal):
# Initialize the frontier as a priority queue
frontier = []
# Add the start state to the frontier with zero cost and heuristic
heapq.heappush(frontier, (0 + 0, [start]))
# Initialize the explored set as an empty set
explored = set()
# Loop until the frontier is empty
while frontier:
# Pop the state with the lowest priority from the frontier
priority, path = heapq.heappop(frontier)
# Get the last state in the path
state = path[-1]
# If the state is the goal state
if state == goal:
# Return the path as the solution
return path
# If the state is not in the explored set
if tuple(state) not in explored:
# Add the state to the explored set
explored.add(tuple(state))
# Loop through the possible actions
for action in actions:
# Get the next state by applying the action
next_state = transition(state, action)
# If the next state is not in the explored set
if tuple(next_state) not in explored:
# Get the cost of the next state
cost = path_cost(path + [next_state])
# Get the heuristic of the next state
h = heuristic(next_state)
# Add the next state to the frontier with the cost and heuristic as priority
heapq.heappush(frontier, (cost + h, path + [next_state]))
# Return None if no solution is found
return None

# Test the algorithm


solution = astar(start_state, goal_state)
if solution:
print("Solution found!")
for state in solution:
print(state)
else:
print("No solution found!")
3. Executing the python code
4. Final result
5. explanation of the chosen heuristic function
For the eight-puzzle problem, the Manhattan distance has been used as the heuristic
function. The distance between two places in a grid-based system (like a chessboard)
when movement is limited to horizontal and vertical lines is known as the Manhattan
distance, often referred to as the L1 norm or taxicab distance.

By adding up the horizontal and vertical distances between each tile's current position
and its goal position, the Manhattan distance heuristic calculates the cost of moving from
the current state to the goal state in the context of the 8-puzzle.

The formula for the Manhattan distance between two points (x1, y1) and (x2, y2) is given
by:

Manhattan Distance = |x1 – x2| + |y1 – y2|

The heuristic function in the 8-puzzle adds up the Manhattan distances for every tile, with
the exception of the blank tile, which is represented by the number 0. Because it never
overestimates the actual cost to achieve the goal, this offers an accepted heuristic.
Furthermore, it is consistent, which means that the heuristic value of a node is less than or
equal to the total cost of contacting all neighbors plus their respective heuristic values.

The Manhattan distance heuristic directs the A* algorithm to investigate routes that have
a higher probability of leading to the ideal answer. Because of its effectiveness and
simplicity, it is frequently utilized in grid-based pathfinding issues.

You might also like