forked from jtan-gh/multi-agent-path-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_experiments.py
130 lines (115 loc) · 4.43 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
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
import argparse
import glob
import sys
from pathlib import Path
from CBSSolver import CBSSolver
from CGSolver import CGSolver
from DGSolver import DGSolver
from WDGSolver import WDGSolver
from independent import IndependentSolver
from prioritized import PrioritizedPlanningSolver
from visualize import Animation
from single_agent_planner import get_sum_of_cost, compute_heuristics
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()
print("hello world")
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 == "CBS_cg":
print("***Run CBS_cg***")
cbs = CGSolver(my_map, starts, goals)
paths = cbs.find_solution(args.disjoint)
elif args.solver == "CBS_dg":
print("***Run CBS_dg***")
cbs = DGSolver(my_map, starts, goals)
paths = cbs.find_solution(args.disjoint)
elif args.solver == "CBS_wdg":
print("***Run CBS_wdg***")
cbs = WDGSolver(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)
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()