Informed Search

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

IMPLEMENTATION OF INFORMED SEARCH ALGORITHM

AIM:

To implement the informed search using A* algorithm in python

programming language.

ALGORITHM:

1. Initialize the open list and closed list

2. Add the starting node to the open list

3. While the open list is not empty:

a. Find the node with the lowest f cost in the open list (f = g + h, where g is the

cost of the path from the starting node to the current node, and h is the heuristic

estimate of the cost from the current node to the goal node)

b. If the current node is the goal node, return the path

c. Generate the successors of the current node

d. For each successor:

i. Calculate the successor's g cost (the cost of the path from the starting node

to the successor)

ii. Calculate the successor's h cost (the heuristic estimate of the cost from the

successor to the goal node)

iii. Calculate the successor's f cost (f = g + h)

iv. If the successor is already in the closed list and the new f cost is higher

than the old f cost, skip the successor

v. If the successor is already in the open list and the new f cost is higher than

the old f cost, skip the successor

vi. Otherwise, add the successor to the open list and update its f cost

e. Add the current node to the closed list

4. If the open list is empty and the goal node has not been reached, there is nosolution

5.Stop.
PROGRAM:

from queue import PriorityQueue

def astar(graph, start, goal):

frontier = PriorityQueue()

frontier.put(start, 0)

came_from = {}

cost_so_far = {}

came_from[start] = None

cost_so_far[start] = 0

while not frontier.empty():

current = frontier.get()

if current == goal:

break

for next_node in graph[current]:

new_cost = cost_so_far[current] + graph[current][next_node]

if next_node not in cost_so_far or new_cost < cost_so_far[next_node]:

cost_so_far[next_node] = new_cost

priority = new_cost + heuristic(goal, next_node)

frontier.put(next_node, priority)

came_from[next_node] = current

return came_from, cost_so_far

def heuristic(goal, node):

return abs(goal[0]-node[0]) + abs(goal[1]-node[1])

graph = {

(0,0): {(0,1):1, (1,0):1},

(0,1): {(0,0):1, (1,1):1},

(1,0): {(0,0):1, (1,1):1},

(1,1): {(0,1):1, (1,0):1}


}

start = (0,0)

goal = (1,1)

came_from, cost_so_far = astar(graph, start, goal)

path = [goal]

node = goal

while node != start:

node = came_from[node]

path.append(node)

path.reverse()

print("Shortest path:", path)

OUTPUT:

Shortest path :[(0,0),(0,1),(1,1)]

RESULT:

Thus the python program to implement informed search was executed successfully and output was
verified.

You might also like