-
Notifications
You must be signed in to change notification settings - Fork 7
/
CT.py
92 lines (73 loc) · 2.46 KB
/
CT.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import numpy as np
import time
import matplotlib.pyplot as plt
import random
from heapq import heappop, heappush
from MAPF import Map
from MAPF import read_map_from_movingai_file, read_tasks_from_movingai_file
class HighNode:
'''
HighNode class represents a high-level search node
- vertexCons: vertex constraints of the node
- edgeCons: edge constraints of the node
- sol: solution of the node
- g: cost of the solution of the node
- h: h-value of the node
- F: f-value of the node
- parent: pointer to the parent-node
'''
def __init__(self, vertexCons={}, edgeCons={}, sol={}, g=0, h=0, F=None, parent=None, k=0):
self.vertexCons = vertexCons
self.edgeCons = edgeCons
self.sol = sol
self.g = g
self.h = h
self.k = k
if F is None:
self.F = g + h
else:
self.F = F
self.parent = parent
def __eq__(self, other):
return (self.vertexCons == other.vertexCons) and (self.edgeCons == other.edgeCons) and \
(self.sol == other.sol)
def __lt__(self, other):
return self.F < other.F or ((self.F == other.F) and (self.h < other.h)) \
or ((self.F == other.F) and (self.h == other.h))
class LowNode:
'''
LowNode class represents a low-level search node
- i, j: coordinates of corresponding grid element
- g: g-value of the node
- h: h-value of the node
- F: f-value of the node
- parent: pointer to the parent-node
'''
def __init__(self, coord, g=0, h=0, F=None, parent=None):
self.i = coord[0]
self.j = coord[1]
self.g = g
self.h = h
if F is None:
self.F = g + h
else:
self.F = F
self.parent = parent
def __eq__(self, other):
return (self.i == other.i) and (self.j == other.j) and (self.g == other.g)
def __lt__(self, other):
return self.F < other.F or ((self.F == other.F) and (self.h < other.h)) \
or ((self.F == other.F) and (self.h == other.h))
def MakePath(goal):
'''
Creates a path by tracing parent pointers from the goal node to the start node
It also returns path's length.
'''
length = goal.g
current = goal
path = []
while current.parent:
path.append(current)
current = current.parent
path.append(current)
return path[::-1], length