-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
279 lines (222 loc) · 6.76 KB
/
utils.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import json
import numpy as np
import os
import pickle
import joblib
import platform
import torch
import tqdm
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
def multiprocess(func, tasks, cpus=None):
if cpus == 1 or len(tasks) == 1:
return [func(t) for t in tasks]
with Pool(cpus or os.cpu_count()) as pool:
return list(pool.imap(func, tasks))
def multithread(func, tasks, cpus=None, show_bar=True):
bar = lambda x: tqdm(x, total=len(tasks)) if show_bar else x
if cpus == 1 or len(tasks) == 1:
return [func(t) for t in bar(tasks)]
with ThreadPool(cpus or os.cpu_count()) as pool:
return list(bar(pool.imap(func, tasks)))
SEPA2IX = {
'aggregation': 0,
'clique': 1,
'disjunctive': 2,
'gomory': 3,
'impliedbounds': 4,
'mcf': 5,
'oddcycle': 6,
'strongcg': 7,
'zerohalf': 8,
}
IX2SEPA = {ix: sepa for sepa, ix in SEPA2IX.items()}
SAVE_DIR = '../exp_optlearn/runs/exp_optdata' # TODO: need to double check
def get_data_directory(args): # TODO: need to double check
instances = os.path.basename(os.path.normpath(os.path.dirname(args.path_to_instance)))
model = os.path.basename(os.path.normpath(args.path_to_instance))
data_directory = os.path.join(
SAVE_DIR,
instances,
args.config,
model,
args.state,
args.samplingstrategy,
f'offset-{args.random_offset}',
)
os.makedirs(data_directory, exist_ok=True)
return data_directory
def save_state(path, state):
os.makedirs(path, exist_ok=True)
for k, obj in state.items():
obj_path = os.path.join(path, k)
if isinstance(obj, np.ndarray):
save_func = save_numpy
obj_path += '.npy'
else:
save_func = save_pickle
obj_path += '.pkl'
save_func(obj_path, obj)
def save_json(path, d):
with open(path, 'w') as file:
json.dump(d, file, indent=4)
def load_json(path):
with open(path, 'r') as f:
d = json.load(f)
return d
def save_numpy(path, arr):
with open(path, 'wb') as file:
np.save(file, arr)
def load_numpy(path):
with open(path, 'rb') as file:
arr = np.load(path)
return arr
def save_pickle(path, obj):
with open(path, 'wb') as file:
pickle.dump(obj, file, protocol=5)
def load_pickle(path):
with open(path, 'rb') as file:
obj = pickle.load(file)
return obj
def save_joblib(path, obj):
joblib.dump(obj, path)
def load_joblib(path):
return joblib.load(path)
class ExitStatus:
CONVERGED_DUAL = 0
CONVERGED_INTEGRAL = 1
MAXITER = 2
ERROR_TRIVIAL = 3
ERROR_SEPARATION = 4
ERROR_NOCUTS = 5
ERROR_DUAL = 6
ERROR_SIGN_SWITCH = 7
ERROR_IGC_MESS = 8
ERROR_DEF_JSON_MISSING = 9
ERROR_EXP_JSON_MISSING = 10
#############################################################
# exp_optlearn util
#############################################################
def save_json(path, d):
with open(path, 'w') as file:
json.dump(d, file, indent=4)
def load_json(path):
with open(path, 'r') as f:
d = json.load(f)
return d
def save_pickle(path, d):
with open(path, 'wb') as file:
pickle.dump(d, file, protocol=pickle.HIGHEST_PROTOCOL)
def load_pickle(path):
with open(path, 'rb') as file:
d = pickle.load(file)
return d
def save_numpy(path, arr):
with open(path, 'wb') as file:
np.save(file, arr)
def load_numpy(path):
with open(path, 'rb') as file:
arr = np.load(path)
return arr
def save_joblib(path, obj):
joblib.dump(obj, path)
def load_joblib(path):
return joblib.load(path)
def find_all_paths_sepa(
split,
instances,
state,
data_dir='runs'
):
paths = []
basepath = f'./{data_dir}/{instances}_{split}'
models = []
if not os.path.exists(basepath): return paths
for f in os.listdir(basepath):
if f.startswith('model-'):
models.append(f)
for model in models:
model_path = f'{basepath}/{model}/{state}/'
for root, dirs, _ in os.walk(model_path):
for d in dirs:
if d.startswith('nsepacut'):
path = f'{root}/{d}'
paths.append(path)
return paths
def find_all_paths(
split,
instances,
config,
state,
samplingstrategy,
):
paths = []
basepath = f'./runs/exp_optdata/{instances}_{split}'
if is_local():
basepath += 'mock'
basepath = f'{basepath}/{config}'
models = []
if not os.path.exists(basepath): return paths
for f in os.listdir(basepath):
if f.startswith('model-'):
models.append(f)
for model in models:
model_path = f'{basepath}/{model}/{state}/{samplingstrategy}'
for root, dirs, _ in os.walk(model_path):
for d in dirs:
if d.startswith('nsepacut'):
path = f'{root}/{d}'
paths.append(path)
return paths
class AverageMeter():
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def is_local():
return platform.system() == 'Darwin'
"""
Utility functions to load and save torch model checkpoints
"""
def load_checkpoint(net, optimizer=None, step='max', save_dir='checkpoints'):
os.makedirs(save_dir, exist_ok=True)
checkpoints = [x for x in os.listdir(save_dir) if not x.startswith('events')]
if step == 'max':
step = 0
if checkpoints:
step, last_checkpoint = max([(int(x.split('.')[0]), x) for x in checkpoints])
else:
last_checkpoint = str(step) + '.pth'
if step:
save_path = os.path.join(save_dir, last_checkpoint)
state = torch.load(save_path, map_location='cpu')
net.load_state_dict(state['net'])
if optimizer:
optimizer.load_state_dict(state['optimizer'])
print('Loaded checkpoint %s' % save_path)
return step
def save_checkpoint(net, optimizer, step, save_dir='checkpoints'):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = os.path.join(save_dir, str(step) + '.pth')
torch.save(dict(net=net.state_dict(), optimizer=optimizer.state_dict()), save_path)
print('Saved checkpoint %s' % save_path)
if __name__ == '__main__':
paths = find_all_paths(
'train',
'binpacking-66-66',
'all-def-def',
'learn1',
'mixed1'
)
print(len(paths))
import pdb; pdb.set_trace()