-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapf-lambda2.py
63 lines (56 loc) · 1.77 KB
/
mapf-lambda2.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
# This is a simple example of how to calculate the lambda2 of map using networkx library
import networkx as nx
import numpy as np
import yaml
map_folder = "./dataset/MAPF_Benchmark/"
map_name = "maze-32-32-4.map"
map_path = map_folder + map_name
# load yaml map
if map_path.split('.')[-1] == "yaml":
with open(map_path) as f:
map_data = yaml.load(f, Loader=yaml.FullLoader)
dim_x = map_data["dimensions"][0]
dim_y = map_data["dimensions"][1]
obs = map_data["obstacles"]
# load MAPF benchmark map
elif map_path.split('.')[-1] == "map":
with open(map_path) as f:
if f.readline() != "type octile\n":
print("Bad mapfile!")
exit()
else:
dim_y = int(f.readline().split(" ")[-1])
dim_x = int(f.readline().split(" ")[-1])
f.readline()
x = y = 0
obs = []
for line in f.readlines():
for l in line:
if l != ".":
obs.append([x, y])
x += 1
x = 0
y += 1
else:
print("Only support .yaml and .map format!")
exit()
# ceate graph
G = nx.Graph()
movements = [(-1, 0), (0, 1), (1, 0), (0, -1)]
# add nodes
for i in range(dim_x):
for j in range(dim_y):
if [i, j] not in obs:
G.add_node((i, j))
# add edges
for i in range(dim_x):
for j in range(dim_y):
if [i, j] not in obs:
# check neighbors
for mi, mj in movements:
if -1 < mi + i < dim_x and -1 < mj + j < dim_y and [mi + i, mj + j] not in obs:
G.add_edge((i, j), (mi + i, mj + j))
# calculate eigen values
eigen = nx.normalized_laplacian_spectrum(G)
print('map: %s' % map_name)
print('lambda2 is: %f' % eigen[1])