-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbests.py
220 lines (156 loc) · 5.57 KB
/
bests.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import json
import csv
import platform
from typing import List, Dict, Any
from copy import deepcopy
from pathlib import Path
from collections import defaultdict
from parse_solution import Reference, parse_solution
script_location = Path("./")
bks_location = script_location / "best_known_solutions"
unknown = "???"
def read_overwrites():
with open(script_location / "overwrite.json") as fd:
over = [x for x in json.load(fd) if x]
overwrites = dict(
(o["file"], o["who"])
for o in over
)
return overwrites
def read_refs():
references = {unknown: unknown}
with open(script_location / "refs.json") as fd:
refs = json.load(fd)
for reference in refs:
for variant in refs[reference]:
ref = Reference(variant["authors"], variant["reference"])
references[ref] = reference
return references
def read_file(path) -> List[str]:
with open(path) as fd:
return list(fd)
def get_reference(lines: List[str]) -> Reference:
if len(lines) != 0:
return parse_solution(lines)["who"]
return unknown
def overwrite(
filename: str,
overwrites: Dict[str, Any],
who_detail
) -> (str, List[str]):
if filename in overwrites:
who = overwrites[filename]
if who_detail == unknown or (who_detail[0] in who):
who_detail = who
else:
who = who_detail
return who, who_detail
def parse_filename(filename: str) -> (str, str, str):
inst, res = filename.split(".", 1)
routes, dist_dot_txt = res.split('_')
dist = dist_dot_txt.rsplit('.', 1)[0]
_, fraction = dist.split(".")
dist += "0" * (4 - len(fraction))
return inst, routes, dist
def is_better(previous_best, routes, distance):
previous_best_rs = int(previous_best["routes"])
rs = int(routes)
better_routes = (rs < previous_best_rs)
same_routes = (rs == previous_best_rs)
better_dist = (float(distance) < float(previous_best["distance"]))
return better_routes or (same_routes and better_dist)
def generate_bks(
refs,
overwrites,
inst_path):
db = defaultdict(list)
dates = (os.listdir(inst_path))
for date in sorted(dates):
path = inst_path / date
for filename in os.listdir(path):
full_path = path / filename
solution_file = read_file(full_path)
inst, routes, dist = parse_filename(filename)
reference = get_reference(solution_file)
who_detail = [refs[reference]]
who, who_detail = overwrite(filename, overwrites, who_detail)
if inst not in db or is_better(db[inst][-1], routes, dist):
db[inst].append({
"instance": inst,
"when": date,
"routes": routes,
"distance": dist,
"who": " & ".join(sorted(who)),
"detailed": " & ".join(sorted(who_detail)),
"url": full_path
})
return db
def make_table(
db: Dict[str, List[Any]], insts: List[str]) -> List[Dict[str, Any]]:
out = []
for inst in insts:
best = db[inst][-1]
out.append(best)
return out
left = ":---"
right = "---:"
center = ":---:"
def mdrow(row: List[str]) -> str:
return " | ".join(str(r) for r in row)
def make_md_table(table) -> List[str]:
tbl = deepcopy(table)
out = []
keys = ["instance", "routes", "distance", "when", "who", "notes"]
align = [center, center, center, center, center, center]
out.append(mdrow(keys))
out.append(mdrow(align))
for row in tbl:
row["distance"] = f'[{row["distance"]}]({row["url"]})'
row["notes"] = ""
if row["who"] != row["detailed"]:
row["notes"] = f'contributed by {row["detailed"]}'
fields = [row[what] for what in keys]
out.append(mdrow(fields))
return out
def make_full_bks_db():
refs = read_refs()
overwrites = read_overwrites()
db = {}
for benchmark in ["LiLim", "GehringHomberger"]:
db = {**db, **generate_bks(refs, overwrites, bks_location / benchmark)}
return db
def make_benchmarks():
with open(script_location / "tables.json") as fd:
tables = json.load(fd)
names = {
"LiLim": "Li and Lim PDPTW Benchmark",
"GehringHomberger": "Gehring-Homberger CVRPTW Benchmark"
}
refs = read_refs()
overwrites = read_overwrites()
firstline = True
with open("tables.md", 'w') as out, open("tables.csv", 'w') as csv_out:
if platform.system() == "Linux":
c = csv.writer(csv_out, lineterminator='\n')
else:
c = csv.writer(csv_out)
for benchmark in tables:
db = generate_bks(refs, overwrites, bks_location / benchmark)
out.write(f"# {names[benchmark]}\n")
for size in tables[benchmark]:
groups = tables[benchmark][size]
out.write(f"## {size} clients\n")
for instances in groups:
table = make_table(db, instances)
mdtable = make_md_table(table)
if firstline:
c.writerow(["benchmark", "clients", *table[0]])
firstline = False
for row in table:
c.writerow([benchmark, size, *row.values()])
for line in mdtable:
out.write(line + "\n")
out.write("\n")
if __name__ == "__main__":
make_benchmarks()