Memory Bounded A Star Algorithm
Memory Bounded A Star Algorithm
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.
import heapq
class Node:
self.name = name
self.x = x
self.y = y
self.g = float('inf')
self.h = float('inf')
self.f = float('inf')
self.parent = None
open_list = []
closed_list = set()
revisited_nodes = deque()
start.g = 0
start.f = start.h
heapq.heappush(open_list, start)
while open_list:
oldest = revisited_nodes.popleft()
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)
if neighbor in closed_list:
continue
neighbor.g = tentative_g
neighbor.h = heuristic(neighbor, goal)
neighbor.parent = current
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['E']: [nodes['D']],
start_node = nodes['A']
goal_node = nodes['E']
memory_limit = 5
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
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
class Node:
self.x = x
self.y = y
self.walkable = walkable
self.parent = None
neighbors = []
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right
if grid[nx][ny].walkable:
neighbors.append(grid[nx][ny])
return neighbors
open_list = []
closed_list = set()
open_set = set()
start.g = 0
start.f = start.h
heapq.heappush(open_list, start)
open_set.add(start)
while open_list:
oldest = revisited_nodes.popleft()
open_set.remove(oldest)
closed_list.add((oldest.x, oldest.y))
current = heapq.heappop(open_list)
open_set.remove(current)
path = []
while current:
path.append((current.x, current.y))
current = current.parent
closed_list.add((current.x, current.y))
neighbor.g = tentative_g
neighbor.parent = current
heapq.heappush(open_list, neighbor)
open_set.add(neighbor)
revisited_nodes.append(neighbor)
return None
# Example usage:
obstacles = [(1, 1), (1, 2), (1, 3), (2, 1), (3, 1)]
grid[x][y].walkable = False
start_node = grid[0][0]
goal_node = grid[4][4]
memory_limit = 10
# Find the path
if path:
else:
Output:
Path found: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1), (4, 2),
(4, 3), (4, 4)]