-
Notifications
You must be signed in to change notification settings - Fork 19
/
iddfs.py
36 lines (31 loc) · 1.17 KB
/
iddfs.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
30
31
32
33
34
35
36
import itertools
from solver import Solver
class IDDFS(Solver):
def __init__(self, initial_state):
super(IDDFS, self).__init__(initial_state)
self.frontier = []
def dls(self, limit):
self.frontier.append(self.initial_state)
while self.frontier:
board = self.frontier.pop()
self.explored_nodes.add(tuple(board.state))
if board.goal_test():
self.set_solution(board)
return self.solution
if board.depth < limit:
for neighbor in board.neighbors()[::-1]:
if tuple(neighbor.state) not in self.explored_nodes:
self.frontier.append(neighbor)
self.explored_nodes.add(tuple(neighbor.state))
self.max_depth = max(self.max_depth, neighbor.depth)
return None
def solve(self):
for i in itertools.count():
self.frontier = []
self.explored_nodes = set()
self.max_depth = 0
self.frontier.append(self.initial_state)
sol = self.dls(i)
if sol is not None:
break
return