-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinmatch_vs_prematch_75percent_3teams.py
135 lines (106 loc) · 4.36 KB
/
inmatch_vs_prematch_75percent_3teams.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
131
132
133
134
135
from multiprocessing import Pool
from typing import Optional
from tqdm import tqdm
from python.benchmarks.graph_times import graph_results
from python.benchmarks.map import MapGenerator
import pathlib
from python.benchmarks.parse_map import MapParser
from python.benchmarks.run_with_timeout import run_with_timeout
from python.mstar.rewrite import Config, MatchingStrategy
from python.mstar.rewrite.config import GigaByte
from python.solvers.configurable_mstar_solver import ConfigurableMStar
this_dir = pathlib.Path(__file__).parent.absolute()
name = "inmatch_vs_prematch_75percent_3teams_maps"
processes = 10
def generate_maps():
path = this_dir / name
try:
path.mkdir(parents=True)
except FileExistsError:
print("maps already generated")
return
for i in tqdm(range(1, 16)):
tqdm.write(f"generating {path}")
map_generator = MapGenerator(path)
map_generator.generate_even_batch(
200, # number of maps
20, 20, # size
i, # number of agents
3, # number of teams
prefix=name,
min_goal_distance=0,
open_factor=0.65,
max_neighbors=1
)
def run_benchmark():
batchdir = this_dir / name
parser = MapParser(batchdir)
if (batchdir / "results_inmatch_tmp.txt").exists():
print("data exists")
return
if (batchdir / "results_prematch.txt").exists():
print("data exists")
return
# num agents : solutions
inmatch: dict[int, list[Optional[float]]] = {}
prematch: dict[int, list[Optional[float]]] = {}
all_problems = [[i[1] for i in parser.parse_batch(name.name)] for name in batchdir.iterdir() if name.is_dir()]
all_problems.sort(key=lambda i: len(i[0].goals))
with Pool(processes) as p:
for problems in tqdm(all_problems):
num_agents = len(problems[0].goals)
print("inmatch")
if num_agents <= 1 or sum(1 for i in inmatch[num_agents - 1] if i is not None) != 0:
sols_inmatch = run_with_timeout(p, ConfigurableMStar(
Config(
operator_decomposition=False,
precompute_paths=False,
precompute_heuristic=True,
collision_avoidance_table=False,
recursive=False,
matching_strategy=MatchingStrategy.Inmatch,
max_memory_usage=3 * GigaByte,
debug=False,
)
), problems, 2 * 60)
tqdm.write(f"inmatch with {num_agents} agents: {sols_inmatch}")
inmatch[num_agents] = sols_inmatch
else:
inmatch[num_agents] = [None for i in range(len(problems))]
print("prematch")
if num_agents <= 1 or sum(1 for i in prematch[num_agents - 1] if i is not None) != 0:
sols_prematch = run_with_timeout(p, ConfigurableMStar(
Config(
operator_decomposition=False,
precompute_paths=False,
precompute_heuristic=True,
collision_avoidance_table=False,
recursive=False,
matching_strategy=MatchingStrategy.Prematch,
max_memory_usage=3 * GigaByte,
debug=False,
)
), problems, 2 * 60)
tqdm.write(f"prematch with {num_agents} agents: {sols_prematch}")
prematch[num_agents] = sols_prematch
else:
prematch[num_agents] = [None for i in range(len(problems))]
tqdm.write(str(inmatch))
tqdm.write(str(prematch))
output_data(batchdir / "results_inmatch.txt", inmatch)
output_data(batchdir / "results_prematch.txt", prematch)
def output_data(file: pathlib.Path, data: dict[int, list[float]]):
with open(file, "w") as f:
for i, r in sorted([(a, b) for a, b in data.items()], key=lambda x: x[0]):
f.write(f"{i}: {r}\n")
def main():
batchdir = this_dir / name
generate_maps()
run_benchmark()
graph_results(
(batchdir / "results_inmatch.txt", "inmatch"),
(batchdir / "results_prematch.txt", "prematch"),
batchdir / f"{name}"
)
if __name__ == '__main__':
main()