-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
95 lines (73 loc) · 2.22 KB
/
Copy pathnode.py
File metadata and controls
95 lines (73 loc) · 2.22 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from typing import Optional
from enum import Enum
from dataclasses import dataclass, replace
class NodeType(Enum):
BLANK = 0
START = 1
END = 2
OBSTACLE = 3
@staticmethod
def from_value(value: int) -> 'NodeType':
maxValue = max([e.value for e in NodeType])
if value > maxValue:
return NodeType(maxValue)
return NodeType(value)
@dataclass(frozen=True, eq=False)
class Node:
x: int
y: int
node_type: NodeType
edge_to_parent: Optional['Edge'] = None
@staticmethod
def empty() -> 'Node':
"""Creates an empty node.
:return: An emty node.
"""
return Node(0, 0, NodeType(0))
def is_start(self) -> bool:
"""Checks whether this node is a START.
:return: True if its a START node, else False.
"""
if self.node_type == NodeType.START:
return True
return False
def is_end(self) -> bool:
"""Checks whether this node is an END.
:return: True if its an END node, else False.
"""
if self.node_type == NodeType.END:
return True
return False
def is_obstacle(self) -> bool:
"""Checks whether this node is an OBSTACLE.
:return: True if its an OBSTACLE node, else False.
"""
if self.node_type == NodeType.OBSTACLE:
return True
return False
def has_edge_to_parent(self) -> bool:
"""Checks whether an edge to a parent was set.
:return: True if an edge to a parent was set, else False.
"""
if self.edge_to_parent is None:
return False
return True
def set_edge_to_parent(self, edge: Optional['Edge']) -> 'Node':
return replace(self, edge_to_parent=edge)
def __eq__(self, other):
if not isinstance(other, Node):
return NotImplemented
return self.x == other.x and self.y == other.y
def __hash__(self):
return hash((self.x, self.y))
@dataclass(frozen=True)
class Edge:
target: 'Node'
g: float
h: float
is_diagonal: bool
def get_f(self) -> float:
"""The overall score. The lesser, the better!
:return: g + h.
"""
return self.g + self.h