Experiment – 6
AIM : write a program to implement the heuristic search
Description :
This program implements the A* (A-star) heuristic search algorithm in Python.
The graph is represented as an adjacency list with nodes and weighted
edges.
The heuristic function provides an estimate of the cost from each node
to the goal.
The a_star_search function uses both the actual cost (g-score) and
heuristic (h-score)
To calculate the f-score (f = g + h) and find the optimal path from the
start node
to the goal node. The example usage demonstrates finding a path from
'A' to 'G'.
A Heuristic search in Python is a search strategy that uses a heuristic function
to guide the search process toward the goal more efficiently. The heuristic
function provides an estimate of the cost or distance from a given state to the
goal state. Heuristic searches are widely used in pathfinding and optimization
problems, where finding the best or shortest path is critical.
The algorithm prioritizes exploring nodes with the lowest f-score, balancing
exploration and exploitation to efficiently find the optimal path. The example in
the code demonstrates finding a path from node 'A' to 'G' using a graph and a
heuristic function.
A (A-star) Search*: Uses both the cost to reach a node and a heuristic estimate
of the cost to the goal.
1. Greedy Best-First Search: Uses only the heuristic to guide the search.
2. Hill Climbing: Moves towards the neighbor with the best heuristic value.
3. Beam Search: Explores a limited number of best options at each level.
4. Simulated Annealing: Includes a probability of moving to worse states to
avoid local optima.
Algorithm :
Step 1 : Start
Step 2 : Algorithm for A* Heuristic Search
a. Initialize the open set with the start node, with f-score = 0.
Step 3 : Create g-score and f-score dictionaries, setting all nodes to
infinity except the start node.
Step 4: While the open set is not empty:
a. Select the node with the lowest f-score from the open set.
b. If the node is the goal, reconstruct and return the path.
c. For each neighbor of the current node:
i. Calculate the tentative g-score (current g-score + edge weight).
ii. If this path is better, update the path, g-score, and f-score.
iii. Add the neighbor to the open set.
Step 5 : If the open set is empty and the goal was not reached, return
None.
Step 6 : Stop
Procedure of the A Heuristic Search Code :
1. Initialization:
o The graph is defined as an adjacency list with nodes and weighted
edges.
o The heuristic function is a dictionary that provides an estimated
cost from each node to the goal.
2. Priority Queue:
o PriorityQueue() is used to store nodes with their f-scores (f = g + h)
where:
g: Actual cost to reach the node.
h: Heuristic estimate to the goal.
3. Tracking Costs:
o g_score: Stores the cost from the start node to each node,
initialized to infinity except for the start node (g_score[start] = 0).
o f_score: Stores the total cost estimate (g + h) to reach the goal,
initialized to the heuristic value for the start node.
4. Main Search Loop:
o Nodes are dequeued based on their f-score (lowest first).
o If the goal node is reached, the path is reconstructed by tracing
back through the came_from dictionary.
5. Exploring Neighbors:
o For each neighbor of the current node, calculate the tentative g-
score.
o If this path to the neighbor is better (lower g-score), update:
came_from to track the path.
g_score and f_score for the neighbor.
Push the neighbor into the priority queue with its updated f-
score.
6. Path Reconstruction:
o When the goal is reached, reconstruct the path by following the
came_from dictionary back to the start node.
o The path is returned in the correct order from start to goal.
7. Output:
o If a path is found, it is displayed. Otherwise, the output indicates
that no path exists.
Example Execution:
Start: 'A', Goal: 'G'.
The algorithm finds the optimal path ['A', 'B', 'D', 'G'].
Explaination about Heuristic Search :
Understanding Heuristic Search
Heuristics operates on the search space of a problem to find the best or
closest-to-optimal solution via the use of systematic algorithms. In contrast to a
brute-force approach, which checks all possible solutions exhaustively, a
heuristic search method uses heuristic information to define a route that
seems more plausible than the rest. Heuristics, in this case, refer to a set of
criteria or rules of thumb that offer an estimate of a firm's profitability. Utilizing
heuristic guiding, the algorithms determine the balance between exploration
and exploitation, and thus they can successfully tackle demanding issues.
Therefore, they enable an efficient solution finding process.
Significance of Heuristic Search in AI :
The primary benefit of using heuristic search techniques in AI is their ability to
handle large search spaces. Heuristics help to prioritize which paths are most
likely to lead to a solution, significantly reducing the number of paths that must
be explored. This not only speeds up the search process but also makes it
feasible to solve problems that are otherwise too complex to handle with exact
algorithms.
Components of Heuristic Search
Heuristic search algorithms typically comprise several essential components:
1. State Space: This implies that the totality of all possible states or
settings, which is considered to be the solution for the given problem.
2. Initial State: The instance in the search tree of the highest level with no
null values, serving as the initial state of the problem at hand.
3. Goal Test: The exploration phase ensures whether the present state is a
terminal or consenting state in which the problem is solved.
4. Successor Function: This create a situation where individual states
supplant the current state which represent the possible moves or
solutions in the problem space.
5. Heuristic Function: The function of a heuristic is to estimate the value or
distance from a given state to the target state. It helps to focus the
process on regions or states that has prospect of achieving the goal.
Types of Heuristic Search Techniques :
Over the history of heuristic search algorithms, there have been a lot of
techniques created to improve them further and attend different problem
domains. Some prominent techniques include:
1. A Search Algorithm*
A* Search Algorithm is perhaps the most well-known heuristic search
algorithm. It uses a best-first search and finds the least-cost path from a given
initial node to a target node. It has a heuristic function, often denoted
as f(n)=g(n)+h(n)f(n)=g(n)+h(n), where g(n) is the cost from the start node to n,
and h(n) is a heuristic that estimates the cost of the cheapest path from n to
the goal. A* is widely used in pathfinding and graph traversal.
2. Greedy Best-First Search
Greedy best-first search expands the node that is closest to the goal, as
estimated by a heuristic function. Unlike A*, which takes into account the cost
of the path from the start node to the current node, the greedy best-first
search only prioritizes the estimated cost from the current node to the goal.
This makes it faster but less optimal than A*.
3. Hill Climbing
Hill climbing is a heuristic search used for mathematical optimization problems.
It is a variant of the gradient ascent method. Starting from a random point, the
algorithm takes steps in the direction of increasing elevation or value to find
the peak of the mountain or the optimal solution to the problem. However, it
may settle for a local maximum and not reach the global maximum.
4. Simulated Annealing
Inspired by the process of annealing in metallurgy, simulated annealing is a
probabilistic technique for approximating the global optimum of a given
function. It allows the algorithm to jump out of any local optimums in search of
the global optimum by probabilistically deciding whether to accept or reject a
higher-cost solution during the early phases of the search.
5. Beam Search
Beam search is a heuristic search algorithm that explores a graph by expanding
the most promising nodes in a limited set or "beam". The beam width, which
limits the number of nodes stored in memory, plays a crucial role in the
performance and accuracy of the search.
Applications of Heuristic Search
Heuristic search techniques find application in a wide range of problem-solving
scenarios, including:
1. Pathfinding: Discovery, of the shortest distance that can be found from
the start point to the destination at the point of coordinates or graph.
2. Optimization: Solving the problem of the optimal distribution of
resources, planning or posting to achieve maximum results.
3. Game Playing: The agency of AI with some board games, e.g., chess or
Go, is on giving guidance and making strategy-based decisions to the
agents.
4. Robotics: Scheduling robots` location and movement to guide carefully
expeditions and perform given tasks with high efficiency.
5. Natural Language Processing: Language processing tasks involving
search algorithms, such as parsing or semantic analysis, should be
outlined. That means.
Advantages of Heuristic Search Techniques
Heuristic search techniques offer several advantages:
1. Efficiency: As they are capable of aggressively digesting large areas for
the more promising lines, they can allot more time and resources to
investigate the area.
2. Optimality: If the methods that an algorithm uses are admissible, A*
guarantees of an optimal result.
3. Versatility: Heuristic search methods encompass a spectrum of problems
that are applied to various domains of problems.
Limitations of Heuristic Search Techniques
1. Heuristic Quality: The power of heuristic search strongly depends on the
quality of function the heuristic horizon. If the heuristics are constructed
thoughtlessly, then their level of performance may be low or inefficient.
2. Space Complexity: The main requirement for some heuristic search
algorithms could be a huge memory size in comparison with the others,
especially in cases where the search space considerably increases.
3. Domain-Specificity: It is often the case that devising efficient heuristics
depends on the specifics of the domain, a challenging obstruction to
development of generic approaches.
Source Code :
from queue import PriorityQueue
# Define the graph as an adjacency list and the heuristic function
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('D', 2), ('E', 5)],
'C': [('F', 1)],
'D': [('G', 1)],
'E': [('G', 2)],
'F': [('G', 3)],
'G': []
}
# Heuristic function (estimated cost to goal)
heuristic = {
'A': 7, 'B': 6, 'C': 2, 'D': 1, 'E': 4, 'F': 2, 'G': 0
}
def a_star_search(start, goal):
open_set = PriorityQueue()
open_set.put((0, start))
came_from = {}
g_score = {node: float('inf') for node in graph}
g_score[start] = 0
f_score = {node: float('inf') for node in graph}
f_score[start] = heuristic[start]
while not open_set.empty():
current_f, current_node = open_set.get()
if current_node == goal:
path = []
while current_node in came_from:
path.append(current_node)
current_node = came_from[current_node]
path.append(start)
return path[::-1]
for neighbor, weight in graph[current_node]:
tentative_g_score = g_score[current_node] + weight
if tentative_g_score < g_score[neighbor]:
came_from[neighbor] = current_node
g_score[neighbor] = tentative_g_score
f_score[neighbor] = tentative_g_score + heuristic[neighbor]
open_set.put((f_score[neighbor], neighbor))
return None
# Example usage
start_node = 'A'
goal_node = 'G'
path = a_star_search(start_node, goal_node)
if path:
print(f'Path found: {path}')
else:
print('No path found')
Output :
Path found: ['A', 'B', 'D', 'G']
Result :
This result indicates that the optimal path from the start node 'A' to the goal
node 'G' is through the nodes 'B' and 'D'. The algorithm efficiently finds this
shortest path using both the actual path cost and the heuristic estimate.