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
/
solver.py
74 lines (56 loc) · 2.04 KB
/
solver.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
import argparse
import cProfile
import pstats
import sys
from datetime import datetime
import tools
from environment import ControllerEnvironment, VRPEnvironment
from strategies import solve_dynamic, solve_hindsight
from strategies.config import Config
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--instance")
parser.add_argument("--instance_seed", type=int, default=1)
parser.add_argument("--solver_seed", type=int, default=1)
parser.add_argument("--epoch_tlim", type=int, default=120)
parser.add_argument("--config_loc", default="configs/solver.toml")
parser.add_argument("--profile", action="store_true")
problem_type = parser.add_mutually_exclusive_group()
problem_type.add_argument("--static", action="store_true")
problem_type.add_argument("--hindsight", action="store_true")
return parser.parse_args()
def run(args):
if args.instance is not None:
env = VRPEnvironment(
seed=args.instance_seed,
instance=tools.read_vrplib(args.instance),
epoch_tlim=args.epoch_tlim,
is_static=args.static,
)
else:
# Run within external controller
assert not args.hindsight, "Cannot solve hindsight using controller"
env = ControllerEnvironment(sys.stdin, sys.stdout)
# Make sure these parameters are not used by your solver
args.instance = None
args.instance_seed = None
args.static = None
args.epoch_tlim = None
config = Config.from_file(args.config_loc)
if args.hindsight:
solve_hindsight(env, config.static(), args.solver_seed)
else:
solve_dynamic(env, config, args.solver_seed)
def main():
args = parse_args()
if args.profile:
with cProfile.Profile() as profiler:
run(args)
stats = pstats.Stats(profiler).strip_dirs().sort_stats("time")
stats.print_stats()
now = datetime.now().isoformat()
stats.dump_stats(f"tmp/profile-{now}.pstat")
else:
run(args)
if __name__ == "__main__":
main()