0% found this document useful (0 votes)
4 views4 pages

Memory Bounded Search Algorithm

The document describes a memory-bounded A* search algorithm implemented in Python for solving a sliding puzzle. It includes functions for heuristic calculation, memory management, node selection, and successor generation, along with an example usage that tests the algorithm with different memory limits. The results show that a solution was found with a memory limit of 10, while no solution was found with a limit of 1.
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)
4 views4 pages

Memory Bounded Search Algorithm

The document describes a memory-bounded A* search algorithm implemented in Python for solving a sliding puzzle. It includes functions for heuristic calculation, memory management, node selection, and successor generation, along with an example usage that tests the algorithm with different memory limits. The results show that a solution was found with a memory limit of 10, while no solution was found with a limit of 1.
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/ 4

// 2.

b INFORMED SEARCH ALGORITHM MEMORY-BOUNDED A*

class Node:
def __init__(self, state, parent=None, action=None):
self.state = state
self.parent = parent
self.action = action

# Define the heuristic function (Manhattan distance)


def heuristic(state):
distance = 0
for i in range(9):
if state[i] != 0:
distance += abs(i // 3 - (state[i] - 1) // 3) + abs(i
% 3 - (state[i] - 1) % 3)
return distance

# Define the memory usage function


def memory_usage(open_list, closed_list):
return len(open_list) + len(closed_list)

# Define the function to prune memory


def prune_memory(open_list, closed_list):
# Prune the least promising nodes from the open list
open_list.sort(key=lambda x: heuristic(x.state), reverse=True)
open_list[:] = open_list[:len(open_list) // 2] # Keep only the
top half of the open list

# Define the function to select the best node


def select_best_node(open_list):
return min(open_list, key=lambda x: heuristic(x.state))

# Define the function to check if a node is the goal state


def is_goal(node):
return node.state == goal_state

# Define the function to generate successors


def generate_successors(node):
successors = []
zero_index = node.state.index(0)
for move in moves[zero_index]:
new_state = list(node.state)
new_state[zero_index], new_state[move] = new_state[move],
new_state[zero_index]
successors.append(Node(tuple(new_state), parent=node,
action=move))
return successors
# Define the function to check if a successor is redundant
def redundant(successor, open_list, closed_list):
for node in open_list + closed_list:
if node.state == successor.state:
return True
return False

# Define the memory-bounded search function


def MemoryBoundedSearch(initial_state, memory_limit):
node = Node(initial_state)
open_list = [node]
closed_list = []

while open_list:
if memory_usage(open_list, closed_list) > memory_limit:
prune_memory(open_list, closed_list)

# No solution found within memory limit


if not open_list:
return None

current_node = select_best_node(open_list)
# Return the goal node
if is_goal(current_node):
return current_node

open_list.remove(current_node)
closed_list.append(current_node)

for successor in generate_successors(current_node):


if not redundant(successor, open_list, closed_list):
open_list.append(successor)

# No solution found within memory limit


return None

# Define the goal state


goal_state = (1, 2, 3, 4, 5, 6, 7, 8, 0)

# Define the possible moves


moves = {
0: [1, 3],
1: [0, 2, 4],
2: [1, 5],
3: [0, 4, 6],
4: [1, 3, 5, 7],
5: [2, 4, 8],
6: [3, 7],
7: [4, 6, 8],
8: [5, 7]
}

# Example usage
initial_state = (1, 2, 3, 4, 5, 6, 0, 7, 8) # Initial state of the
puzzle

print("Case 1 with Memory Limit 1")


memory_limit = 1 # Set memory limit

goal_node = MemoryBoundedSearch(initial_state, memory_limit)


if goal_node:
print("Solution found!")
# Print the solution path if needed
while goal_node.parent:
print("Action:", goal_node.action)
print("State:")
print(goal_node.state[:3])
print(goal_node.state[3:6])
print(goal_node.state[6:])
print()
goal_node = goal_node.parent
else:
print("Memory limit exceeded. No solution found within the given
memory limit.")

print("\nCase 1 with Memory Limit 10")


memory_limit = 10 # Set memory limit
goal_node = MemoryBoundedSearch(initial_state, memory_limit)
if goal_node:
print("Solution found!")
# Print the solution path if needed
while goal_node.parent:
print("Action:", goal_node.action)
print("State:")
print(goal_node.state[:3])
print(goal_node.state[3:6])
print(goal_node.state[6:])
print()
goal_node = goal_node.parent
else:
print("Memory limit exceeded. No solution found within the given
memory limit.")

Output:
Case 1 with Memory Limit 1
Memory limit exceeded. No solution found within the given
memory limit.

Case 1 with Memory Limit 10


Solution found!
Action: 8
State:
(1, 2, 3)
(4, 5, 6)
(7, 8, 0)

Action: 7
State:
(1, 2, 3)
(4, 5, 6)
(7, 0, 8)

You might also like