-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_experiments.py
118 lines (101 loc) · 3.9 KB
/
run_experiments.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python
import argparse
import glob
from pathlib import Path
from cbs import CBSSolver
from independent import IndependentSolver
from prioritized import PrioritizedPlanningSolver
from visualize import Animation
from single_agent_planner import get_sum_of_cost
SOLVER = "CBS"
def print_mapf_instance(my_map, starts, goals):
print('Start locations')
print_locations(my_map, starts)
print('Goal locations')
print_locations(my_map, goals)
def print_locations(my_map, locations):
starts_map = [[-1 for _ in range(len(my_map[0]))] for _ in range(len(my_map))]
for i in range(len(locations)):
starts_map[locations[i][0]][locations[i][1]] = i
to_print = ''
for x in range(len(my_map)):
for y in range(len(my_map[0])):
if starts_map[x][y] >= 0:
to_print += str(starts_map[x][y]) + ' '
elif my_map[x][y]:
to_print += '@ '
else:
to_print += '. '
to_print += '\n'
print(to_print)
def import_mapf_instance(filename):
f = Path(filename)
if not f.is_file():
raise BaseException(filename + " does not exist.")
f = open(filename, 'r')
# first line: #rows #columns
line = f.readline()
rows, columns = [int(x) for x in line.split(' ')]
rows = int(rows)
columns = int(columns)
# #rows lines with the map
my_map = []
for r in range(rows):
line = f.readline()
my_map.append([])
for cell in line:
if cell == '@':
my_map[-1].append(True)
elif cell == '.':
my_map[-1].append(False)
# #agents
line = f.readline()
num_agents = int(line)
# #agents lines with the start/goal positions
starts = []
goals = []
for a in range(num_agents):
line = f.readline()
sx, sy, gx, gy = [int(x) for x in line.split(' ')]
starts.append((sx, sy))
goals.append((gx, gy))
f.close()
return my_map, starts, goals
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Runs various MAPF algorithms')
parser.add_argument('--instance', type=str, default=None,
help='The name of the instance file(s)')
parser.add_argument('--batch', action='store_true', default=False,
help='Use batch output instead of animation')
parser.add_argument('--disjoint', action='store_true', default=False,
help='Use the disjoint splitting')
parser.add_argument('--solver', type=str, default=SOLVER,
help='The solver to use (one of: {CBS,Independent,Prioritized}), defaults to ' + str(SOLVER))
args = parser.parse_args()
result_file = open("results.csv", "w", buffering=1)
for file in sorted(glob.glob(args.instance)):
print("***Import an instance***")
my_map, starts, goals = import_mapf_instance(file)
print_mapf_instance(my_map, starts, goals)
if args.solver == "CBS":
print("***Run CBS***")
cbs = CBSSolver(my_map, starts, goals)
paths = cbs.find_solution(args.disjoint)
elif args.solver == "Independent":
print("***Run Independent***")
solver = IndependentSolver(my_map, starts, goals)
paths = solver.find_solution()
elif args.solver == "Prioritized":
print("***Run Prioritized***")
solver = PrioritizedPlanningSolver(my_map, starts, goals)
paths = solver.find_solution()
else:
raise RuntimeError("Unknown solver!")
cost = get_sum_of_cost(paths, goals,starts)
result_file.write("{},{}\n".format(file, cost))
if not args.batch:
print("***Test paths on a simulation***")
animation = Animation(my_map, starts, goals, paths)
# animation.save("output.mp4", 1.0)
animation.show()
result_file.close()