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

Memory Bounded A Star Algorithm

program

Uploaded by

sasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Memory Bounded A Star Algorithm

program

Uploaded by

sasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Memory Bounded A* (MBA*) Algorithm

Memory Bounded A* (MBA*) is a variant of A* designed to operate within limited memory.


It maintains a bounded open list and periodically revisits nodes that are evicted from the list.

Key Differences:

1. A Algorithm*:
o Uses an open list to store nodes to be evaluated.
o Does not have a strict memory limit.
2. Memory Bounded A (MBA) Algorithm**:
o Limits the size of the open list.
o Uses a mechanism (like a deque) to manage nodes that are removed from the
open list but may need to be revisited.

# IMPLEMENT PROGRAM IN PYTHON FOR MEMORY BOUNDED A* ALGORITHMS – sample 1

import heapq

from collections import deque

class Node:

def __init__(self, name, x, y):

self.name = name

self.x = x

self.y = y

self.g = float('inf')

self.h = float('inf')

self.f = float('inf')

self.parent = None

def __lt__(self, other):

return self.f < other.f

def heuristic(a, b):

return abs(a.x - b.x) + abs(a.y - b.y)

def mba_star(start, goal, neighbors, memory_limit):

open_list = []
closed_list = set()

revisited_nodes = deque()

start.g = 0

start.h = heuristic(start, goal)

start.f = start.h

heapq.heappush(open_list, start)

while open_list:

if len(open_list) > memory_limit:

oldest = revisited_nodes.popleft()

if oldest not in closed_list:

closed_list.add(oldest)

current = heapq.heappop(open_list)

if current == goal:

path = []

while current:

path.append(current.name)

current = current.parent

return path[::-1]

closed_list.add(current)

for neighbor in neighbors[current]:

if neighbor in closed_list:

continue

tentative_g = current.g + 1 # Assuming uniform cost

if tentative_g < neighbor.g:

neighbor.g = tentative_g
neighbor.h = heuristic(neighbor, goal)

neighbor.f = neighbor.g + neighbor.h

neighbor.parent = current

if neighbor not in [node for _, node in open_list]:

heapq.heappush(open_list, neighbor)

revisited_nodes.append(neighbor)

return None

# Example usage:

nodes = {name: Node(name, x, y) for name, x, y in [('A', 0, 0), ('B', 1, 0), ('C', 1, 1), ('D', 2, 1), ('E', 2,
2)]}

neighbors = {

nodes['A']: [nodes['B']],

nodes['B']: [nodes['A'], nodes['C']],

nodes['C']: [nodes['B'], nodes['D']],

nodes['D']: [nodes['C'], nodes['E']],

nodes['E']: [nodes['D']],

start_node = nodes['A']

goal_node = nodes['E']

memory_limit = 5

path = mba_star(start_node, goal_node, neighbors, memory_limit)

print("Path found:", path)

output:
Path found: ['A', 'B', 'C', 'D', 'E']
Memory Bounded A* (MBA*) is designed to manage memory constraints by using a
bounded open list and revisiting nodes that were removed from the open list. Here’s a
practical example of MBA* for pathfinding on a grid.

Problem Setup

 Grid: A 2D grid with walkable and blocked cells.


 Start and Goal: Starting and goal positions are specified on the grid.
 Memory Limit: MBA* uses a limited-size open list and maintains a record of
recently visited nodes.

Explanation:

1. Node Class:
o __init__: Initializes node with position, walkability, and cost values.
o __lt__: Comparison method for priority queue operations.
2. Heuristic Function:
o Uses Manhattan distance to estimate the cost from the current node to the goal
node.
3. Get Neighbors Function:
o Returns the walkable neighbors of the current node.
4. MBA Algorithm*:
o Uses an open list with a memory limit.
o Manages nodes in open_list and maintains a revisited_nodes deque to
track nodes that were evicted from the open list.
o If the open list exceeds the memory limit, it evicts the oldest node and adds it
to the closed list for potential re-evaluation.
o Continues until the goal is found or all possible nodes are explored.

Key Points:

 Memory Management: MBA* maintains a bounded open list and a deque for evicted
nodes, helping to manage memory constraints.
 Uniform Cost: Assumes uniform cost for movement between nodes (i.e., each step
costs 1).

This implementation should handle memory constraints better than a standard A* algorithm
by managing the open list size and potentially revisiting nodes when necessary. Adjust the
memory_limit parameter as needed based on the problem’s constraints.
# IMPLEMENT PROGRAM IN PYTHON FOR MEMORY BOUNDED A* ALGORITHMS – sample 2

import heapq

from collections import deque

class Node:

def __init__(self, x, y, walkable=True):

self.x = x

self.y = y

self.walkable = walkable

self.g = float('inf') # Cost from start to this node

self.h = float('inf') # Heuristic cost to goal

self.f = float('inf') # Total cost

self.parent = None

def __lt__(self, other):

return self.f < other.f

def heuristic(a, b):

return abs(a.x - b.x) + abs(a.y - b.y) # Manhattan distance

def get_neighbors(node, grid):

neighbors = []

directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right

for direction in directions:

nx, ny = node.x + direction[0], node.y + direction[1]

if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]):

if grid[nx][ny].walkable:

neighbors.append(grid[nx][ny])

return neighbors

def mba_star(start, goal, grid, memory_limit):

open_list = []
closed_list = set()

open_set = set()

revisited_nodes = deque() # To track evicted nodes for re-evaluation

start.g = 0

start.h = heuristic(start, goal)

start.f = start.h

heapq.heappush(open_list, start)

open_set.add(start)

while open_list:

# Maintain memory limit by evicting nodes if necessary

if len(open_list) > memory_limit:

oldest = revisited_nodes.popleft()

open_set.remove(oldest)

closed_list.add((oldest.x, oldest.y))

current = heapq.heappop(open_list)

open_set.remove(current)

if (current.x, current.y) == (goal.x, goal.y):

path = []

while current:

path.append((current.x, current.y))

current = current.parent

return path[::-1] # Return reversed path

closed_list.add((current.x, current.y))

for neighbor in get_neighbors(current, grid):

if (neighbor.x, neighbor.y) in closed_list:


continue

tentative_g = current.g + 1 # Assuming uniform cost (1 for each step)

if tentative_g < neighbor.g:

neighbor.g = tentative_g

neighbor.h = heuristic(neighbor, goal)

neighbor.f = neighbor.g + neighbor.h

neighbor.parent = current

if neighbor not in open_set:

heapq.heappush(open_list, neighbor)

open_set.add(neighbor)

revisited_nodes.append(neighbor)

return None

# Example usage:

# Create a 5x5 grid

grid = [[Node(x, y) for y in range(5)] for x in range(5)]

# Define some obstacles

obstacles = [(1, 1), (1, 2), (1, 3), (2, 1), (3, 1)]

for (x, y) in obstacles:

grid[x][y].walkable = False

# Define start and goal nodes

start_node = grid[0][0]

goal_node = grid[4][4]

# Memory limit for the open list

memory_limit = 10
# Find the path

path = mba_star(start_node, goal_node, grid, memory_limit)

if path:

print("Path found:", path)

else:

print("No path found")

Output:
Path found: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1), (4, 2),
(4, 3), (4, 4)]

You might also like