0% found this document useful (0 votes)
26 views

56d0f5_Informed Search Algorithms

Uploaded by

Muskan Ijaz
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

56d0f5_Informed Search Algorithms

Uploaded by

Muskan Ijaz
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Informed Search Algorithms

A* Algorithm
Introduction to Artificial Intelligence
Informed Search Algorithms
• Informed Search Algorithms use heuristic (A heuristic is an estimation
of how close a node is to the goal) knowledge to improve search
efficiency.
Unlike uninformed algorithms like BFS and DFS, informed search
algorithms prioritize nodes based on their estimated cost to the goal.
• A* Search Algorithm?
The A (A-Star) algorithm* is an informed search algorithm that is widely used
for pathfinding and graph traversal. It finds the shortest path from a starting
point to a goal.
A* Search Algorithm
• A* uses a combination of two costs:
• g(n): The cost of the path from the start node to the current node n.
• h(n): The estimated cost (heuristic) from the current node n to the goal node.
• f(n): The total cost of the node:
• 𝑓(𝑛)=𝑔(𝑛)+ℎ(𝑛)f(n)=g(n)+h(n)
How A Works: Step-by-Step
• Start from the initial node.
• Calculate the f(n) value for all neighboring nodes.
• Select the node with the lowest f(n) value and expand it.
• Repeat the process until the goal node is reached.
Real-World Scenario:
Pathfinding for a Delivery Robot
• Imagine a delivery robot moving in a warehouse grid. Obstacles (like
boxes) block some cells, and the robot uses the A* algorithm to find
the shortest path to deliver packages.
• OR Self Room cleaner Robot is the example of A* Algorithm.
A* Algorithm Example with Code
A* Algorithm Example with Code
A* Algorithm Example with Code

C:\Users\1671\
Desktop\full code.tx
Step by Step Explanation of the
code

First import the heapq library, which provides an implementation of a priority queue. This is useful for
efficiently retrieving the node with the lowest cost during the pathfinding process.

This function implements the A* algorithm. It takes three parameters:


grid: A 2D list representing the environment (0 for free space, 1 for obstacles).
start: The starting position as a tuple (x, y).
goal: The target position as a tuple (x, y).
Step by Step Explanation of the
code

Now we determine the number of rows and columns in the grid.


• First we initialize an empty list open_set to keep track of nodes to explore, starting with the start node with a priority
of 0.
• came_from is a dictionary that will help us reconstruct the path later by storing the parent of each node.
• g_score keeps track of the cost to reach each node from the start. Initially, the cost to reach the start node is 0.
• f_score is the estimated total cost from the start to the goal through the current node. It is initialized using a
heuristic function (Manhattan distance) to estimate the distance from the start to the goal.
Step by Step Explanation of the
code

The main loop continues until there are no nodes left to explore in open_set.
We pop the node with the lowest f_score from the priority queue, which is our current node.

If the current node is the goal, we call the reconstruct_path function to build the path from the start to
the goal and return it.

We retrieve all valid neighboring nodes of the current node using the get_neighbors function.
We calculate the tentative g_score for each neighbor. Here, we assume the cost to move to a neighbor is 1.
Step by Step Explanation of the
code

If this new path to the neighbor is better (shorter) than any previously recorded path, we update:
• came_from to record the current node as the parent of the neighbor.
• g_score for the neighbor with the new cost.
• f_score for the neighbor using the updated g_score and the heuristic.
We then add the neighbor to the open_set for further exploration.
If the loop ends without finding a path to the goal, we return None.
Step by Step Explanation of the
code

This function calculates the Manhattan distance, which is the sum of the absolute differences of their
coordinates. It serves as a heuristic to estimate the cost from the current node to the goal.
Step by Step Explanation of the
code

This function retrieves all valid neighboring nodes of a given node in the grid.
We initialize an empty list to store valid neighbors and unpack the current node's coordinates

We check all four possible directions (up, down, left, right) by adjusting the current coordinates.

For each new position, we check if it is within the grid bounds and if it is free (0). If valid, we add it to the neighbors list.
Step by Step Explanation of the
code
This function reconstructs the path from the start to the goal using the came_from dictionary.

We initialize the path with the goal node and backtrack through the came_from dictionary to build
the complete path.
Finally, we reverse the path to present it from the start to the goal.
Step by Step Explanation of the
code

We define a grid where 0 represents free space and 1 represents obstacles. This grid will be used for the pathfinding.

We specify the starting position at the top-left corner and the goal position at the bottom-right corner of the grid.

We call the astar function with the grid, start, and goal positions to find the shortest path.
Step by Step Explanation of the
code

Finally, we check if a path was found. If so, we print the steps in the path; otherwise, we indicate that no path
was found.

You might also like