-
Notifications
You must be signed in to change notification settings - Fork 4
/
reproduction.py
77 lines (59 loc) · 2.25 KB
/
reproduction.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
"""
Reproduction Script for all results presented
"""
import csv
from argparse import ArgumentParser
from copy import deepcopy
import torch
from agents import IRPAgent, TSPAgent, VRPAgent, RandomAgent
from gym_vrp.envs import IRPEnv, TSPEnv, VRPEnv
env_dict = {"TSP": TSPEnv, "VRP": VRPEnv, "IRP": IRPEnv}
agent_dict = {"TSP": TSPAgent, "VRP": VRPAgent, "IRP": IRPAgent}
def reproduce(
seeds: list,
num_nodes: int,
batch_size: int,
csv_path: str,
model_path: str,
num_draw: int,
env_type: str,
):
with open(csv_path, "w+", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Model", "Seed", "Mean Distance"])
for seed in seeds:
env = env_dict[env_type](
num_nodes=num_nodes, batch_size=batch_size, num_draw=num_draw, seed=seed,
)
env_r = deepcopy(env)
env.enable_video_capturing(
video_save_path=f"./videos/video_{env_type}_{num_nodes}_{seed}.mp4"
)
agent = agent_dict[env_type](seed=seed)
agent.model.load_state_dict(torch.load(model_path))
random_agent = RandomAgent(seed=seed)
random_agent.eval()
loss_a = agent.evaluate(env)
loss_r = random_agent(env_r)
with open(csv_path, "a", newline="") as file:
writer = csv.writer(file)
for agent_loss, random_loss in zip(loss_a, loss_r):
writer.writerow([f"{env_type}-Agent", seed, agent_loss.item()])
writer.writerow(
[f"{env_type}-Random-Agent", seed, random_loss.mean().item()]
)
if __name__ == "__main__":
parser = ArgumentParser()
# hparams
parser.add_argument("--seeds", type=int, nargs="+", default=[1234, 2468, 2048])
parser.add_argument("--batch_size", type=int, default=256)
parser.add_argument("--num_nodes", type=int, default=20)
parser.add_argument("--num_draw", type=int, default=3)
parser.add_argument("--csv_path", type=str, default="reproduction_results.csv")
parser.add_argument(
"--model_path", type=str, default="./check_points/model_epoch__tsp_850.pt"
)
parser.add_argument("--env_type", type=str, default="TSP")
args = parser.parse_args()
print(vars(args))
reproduce(**vars(args))