-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMAPF.py
99 lines (77 loc) · 2.37 KB
/
MAPF.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
93
94
95
96
97
98
import numpy as np
import time
import matplotlib.pyplot as plt
import random
from heapq import heappop, heappush
'''Grid'''
class Map:
#
def __init__(self):
'''
Default constructor
'''
self.width = 0
self.height = 0
self.cells = []
def SetGridCells(self, width, height, gridCells):
'''
Initialization of map by list of cells.
'''
self.width = width
self.height = height
self.cells = gridCells
def inBounds(self, i, j):
'''
Check if the cell is on a grid.
'''
return (0 <= i < self.width) and (0 <= j < self.height)
def Traversable(self, i, j):
'''
Check if the cell is not an obstacle.
'''
return not self.cells[j][i]
def GetNeighbors(self, i, j):
'''
Get a list of neighbouring cells as (i,j) tuples.
'''
neighbors = [[i, j]]
delta = [[0, 1], [1, 0], [0, -1], [-1, 0]]
for d in delta:
if self.inBounds(i + d[0], j + d[1]) and self.Traversable(i + d[0], j + d[1]):
neighbors.append((i + d[0], j + d[1]))
return neighbors
def read_map_from_movingai_file(path):
map_file = open(path)
count = 0
name = map_file.readline()
height = int(map_file.readline().split()[1])
width = int(map_file.readline().split()[1])
type_ = map_file.readline()
cells = [[0 for _ in range(width)] for _ in range(height)]
i = 0
j = 0
for l in map_file:
j = 0
for c in l:
if c == '.':
cells[i][j] = 0
elif c == 'T' or c == '@':
cells[i][j] = 1
else:
continue
j += 1
if j != width:
raise Exception("Size Error. Map width = ", j, ", but must be", width, "(map line: ", i, ")")
i += 1
if(i == height):
break
return (width, height, cells)
def read_tasks_from_movingai_file(path):
tasks = []
task_file = open(path)
for l in task_file:
new_task = l.split()[4:]
if len(new_task) != 0:
tasks.append(list(map(float,new_task)))
#возвращает числа: координаты начала и конца, длину пути
return np.array(tasks, int)