I was trying to use the same way as breadth first search (BFS) or binary tree, but the count gave me a huge number, way bigger than BFS, which does not make sense, since IDS and BFS have similar number of nodes generated. I realized that the count included not only the nodes but also the times they were visited. Since in IDS, nodes are visited more than once, I got a much bigger count number. But how to count only the number of nodes generated? Any suggestions are highly appreciated. I attached the code below for the IDS function, and bolded the parts that I used to count the nodes, which seems not working.
def depth_limited_search(problem, limit=50):
"""[Figure 3.17]"""
def recursive_dls(node, problem, limit):
if problem.goal_test(node.state):
print('The total nodes are: ', count[0])
return node
elif limit == 0:
return 'cutoff'
else:
cutoff_occurred = False
for child in node.expand(problem):
count[0] += 1
result = recursive_dls(child, problem, limit - 1)
if result == 'cutoff':
cutoff_occurred = True
elif result is not None:
return result
return 'cutoff' if cutoff_occurred else None
# Body of depth_limited_search:
count = [0]
return recursive_dls(Node(problem.initial), problem, limit)
def iterative_deepening_search(problem):
"""[Figure 3.18]"""
for depth in range(sys.maxsize):
result = depth_limited_search(problem, depth)
if result != 'cutoff':
return result