-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.py
33 lines (22 loc) · 872 Bytes
/
Node.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
from Problem import Problem
class Node(object):
def __init__(self, state, parent=None, action=None):
self.action = action
self.state = list(state)
self.parent = parent
def __repr__(self):
return "Node {}".format(self.state)
def expand(self, problem):
return [self.child_node(problem, action)
for action in problem.actions(self.state)]
def child_node(self, problem, action):
next = problem.result(self.state, action)
return Node(next, self, action)
def solution(self):
"""Returns the solution, the sequence of actions it took to get to this node"""
node, path_back = self, []
while node:
if node.action is not None:
path_back.append(node.action)
node = node.parent
return list(reversed(path_back))