-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchAgent.py
30 lines (24 loc) · 883 Bytes
/
SearchAgent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from Node import Node
from Problem import Problem
import sys
def iterative_deepening_search(problem):
for depth in range(sys.maxsize):
result = depth_limited_search(problem, depth)
if result != "cutoff":
return result
def depth_limited_search(problem, limit = 200 ):
def recursive_dls(node, problem, limit):
if problem.goal_test(node.state):
return node
elif limit == 0:
return "cutoff"
else:
cutoff = False
for child in node.expand(problem):
result = recursive_dls(child, problem, limit - 1)
if result == "cutoff":
cutoff = True
elif result is not None:
return result
return "cutoff" if cutoff else None
return recursive_dls(Node(problem.initial), problem, limit)