This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
generated from ortec/euro-neurips-vrp-2022-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark_dynamic.py
126 lines (99 loc) · 3.05 KB
/
benchmark_dynamic.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
import argparse
from functools import partial
from glob import glob
from pathlib import Path
from time import perf_counter
import numpy as np
from tqdm.contrib.concurrent import process_map
import tools
from environment import VRPEnvironment
from strategies import solve_dynamic, solve_hindsight
from strategies.config import Config
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--instance_seed", type=int, default=1)
parser.add_argument("--solver_seed", type=int, default=1)
parser.add_argument("--num_procs", type=int, default=4)
parser.add_argument(
"--config_loc", default="configs/benchmark_dynamic.toml"
)
parser.add_argument(
"--instance_pattern", default="instances/ORTEC-VRPTW-ASYM-*.txt"
)
parser.add_argument("--hindsight", action="store_true")
stop = parser.add_mutually_exclusive_group(required=True)
stop.add_argument("--epoch_tlim", type=int)
stop.add_argument("--phase", choices=["quali", "final"])
return parser.parse_args()
def solve(
loc: str,
instance_seed: int,
solver_seed: int,
config_loc: str,
hindsight: bool,
epoch_tlim,
phase,
**kwargs,
):
path = Path(loc)
if phase is not None:
tlim = tools.dynamic_time_limit(phase)
else:
tlim = epoch_tlim
env = VRPEnvironment(
seed=instance_seed, instance=tools.read_vrplib(path), epoch_tlim=tlim
)
start = perf_counter()
config = Config.from_file(config_loc)
if hindsight:
costs, routes = solve_hindsight(env, config.static(), solver_seed)
else:
costs, routes = solve_dynamic(env, config, solver_seed)
run_time = round(perf_counter() - start, 2)
return (
path.stem,
instance_seed,
sum(costs.values()),
tuple(costs.values()),
tuple([len(rts) for rts in routes.values()]),
tuple([sum(len(route) for route in sol) for sol in routes.values()]),
run_time,
)
def main():
args = parse_args()
func = partial(solve, **vars(args))
func_args = glob(args.instance_pattern)
tqdm_kwargs = dict(max_workers=args.num_procs, unit="instance")
data = process_map(func, func_args, **tqdm_kwargs)
headers = [
"Instance",
"Seed",
"Total",
"Costs",
"Routes",
"Requests",
"Time (s)",
]
dtypes = [
("inst", "U37"),
("seed", int),
("total", int),
("costs", tuple),
("routes", tuple),
("requests", tuple),
("time", float),
]
data = np.asarray(data, dtype=dtypes)
table = tools.tabulate(headers, data)
print(
Path(__file__).name,
" ".join(f"--{key} {value}" for key, value in vars(args).items()),
)
config = Config.from_file(args.config_loc)
print("dynamic config:")
print(config.dynamic())
print("\n", table, "\n", sep="")
print(f" Avg. objective: {data['total'].mean():.0f}")
print(f" Avg. run-time (s): {data['time'].mean():.2f}")
if __name__ == "__main__":
main()