-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.py
72 lines (63 loc) · 1.73 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: UTF-8 -*-
import copy
class Node:
"""
A node is a position (immutable) in the puzzle
It also contains the gScore, hScore, fScore and
the list of moves which bring us to this position
"""
def __init__(self, position, moves, heuristic):
"""
Builds a new node
"""
self._position = position
self._moves = moves
self._heuristic = heuristic
self._hScore = None
def getPosition(self):
"""
Get position of the Node (nested list of integers)
"""
return copy.deepcopy(self._position)
def getGScore(self):
"""
Get gScore of the Node
"""
return len(self._moves)
def getHScore(self):
"""
Get hScore of this Node. Heuristic passed in
the constructor will be used for computation
"""
if self._hScore is None:
self._hScore = self._heuristic.compute(self)
return self._hScore
def getFScore(self):
"""
Get fScore of the Node
fScore = gScore + hScore
"""
return self.getGScore() + self.getHScore()
def getMoves(self):
"""
Return list of moves which bring us to
this position
"""
return copy.copy(self._moves)
def getHeuristic(self):
"""
Return heuristic used to compute hScore for this node
"""
return self._heuristic
def getCoordByValue(self, value):
"""
Get i and j coord of the given value
"""
i = 0
for row in self._position:
j = 0
for cell in row:
if cell == value:
return [i, j]
j += 1
i += 1