File size: 3,345 Bytes
ad7641a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from globals import *


# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
# CLASSES
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
class AStarNode:
    def __init__(self, n: Node, t: int, h: int, parent: Self | None = None):
        self.n: Node = n
        self.x: int = n.x
        self.y: int = n.y
        self.t: int = t
        self.h: int = h
        self.f: int = t + h
        self.parent: Self | None = parent
        self.neighbours: List[str] = n.neighbours[:]
        self.neighbours_nodes: List[Node] = n.neighbours_nodes[:]
        self.xy_name: str = f'{self.x}_{self.y}'
        self.xyt_name: str = f'{self.x}_{self.y}_{self.t}'

    def __eq__(self, other: Self) -> bool:
        return self.xyt_name == other.xyt_name

    def __lt__(self, other: Self) -> bool:
        # for sort
        if self.f < other.f:
            return True
        if self.f > other.f:
            return False
        if self.h < other.h:
            return True
        # critic to put >=
        if self.h >= other.h:
            return False
        raise RuntimeError('uuuups')

    # def __hash__(self):
    #     return hash(self.xyt_name)

    def __str__(self) -> str:
        return f'{self.xyt_name} [{self.f}]'

    def __repr__(self) -> str:
        return f'{self.xyt_name} [{self.f}]'


# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
# FUNCTIONS
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
def reconstruct_path(i_node: AStarNode) -> List[Node]:
    # we have reached the end
    path: List[Node] = []
    node_current = i_node
    while node_current is not None:
        path.append(node_current.n)
        node_current = node_current.parent
    path.reverse()
    return path


def get_latest_vc_on_node(i_node: AStarNode, vc_np: np.ndarray | None) -> int:
    """
    :param i_node:
    :param vc_np: vertex constraints [x, y, t] = bool
    :return: int
    """
    if vc_np is None:
        return 0
    vc_times = vc_np[i_node.x, i_node.y, :]
    indices = np.argwhere(vc_times == 1)
    if len(indices) == 0:
        return 0
    return int(indices[-1][0])