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

LAB5 - Ai

The document describes an A* search algorithm implementation for pathfinding. It includes the A* search function definition, neighbor getting and heuristic functions, and sample graph and calls to find paths from start to goal nodes.

Uploaded by

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

LAB5 - Ai

The document describes an A* search algorithm implementation for pathfinding. It includes the A* search function definition, neighbor getting and heuristic functions, and sample graph and calls to find paths from start to goal nodes.

Uploaded by

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

Artificial Intelligence (CS-316L) LAB # 5

LAB # 05
Tasks:
def aStarAlgo(start_node, stop_node):

open_set = set(start_node)

closed_set = set()

g = {}

parents = {}

g[start_node] = 0

parents[start_node] = start_node

while len(open_set) > 0:

n = None

for v in open_set:

if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):

n=v

if n == stop_node or Graph_nodes[n] == None:

pass

else:

for (m, weight) in get_neighbors(n):

if m not in open_set and m not in closed_set:

open_set.add(m)

parents[m] = n

g[m] = g[n] + weight

else:

if g[m] > g[n] + weight:

g[m] = g[n] + weight

parents[m] = n

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 5

if m in closed_set:

closed_set.remove(m)

open_set.add(m)

if n == None:

print('Path does not exist!')

return None

if n == stop_node:

path = []

while parents[n] != n:

path.append(n)

n = parents[n]

path.append(start_node)

path.reverse()

print('Path found: {}'.format(path))

return path

open_set.remove(n)

closed_set.add(n)

print('Path does not exist!')

return None

def get_neighbors(v):

if v in Graph_nodes:

return Graph_nodes[v]

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 5

else:

return None

def heuristic(n):

H_dist = {

'S': 3,

'A': 2,

'B': 1,

'G': 0

return H_dist[n]

Graph_nodes = {

'S': [('A', 2), ('B', 2)],

'A': [('G', 2)],

'B': [('G', 3)]

aStarAlgo('S','G')

Output:

Tasks:

def aStarAlgo(start_node, stop_node):

open_set = set(start_node)

closed_set = set()

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 5

g = {}

parents = {}

g[start_node] = 0

parents[start_node] = start_node

while len(open_set) > 0:

n = None

for v in open_set:

if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):

n=v

if n == stop_node or Graph_nodes[n] == None:

pass

else:

for (m, weight) in get_neighbors(n):

if m not in open_set and m not in closed_set:

open_set.add(m)

parents[m] = n

g[m] = g[n] + weight

else:

if g[m] > g[n] + weight:

g[m] = g[n] + weight

parents[m] = n

if m in closed_set:

closed_set.remove(m)

open_set.add(m)

if n == None:

print('Path does not exist!')

return None

if n == stop_node:

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 5

path = []

while parents[n] != n:

path.append(n)

n = parents[n]

path.append(start_node)

path.reverse()

print('Path found: {}'.format(path))

return path

open_set.remove(n)

closed_set.add(n)

print('Path does not exist!')

return None

def get_neighbors(v):

if v in Graph_nodes:

return Graph_nodes[v]

else:

return None

def heuristic(n):

H_dist = {

'S': 1,

'A': 3,

'B': 3,

'C': 0,

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 5

'D': 0

return H_dist[n]

Graph_nodes = {

'S': [('A',3),('B',2)],

'A': [('B', 3),('C', 1),('D', 3)],

'B': [('C', 5),('D', 3)],

'C': [('D',2)],

'D': None

aStarAlgo('S','C')

aStarAlgo('S','D')

Output:

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)

You might also like