-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoring_data_prep.py
213 lines (188 loc) · 7.16 KB
/
scoring_data_prep.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
#
from typing import Union, Dict, List, Optional
from copy import deepcopy
from warnings import warn, filterwarnings
from datetime import datetime
from timeit import default_timer
import itertools as it
import os
import shutil
import pickle
import argparse
import numpy as np
from lib.problem import RPInstance
filterwarnings("ignore", category=np.VisibleDeprecationWarning)
def hash_instance(inst: Union[Dict, RPInstance]):
if isinstance(inst, RPInstance):
return hash(inst.coords.tobytes() + inst.demands.tobytes())
elif isinstance(inst, dict):
return hash(inst['coords'].tobytes() + inst['demands'].tobytes())
else:
raise TypeError(type(inst))
def hash_subgraph(sg: List[List[int]]):
return tuple(np.unique(list(it.chain.from_iterable(sg))).tolist())
def cat_array(arrs: List, dim: int = 0):
a = arrs[0]
if a is None:
return arrs
elif isinstance(a, np.ndarray):
try:
return np.stack(arrs, axis=dim)
except ValueError:
return np.array(arrs, dtype=object)
elif isinstance(a, (int, float, np.generic)):
return np.array(arrs)
elif isinstance(a, (list, tuple)):
return np.array(arrs)
else:
raise ValueError(a)
def convert(
file_paths: Union[str, List[str]],
fname: Optional[str] = None,
offset: int = 0,
limit: int = None,
remove_duplicates: bool = True,
try_load: bool = False
):
if isinstance(file_paths, str):
file_paths = [file_paths]
sg_hash_table = {}
instances = {}
data = None
n_duplicates = 0
size, num_inst = 0, 0
cfgs, fstrings = [], []
for fpth in file_paths:
fstrings.append(os.path.basename(os.path.splitext(fpth)[0]))
filepath = os.path.normpath(os.path.expanduser(fpth))
i = 1
print(f"loading data from: {filepath}")
with open(filepath, 'rb') as f:
try:
# first file is cfg dict
cfg = pickle.load(f)
cfgs.append(cfg)
while True:
if limit and i > limit:
break
if i > offset:
insert = True
dat = pickle.load(f)
if data is None:
data = {k: [] for k in dat}
inst = dat['instance']
ihash = hash_instance(inst)
if ihash not in instances:
instances[ihash] = deepcopy(inst)
sg_hash_table[ihash] = {}
if remove_duplicates:
sghash = hash_subgraph(dat['sg_old_routes'])
if sghash not in sg_hash_table[ihash]:
sg_hash_table[ihash][sghash] = dat['sg_old_cost']-dat['sg_old_cost']
else:
imp_diff = abs(sg_hash_table[ihash][sghash] -
(dat['sg_old_cost']-dat['sg_old_cost']))
if imp_diff < 0.001: # same sg, same cost -> duplicate
insert = False
n_duplicates += 1
else: # same sg, vastly different cost -> adapt
sg_hash_table[ihash][sghash] = (
sg_hash_table[ihash][sghash] + (dat['sg_old_cost']-dat['sg_old_cost'])
)/2
if insert:
dat['instance'] = ihash
for k, v in dat.items():
if k != "sg_solver_cfg":
data[k].append(v)
else:
# skip
pickle.load(f)
if i % 10000 == 1:
print(f"loaded: {i-1:09d}")
i += 1
except EOFError:
pass
#
print(f"finished conversion: {len(data['instance']) - size} data points "
f"for {len(instances) - num_inst} instances.")
size = len(data['instance'])
num_inst = len(instances)
print(f"excluded {n_duplicates}.")
# save data as npz
data.pop("sg_solver_cfg") # remove solver cfgs
for k in data.keys():
data[k] = cat_array(data[k])
data['cfg'] = np.array(cfgs, dtype=object)
data['size'] = np.array([size])
data['all_instances'] = np.array([instances], dtype=object)
if fname is None:
fname = '+'.join(fstrings)
if os.path.splitext(fname)[1] != ".npz":
fname = f"{fname}.npz"
fname = os.path.join(os.path.dirname(file_paths[0]), fname)
print(f"saving to: {fname}")
if os.path.isfile(fname) and os.path.exists(fname):
print(f'Dataset file with same name exists already: {fname}')
pre, ext = os.path.splitext(fname)
new_f = pre + '_' + datetime.utcnow().strftime('%Y%m%d%H%M%S%f') + ext
print(f'archiving existing file to: {new_f}')
shutil.copy2(fname, new_f)
os.remove(fname)
np.savez_compressed(fname, **data)
print("done.\n")
if try_load:
del data
del instances
del sg_hash_table
try:
data = np.load(fname, allow_pickle=True)
keys = data.files.copy()
print(type(data), data, keys)
keys.remove('cfg')
keys.remove('size')
keys.remove('all_instances')
_keys = deepcopy(keys)
_data = {k: data[k] for k in _keys}
for idx in [0, 5, -1]:
d = {
# k: self._file_handle[k][idx] for k in self._keys
k: _data[k][idx] for k in _keys
}
print(d)
inst = next(iter(data.get('all_instances')[0].values()))
print(inst)
try:
inst = RPInstance.make(**inst)
print(inst)
except Exception as e:
warn(f"ERROR when creating instances: {e}")
except Exception as e:
warn(f"ERROR when loading: {e}")
del data
# ============= #
# ### TEST #### #
# ============= #
def _test():
PTHS = [
"data/_TEST/nrr_data_2_lkh_sweep_sweep_rnd_all_max_trials100.dat",
"data/_TEST/nrr_data_2_lkh_sweep_sweepxx_rnd_all_max_trials100.dat"
]
convert(PTHS, limit=2000)
# ## MAIN ## #
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('path', type=str, nargs='+')
parser.add_argument('-o', '--offset', type=int, default=0)
parser.add_argument('-l', '--limit', type=int, default=None)
parser.add_argument('-n', '--fname', type=str, default=None)
parser.add_argument('--try_load', action='store_true')
parser.add_argument('--with_duplicates', action='store_true')
args = parser.parse_args()
convert(
args.path,
fname=args.fname,
offset=args.offset,
limit=args.limit,
remove_duplicates=not args.with_duplicates,
try_load=args.try_load
)