53 lines
1.5 KiB
Python
Executable File
53 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import problem
|
|
|
|
|
|
def depth_limited_search(node, depth, path):
|
|
if depth == 0:
|
|
if problem.is_goal(node):
|
|
return (node, True, path)
|
|
return (None, True, path)
|
|
elif depth > 0:
|
|
any_remaining = False
|
|
for act in node.actions:
|
|
child = act.result_state
|
|
# path[child] = node
|
|
updated_path = path #[:]
|
|
updated_path.append(child)
|
|
i = 0
|
|
for item in updated_path:
|
|
print(str(i) + ") " + item.to_string())
|
|
i += 1
|
|
found, remaining, deep_path = depth_limited_search(
|
|
child, depth - 1, updated_path)
|
|
if found is not None:
|
|
return (found, True, deep_path)
|
|
if remaining:
|
|
any_remaining = True
|
|
return (None, any_remaining, None)
|
|
|
|
|
|
def iterative_deepening_dfs(root):
|
|
idd_path = []
|
|
for depth in range(0, 999):
|
|
found, remaining, deep_path = depth_limited_search(root, depth, [])
|
|
if found is not None:
|
|
return found, deep_path
|
|
elif not remaining:
|
|
return None, deep_path
|
|
print("Overflow in iterative deepening depth-first search")
|
|
|
|
|
|
finish, result_path = iterative_deepening_dfs(problem.start)
|
|
print(finish.to_string())
|
|
|
|
if finish:
|
|
print("Finished DFS of path")
|
|
# current = finish
|
|
# while current is not None:
|
|
# print(current.to_string())
|
|
# current = result_path[current]
|
|
for item in result_path:
|
|
print(item.to_string())
|