cs471/hw1/mocull-hw1/dfs.py

42 lines
1.2 KiB
Python
Raw Normal View History

2019-01-24 22:35:36 -05:00
#!/usr/bin/env python3
import problem
def depth_limited_search(node, depth):
if depth == 0:
if problem.is_goal(node):
return (node, True)
return (None, True)
elif depth > 0:
if node == problem.start:
print("Initial state")
print(node.to_string())
any_remaining = False
for act in node.actions:
child = act.result_state
print(act.to_string())
print(child.to_string())
found, remaining = depth_limited_search(child, depth - 1)
if found is not None:
return (found, True)
if remaining:
any_remaining = True
return (None, any_remaining)
def iterative_deepening_dfs(root):
for depth in range(1, 100):
print("Iterating to maximum depth " + str(depth))
found, remaining = depth_limited_search(root, depth)
if found is not None:
print("Goal state found")
return found
elif not remaining:
print("Goal state could not be found")
return None
print("Overflow in iterative deepening depth-first search")
finish = iterative_deepening_dfs(problem.start)