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

Assignment

This document contains code for implementing greedy best-first search and A* search algorithms. It defines functions for adding edges to a graph, performing best-first search on the graph, and defining Node class for A* search. The A* search code also includes functions for sorting nodes, checking if a node should be added to the open list, and drawing the search grid. The code reads in a maze from a file and uses A* search to find the shortest path from the start to end node.

Uploaded by

• Ali •
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)
21 views

Assignment

This document contains code for implementing greedy best-first search and A* search algorithms. It defines functions for adding edges to a graph, performing best-first search on the graph, and defining Node class for A* search. The A* search code also includes functions for sorting nodes, checking if a node should be added to the open list, and drawing the search grid. The code reads in a maze from a file and uses A* search to find the shortest path from the start to end node.

Uploaded by

• Ali •
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/ 5

NAME : ALI MOHAMMED IBRAHIM

CLASS : 4 C

GREEEDY BEST-FIRST SEARCH:-

from queue import PriorityQueue

# Filling adjacency matrix with empty arrays


vertices = 14
graph = [[] for i in range(vertices)]

# Function for adding edges to graph


def add_edge(x, y, cost):
    graph[x].append((y, cost))
    graph[y].append((x, cost))

# Function For Implementing Best First Search


# Gives output path having the lowest cost
def best_first_search(source, target, vertices):
    visited = [0] * vertices
    pq = PriorityQueue()
    pq.put((0, source))
    print("Path: ")
    while not pq.empty():
        u = pq.get()[1]
        # Displaying the path having the lowest cost
        print(u, end=" ")
        if u == target:
            break

        for v, c in graph[u]:
            if not visited[v]:
                visited[v] = True
                pq.put((c, v))
    print()

if __name__ == '__main__':
    # The nodes shown in above example(by alphabets) are
    # implemented using integers add_edge(x,y,cost);
    add_edge(0, 1, 1)
    add_edge(0, 2, 8)
    add_edge(1, 2, 12)
    add_edge(1, 4, 13)
    add_edge(2, 3, 6)
    add_edge(4, 3, 3)

    source = 0
    target = 2
    best_first_search(source, target, vertices)

A* SEACRH:-

class Node:
 
    def __init__(self, position:(), parent:()):
        self.position = position
        self.parent = parent
        self.g = 0
        self.h = 0
        self.f = 0
    def __eq__(self, other):
        return self.position == other.position
   
    def __lt__(self, other):
         return self.f < other.f
   
    def __repr__(self):
        return ('({0},{1})'.format(self.position, self.f))

def draw_grid(map, width, height, spacing=2, **kwargs):


    for y in range(height):
        for x in range(width):
            print('%%-%ds' % spacing % draw_tile(map, (x, y), kwargs), end='')
        print()

def draw_tile(map, position, kwargs):


   
   
    value = map.get(position)
   
    if 'path' in kwargs and position in kwargs['path']: value = '+'
   
    if 'start' in kwargs and position == kwargs['start']: value = '@'
   
    if 'goal' in kwargs and position == kwargs['goal']: value = '$'
   
    return value

def astar_search(map, start, end):


   
   
    open = []
    closed = []
   
    start_node = Node(start, None)
    goal_node = Node(end, None)
   
    open.append(start_node)
   
 
    while len(open) > 0:
       
        open.sort()
       
        current_node = open.pop(0)
       
        closed.append(current_node)
       
       
        if current_node == goal_node:
            path = []
            while current_node != start_node:
                path.append(current_node.position)
                current_node = current_node.parent
         
            return path[::-1]
       
        (x, y) = current_node.position
       
        neighbors = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
       
        for next in neighbors:
           
            map_value = map.get(next)
           
            if(map_value == '#'):
                continue
           
            neighbor = Node(next, current_node)
           
            if(neighbor in closed):
                continue
         
            neighbor.g = abs(neighbor.position[0] - start_node.position[0]) +
abs(neighbor.position[1] - start_node.position[1])
            neighbor.h = abs(neighbor.position[0] - goal_node.position[0]) +
abs(neighbor.position[1] - goal_node.position[1])
            neighbor.f = neighbor.g + neighbor.h
           
            if(add_to_open(open, neighbor) == True):
               
                open.append(neighbor)
   
    return None

def add_to_open(open, neighbor):


    for node in open:
        if (neighbor == node and neighbor.f >= node.f):
            return False

def main():
   
    map = {}
    chars = ['c']
    start = None
    end = None
    width = 0
    height = 0
   
    fp = open('maze1.txt', 'r')
    while len(chars) > 0:
       
        chars = [str(i) for i in fp.readline().strip()]
        width = len(chars) if width == 0 else width
     
        for x in range(len(chars)):
            map[(x, height)] = chars[x]
            if(chars[x] == '@'):
                start = (x, height)
            elif(chars[x] == '$'):
                end = (x, height)
       
        if(len(chars) > 0):
            height += 1
    fp.close()
   
    path = astar_search(map, start, end)
    print()
    print(path)
    print()
    draw_grid(map, width, height, spacing=1, path=path, start=start, goal=end)
    print()
    print('Steps to goal: {0}'.format(len(path)))
    print()
if __name__ == "__main__": main()

You might also like