#!/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: print("Depth is now " + str(depth)) 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: print("Goal state found") return (found, True) if remaining: any_remaining = True return (None, any_remaining) def iterative_deepening_dfs(root): for depth in range(1, 999): print("Examining at maximum depth " + str(depth)) found, remaining = depth_limited_search(root, depth) if found is not None: return found elif not remaining: return None print("Overflow in iterative deepening depth-first search") finish = iterative_deepening_dfs(problem.start) print(finish.to_string())