forked from abigailhayes/rl-for-vrp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_count.py
93 lines (81 loc) · 3.31 KB
/
instance_count.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
"""Counting the number of solutions returned in each CVRP experiment"""
import json
import pandas as pd
def main():
# Load in current counts
settings_df = pd.read_csv("results/other/settings.csv")
instances_df = pd.read_csv("results/other/instance_count.csv")
instances_df_tw = pd.read_csv("results/other/instance_count_tw.csv")
# results[test][example]
# Update dataframe
def instance_row(ident):
"""Count the instances for a given id"""
json_paths = [
f"results/exp_{ident}/results_a.json",
f"results/exp_{ident}/results_b.json",
]
if (settings_df[settings_df["id"] == ident]["method"] == "nazari").item():
# When data is split between beam and greedy
output_greedy = {"id": ident, "notes": "greedy"}
output_beam = {"id": ident, "notes": "beam"}
for json_path in json_paths:
with open(json_path) as json_data:
data = json.load(json_data)
for key in data:
output_greedy[key] = len(data[key]["greedy"])
output_beam[key] = len(data[key]["beam"])
return pd.concat(
[
pd.DataFrame.from_dict([output_greedy]),
pd.DataFrame.from_dict([output_beam]),
],
ignore_index=True,
)
else:
# When data is stored directly for each instance
output = {"id": ident}
for json_path in json_paths:
try:
with open(json_path) as json_data:
data = json.load(json_data)
for key in data:
output[key] = len(data[key])
except ValueError:
pass
return pd.DataFrame.from_dict([output])
def instance_row_tw(ident):
"""Count the instances for a given id for CVRPTW"""
output = {"id": ident}
try:
with open(f"results/exp_{ident}/results.json") as json_data:
data = json.load(json_data)
for key in data:
for variant in ['RC1', 'RC2', 'R1', 'R2', 'C1', 'C2']:
new_key = variant + "_" + str(key)
output[new_key] = sum([1 for instance in data[key].keys() if (instance.startswith(variant) and data[key][instance] is not None)])
except ValueError:
pass
return pd.DataFrame.from_dict([output])
# Run over new ids for CVRP
targets = [
ident
for ident in settings_df[settings_df["problem"] == "CVRP"]["id"]
if ident not in list(instances_df["id"])
]
for ident in targets:
instances_df = pd.concat([instances_df, instance_row(ident)], ignore_index=True)
# And for CVRPTW
targets = [
ident
for ident in settings_df[settings_df["problem"] == "CVRPTW"]["id"]
if ident not in list(instances_df_tw["id"])
]
for ident in targets:
instances_df_tw = pd.concat(
[instances_df_tw, instance_row_tw(ident)], ignore_index=True
)
# Save output
instances_df.to_csv("results/other/instance_count.csv", index=False)
instances_df_tw.to_csv("results/other/instance_count_tw.csv", index=False)
if __name__ == "__main__":
main()