-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
51 lines (41 loc) · 1.46 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Referenced from https://github.com/aimacode/aima-python/blob/master/search.py
"""
class Node:
def __init__(self, state, parent=None, action=None, path_cost=0):
self.state = state
self.parent = parent
self.action = action
self.path_cost = path_cost
self.depth = 0
if parent:
self.depth = parent.depth + 1
def __repr__(self):
return "<Node {}>".format(self.state)
def __lt__(self, node):
return self.state < node.state
def expand(self, problem):
return [self.child_node(problem, action)
for action in problem.actions(self)]
def child_node(self, problem, action):
next_state = problem.result(self, action)
# next_node = Node(next_state, self, action, problem.path_cost(self.path_cost, self.state, action, next_state))
return next_state
@property
def solution(self):
return [node.action for node in self.path[1:]]
@property
def path(self):
node, path_back = self, []
while node:
path_back.append(node)
node = node.parent
return list(reversed(path_back))
def __eq__(self, other):
if isinstance(other, Node):
return self.state == other.state and self.action == other.action \
and self.depth == other.depth and self.parent == other.parent
else:
return False
def __hash__(self):
return hash(self.state)